aboutsummaryrefslogtreecommitdiff
path: root/tree.c
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-08-18 22:33:52 -0400
committerTucker Evans <tuckerevans24@gmail.com>2019-08-18 22:33:52 -0400
commitbb9070ca0d79d2314c25b83e4496f43488446734 (patch)
tree3246d6c237eac1ca2802c241feb77545d12a40e2 /tree.c
parentceb4fb57521582835643d9bfc3dfda89eca6f1f0 (diff)
parentdd07055aca1c45d147d773350e0c21930822a74f (diff)
Merge branch 'master' into mem-management
Diffstat (limited to 'tree.c')
-rw-r--r--tree.c128
1 files changed, 88 insertions, 40 deletions
diff --git a/tree.c b/tree.c
index 7c79ddd..bc4a770 100644
--- a/tree.c
+++ b/tree.c
@@ -1,12 +1,14 @@
+#include "tree.h"
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
-#include "node.h"
-#include "tree.h"
#include "y.tab.h"
+#include "scope.h"
#include "pc.h"
+#include "sem_check.h"
/* parse tree funcs */
ptree* mktree(type, l, r)
@@ -27,7 +29,7 @@ ptree* mkid(n)
node *n;
{
ptree *p = mktree(ID, NULL, NULL);
- p->attr.nval = n; /* memory leak? double strdup*/
+ p->attr.nval = n;
return p;
}
@@ -61,22 +63,42 @@ int type;
ptree *list;
{
assert(list);
+ if (list->type == ID) {
+ list->attr.nval->var_type = type;
+ return;
+ }
+
while (list->r && list->r->type == ID) {
/*Set type of right child through list*/
list->r->attr.nval->var_type = type;
-
+
if (list->l) {
if (list->l->type == LIST) {
list = list->l;
continue; /*Continue down list*/
} else if (list->l->type == ID)
- /*Set type of first declared ID (only left node in LIST)*/
+ /*Set type of first declared ID
+ (only left node in LIST)*/
list->l->attr.nval->var_type = type;
}
return; /*At _end_ of list (did not continue)*/
}
}
+void set_ret_type(t)
+ptree *t;
+{
+ if (!t)
+ return;
+
+
+ set_ret_type(t->l);
+ set_ret_type(t->r);
+ t->ret_type = check_ret_type(t);
+
+ return;
+}
+
/*PRINT FUNCS*/
@@ -100,42 +122,68 @@ int spaces;
for (i = 0; i < spaces; i++)
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 %d]", t->attr.nval->name, t->attr.nval->var_type);
- 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;
- default:
- fprintf(stderr, "\t%d", t->type);
- yyerror("Error in tree_print");
+ 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:
+ if (t->r && t->r->attr.nval)
+ fprintf(stderr, "[ID: %s %s]",
+ t->r->attr.nval->name,
+ pretty_type(
+ t->attr.nval->var_type));
+ else
+ fprintf(stderr, "[ID: %s %s]",
+ t->attr.nval->name,
+ pretty_type(
+ t->attr.nval->var_type));
+ 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;
+ default:
+ fprintf(stderr, "\t%d", t->type);
+ yyerror("Error in tree_print");
}
- fprintf(stderr,"\n");
+ fprintf(stderr," %d\n", t->ret_type);
aux_tree_print(t->l, spaces + 2);
fprintf(stderr,"\n");
aux_tree_print(t->r, spaces + 2);