frontend: update codegen usage to only use pre-order ast traversal

This commit is contained in:
2025-04-16 21:59:32 +01:00
parent f0e6237473
commit 635d23d2e1
2 changed files with 80 additions and 13 deletions

View File

@@ -16,6 +16,7 @@ enum {
ARG_SOURCE_FILE = 200,
OPT_SHOW_LEX_TOKENS,
OPT_SHOW_AST_NODES,
OPT_SHOW_AST_NODES_POST,
OPT_SHOW_IR,
};
@@ -23,8 +24,14 @@ static enum ivy_status node_codegen(
struct ivy_ast_node *node, enum ivy_ast_iteration_type iteration_type,
struct ivy_ast_node_iterator *it, void *arg)
{
if (iteration_type != IVY_AST_ITERATION_PRE) {
return IVY_OK;
}
struct ivy_codegen *gen = arg;
return ivy_codegen_push_node(gen, node, iteration_type);
struct print_ast_node_args print_args = {.postorder = true};
print_ast_node(node, iteration_type, it, &print_args);
return ivy_codegen_push_node(gen, node, node->n_it.it_depth);
}
static int compile_file(const char *path, const b_arglist *args)
@@ -35,6 +42,10 @@ static int compile_file(const char *path, const b_arglist *args)
bool show_ast = b_arglist_get_count(
args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID)
> 0;
bool show_ast_post
= b_arglist_get_count(
args, OPT_SHOW_AST_NODES_POST, B_COMMAND_INVALID_ID)
> 0;
bool show_ir
= b_arglist_get_count(args, OPT_SHOW_IR, B_COMMAND_INVALID_ID) > 0;
@@ -108,14 +119,31 @@ static int compile_file(const char *path, const b_arglist *args)
int r = (status == IVY_OK || status == IVY_ERR_EOF) ? 0 : -1;
ivy_file_close(src);
ivy_lexer_destroy(lex);
if (r == 0 && show_ast) {
if (r != 0) {
return r;
}
if (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, NULL);
}
ivy_lexer_destroy(lex);
if (show_ast && show_ast_post) {
printf("------\n");
}
if (show_ast_post) {
struct ivy_ast_node *root = ivy_parser_root_node(parser);
struct ivy_ast_node_iterator it = {};
struct print_ast_node_args args = {
.indent = true,
.postorder = true,
};
ivy_ast_node_iterate(root, &it, print_ast_node, &args);
}
if (r != 0) {
ivy_parser_destroy(parser);
@@ -129,13 +157,15 @@ static int compile_file(const char *path, const b_arglist *args)
ivy_codegen_start_module(codegen);
struct ivy_ast_node *node = NULL;
while ((node = ivy_parser_dequeue_node(parser))) {
struct ivy_ast_node_iterator it = {0};
ivy_ast_node_iterate(node, &it, node_codegen, codegen);
ivy_ast_node_destroy(node);
struct ivy_ast_node *node = ivy_parser_root_node(parser);
struct ivy_ast_node_iterator it = {0};
status = ivy_ast_node_iterate(node, &it, node_codegen, codegen);
if (status != IVY_OK) {
printf("codegen failed: error %s\n", ivy_status_to_string(status));
}
ivy_codegen_push_eof(codegen);
struct mie_module *mod = NULL;
ivy_codegen_end_module(codegen, &mod);
@@ -200,6 +230,15 @@ B_COMMAND(CMD_COMPILE, CMD_ROOT)
"input files.");
}
B_COMMAND_OPTION(OPT_SHOW_AST_NODES_POST)
{
B_OPTION_LONG_NAME("show-ast-post");
B_OPTION_SHORT_NAME('A');
B_OPTION_DESC(
"print the abstract syntax tree generated from the "
"input files in post-order.");
}
B_COMMAND_OPTION(OPT_SHOW_IR)
{
B_OPTION_LONG_NAME("show-ir");

View File

@@ -16,10 +16,11 @@
enum {
OPT_SHOW_LEX_TOKENS = 100,
OPT_SHOW_AST_NODES,
OPT_SHOW_IR,
};
struct repl {
bool r_show_lex, r_show_ast;
bool r_show_lex, r_show_ast, r_show_ir;
struct line_ed *r_ed;
struct ivy_lexer *r_lex;
struct ivy_parser *r_parse;
@@ -45,8 +46,12 @@ static enum ivy_status add_node_to_codegen(
struct ivy_ast_node *node, enum ivy_ast_iteration_type iteration_type,
struct ivy_ast_node_iterator *it, void *arg)
{
if (iteration_type != IVY_AST_ITERATION_PRE) {
return IVY_OK;
}
struct repl *repl = arg;
return ivy_codegen_push_node(repl->r_codegen, node, iteration_type);
return ivy_codegen_push_node(repl->r_codegen, node, node->n_it.it_depth);
}
static int repl_eval_node(struct repl *repl, struct ivy_ast_node *node)
@@ -59,14 +64,25 @@ static int repl_eval_node(struct repl *repl, struct ivy_ast_node *node)
if (repl->r_show_ast) {
ivy_ast_node_iterate(node, &it, print_ast_node, &args);
printf("------\n");
args.indent = false;
args.postorder = true;
ivy_ast_node_iterate(node, &it, print_ast_node, &args);
}
enum ivy_status status
= ivy_ast_node_iterate(node, &it, add_node_to_codegen, repl);
if (!repl->r_show_ir) {
return 0;
}
enum ivy_status status = ivy_ast_node_iterate(
(struct ivy_ast_node *)node, &it, add_node_to_codegen, repl);
if (status != IVY_OK) {
printf("codegen error: %s\n", ivy_status_to_string(status));
return -1;
}
ivy_codegen_push_eof(repl->r_codegen);
struct mie_module *mod = ivy_codegen_get_current_module(repl->r_codegen);
mie_ir_converter_set_src_value(repl->r_converter, MIE_VALUE(mod));
mie_ir_converter_process(repl->r_converter);
@@ -184,6 +200,8 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
repl->r_show_ast = b_arglist_get_count(
args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID)
> 0;
repl->r_show_ir
= b_arglist_get_count(args, OPT_SHOW_IR, B_COMMAND_INVALID_ID) > 0;
ivy_codegen_start_module(repl->r_codegen);
@@ -218,12 +236,15 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
continue;
}
struct ivy_ast_node *child = NULL;
struct ivy_ast_node *unit = ivy_parser_root_node(repl->r_parse);
repl_eval_node(repl, unit);
#if 0
while ((child = ivy_parser_dequeue_node(repl->r_parse))) {
repl_eval_node(repl, child);
ivy_ast_node_destroy(child);
}
#endif
}
repl_destroy(repl);
@@ -254,4 +275,11 @@ B_COMMAND(CMD_REPL, CMD_ROOT)
"print the abstract syntax tree generated from the "
"input.");
}
B_COMMAND_OPTION(OPT_SHOW_IR)
{
B_OPTION_LONG_NAME("show-ir");
B_OPTION_SHORT_NAME('i');
B_OPTION_DESC("print the Mie IR generated from the input.");
}
}