libabacus/src/debug.c

46 lines
1.4 KiB
C
Raw Normal View History

#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[] = {
2018-04-21 14:09:01 -07:00
"none", "base", "id", "num", "op", "reserved_op",
"prefix_op", "postfix_op", "block", "void", "if", "while",
"dowhile", "call", "fun", "fun_param", "return"};
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-04-21 14:09:01 -07:00
if (libab_tree_has_string(tree->variant)) {
2018-02-26 18:14:31 -08:00
fprintf(file, ": %s", tree->string_value);
}
2018-02-26 18:14:31 -08:00
fprintf(file, "\n");
}
2018-02-26 18:14:31 -08:00
void _debug_print_tree(libab_tree* tree, FILE* file, int depth) {
int i = depth;
2018-04-21 14:09:01 -07:00
while (i--)
printf(" ");
2018-02-26 18:14:31 -08:00
_debug_print_tree_node(tree, file);
2018-04-21 14:09:01 -07:00
if (libab_tree_has_vector(tree->variant)) {
vec_foreach(&tree->children, NULL, compare_always,
2018-04-21 14:09:01 -07:00
_debug_foreach_print_tree, file, depth + 1);
}
}
int _debug_foreach_print_tree(void* data, va_list args) {
2018-02-26 18:14:31 -08:00
FILE* file = va_arg(args, FILE*);
int depth = va_arg(args, int);
2018-02-26 18:14:31 -08:00
_debug_print_tree(data, file, depth);
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);
}
void libab_debug_print_tree(libab_tree* print) {
2018-02-26 18:14:31 -08:00
_debug_print_tree(print, stdout, 0);
}