Add function to free tree nodes.

This commit is contained in:
2018-02-17 12:55:50 -08:00
parent ab683cbd7a
commit 841c2f15e5
3 changed files with 31 additions and 1 deletions

22
src/tree.c Normal file
View File

@@ -0,0 +1,22 @@
#include "tree.h"
#include <stdlib.h>
void libab_tree_free(libab_tree* tree) {
int free_string = 0;
int free_vector = 0;
if(tree->variant == BASE || tree->variant == OP ||
tree->variant == UNARY_OP || tree->variant == BLOCK ||
tree->variant == FUN || tree->variant == IF ||
tree->variant == WHILE || tree->variant == DOWHILE ||
tree->variant == FOR || tree->variant == CALL ||
tree->variant == RETURN) {
free_vector = 1;
}
if(tree->variant == ID || tree->variant == STR || tree->variant == NUM ||
tree->variant == KW || tree->variant == OP || tree->variant == UNARY_OP ||
tree->variant == FUN || tree->variant == CALL) {
free_string = 1;
}
if(free_string) free(tree->string_value);
if(free_vector) vec_free(&tree->children);
}