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");