Skip to content

Commit

Permalink
Dump the a representation of the ast
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigmanificient committed Apr 20, 2024
1 parent 85a7018 commit e1b35f7
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ static void tree_free(ast *root) {
free(root);
}

static void print_depth(int depth) {
for (int i = 1; i < depth; i++)
printf(" ");
if (depth)
printf("- ");
}

static void traverse_ast(ast *root, int depth) {
if (root->any.typ == AST_NUM) {
print_depth(depth);
printf("(int: %d)", root->num.val);
return;
}
print_depth(depth);
if (root->any.typ == AST_UNARY) {
printf("([%s]\n", root->unary.tok->val);
traverse_ast(root->unary.next, depth + 1);
} else if (root->any.typ == AST_BINOP) {
printf("([%s]\n", root->binop.tok->val);
traverse_ast(root->binop.prev, depth + 1);
printf("\n");
traverse_ast(root->binop.next, depth + 1);
}
printf(")");
if (depth == 0)
printf("\n");
}

static bool expr_run(char **argv) {
lexer lex = {.argv = argv, 0};
parser p = {.tok = lex_get_next_token(&lex), .lx = &lex};
Expand All @@ -30,8 +58,9 @@ static bool expr_run(char **argv) {
fprintf(stderr, "Failed to create the AST");
return EXIT_FAILURE;
}
free(lex.tokens);
traverse_ast(root, 0);
tree_free(root);
free(lex.tokens);
return true;
}

Expand Down

0 comments on commit e1b35f7

Please sign in to comment.