diff options
-rw-r--r-- | node.c | 37 | ||||
-rw-r--r-- | pc.y | 5 |
2 files changed, 24 insertions, 18 deletions
@@ -12,7 +12,7 @@ char *str; node *p = malloc(sizeof(node)); assert(p); - p->name = strdup(str); + p->name = str; p->next = NULL; p->var_type = -1; @@ -38,7 +38,7 @@ char *str; return NULL; } -node* list_insert(root, str) /*TODO change to accept double pointer*/ +node* list_insert(root, str) node *root; char * str; { @@ -46,6 +46,23 @@ char * str; p->next = root; return p; } +void free_node(n) +node *n; +{ + free(n->name); + n->name = NULL; + + if (n->func_info) + free(n->func_info); + n->func_info = NULL; + + if (n->array_info) + free(n->array_info); + n->array_info = NULL; + + free(n); + n = NULL; +} void free_list(n) node *n; @@ -54,21 +71,7 @@ node *n; for(tmp = n; tmp;) { n = tmp->next; - - free(tmp->name); - tmp->name = NULL; - - if (tmp->func_info) - free(tmp->func_info); - tmp->func_info = NULL; - - if (tmp->array_info) - free(tmp->array_info); - tmp->array_info = NULL; - - free(tmp); - tmp = NULL; - + free_node(tmp); tmp = n; } } @@ -2,6 +2,7 @@ #include <stdlib.h> #include <stddef.h> #include <assert.h> +#include <string.h> #include "node.h" #include "scope.h" @@ -201,7 +202,9 @@ sub_prog_head tmp->var_type = $5; - cur_scope->ret_var = scope_insert(cur_scope, $2); + /* Need to duplicate ID.name so it is not freed with inner + * scope*/ + cur_scope->ret_var = scope_insert(cur_scope, strdup($2)); cur_scope->ret_var->var_type = $5; } |PROC ID arguments ';' |