2018-02-17 12:55:50 -08:00
|
|
|
#include "tree.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-02-25 22:57:45 -08:00
|
|
|
int libab_tree_has_vector(libab_tree_variant variant) {
|
2018-03-08 21:13:25 -08:00
|
|
|
return variant == TREE_BASE || variant == TREE_OP ||
|
2018-03-17 18:25:23 -07:00
|
|
|
variant == TREE_PREFIX_OP || variant == TREE_POSTFIX_OP || variant == TREE_BLOCK ||
|
2018-03-08 21:13:25 -08:00
|
|
|
variant == TREE_IF || variant == TREE_CALL || variant == TREE_WHILE ||
|
2018-03-08 21:31:24 -08:00
|
|
|
variant == TREE_DOWHILE || variant == TREE_FUN ||
|
2018-03-16 23:09:41 -07:00
|
|
|
variant == TREE_RETURN || variant == TREE_RESERVED_OP;
|
2018-02-25 22:57:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int libab_tree_has_string(libab_tree_variant variant) {
|
2018-03-08 21:13:25 -08:00
|
|
|
return variant == TREE_ID || variant == TREE_NUM ||
|
2018-03-17 18:25:23 -07:00
|
|
|
variant == TREE_OP || variant == TREE_PREFIX_OP || variant == TREE_POSTFIX_OP ||
|
2018-03-16 23:09:41 -07:00
|
|
|
variant == TREE_FUN || variant == TREE_FUN_PARAM ||
|
|
|
|
variant == TREE_RESERVED_OP;
|
2018-03-08 21:10:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int libab_tree_has_type(libab_tree_variant variant) {
|
2018-03-08 21:13:25 -08:00
|
|
|
return variant == TREE_FUN_PARAM || variant == TREE_FUN;
|
2018-02-17 19:20:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void libab_tree_free(libab_tree* tree) {
|
|
|
|
int free_string = 0;
|
|
|
|
int free_vector = 0;
|
2018-03-08 21:10:05 -08:00
|
|
|
int free_type = 0;
|
2018-02-25 22:57:45 -08:00
|
|
|
free_vector = libab_tree_has_vector(tree->variant);
|
|
|
|
free_string = libab_tree_has_string(tree->variant);
|
2018-03-08 21:10:05 -08:00
|
|
|
free_type = libab_tree_has_type(tree->variant);
|
2018-02-17 12:55:50 -08:00
|
|
|
if(free_string) free(tree->string_value);
|
|
|
|
if(free_vector) vec_free(&tree->children);
|
2018-03-08 21:10:05 -08:00
|
|
|
if(free_type && tree->parse_type)
|
|
|
|
libab_parsetype_free_recursive(tree->parse_type);
|
2018-02-17 12:55:50 -08:00
|
|
|
}
|
2018-02-17 19:20:10 -08:00
|
|
|
|
|
|
|
int _tree_foreach_free(void* data, va_list args) {
|
|
|
|
libab_tree_free_recursive(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void libab_tree_free_recursive(libab_tree* tree) {
|
2018-02-25 22:57:45 -08:00
|
|
|
if(libab_tree_has_vector(tree->variant)) {
|
2018-02-17 19:20:10 -08:00
|
|
|
vec_foreach(&tree->children, NULL, compare_always, _tree_foreach_free);
|
|
|
|
}
|
|
|
|
libab_tree_free(tree);
|
|
|
|
free(tree);
|
|
|
|
}
|