frontend: update ast traversal api usage

This commit is contained in:
2025-04-14 09:46:36 +01:00
parent fd91bb71c0
commit c682cbb15a
3 changed files with 53 additions and 18 deletions

View File

@@ -17,8 +17,12 @@ enum {
static int compile_file(const char *path, const b_arglist *args)
{
bool show_lex = b_arglist_get_count(args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID) > 0;
bool show_ast = b_arglist_get_count(args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID) > 0;
bool show_lex = b_arglist_get_count(
args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID)
> 0;
bool show_ast = b_arglist_get_count(
args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID)
> 0;
FILE *fp = fopen(path, "r");
if (!fp) {
@@ -83,7 +87,8 @@ static int compile_file(const char *path, const b_arglist *args)
if (r == 0 && show_ast) {
struct ivy_ast_node *root = ivy_parser_root_node(parser);
struct ivy_ast_node_iterator it = {0};
ivy_ast_node_iterate(root, &it, print_ast_node);
ivy_ast_node_iterate(
root, &it, IVY_AST_ITERATE_REGULAR, print_ast_node);
}
if (lex) {

View File

@@ -13,6 +13,10 @@ enum {
OPT_SHOW_AST_NODES,
};
struct repl {
bool r_show_lex, r_show_ast;
};
static void skip_line(struct ivy_lexer *lex)
{
while (ivy_lexer_tokens_available(lex)) {
@@ -27,24 +31,49 @@ static void skip_line(struct ivy_lexer *lex)
}
}
static int repl_eval_node(struct repl *repl, struct ivy_ast_node *node)
{
struct ivy_ast_node_iterator it = {0};
if (repl->r_show_ast) {
ivy_ast_node_iterate(
node, &it, IVY_AST_ITERATE_REGULAR, print_ast_node);
}
ivy_ast_node_iterate(node, &it, IVY_AST_ITERATE_POSTORDER, print_ast_node);
return 0;
}
int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
{
b_printf("[bold,bright_red]error[[E0384][reset,bold,white]: cannot assign twice to immutable variable `i`[reset]\n");
b_printf(
"[bold,bright_red]error[[E0384][reset,bold,white]: cannot "
"assign twice to immutable variable `i`[reset]\n");
b_printf("[bold,bright_blue] -->[reset] src/main.rs:7:3\n");
b_printf("[bold,bright_blue] |[reset]\n");
b_printf("[bold,bright_blue]4 |[reset] let i = 0;\n");
b_printf("[bold,bright_blue] | -[reset]\n");
b_printf("[bold,bright_blue] | |[reset]\n");
b_printf("[bold,bright_blue] | first assignment to `i`[reset]\n");
b_printf("[bold,bright_blue] | help: make this binding mutable: `mut i`[reset]\n");
b_printf(
"[bold,bright_blue] | first assignment to "
"`i`[reset]\n");
b_printf(
"[bold,bright_blue] | help: make this binding "
"mutable: `mut i`[reset]\n");
b_printf("[bold,bright_blue]...[reset]\n");
b_printf("[bold,bright_blue]7 |[reset] i += 1;\n");
b_printf("[bold,bright_blue] |[bold,bright_red] ^^^^^^ cannot assign twice to immutable variable[reset]\n");
b_printf(
"[bold,bright_blue] |[bold,bright_red] ^^^^^^ cannot "
"assign twice to immutable variable[reset]\n");
bool show_lex
= b_arglist_get_count(args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID) > 0;
bool show_ast
= b_arglist_get_count(args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID) > 0;
struct repl repl = {};
repl.r_show_lex = b_arglist_get_count(
args, OPT_SHOW_LEX_TOKENS, B_COMMAND_INVALID_ID)
> 0;
repl.r_show_ast = b_arglist_get_count(
args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID)
> 0;
struct line_ed *ed = line_ed_create();
line_ed_set_flags(ed, LINE_ED_REMOVE_CONTINUATIONS);
@@ -81,7 +110,7 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
continue;
}
if (show_lex) {
if (repl.r_show_lex) {
print_lex_token(tok);
}
@@ -98,11 +127,11 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
continue;
}
struct ivy_ast_node *root = ivy_parser_root_node(parser);
struct ivy_ast_node *child = NULL;
if (show_ast) {
struct ivy_ast_node_iterator it = {0};
ivy_ast_node_iterate(root, &it, print_ast_node);
while ((child = ivy_parser_dequeue_node(parser))) {
repl_eval_node(&repl, child);
ivy_ast_node_destroy(child);
}
}