frontend: compile: print ast nodes as they are passed to the code generator
when both -a and -i switches are specified, ast nodes will be printed as they are passed to the code generator, rather than all at once /before/ code generation. this makes it easier to determine which ast node is causing a code generation error.
This commit is contained in:
@@ -17,10 +17,14 @@ enum {
|
||||
ARG_SOURCE_FILE = 200,
|
||||
OPT_SHOW_LEX_TOKENS,
|
||||
OPT_SHOW_AST_NODES,
|
||||
OPT_SHOW_AST_NODES_POST,
|
||||
OPT_SHOW_IR,
|
||||
};
|
||||
|
||||
struct codegen_args {
|
||||
struct ivy_codegen *args_gen;
|
||||
bool args_print_ast;
|
||||
};
|
||||
|
||||
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)
|
||||
@@ -29,9 +33,12 @@ static enum ivy_status node_codegen(
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
struct ivy_codegen *gen = arg;
|
||||
struct print_ast_node_args print_args = {.postorder = true};
|
||||
print_ast_node(node, iteration_type, it, &print_args);
|
||||
struct codegen_args *args = arg;
|
||||
struct ivy_codegen *gen = args->args_gen;
|
||||
if (args->args_print_ast) {
|
||||
print_ast_node(node, iteration_type, it, NULL);
|
||||
}
|
||||
|
||||
return ivy_codegen_push_node(gen, node, node->n_it.it_depth);
|
||||
}
|
||||
|
||||
@@ -43,10 +50,6 @@ 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;
|
||||
|
||||
@@ -128,31 +131,17 @@ static int compile_file(const char *path, const b_arglist *args)
|
||||
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);
|
||||
}
|
||||
|
||||
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, NULL);
|
||||
return r;
|
||||
}
|
||||
|
||||
if (show_ast && !show_ir) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (!show_ir) {
|
||||
ivy_parser_destroy(parser, NULL);
|
||||
return 0;
|
||||
@@ -162,9 +151,15 @@ static int compile_file(const char *path, const b_arglist *args)
|
||||
|
||||
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);
|
||||
struct codegen_args gen_args = {
|
||||
.args_gen = codegen,
|
||||
.args_print_ast = show_ast,
|
||||
};
|
||||
|
||||
status = ivy_ast_node_iterate(node, &it, node_codegen, &gen_args);
|
||||
if (status != IVY_OK) {
|
||||
printf("codegen failed: error %s\n", ivy_status_to_string(status));
|
||||
return -1;
|
||||
}
|
||||
|
||||
ivy_codegen_push_eof(codegen);
|
||||
@@ -234,15 +229,6 @@ 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");
|
||||
|
||||
Reference in New Issue
Block a user