From 5c0965f636358aed11499a95cc12387fe5e06047 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 12 Aug 2016 22:28:55 +0200 Subject: Improve ast_debug_ --- code.txt | 2 +- parser.c | 23 ++++++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/code.txt b/code.txt index 4a9abcf..0c0ed92 100644 --- a/code.txt +++ b/code.txt @@ -1,4 +1,4 @@ -def main(){ +def main(argc,argv){ a = 1; b = !2; c = 1 + (- a - 3 + b > -1); diff --git a/parser.c b/parser.c index 6f2ce2d..63c865b 100644 --- a/parser.c +++ b/parser.c @@ -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;ib.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;jf.nargs;j++){ - if(j!=0)fputc(',',stream); + if(j!=0)fprintf(stream,", "); fprintf(stream,"%s",ast->f.args[j]); } fprintf(stream,") "); -- cgit v1.2.3