diff options
Diffstat (limited to 'src/ast_dump.c')
| -rw-r--r-- | src/ast_dump.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/ast_dump.c b/src/ast_dump.c new file mode 100644 index 0000000..0224c09 --- /dev/null +++ b/src/ast_dump.c @@ -0,0 +1,68 @@ +#include "all.h" +#include <stddef.h> +#include <stdio.h> + +static const char *type_name(ast_type_e type) { + switch (type) { + case AST_INC: + return "INC"; + case AST_DEC: + return "DEC"; + case AST_PTR_INC: + return "PTR_INC"; + case AST_PTR_DEC: + return "PTR_DEC"; + case AST_OUT: + return "OUT"; + case AST_IN: + return "IN"; + case AST_LOOP: + return "LOOP"; + case AST_CLEAR: + return "CLEAR"; + default: + return "?"; + } +} + +void ast_dump(ast_node_t *node, int depth) { + while (node) { + + for (int i = 0; i < depth; i++) + fprintf(stderr, " "); + + fprintf(stderr, "%s(%dx / %zu len)", type_name(node->type), node->val, + node->children.len); + + switch (node->type) { + case AST_INC: + case AST_DEC: + case AST_PTR_INC: + case AST_PTR_DEC: + break; + + default: + break; + } + + fprintf(stderr, "\n"); + + if (node->type == AST_LOOP) { + + for (int i = 0; i < depth; i++) + fprintf(stderr, " "); + + fprintf(stderr, "{\n"); + + for (int i = 0; i < node->children.len; i++) + ast_dump(node->children._data[i], depth + 1); + + for (size_t i = 0; i < depth; i++) + fprintf(stderr, " "); + + fprintf(stderr, "}\n"); + } + + node = node->next; + } +} |
