diff options
| author | Elis Eriksson <spelis@spelis.li> | 2026-06-20 20:12:29 +0200 |
|---|---|---|
| committer | Elis Eriksson <spelis@spelis.li> | 2026-06-20 20:12:29 +0200 |
| commit | 38942c34a497a4c6edf080dc64440c1bc486fc46 (patch) | |
| tree | 8fd3c891dff03f649b21aeb02c594e3e50f3160c /src/ast_dump.c | |
| parent | de91ec0542aea5959376ee0d2c9d486268dd9392 (diff) | |
| download | bfc-38942c34a497a4c6edf080dc64440c1bc486fc46.tar bfc-38942c34a497a4c6edf080dc64440c1bc486fc46.tar.gz bfc-38942c34a497a4c6edf080dc64440c1bc486fc46.tar.bz2 bfc-38942c34a497a4c6edf080dc64440c1bc486fc46.tar.lz bfc-38942c34a497a4c6edf080dc64440c1bc486fc46.tar.xz bfc-38942c34a497a4c6edf080dc64440c1bc486fc46.tar.zst bfc-38942c34a497a4c6edf080dc64440c1bc486fc46.zip | |
Pattern matching optimizer and more
it's not the best optimizer but it works.
Also added a -ast_dump flag which dumps the AST to stderr.
Also input is now done in raw mode just because it can be.
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; + } +} |
