diff --git a/include/tree.h b/include/tree.h index 5d83631..fd30fc4 100644 --- a/include/tree.h +++ b/include/tree.h @@ -72,6 +72,20 @@ typedef struct libab_tree_s libab_tree; * @param tree the tree to free. */ void libab_tree_free(libab_tree* tree); +/** + * Determines if the given tree node variant + * should contain a string. + * @param var the variant of the tree node. + * @return true if the tree node variant contains a string. + */ +int libab_tree_has_string(libab_tree_variant var); +/** + * Determines if the given tree node variant + * should contain a vector. + * @param var the variant of the tree node. + * @return true if the tree node variant contains a vector. + */ +int libab_tree_has_vector(libab_tree_variant var); /** * Frees the given tree recursively, * deleting the children first and the moving on diff --git a/src/tree.c b/src/tree.c index c0e0d00..10bf1e9 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,22 +1,22 @@ #include "tree.h" #include -int _tree_has_vector(libab_tree* tree) { - return tree->variant == BASE || tree->variant == OP || - tree->variant == UNARY_OP || tree->variant == BLOCK || - tree->variant == IF; +int libab_tree_has_vector(libab_tree_variant variant) { + return variant == BASE || variant == OP || + variant == UNARY_OP || variant == BLOCK || + variant == IF; +} + +int libab_tree_has_string(libab_tree_variant variant) { + return variant == ID || variant == NUM || + variant == OP || variant == UNARY_OP; } void libab_tree_free(libab_tree* tree) { int free_string = 0; int free_vector = 0; - if(_tree_has_vector(tree)) { - free_vector = 1; - } - if(tree->variant == ID || tree->variant == NUM || - tree->variant == OP || tree->variant == UNARY_OP) { - free_string = 1; - } + free_vector = libab_tree_has_vector(tree->variant); + free_string = libab_tree_has_string(tree->variant); if(free_string) free(tree->string_value); if(free_vector) vec_free(&tree->children); } @@ -27,7 +27,7 @@ int _tree_foreach_free(void* data, va_list args) { } void libab_tree_free_recursive(libab_tree* tree) { - if(_tree_has_vector(tree)) { + if(libab_tree_has_vector(tree->variant)) { vec_foreach(&tree->children, NULL, compare_always, _tree_foreach_free); } libab_tree_free(tree);