Add function to free tree nodes.
This commit is contained in:
parent
ab683cbd7a
commit
841c2f15e5
|
@ -8,7 +8,7 @@ project(libabacus)
|
||||||
|
|
||||||
add_compile_options(-pedantic -Wall)
|
add_compile_options(-pedantic -Wall)
|
||||||
|
|
||||||
add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c)
|
add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c)
|
||||||
add_executable(libabacus src/main.c)
|
add_executable(libabacus src/main.c)
|
||||||
add_subdirectory(external/liblex)
|
add_subdirectory(external/liblex)
|
||||||
|
|
||||||
|
|
|
@ -73,4 +73,12 @@ struct libab_tree_s {
|
||||||
typedef enum libab_tree_variant_e libab_tree_variant;
|
typedef enum libab_tree_variant_e libab_tree_variant;
|
||||||
typedef struct libab_tree_s libab_tree;
|
typedef struct libab_tree_s libab_tree;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees the given tree, using its
|
||||||
|
* variant as a hint as to what
|
||||||
|
* variables need to be freed.
|
||||||
|
* @param tree the tree to free.
|
||||||
|
*/
|
||||||
|
void libab_tree_free(libab_tree* tree);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
22
src/tree.c
Normal file
22
src/tree.c
Normal 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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user