From ceb4fb57521582835643d9bfc3dfda89eca6f1f0 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 4 Aug 2019 13:14:56 -0400 Subject: Add tree freeing --- main.c | 1 + pc.y | 2 ++ tree.c | 11 +++++++++++ tree.h | 2 ++ 4 files changed, 16 insertions(+) diff --git a/main.c b/main.c index dba3b7f..30e03ff 100644 --- a/main.c +++ b/main.c @@ -142,6 +142,7 @@ int main() assert(cur_scope); yyparse(); + free_scope(cur_scope); return 0; } diff --git a/pc.y b/pc.y index d63d8fc..05b50aa 100644 --- a/pc.y +++ b/pc.y @@ -93,6 +93,7 @@ program '.' { print_tree($9); + free_tree($4); } ; @@ -159,6 +160,7 @@ sub_prog_declaration compound_statement { print_tree($4); + free_tree($4); pop_scope(&cur_scope); } ; diff --git a/tree.c b/tree.c index 8a2c3bf..7c79ddd 100644 --- a/tree.c +++ b/tree.c @@ -143,3 +143,14 @@ int spaces; } } + +void free_tree(t) +ptree *t; +{ + if (!t) + return; + + free_tree(t->l); + free_tree(t->r); + free(t); +} diff --git a/tree.h b/tree.h index 0d94f6d..650d06f 100644 --- a/tree.h +++ b/tree.h @@ -26,4 +26,6 @@ ptree* mkop(int, int, ptree*, ptree*); void update_type_info(ptree*, int); +void free_tree(ptree*); + #endif -- cgit v1.1