2023-11-09 13:00:58 -06:00
|
|
|
#include"gen.h"
|
|
|
|
|
|
|
|
Gen gen_new(void)
|
|
|
|
{
|
2023-11-12 07:14:56 -06:00
|
|
|
Gen gen={
|
|
|
|
.labelno=0,
|
|
|
|
.stackptr=NULL,
|
|
|
|
};
|
2023-11-09 13:00:58 -06:00
|
|
|
return gen;
|
|
|
|
}
|
2023-11-10 05:21:58 -06:00
|
|
|
|
|
|
|
void gen_free(Gen*gen)
|
|
|
|
{
|
2023-11-12 06:59:42 -06:00
|
|
|
if(!gen)return;
|
2023-11-10 05:21:58 -06:00
|
|
|
}
|
2023-11-16 12:54:50 -06:00
|
|
|
|
|
|
|
void gen_setrootnode(Gen*gen,PNode*rootnode)
|
|
|
|
{
|
|
|
|
if(!gen||!rootnode)return;
|
|
|
|
gen->rootnode=rootnode;
|
|
|
|
}
|
2024-01-12 17:16:49 -06:00
|
|
|
|
|
|
|
// 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['*']=20;
|
|
|
|
precedence['/']=20;
|
|
|
|
|
|
|
|
if(tokens->size>0)
|
|
|
|
{
|
|
|
|
for(size_t i=0;i<tokens->size;++i)
|
|
|
|
{
|
|
|
|
|
|
|
|
// identifiers
|
|
|
|
switch(vec_at(tokens,i,const Tok*)->type)
|
|
|
|
{
|
|
|
|
|
|
|
|
// integers, keywords (true/false), identifiers
|
|
|
|
case LIDENTIFIER:
|
|
|
|
case LKEYWORD:
|
|
|
|
case LINTEGER:
|
|
|
|
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;
|
|
|
|
}
|