summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-08-12 22:28:55 +0200
committertomsmeding <tom.smeding@gmail.com>2016-08-12 22:32:01 +0200
commit5c0965f636358aed11499a95cc12387fe5e06047 (patch)
tree31eecbc06241f8a02ff606c7db867603b4b6ad64
parent1d274cdb2fa2cfe33397898358b5e77fe818a2fc (diff)
Improve ast_debug_
-rw-r--r--code.txt2
-rw-r--r--parser.c23
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;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,") ");