diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-08-12 22:28:55 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-08-12 22:32:01 +0200 |
commit | 5c0965f636358aed11499a95cc12387fe5e06047 (patch) | |
tree | 31eecbc06241f8a02ff606c7db867603b4b6ad64 | |
parent | 1d274cdb2fa2cfe33397898358b5e77fe818a2fc (diff) |
Improve ast_debug_
-rw-r--r-- | code.txt | 2 | ||||
-rw-r--r-- | parser.c | 23 |
2 files changed, 11 insertions, 14 deletions
@@ -1,4 +1,4 @@ -def main(){ +def main(argc,argv){ a = 1; b = !2; c = 1 + (- a - 3 + b > -1); @@ -37,17 +37,17 @@ static char hexencode(int n){ } static bool semicolon_needed_after(const AST *after){ - DBGF("semicolon_needed_after(type %d) -> ",after->type); + //DBGF("semicolon_needed_after(type %d) -> ",after->type); switch(after->type){ case AST_BLOCK: case AST_IF: case AST_WHILE: case AST_FUNC: - DBGF("false\n"); + //DBGF("false\n"); return false; default: - DBGF("true\n"); + //DBGF("true\n"); return true; } } @@ -720,7 +720,8 @@ static void ast_debug_(FILE *stream,const AST *ast,int indent){ for(int i=0;i<ast->b.len;i++){ INDENT ast_debug_(stream,ast->b.exprs[i],indent); - fprintf(stream,";\n"); + if(semicolon_needed_after(ast->b.exprs[i]))fputc(';',stream); + fputc('\n',stream); } indent--; INDENT @@ -771,19 +772,15 @@ static void ast_debug_(FILE *stream,const AST *ast,int indent){ case AST_IF: fprintf(stream,"if "); ast_debug_(stream,ast->i.cond,indent); - fputc('\n',stream); - indent++; - INDENT + fputc(' ',stream); ast_debug_(stream,ast->i.thenb,indent); - indent--; + if(semicolon_needed_after(ast->i.thenb))fputc(';',stream); if(ast->i.elseb){ fputc('\n',stream); INDENT - fprintf(stream,"else\n"); - indent++; - INDENT + fprintf(stream,"else "); ast_debug_(stream,ast->i.elseb,indent); - indent--; + if(semicolon_needed_after(ast->i.elseb))fputc(';',stream); } break; @@ -794,7 +791,7 @@ static void ast_debug_(FILE *stream,const AST *ast,int indent){ case AST_FUNC: fprintf(stream,"def %s(",ast->f.name); for(int j=0;j<ast->f.nargs;j++){ - if(j!=0)fputc(',',stream); + if(j!=0)fprintf(stream,", "); fprintf(stream,"%s",ast->f.args[j]); } fprintf(stream,") "); |