eval: count number of function arguments in invocation

This commit is contained in:
corey 2024-01-22 10:14:39 -06:00
parent 54c21f46f8
commit cd808539c3
4 changed files with 67 additions and 3 deletions

62
gen.c
View File

@ -670,10 +670,70 @@ void gen_eval(Gen*gen,const PNode*pn,FILE*file)
rpn_stack=gen_i2r(&pn->tokens);
if(gen->showrpn)vec_print_tokens(&rpn_stack);
// TODO: Find function invocations in pn->tokens
// Find function invocations in pn->tokens
// and count their number of arguments. This
// will be used to pop the right number of
// values from the stack.
for(size_t i=0;i<pn->tokens.size;++i)
{
for(size_t j=0;j<gen->rootnode->funcs.size;++j)
{
// Identified a function
if(strcmp(vec_at(&pn->tokens,i,const Tok*)->str.buffer,
vec_at(&gen->rootnode->funcs,j,const Func*)->name)==0)
{
Tok*functok=vec_at(&pn->tokens,i,Tok*);
/* printf("func '%s': [",functok->str.buffer); */
functok->fn_no_args=0;
size_t parenlvl=0;
bool foundone=false;
// Find function tokens, record number of
// arguments passed to them
for(size_t k=i+1;k<pn->tokens.size;++k)
{
Tok*curtok=vec_at(&pn->tokens,k,Tok*);
switch(curtok->subtype)
{
case LLPAREN:
++parenlvl;
break;
case LRPAREN:
{
if(foundone)
{
++functok->fn_no_args;
foundone=false;
}
--parenlvl;
if(parenlvl==0)
k=pn->tokens.size;
}
break;
case LSCOMMA:
if(foundone)
{
++functok->fn_no_args;
foundone=false;
}
break;
default:
foundone=true;
break;
}
/* printf("%s%s",curtok->str.buffer,k<pn->tokens.size-1?" ":""); */
}
/* printf("'%s': %lu\n",functok->str.buffer,functok->fn_no_args); */
}
}
}
/* gen_eval_analyze(pn); */

3
lex.c
View File

@ -12,7 +12,7 @@
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",NULL};
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",};
/* static char*operators[]={";","=","+=","-=","*=","/=","+","-","/","*","(",")","{","}"}; */
@ -172,6 +172,7 @@ void lex_string(Lexer*lex,char*input_string)
case '/':opmatch(LSDIV,false);break;
case '!':opmatch(LSNOT,false);break;
case ':':opmatch(LSCOLON,false);break;
case ',':opmatch(LSCOMMA,false);break;
case '=':
{
opmatch(LASSIGN,false);

2
lex.h
View File

@ -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, };
enum LEXSUBTYPE {LENDSTATEMENT=55, LASSIGN, LLPAREN, LRPAREN, LLCBRACE, LRCBRACE, LSMINUS, LADD, LSMUL, LSDIV, LSNOT, LSCOLON, LSCOMMA, };
extern const char*lextype_names[];
extern const char*lexsubtype_names[];
extern const char*lextype_colors[];

3
tok.h
View File

@ -15,6 +15,9 @@ typedef struct Tok
uint32_t type;
uint32_t subtype;
size_t line;
size_t fn_no_args; // if this is a function invocation,
// how many arguments are being passed
// (used individually per expression)
} Tok;
Tok tok_new(void);