diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-10-06 15:37:24 -0400 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-10-06 15:37:24 -0400 |
commit | a0ae72558a301f4a0082739712d30919f5e1454e (patch) | |
tree | 1181bfb020af44a7497238b94dd8a1f40c7a54f3 | |
parent | 563010bc7d2ad932b6096e90c048f8a6cdccef82 (diff) |
Fix tree_print
Tree-print now shows "branches" of tree for readability
Now tests if t is null and returns rather then testing t is not null and
then printing the node, this allows for less indentation in the code.
-rw-r--r-- | tree.c | 173 |
1 files changed, 89 insertions, 84 deletions
@@ -149,92 +149,97 @@ ptree* t; int spaces; { int i; - if ( t ) { - for (i = 0; i < spaces; i++) + if (!t) + return; + + for (i = 0; i < spaces - 1; i++) + if (!(i%2)) + fprintf(stderr,"|"); + else fprintf(stderr," "); - switch (t->type) { - case ADDOP: - fprintf(stderr, "[ADDOP]"); - break; - case MULOP: - fprintf(stderr, "[MULOP]"); - break; - case RELOP: - fprintf(stderr, "[RELOP]"); - break; - case NOT: - fprintf(stderr, "[NOT]"); - break; - case ARRAY_ACCESS: - fprintf(stderr, "[ARRAY ACCESS]"); - break; - case LIST: - fprintf(stderr, "[LIST]"); - break; - case ID: - fprintf(stderr, "[ID: %s %s, ", - t->attr.nval->name, - pretty_type( - t->attr.nval->var_type)); - if (t->attr.nval->func_info) - fprintf(stderr, "\t %d", - t->attr.nval->func_info->argc); - fprintf(stderr, "]"); - break; - case INUM: - fprintf(stderr, "[INUM: %d]", t->attr.ival); - break; - case RNUM: - fprintf(stderr, "[RNUM: %f]", t->attr.rval); - break; - case ASSIGNOP: - fprintf(stderr, "[ASSIGN]"); - break; - case IF: - fprintf(stderr, "[IF]"); - break; - case THEN: - fprintf(stderr, "[THEN]"); - break; - case WHILE: - fprintf(stderr, "[WHILE]"); - break; - case FOR: - fprintf(stderr, "[FOR]"); - break; - case TO: - fprintf(stderr, "[TO]"); - break; - case DT: - fprintf(stderr, "[DOWN-TO]"); - break; - case SUB: - fprintf(stderr, "[SUB]"); - break; - case PCALL: - fprintf(stderr,"[P]"); - case FCALL: - fprintf(stderr, "[CALL]"); - break; - case INT: - case REAL: - fprintf(stderr, "[STD TYPE]"); - break; - case ARRAY - INT: - case ARRAY - REAL: - fprintf(stderr, "[ARRAY]"); - break; - default: - fprintf(stderr, "[?: %d]", t->type); - yyerror("Error in tree_print"); - } - fprintf(stderr," %d\n", t->ret_type); - aux_tree_print(t->l, spaces + 2); - fprintf(stderr,"\n"); - aux_tree_print(t->r, spaces + 2); - fprintf(stderr,"\n"); - } + if (spaces) + fprintf(stderr,"-"); + + switch (t->type) { + case ADDOP: + fprintf(stderr, "[ADDOP]"); + break; + case MULOP: + fprintf(stderr, "[MULOP]"); + break; + case RELOP: + fprintf(stderr, "[RELOP]"); + break; + case NOT: + fprintf(stderr, "[NOT]"); + break; + case ARRAY_ACCESS: + fprintf(stderr, "[ARRAY ACCESS]"); + break; + case LIST: + fprintf(stderr, "[LIST]"); + break; + case ID: + fprintf(stderr, "[ID: %s %s, ", + t->attr.nval->name, + pretty_type( + t->attr.nval->var_type)); + if (t->attr.nval->func_info) + fprintf(stderr, "\t %d", + t->attr.nval->func_info->argc); + fprintf(stderr, "]"); + break; + case INUM: + fprintf(stderr, "[INUM: %d]", t->attr.ival); + break; + case RNUM: + fprintf(stderr, "[RNUM: %f]", t->attr.rval); + break; + case ASSIGNOP: + fprintf(stderr, "[ASSIGN]"); + break; + case IF: + fprintf(stderr, "[IF]"); + break; + case THEN: + fprintf(stderr, "[THEN]"); + break; + case WHILE: + fprintf(stderr, "[WHILE]"); + break; + case FOR: + fprintf(stderr, "[FOR]"); + break; + case TO: + fprintf(stderr, "[TO]"); + break; + case DT: + fprintf(stderr, "[DOWN-TO]"); + break; + case SUB: + fprintf(stderr, "[SUB]"); + break; + case PCALL: + fprintf(stderr,"[P]"); + case FCALL: + fprintf(stderr, "[CALL]"); + break; + case INT: + case REAL: + fprintf(stderr, "[STD TYPE]"); + break; + case ARRAY - INT: + case ARRAY - REAL: + fprintf(stderr, "[ARRAY]"); + break; + default: + fprintf(stderr, "[?: %d]", t->type); + yyerror("Error in tree_print"); + } + fprintf(stderr," %d\n", t->ret_type); + aux_tree_print(t->l, spaces + 2); + aux_tree_print(t->r, spaces + 2); } void free_tree(t) |