basic reference support
This commit is contained in:
parent
07cf75d0b6
commit
183f2439d2
80
gen.c
80
gen.c
|
@ -16,12 +16,6 @@ void gen_free(Gen*gen)
|
|||
if(!gen)return;
|
||||
}
|
||||
|
||||
void gen_setrootnode(Gen*gen,PNode*rootnode)
|
||||
{
|
||||
if(!gen||!rootnode)return;
|
||||
gen->rootnode=rootnode;
|
||||
}
|
||||
|
||||
bool gen_findret(const PNode*pn,bool found)
|
||||
{
|
||||
if(found)return true;
|
||||
|
@ -33,41 +27,6 @@ bool gen_findret(const PNode*pn,bool found)
|
|||
return false;
|
||||
}
|
||||
|
||||
/* static void gen_print_string(Gen*gen,const PNode*pn,FILE*file,const Str*str) */
|
||||
/* { */
|
||||
/* if(!gen)return; */
|
||||
/* if(!pn)return; */
|
||||
/* if(!file)return; */
|
||||
/* if(!str)return; */
|
||||
/* */
|
||||
/* for(size_t i=0;i<str->size;++i) */
|
||||
/* { */
|
||||
/* switch(str->buffer[i]) */
|
||||
/* { */
|
||||
/* */
|
||||
/* // Escape codes */
|
||||
/* case '\\': */
|
||||
/* if(i<str->size-1) */
|
||||
/* { */
|
||||
/* switch(str->buffer[i+1]) */
|
||||
/* { */
|
||||
/* case 'n':fputc('\n',file);++i;break; */
|
||||
/* case 't':fputc('\t',file);++i;break; */
|
||||
/* case '"':fputc('"',file);++i;break; */
|
||||
/* case '\\':fputc('\\',file);++i;break; */
|
||||
/* } */
|
||||
/* } */
|
||||
/* break; */
|
||||
/* */
|
||||
/* default: */
|
||||
/* fputc(str->buffer[i],file); */
|
||||
/* break; */
|
||||
/* */
|
||||
/* } */
|
||||
/* } */
|
||||
/* fprintf(file,"\n"); */
|
||||
/* } */
|
||||
|
||||
void vec_print_tokens(Vec*tokens)
|
||||
{
|
||||
if(!tokens)return;
|
||||
|
@ -113,9 +72,11 @@ void gen_declare_variable(Gen*gen,PNode*pn,FILE*file)
|
|||
// Check syntax
|
||||
if(pn->tokens.size>1)
|
||||
{
|
||||
if(vec_at(&pn->tokens,1,const Tok*)->type!=LOPERATOR||(strcmp(vec_at(&pn->tokens,1,const Tok*)->str.buffer,"=")))
|
||||
if(vec_at(&pn->tokens,1,const Tok*)->type!=LOPERATOR&&
|
||||
vec_at(&pn->tokens,1,const Tok*)->type!=LASSIGN&&
|
||||
vec_at(&pn->tokens,1,const Tok*)->type!=LSCOLON)
|
||||
{
|
||||
err_log("%u: expected either ';' or initializer",vec_at(&pn->tokens,1,const Tok*)->line);
|
||||
err_log("%u: expected either ';', type specifier, or initializer",vec_at(&pn->tokens,1,const Tok*)->line);
|
||||
}
|
||||
}
|
||||
else if(pn->tokens.size==0)
|
||||
|
@ -253,8 +214,6 @@ void gen_eval_analyze(const PNode*pn)
|
|||
switch(vec_at(&rpn_stack,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
// TODO: Find function names here and
|
||||
// evaluate (?) its arglist
|
||||
case LIDENTIFIER:
|
||||
printf("!%s ",vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
break;
|
||||
|
@ -586,6 +545,7 @@ Vec gen_i2r(const Vec*tokens)
|
|||
precedence['+']=10;
|
||||
precedence['-']=10;
|
||||
precedence['!']=10;
|
||||
precedence['&']=10;
|
||||
precedence['*']=20;
|
||||
precedence['/']=20;
|
||||
precedence[',']=25;
|
||||
|
@ -848,6 +808,9 @@ void gen_eval(Gen*gen,const PNode*pn,FILE*file)
|
|||
if(vec_at(&rpn_stack,i,const Tok*)->subtype==LSCOMMA)
|
||||
nops=0;
|
||||
|
||||
if(vec_at(&rpn_stack,i,const Tok*)->subtype==LSREF)
|
||||
nops=1;
|
||||
|
||||
// Get top two stack elements (variables, integers, etc.)
|
||||
for(size_t k=0;k<nops;++k)
|
||||
{
|
||||
|
@ -1031,6 +994,8 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
sprintf(operator,"idivl");
|
||||
else if(curtok->subtype==LASSIGN)
|
||||
sprintf(operator,"movl");
|
||||
else if(curtok->subtype==LSREF)
|
||||
sprintf(operator,ld.lea_wordsize);
|
||||
|
||||
// Print output
|
||||
if(gen->showeval)
|
||||
|
@ -1065,6 +1030,7 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
{
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LSNOT:
|
||||
switch(elem[0].type)
|
||||
{
|
||||
|
@ -1083,7 +1049,31 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
opos+=sprintf(output_buffer+opos,"\tmovzbl %%al,%%eax\n");
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
break;
|
||||
|
||||
// Create reference from var
|
||||
case LSREF:
|
||||
switch(elem[0].type)
|
||||
{
|
||||
|
||||
case EE_VAR:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
break;
|
||||
|
||||
default:
|
||||
err_log("%u: cannot take reference of '%s'",vec_at(&pn->tokens,0,Tok*)->line,operands[0]);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if(push_elem)
|
||||
vec_push(stack,&tmpelem);
|
||||
|
||||
fwrite(output_buffer,1,opos,file);
|
||||
}
|
||||
|
||||
|
||||
|
|
1
gen.h
1
gen.h
|
@ -50,6 +50,7 @@ typedef struct LangDef
|
|||
const char*const sp;
|
||||
const char*const bp;
|
||||
const char*const scratch1;
|
||||
const char*const lea_wordsize;
|
||||
}LangDef;
|
||||
|
||||
/* static void gen_print_string(Gen*gen,const PNode*pn,FILE*file,const Str*str) */
|
||||
|
|
1
i386.c
1
i386.c
|
@ -16,6 +16,7 @@ LangDef gen_i386_langdef(void)
|
|||
.sp="esp",
|
||||
.bp="ebp",
|
||||
.scratch1="ecx",
|
||||
.lea_wordsize="leal",
|
||||
};
|
||||
return ld;
|
||||
}
|
||||
|
|
9
lex.c
9
lex.c
|
@ -12,9 +12,11 @@
|
|||
|
||||
const char*lextype_names[]={"LNONE","LIDENTIFIER","LINTEGER","LFLOAT","LSTRING","LOPERATOR","LKEYWORD","LCOMMENT","LMINUS","LFAKE",NULL};
|
||||
const char*lextype_colors[]={"\033[0m","\033[0m","\033[36m","\033[35m","\033[32m","\033[0m","\033[33m","\033[34m"};
|
||||
const char*lexsubtype_names[]={"LENDSTATEMENT","LASSIGN","LLPAREN","LRPAREN","LLCBRACE","LRCBRACE","LSMINUS","LADD","LSMUL","LSDIV","LSNOT","LSCOLON","LSCOMMA",NULL};
|
||||
static const char*operator_chars="-+*/=;(),.{}<>\\!:";
|
||||
static const char*keywords[]={"do","false","fn","for","if","let","ret","true","while","call","asm","ext",};
|
||||
const char*lexsubtype_names[]={"LENDSTATEMENT","LASSIGN","LLPAREN","LRPAREN","LLCBRACE","LRCBRACE","LSMINUS","LADD","LSMUL","LSDIV","LSNOT","LSCOLON","LSCOMMA","LSREF",NULL};
|
||||
static const char*operator_chars="-+*/=;(),.{}<>\\!:&";
|
||||
static const char*keywords[]={"do","false","fn","for","if","let","ret","true","while","call","asm","ext",
|
||||
/* type specifiers */
|
||||
"i32",};
|
||||
/* static char*operators[]={";","=","+=","-=","*=","/=","+","-","/","*","(",")","{","}"}; */
|
||||
|
||||
Lexer lex_new(void)
|
||||
|
@ -173,6 +175,7 @@ void lex_string(Lexer*lex,char*input_string)
|
|||
case '!':opmatch(LSNOT,false);break;
|
||||
case ':':opmatch(LSCOLON,false);break;
|
||||
case ',':opmatch(LSCOMMA,false);break;
|
||||
case '&':opmatch(LSREF,false);break;
|
||||
case '=':
|
||||
{
|
||||
opmatch(LASSIGN,false);
|
||||
|
|
2
lex.h
2
lex.h
|
@ -13,7 +13,7 @@
|
|||
#define vec_pushl(v,l) do{Tok x=l;vec_push(v,&x);}while(0)
|
||||
|
||||
enum LEXTYPE {LNONE=0, LIDENTIFIER, LINTEGER, LFLOAT, LSTRING, LOPERATOR, LKEYWORD, LCOMMENT, LMINUS, LFAKE, };
|
||||
enum LEXSUBTYPE {LENDSTATEMENT=55, LASSIGN, LLPAREN, LRPAREN, LLCBRACE, LRCBRACE, LSMINUS, LADD, LSMUL, LSDIV, LSNOT, LSCOLON, LSCOMMA, };
|
||||
enum LEXSUBTYPE {LENDSTATEMENT=55, LASSIGN, LLPAREN, LRPAREN, LLCBRACE, LRCBRACE, LSMINUS, LADD, LSMUL, LSDIV, LSNOT, LSCOLON, LSCOMMA, LSREF, };
|
||||
extern const char*lextype_names[];
|
||||
extern const char*lexsubtype_names[];
|
||||
extern const char*lextype_colors[];
|
||||
|
|
7
vec.h
7
vec.h
|
@ -27,6 +27,13 @@ typedef struct Vec
|
|||
bool isfloat;
|
||||
} Vec;
|
||||
|
||||
typedef struct Slice
|
||||
{
|
||||
Vec*vec;
|
||||
size_t start;
|
||||
size_t end;
|
||||
} Slice;
|
||||
|
||||
Vec vec_new(size_t unitsize);
|
||||
void vec_free(Vec*v);
|
||||
void vec_grow(Vec*v);
|
||||
|
|
Loading…
Reference in New Issue
Block a user