2018-02-25 23:09:35 -08:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
int _debug_foreach_print_tree(void* data, va_list args);
|
|
|
|
|
|
|
|
const char* _debug_node_name(libab_tree_variant var) {
|
|
|
|
static const char* names[] = {
|
|
|
|
"none",
|
|
|
|
"base",
|
|
|
|
"id",
|
|
|
|
"num",
|
|
|
|
"op",
|
2018-03-16 23:09:41 -07:00
|
|
|
"reserved_op",
|
2018-03-17 18:25:23 -07:00
|
|
|
"prefix_op",
|
|
|
|
"postfix_op",
|
2018-02-25 23:09:35 -08:00
|
|
|
"block",
|
|
|
|
"void",
|
|
|
|
"if",
|
2018-02-27 11:57:45 -08:00
|
|
|
"while",
|
2018-02-27 12:30:37 -08:00
|
|
|
"dowhile",
|
2018-03-08 21:10:05 -08:00
|
|
|
"call",
|
|
|
|
"fun",
|
2018-03-08 21:31:24 -08:00
|
|
|
"fun_param",
|
|
|
|
"return"
|
2018-02-25 23:09:35 -08:00
|
|
|
};
|
|
|
|
return names[var];
|
|
|
|
}
|
|
|
|
|
2018-02-26 18:14:31 -08:00
|
|
|
void _debug_print_tree_node(libab_tree* tree, FILE* file) {
|
|
|
|
fprintf(file, "%s", _debug_node_name(tree->variant));
|
2018-02-25 23:09:35 -08:00
|
|
|
if(libab_tree_has_string(tree->variant)) {
|
2018-02-26 18:14:31 -08:00
|
|
|
fprintf(file, ": %s", tree->string_value);
|
2018-02-25 23:09:35 -08:00
|
|
|
}
|
2018-02-26 18:14:31 -08:00
|
|
|
fprintf(file, "\n");
|
2018-02-25 23:09:35 -08:00
|
|
|
}
|
|
|
|
|
2018-02-26 18:14:31 -08:00
|
|
|
void _debug_print_tree(libab_tree* tree, FILE* file, int depth) {
|
2018-02-25 23:09:35 -08:00
|
|
|
int i = depth;
|
|
|
|
while(i--) printf(" ");
|
2018-02-26 18:14:31 -08:00
|
|
|
_debug_print_tree_node(tree, file);
|
2018-02-25 23:09:35 -08:00
|
|
|
if(libab_tree_has_vector(tree->variant)) {
|
|
|
|
vec_foreach(&tree->children, NULL, compare_always,
|
2018-02-26 18:14:31 -08:00
|
|
|
_debug_foreach_print_tree, file, depth + 1);
|
2018-02-25 23:09:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int _debug_foreach_print_tree(void* data, va_list args) {
|
2018-02-26 18:14:31 -08:00
|
|
|
FILE* file = va_arg(args, FILE*);
|
2018-02-25 23:09:35 -08:00
|
|
|
int depth = va_arg(args, int);
|
2018-02-26 18:14:31 -08:00
|
|
|
_debug_print_tree(data, file, depth);
|
2018-02-25 23:09:35 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-26 18:14:31 -08:00
|
|
|
void libab_debug_fprint_tree(libab_tree* print, FILE* file) {
|
|
|
|
_debug_print_tree(print, file, 0);
|
|
|
|
}
|
|
|
|
|
2018-02-25 23:09:35 -08:00
|
|
|
void libab_debug_print_tree(libab_tree* print) {
|
2018-02-26 18:14:31 -08:00
|
|
|
_debug_print_tree(print, stdout, 0);
|
2018-02-25 23:09:35 -08:00
|
|
|
}
|