From e9ae811ffaa08caef0acca643ca1e7a1ff72396f Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 27 Jul 2019 12:40:16 -0400 Subject: Add update types during declaration --- makefile | 4 ++-- pc.y | 6 ++---- tree.c | 29 +++++++++++++++++++++++++++-- tree.h | 2 ++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/makefile b/makefile index 9a2f51a..2ae5ae0 100644 --- a/makefile +++ b/makefile @@ -3,8 +3,8 @@ FLAGS = -g YACC = yacc LEX = lex -mypc: y.tab.o lex.yy.o tree.o hash.o node.o pc.o - $(CC) $(FLAGS) -o mypc main.o tree.o hash.o node.o y.tab.o lex.yy.o -lfl -ly +mypc: y.tab.o lex.yy.o tree.o node.o pc.o + $(CC) $(FLAGS) -o mypc main.o tree.o node.o y.tab.o lex.yy.o -lfl -ly pc.o: main.c pc.h $(CC) $(FLAGS) -c main.c diff --git a/pc.y b/pc.y index 495f64a..050aedc 100644 --- a/pc.y +++ b/pc.y @@ -2,6 +2,7 @@ #include #include +#include "node.h" #include "tree.h" #include "y.tab.h" #include "pc.h" @@ -108,10 +109,7 @@ id_list var_declarations :var_declarations VAR id_list ':' type ';' { - ptree *tmp; - for(tmp = $3; tmp; tmp = tmp->l) { - tmp->type = $5; - } + update_type_info($3, $5); } |/*empty*/ ; diff --git a/tree.c b/tree.c index 0301ec3..3a2afcf 100644 --- a/tree.c +++ b/tree.c @@ -27,7 +27,8 @@ ptree* mkid(str) char *str; { ptree *p = mktree(ID, NULL, NULL); - p->attr.sval = strdup(str); /* memory leak? double strdup*/ + p->attr.nval = malloc(sizeof(node)); /*TODO fix hacky node create*/ + p->attr.nval->name = strdup(str); /* memory leak? double strdup*/ return p; } @@ -56,6 +57,30 @@ ptree *l, *r; return p; } +void update_type_info(list, type) +int type; +ptree *list; +{ + assert(list); + 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)*/ + list->l->attr.nval->var_type = type; + } + return; /*At _end_ of list (did not continue)*/ + } +} + + +/*PRINT FUNCS*/ + void print_tree(t) ptree *t; { @@ -94,7 +119,7 @@ int spaces; fprintf(stderr, "[LIST]"); break; case ID: - fprintf(stderr, "[ID: %s]", t->attr.sval); + fprintf(stderr, "[ID: %s %d]", t->attr.nval->name, t->attr.nval->var_type); break; case INUM: fprintf(stderr, "[INUM: %d]", t->attr.ival); diff --git a/tree.h b/tree.h index b17c276..5506657 100644 --- a/tree.h +++ b/tree.h @@ -25,4 +25,6 @@ ptree* mkinum(int); ptree* mkrnum(float); ptree* mkop(int, int, ptree*, ptree*); +void update_type_info(ptree*, int); + #endif -- cgit v1.1