extra debug information
This commit is contained in:
parent
43af3d79c8
commit
0a035e5901
2
Makefile
2
Makefile
@ -2,7 +2,7 @@
|
||||
# VARIABLES/MACROS
|
||||
#
|
||||
CFLAGS= -Wfatal-errors -Wall -Wextra
|
||||
LDFLAGS= -g -no-pie -z noexecstack -lm
|
||||
LDFLAGS= -g3 -no-pie -z noexecstack -lm
|
||||
NASMFLAGS= -Fdwarf -felf64
|
||||
OBJS= str.o tok.o vec.o lex.o pnode.o reg.o mem.o state.o err.o x86_64.o run.o ir.o gen.o
|
||||
LIB= libpar.a
|
||||
|
28
lex.c
28
lex.c
@ -31,7 +31,7 @@ void lex_free(Lexer*l)
|
||||
{
|
||||
if(!l)return;
|
||||
for(size_t i=0;i<l->tokens.size;++i)
|
||||
str_free(&((Tok*)l->tokens.buffer)[i].str);
|
||||
str_free(&vec_at(&l->tokens,i,Tok*)->str);
|
||||
vec_free(&(l->tokens));
|
||||
}
|
||||
|
||||
@ -60,14 +60,17 @@ void lex_string(Lexer*l,char*s)
|
||||
* mode uint32_t change lexer mode to this
|
||||
* keepch bool will we retain this character in the token string?
|
||||
*****/
|
||||
#define initmatch(chset,lmode,keepch) if(s[i]&&memchr((chset),s[i],strlen((chset)))){l->mode=(lmode);if(keepch)--i;vec_pushl(&l->tokens,((Tok){.str=str_new(),.type=l->mode,.line=current_line}));str_clear(&tmpstr);}
|
||||
#define initmatch(chset,lmode,keepch) if(s[i]&&memchr((chset),s[i],strlen((chset)))){l->mode=(lmode);if(keepch)--i;\
|
||||
Tok _tmptok={.str=str_new(),.type=l->mode,.line=current_line};vec_push(&l->tokens,&_tmptok);\
|
||||
str_clear(&tmpstr);}
|
||||
|
||||
/*****
|
||||
* modeterminate
|
||||
* - Finalize current token lexing and set state to LNONE
|
||||
* keepch bool will we retain this character in the token string?
|
||||
*****/
|
||||
#define modeterminate(keepch) do{l->mode=LNONE;if(keepch)--i;str_assign(&((Tok*)l->tokens.buffer)[l->tokens.size-1].str,tmpstr.buffer);((Tok*)l->tokens.buffer)[l->tokens.size-1].line=current_line;ch[0]=s[i];str_append(&tmpstr,ch);}while(0)
|
||||
//&((Tok*)l->tokens.buffer)[l->tokens.size-1].str
|
||||
#define modeterminate(keepch) do{l->mode=LNONE;if(keepch)--i;str_assign(&(vec_at(&l->tokens,l->tokens.size-1,Tok*)->str),tmpstr.buffer);vec_at(&l->tokens,l->tokens.size-1,Tok*)->line=current_line;ch[0]=s[i];str_append(&tmpstr,ch);}while(0)
|
||||
|
||||
/*****
|
||||
* modematch(chset,logic,keepch)
|
||||
@ -96,7 +99,8 @@ void lex_string(Lexer*l,char*s)
|
||||
// Extra step: turn identifiers into keywords
|
||||
if(l->tokens.size)
|
||||
{
|
||||
Tok*lasttok=&((Tok*)l->tokens.buffer)[l->tokens.size-2];
|
||||
//Tok*lasttok=&((Tok*)l->tokens.buffer)[l->tokens.size-2];
|
||||
Tok*lasttok=vec_at(&l->tokens,l->tokens.size-2,Tok*);
|
||||
if(lasttok->type==LIDENTIFIER)
|
||||
{
|
||||
for(size_t j=0;j<sizeof(keywords)/sizeof(char*);++j)
|
||||
@ -129,7 +133,7 @@ void lex_string(Lexer*l,char*s)
|
||||
case LINTEGER:modematch("0123456789.",false,true);
|
||||
if(s[i]=='.')
|
||||
{
|
||||
((Tok*)l->tokens.buffer)[l->tokens.size-1].type=LFLOAT;
|
||||
vec_at(&l->tokens,l->tokens.size-1,Tok*)->type=LFLOAT;
|
||||
l->mode=LFLOAT;
|
||||
}
|
||||
break;
|
||||
@ -137,8 +141,8 @@ void lex_string(Lexer*l,char*s)
|
||||
case LOPERATOR:modematch(operator_chars,false,true);
|
||||
switch(s[i])
|
||||
{
|
||||
#define opmatch(lstype,keepch) do{((Tok*)l->tokens.buffer)[l->tokens.size-1].subtype=lstype;modeterminate(keepch);}while(0)
|
||||
#define opmatch_nodup(lstype,keepch) do{l->mode=LNONE;((Tok*)l->tokens.buffer)[l->tokens.size-1].line=current_line;ch[0]=s[i];str_append(&tmpstr,ch);}while(0)
|
||||
#define opmatch(lstype,keepch) do{vec_at(&l->tokens,l->tokens.size-1,Tok*)->subtype=lstype;modeterminate(keepch);}while(0)
|
||||
#define opmatch_nodup(lstype,keepch) do{l->mode=LNONE;vec_at(&l->tokens,l->tokens.size-1,Tok*)->line=current_line;ch[0]=s[i];str_append(&tmpstr,ch);}while(0)
|
||||
case ';':opmatch(LENDSTATEMENT,false);break;
|
||||
case '(':opmatch(LLPAREN,false);break;
|
||||
case ')':opmatch(LRPAREN,false);break;
|
||||
@ -147,7 +151,7 @@ void lex_string(Lexer*l,char*s)
|
||||
case '*':opmatch(0,false);break;
|
||||
case '=':
|
||||
{
|
||||
char*ss=((Tok*)l->tokens.buffer)[l->tokens.size-1].str.buffer;
|
||||
char*ss=vec_at(&l->tokens,l->tokens.size-1,Tok*)->str.buffer;
|
||||
if(!ss)break;
|
||||
if(strcmp("==",ss)==0)opmatch_nodup(0,false);
|
||||
else if(strcmp("+=",ss)==0)opmatch_nodup(LASSIGN,false);
|
||||
@ -164,14 +168,14 @@ void lex_string(Lexer*l,char*s)
|
||||
case LMINUS://modematch("0123456789=",true,true);
|
||||
if(s[i]=='-')
|
||||
{
|
||||
((Tok*)l->tokens.buffer)[l->tokens.size-1].type=LOPERATOR;
|
||||
((Tok*)l->tokens.buffer)[l->tokens.size-1].subtype=LSMINUS;
|
||||
vec_at(&l->tokens,l->tokens.size-1,Tok*)->type=LOPERATOR;
|
||||
vec_at(&l->tokens,l->tokens.size-1,Tok*)->subtype=LSMINUS;
|
||||
ch[0]=s[i];str_append(&tmpstr,ch);
|
||||
modeterminate(false);
|
||||
}
|
||||
else// if(s[i]=='=')
|
||||
{
|
||||
((Tok*)l->tokens.buffer)[l->tokens.size-1].type=LOPERATOR;
|
||||
vec_at(&l->tokens,l->tokens.size-1,Tok*)->type=LOPERATOR;
|
||||
l->mode=LOPERATOR;
|
||||
modeterminate(true);
|
||||
}
|
||||
@ -193,7 +197,7 @@ void lex_print(Lexer*l)
|
||||
printf("%p: (%lu/%lu) [",l,l->tokens.size,l->tokens.capacity);
|
||||
for(size_t i=0;i<l->tokens.size;++i)
|
||||
{
|
||||
Tok*tok=&((Tok*)l->tokens.buffer)[i];
|
||||
Tok*tok=vec_at(&l->tokens,i,Tok*);
|
||||
printf("'%s'(%s %u)",
|
||||
tok->str.buffer,
|
||||
((tok->subtype)?(lexsubtype_names[tok->subtype-LENDSTATEMENT]):(lextype_names[tok->type])),
|
||||
|
Loading…
Reference in New Issue
Block a user