frontend: update codegen usage to only use pre-order ast traversal
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user