diff --git a/CMakeLists.txt b/CMakeLists.txt index d21fff8..89ab926 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ project(libabacus) add_compile_options(-pedantic -Wall) -add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c) +add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c) add_executable(libabacus src/main.c) add_subdirectory(external/liblex) diff --git a/include/debug.h b/include/debug.h new file mode 100644 index 0000000..f4d9fe7 --- /dev/null +++ b/include/debug.h @@ -0,0 +1,8 @@ +#ifndef LIBABACUS_DEBUG_H +#define LIBABACUS_DEBUG_H + +#include "tree.h" + +void libab_debug_print_tree(libab_tree* print); + +#endif diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..e39158d --- /dev/null +++ b/src/debug.c @@ -0,0 +1,48 @@ +#include "debug.h" +#include + +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", + "unary_op", + "block", + "void", + "if", + "call" + }; + return names[var]; +} + +void _debug_print_tree_node(libab_tree* tree) { + printf("%s", _debug_node_name(tree->variant)); + if(libab_tree_has_string(tree->variant)) { + printf(": %s", tree->string_value); + } + printf("\n"); +} + +void _debug_print_tree(libab_tree* tree, int depth) { + int i = depth; + while(i--) printf(" "); + _debug_print_tree_node(tree); + if(libab_tree_has_vector(tree->variant)) { + vec_foreach(&tree->children, NULL, compare_always, + _debug_foreach_print_tree, depth + 1); + } +} + +int _debug_foreach_print_tree(void* data, va_list args) { + int depth = va_arg(args, int); + _debug_print_tree(data, depth); + return 0; +} + +void libab_debug_print_tree(libab_tree* print) { + _debug_print_tree(print, 0); +}