PIF+PWHILE eval, 'true'/'false' eval
This commit is contained in:
parent
478e99cc43
commit
545b77c6be
122
x86_64.c
122
x86_64.c
@ -78,10 +78,10 @@ void gen_x86_64(PNode*pn,FILE*file)
|
||||
else
|
||||
fprintf(file,"\tmov eax,-%s\n",vec_at(&pn->tokens,1,Tok*)->str.buffer);
|
||||
}
|
||||
else if(vec_at(&pn->tokens,0,Tok*)->type!=LINTEGER)
|
||||
{
|
||||
err_log("%u: returning non-integer",vec_at(&pn->tokens,0,Tok*)->line);
|
||||
}
|
||||
/* else if(vec_at(&pn->tokens,0,Tok*)->type!=LINTEGER) */
|
||||
/* { */
|
||||
/* err_log("%u: returning non-integer",vec_at(&pn->tokens,0,Tok*)->line); */
|
||||
/* } */
|
||||
else
|
||||
gen_x86_64_eval(pn,file);
|
||||
}
|
||||
@ -152,15 +152,31 @@ void gen_x86_64(PNode*pn,FILE*file)
|
||||
|
||||
case PIF:
|
||||
if(pn->tokens.size>1)
|
||||
fprintf(file,"\tcmp eax,0 ;%s\n",vec_at(&pn->tokens,1,Tok*)->str.buffer);
|
||||
{
|
||||
gen_x86_64_eval(pn,file);
|
||||
fprintf(file,"\tcmp eax,0\n");
|
||||
}
|
||||
fprintf(file,"\tjz .L%02lu\n",++labelno);
|
||||
fprintf(file,".L%02lu:\n",labelno);
|
||||
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_x86_64(vec_at(&pn->pnodes,i,PNode*),file);
|
||||
|
||||
fprintf(file,".L%02lu\n",labelno);
|
||||
break;
|
||||
|
||||
case PWHILE:
|
||||
fprintf(file,".L%02lu:\n",++labelno);
|
||||
if(pn->tokens.size>1)
|
||||
fprintf(file,"\tcmp eax,0 ;%s\n",vec_at(&pn->tokens,1,Tok*)->str.buffer);
|
||||
{
|
||||
gen_x86_64_eval(pn,file);
|
||||
fprintf(file,"\tcmp eax,0\n");
|
||||
}
|
||||
fprintf(file,"\tjz .L%02lu\n",++labelno);
|
||||
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_x86_64(vec_at(&pn->pnodes,i,PNode*),file);
|
||||
|
||||
fprintf(file,"\tjmp .L%02lu\n",labelno-1);
|
||||
fprintf(file,".L%02lu\n",labelno);
|
||||
break;
|
||||
|
||||
@ -275,50 +291,55 @@ Vec gen_x86_64_i2r(Vec*tokens)
|
||||
{
|
||||
|
||||
// identifiers
|
||||
if(vec_at(tokens,i,Tok*)->type==LIDENTIFIER)
|
||||
vec_push(&stack,vec_at(tokens,i,Tok*));
|
||||
|
||||
// integers
|
||||
if(vec_at(tokens,i,Tok*)->type==LINTEGER)
|
||||
vec_push(&stack,vec_at(tokens,i,Tok*));
|
||||
// operators
|
||||
else if(vec_at(tokens,i,Tok*)->type==LOPERATOR)
|
||||
switch(vec_at(tokens,i,Tok*)->type)
|
||||
{
|
||||
|
||||
// '('
|
||||
if(vec_at(tokens,i,Tok*)->subtype==LLPAREN)
|
||||
{
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
}
|
||||
// integers, keywords (true/false), identifiers
|
||||
case LIDENTIFIER:
|
||||
case LKEYWORD:
|
||||
case LINTEGER:
|
||||
vec_push(&stack,vec_at(tokens,i,Tok*));
|
||||
break;
|
||||
|
||||
// ')'
|
||||
else if(vec_at(tokens,i,Tok*)->subtype==LRPAREN)
|
||||
{
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,Tok*)->subtype!=LLPAREN)
|
||||
// operators
|
||||
case LOPERATOR:
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
|
||||
// '('
|
||||
if(vec_at(tokens,i,Tok*)->subtype==LLPAREN)
|
||||
{
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
}
|
||||
|
||||
// ')'
|
||||
else if(vec_at(tokens,i,Tok*)->subtype==LRPAREN)
|
||||
{
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,Tok*)->subtype!=LLPAREN)
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,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,Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,Tok*)->str.buffer[0]])
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
}
|
||||
|
||||
}
|
||||
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,Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,Tok*)->str.buffer[0]])
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -364,6 +385,15 @@ void gen_x86_64_eval(PNode*pn,FILE*file)
|
||||
switch(vec_at(&rpn_stack,i,Tok*)->type)
|
||||
{
|
||||
|
||||
case LKEYWORD:
|
||||
{
|
||||
if(strcmp("true",vec_at(&rpn_stack,i,Tok*)->str.buffer)==0)
|
||||
vec_pushi(&stack,1);
|
||||
else if(strcmp("false",vec_at(&rpn_stack,i,Tok*)->str.buffer)==0)
|
||||
vec_pushi(&stack,0);
|
||||
}
|
||||
break;
|
||||
|
||||
case LIDENTIFIER:
|
||||
{
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user