Make the has string and has vector functions public.

This commit is contained in:
Danila Fedorin 2018-02-25 22:57:45 -08:00
parent 52ac67026f
commit 88108bb3c0
2 changed files with 26 additions and 12 deletions

View File

@ -72,6 +72,20 @@ typedef struct libab_tree_s libab_tree;
* @param tree the tree to free. * @param tree the tree to free.
*/ */
void libab_tree_free(libab_tree* tree); 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, * Frees the given tree recursively,
* deleting the children first and the moving on * deleting the children first and the moving on

View File

@ -1,22 +1,22 @@
#include "tree.h" #include "tree.h"
#include <stdlib.h> #include <stdlib.h>
int _tree_has_vector(libab_tree* tree) { int libab_tree_has_vector(libab_tree_variant variant) {
return tree->variant == BASE || tree->variant == OP || return variant == BASE || variant == OP ||
tree->variant == UNARY_OP || tree->variant == BLOCK || variant == UNARY_OP || variant == BLOCK ||
tree->variant == IF; 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) { void libab_tree_free(libab_tree* tree) {
int free_string = 0; int free_string = 0;
int free_vector = 0; int free_vector = 0;
if(_tree_has_vector(tree)) { free_vector = libab_tree_has_vector(tree->variant);
free_vector = 1; free_string = libab_tree_has_string(tree->variant);
}
if(tree->variant == ID || tree->variant == NUM ||
tree->variant == OP || tree->variant == UNARY_OP) {
free_string = 1;
}
if(free_string) free(tree->string_value); if(free_string) free(tree->string_value);
if(free_vector) vec_free(&tree->children); 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) { 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); vec_foreach(&tree->children, NULL, compare_always, _tree_foreach_free);
} }
libab_tree_free(tree); libab_tree_free(tree);