gen_findvar, gen_findfunc
This commit is contained in:
parent
bf96ae53a5
commit
67d3a45adc
264
gen.c
264
gen.c
@ -22,103 +22,6 @@ void gen_setrootnode(Gen*gen,PNode*rootnode)
|
||||
gen->rootnode=rootnode;
|
||||
}
|
||||
|
||||
// Convert infix to RPN
|
||||
// Vec<Tok>
|
||||
Vec gen_i2r(const Vec*tokens)
|
||||
{
|
||||
Vec stack=vec_new(sizeof(const Tok));
|
||||
Vec operators=vec_new(sizeof(const Tok));
|
||||
int precedence[1024]={0};
|
||||
precedence['(']=5;
|
||||
precedence[')']=5;
|
||||
precedence['+']=10;
|
||||
precedence['-']=10;
|
||||
precedence['!']=10;
|
||||
precedence['*']=20;
|
||||
precedence['/']=20;
|
||||
|
||||
if(tokens->size>0)
|
||||
{
|
||||
for(size_t i=0;i<tokens->size;++i)
|
||||
{
|
||||
|
||||
//printf("[[%s]]\n",vec_at(tokens,i,const Tok*)->str.buffer);
|
||||
|
||||
// identifiers
|
||||
switch(vec_at(tokens,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
// integers, keywords (true/false), identifiers
|
||||
case LIDENTIFIER:
|
||||
case LKEYWORD:
|
||||
case LINTEGER:
|
||||
case LFLOAT:
|
||||
vec_push(&stack,vec_at(tokens,i,const Tok*));
|
||||
break;
|
||||
|
||||
// operators
|
||||
case LOPERATOR:
|
||||
{
|
||||
|
||||
// '('
|
||||
if(vec_at(tokens,i,const Tok*)->subtype==LLPAREN)
|
||||
{
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
// ')'
|
||||
else if(vec_at(tokens,i,const Tok*)->subtype==LRPAREN)
|
||||
{
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,const Tok*)->subtype!=LLPAREN)
|
||||
{
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
||||
// Default operators
|
||||
else
|
||||
{
|
||||
|
||||
// Pop operators from operators ==> stack
|
||||
// who have higher precedence than current
|
||||
// operator
|
||||
while(operators.size>0&&precedence[(uint32_t)vec_at(&operators,operators.size-1,const Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,const Tok*)->str.buffer[0]])
|
||||
{
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
while(operators.size>0)
|
||||
{
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
||||
/* printf("s:"); */
|
||||
/* vec_print_tokens(&stack); */
|
||||
/* printf("o:"); */
|
||||
/* vec_print_tokens(&operators); */
|
||||
|
||||
/* vec_free(&stack); */
|
||||
vec_free(&operators);
|
||||
|
||||
// move ownership to caller
|
||||
return stack;
|
||||
}
|
||||
|
||||
bool gen_findret(const PNode*pn,bool found)
|
||||
{
|
||||
if(found)return true;
|
||||
@ -460,6 +363,38 @@ size_t gen_stacksize(const PNode*pn)
|
||||
return stacksize;
|
||||
}
|
||||
|
||||
// Identify functions
|
||||
bool gen_findfunc(const Gen*const gen,eval_elem*e)
|
||||
{
|
||||
if(!gen){err_log("gen_findfunc: bad gen ");return false;}if(!e){err_log("gen_findfunc: bad eval_elem ");return false;}if(!e->val.name){err_log("gen_findfunc: bad eval_elem name ");return false;}
|
||||
|
||||
for(size_t j=0;j<gen->rootnode->funcs.size;++j)
|
||||
{
|
||||
if(strcmp(vec_at(&gen->rootnode->funcs,j,Func*)->name,e->val.name)==0)
|
||||
{
|
||||
e->type=EE_FUNC;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool gen_findvar(const Gen*const gen,eval_elem*e)
|
||||
{
|
||||
for(size_t j=0;j<gen->stackptr->vars.size;++j)
|
||||
{
|
||||
if(strcmp(vec_at(&gen->stackptr->vars,j,Var*)->name,e->val.name)==0)
|
||||
{
|
||||
e->type=EE_VAR;
|
||||
e->vstackloc=vec_at(&gen->stackptr->vars,j,Var*)->stackloc;
|
||||
e->vtype=vec_at(&gen->stackptr->vars,j,Var*)->type;
|
||||
//var_stackloc[k]=vec_at(&gen->stackptr->vars,j,Var*)->stackloc;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void gen_code(Gen*gen,PNode*pn,FILE*file)
|
||||
{
|
||||
|
||||
@ -650,6 +585,103 @@ void gen_code(Gen*gen,PNode*pn,FILE*file)
|
||||
//gen_code(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
}
|
||||
|
||||
// Convert infix to RPN
|
||||
// Vec<Tok>
|
||||
Vec gen_i2r(const Vec*tokens)
|
||||
{
|
||||
Vec stack=vec_new(sizeof(const Tok));
|
||||
Vec operators=vec_new(sizeof(const Tok));
|
||||
int precedence[1024]={0};
|
||||
precedence['(']=5;
|
||||
precedence[')']=5;
|
||||
precedence['+']=10;
|
||||
precedence['-']=10;
|
||||
precedence['!']=10;
|
||||
precedence['*']=20;
|
||||
precedence['/']=20;
|
||||
|
||||
if(tokens->size>0)
|
||||
{
|
||||
for(size_t i=0;i<tokens->size;++i)
|
||||
{
|
||||
|
||||
//printf("[[%s]]\n",vec_at(tokens,i,const Tok*)->str.buffer);
|
||||
|
||||
// identifiers
|
||||
switch(vec_at(tokens,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
// integers, keywords (true/false), identifiers
|
||||
case LIDENTIFIER:
|
||||
case LKEYWORD:
|
||||
case LINTEGER:
|
||||
case LFLOAT:
|
||||
vec_push(&stack,vec_at(tokens,i,const Tok*));
|
||||
break;
|
||||
|
||||
// operators
|
||||
case LOPERATOR:
|
||||
{
|
||||
|
||||
// '('
|
||||
if(vec_at(tokens,i,const Tok*)->subtype==LLPAREN)
|
||||
{
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
// ')'
|
||||
else if(vec_at(tokens,i,const Tok*)->subtype==LRPAREN)
|
||||
{
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,const Tok*)->subtype!=LLPAREN)
|
||||
{
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
||||
// Default operators
|
||||
else
|
||||
{
|
||||
|
||||
// Pop operators from operators ==> stack
|
||||
// who have higher precedence than current
|
||||
// operator
|
||||
while(operators.size>0&&precedence[(uint32_t)vec_at(&operators,operators.size-1,const Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,const Tok*)->str.buffer[0]])
|
||||
{
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
while(operators.size>0)
|
||||
{
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
||||
/* printf("s:"); */
|
||||
/* vec_print_tokens(&stack); */
|
||||
/* printf("o:"); */
|
||||
/* vec_print_tokens(&operators); */
|
||||
|
||||
/* vec_free(&stack); */
|
||||
vec_free(&operators);
|
||||
|
||||
// move ownership to caller
|
||||
return stack;
|
||||
}
|
||||
|
||||
// Convert to infix, then evaluate
|
||||
void gen_eval(Gen*gen,const PNode*pn,FILE*file)
|
||||
{
|
||||
@ -731,42 +763,30 @@ void gen_eval(Gen*gen,const PNode*pn,FILE*file)
|
||||
{
|
||||
bool found=false;
|
||||
|
||||
// Identify functions
|
||||
for(size_t j=0;j<gen->rootnode->funcs.size;++j)
|
||||
found=gen_findfunc(gen,&elem[k]);
|
||||
if(found)
|
||||
{
|
||||
if(strcmp(vec_at(&gen->rootnode->funcs,j,Func*)->name,elem[k].val.name)==0)
|
||||
elem[k].type=EE_FUNC;
|
||||
fprintf(file,"\tcall %s\n",elem[k].val.name);
|
||||
{
|
||||
elem[k].type=EE_FUNC;
|
||||
fprintf(file,"\tcall %s\n",elem[k].val.name);
|
||||
{
|
||||
eval_elem tmpelem={.type=EE_ACC,.val.i32=0};
|
||||
vec_push(&stack,&tmpelem);
|
||||
--k;
|
||||
}
|
||||
found=true;
|
||||
break;
|
||||
eval_elem tmpelem={.type=EE_ACC,.val.i32=0};
|
||||
vec_push(&stack,&tmpelem);
|
||||
--k;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Identify variables
|
||||
if(!found)
|
||||
{
|
||||
for(size_t j=0;j<gen->stackptr->vars.size;++j)
|
||||
{
|
||||
if(strcmp(vec_at(&gen->stackptr->vars,j,Var*)->name,elem[k].val.name)==0)
|
||||
{
|
||||
elem[k].type=EE_VAR;
|
||||
elem[k].vstackloc=vec_at(&gen->stackptr->vars,j,Var*)->stackloc;
|
||||
elem[k].vtype=vec_at(&gen->stackptr->vars,j,Var*)->type;
|
||||
//var_stackloc[k]=vec_at(&gen->stackptr->vars,j,Var*)->stackloc;
|
||||
found=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
found=gen_findvar(gen,&elem[k]);
|
||||
if(!found)
|
||||
err_log("%u: undeclared identifier '%s'",vec_at(&pn->tokens,0,Tok*)->line,elem[k].val.name);
|
||||
/* else */
|
||||
/* printf("found '%s'\n",elem[k].val.name); */
|
||||
}
|
||||
else
|
||||
continue;
|
||||
}
|
||||
/* else if(elem[k].type==EE_I32) */
|
||||
/* integers[k]=elem[k].val.i32; */
|
||||
|
8
x86_64.c
8
x86_64.c
@ -32,7 +32,9 @@ void gen_x86_64_evalop(PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file,
|
||||
/* for(size_t i=0;i<nops;++i) */
|
||||
/* { */
|
||||
/* if(elem[i].type==EE_VAR) */
|
||||
/* sprint(operands[i],"%s",elem[i].val.name); */
|
||||
/* sprint(operands[i],"v::%s",elem[i].val.name); */
|
||||
/* else if(elem[i].type==EE_FUNC) */
|
||||
/* sprint(operands[i],"f::%s",elem[i].val.name); */
|
||||
/* else if(elem[i].type==EE_I32) */
|
||||
/* sprint(operands[i],"%d",elem[i].val.i32); */
|
||||
/* else */
|
||||
@ -51,10 +53,12 @@ void gen_x86_64_evalop(PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file,
|
||||
/* sprint(operator,"imull"); */
|
||||
/* else if(strcmp("/",curtok->str.buffer)==0) */
|
||||
/* sprint(operator,"idivl"); */
|
||||
/* else if(strcmp("=",curtok->str.buffer)==0) */
|
||||
/* sprint(operator,"movl"); */
|
||||
/* */
|
||||
/* // Print output */
|
||||
/* if(nops>1) */
|
||||
/* printf("%s %s %s\n",operator,operands[1],operands[0]); */
|
||||
/* printf("%s %s, %s\n",operator,operands[0],operands[1]); */
|
||||
/* else */
|
||||
/* printf("%s %s\n",operator,operands[0]); */
|
||||
/* } */
|
||||
|
Loading…
Reference in New Issue
Block a user