Add a function to print the parse tree.
This commit is contained in:
parent
c18a5e49de
commit
65071d47b3
|
@ -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)
|
||||
|
||||
|
|
8
include/debug.h
Normal file
8
include/debug.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef LIBABACUS_DEBUG_H
|
||||
#define LIBABACUS_DEBUG_H
|
||||
|
||||
#include "tree.h"
|
||||
|
||||
void libab_debug_print_tree(libab_tree* print);
|
||||
|
||||
#endif
|
48
src/debug.c
Normal file
48
src/debug.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "debug.h"
|
||||
#include <stdio.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",
|
||||
"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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user