move func/var identification away from operator eval
This commit is contained in:
parent
dcb6a77c30
commit
be5bd4d5fe
86
gen.c
86
gen.c
@ -726,7 +726,13 @@ void gen_eval(Gen*gen,const PNode*pn,FILE*file)
|
||||
|
||||
case LIDENTIFIER:
|
||||
{
|
||||
// Convert identifiers into functions or vars
|
||||
// Otherwise it is an error
|
||||
eval_elem tmpelem={.type=EE_IDENT,.val.name=vec_at(&rpn_stack,i,const Tok*)->str.buffer};
|
||||
if(!gen_findfunc(gen,&tmpelem))
|
||||
if(!gen_findvar(gen,&tmpelem))
|
||||
err_log("%u: undeclared identifier '%s'",vec_at(&pn->tokens,0,Tok*)->line,tmpelem.val.name);
|
||||
printf("[[%s:%lu]]\n",tmpelem.val.name,tmpelem.type);
|
||||
vec_push(&stack,&tmpelem);
|
||||
}
|
||||
break;
|
||||
@ -759,37 +765,20 @@ void gen_eval(Gen*gen,const PNode*pn,FILE*file)
|
||||
elem[k]=*vec_at(&stack,stack.size-1,eval_elem*);
|
||||
vec_pop(&stack);
|
||||
|
||||
if(elem[k].type==EE_IDENT)
|
||||
if(elem[k].type==EE_FUNC)
|
||||
{
|
||||
bool found=false;
|
||||
|
||||
found=gen_findfunc(gen,&elem[k]);
|
||||
if(found)
|
||||
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;
|
||||
}
|
||||
continue;
|
||||
eval_elem tmpelem={.type=EE_ACC,.val.i32=0};
|
||||
vec_push(&stack,&tmpelem);
|
||||
--k;
|
||||
}
|
||||
|
||||
// Identify variables
|
||||
if(!found)
|
||||
{
|
||||
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; */
|
||||
|
||||
// Still not categorized as func or var
|
||||
else if(elem[k].type==EE_IDENT)
|
||||
err_log("%u: undeclared identifier '%s'",vec_at(&pn->tokens,0,Tok*)->line,elem[k].val.name);
|
||||
|
||||
}
|
||||
gen_x86_64_evalop((PNode*)pn,elem,nops,vec_at(&rpn_stack,i,Tok*),file,&stack);
|
||||
}
|
||||
@ -814,42 +803,31 @@ void gen_eval(Gen*gen,const PNode*pn,FILE*file)
|
||||
fprintf(file,"\tmovl $%d,%%eax\n",value);
|
||||
}
|
||||
|
||||
else if(e.type==EE_IDENT)
|
||||
else if(e.type==EE_IDENT||
|
||||
e.type==EE_FUNC||
|
||||
e.type==EE_VAR)
|
||||
{
|
||||
bool found=false;
|
||||
|
||||
// Identify functions
|
||||
for(size_t j=0;j<gen->rootnode->funcs.size;++j)
|
||||
e=*vec_at(&stack,stack.size-1,eval_elem*);
|
||||
vec_pop(&stack);
|
||||
|
||||
if(e.type==EE_FUNC)
|
||||
{
|
||||
if(strcmp(vec_at(&gen->rootnode->funcs,j,Func*)->name,e.val.name)==0)
|
||||
fprintf(file,"\tcall %s\n",e.val.name);
|
||||
{
|
||||
e.type=EE_FUNC;
|
||||
fprintf(file,"\tcall %s\n",e.val.name);
|
||||
found=true;
|
||||
break;
|
||||
eval_elem tmpelem={.type=EE_ACC,.val.i32=0};
|
||||
vec_push(&stack,&tmpelem);
|
||||
}
|
||||
}
|
||||
|
||||
// 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,e.val.name)==0)
|
||||
{
|
||||
e.type=EE_VAR;
|
||||
e.vtype=vec_at(&gen->stackptr->vars,j,Var*)->type;
|
||||
e.vstackloc=vec_at(&gen->stackptr->vars,j,Var*)->stackloc;
|
||||
// Still not categorized as func or var
|
||||
else if(e.type==EE_IDENT)
|
||||
err_log("%u: undeclared identifier '%s'",vec_at(&pn->tokens,0,Tok*)->line,e.val.name);
|
||||
|
||||
fprintf(file,"\tmovl -%lu(%%%s),%%eax\n",e.vstackloc,gen_x86_64_bp);
|
||||
found=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(e.type==EE_VAR)
|
||||
fprintf(file,"\tmovl -%lu(%%%s),%%eax\n",e.vstackloc,gen_x86_64_bp);
|
||||
/* fprintf(file,"\tmovl -%lu(%%ebp),%%eax\n",e.vstackloc); */
|
||||
|
||||
if(!found)
|
||||
err_log("%u: undeclared identifier '%s'",vec_at(&pn->tokens,0,Tok*)->line,e.val.name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user