frontend: repl: generate ir from input
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#include <ivy/lang/ast.h>
|
||||
#include <ivy/lang/codegen.h>
|
||||
#include <ivy/lang/lex.h>
|
||||
#include <mie/convert.h>
|
||||
#include <mie/value.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -22,6 +24,7 @@ struct repl {
|
||||
struct ivy_lexer *r_lex;
|
||||
struct ivy_parser *r_parse;
|
||||
struct ivy_codegen *r_codegen;
|
||||
struct mie_ir_converter *r_converter;
|
||||
};
|
||||
|
||||
static void skip_line(struct ivy_lexer *lex)
|
||||
@@ -38,6 +41,14 @@ static void skip_line(struct ivy_lexer *lex)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
struct repl *repl = arg;
|
||||
return ivy_codegen_push_node(repl->r_codegen, node, iteration_type);
|
||||
}
|
||||
|
||||
static int repl_eval_node(struct repl *repl, struct ivy_ast_node *node)
|
||||
{
|
||||
struct ivy_ast_node_iterator it = {0};
|
||||
@@ -50,15 +61,25 @@ static int repl_eval_node(struct repl *repl, struct ivy_ast_node *node)
|
||||
ivy_ast_node_iterate(node, &it, print_ast_node, &args);
|
||||
}
|
||||
|
||||
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 (status != IVY_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void repl_destroy(struct repl *repl)
|
||||
{
|
||||
if (repl->r_converter) {
|
||||
mie_ir_converter_destroy(repl->r_converter);
|
||||
}
|
||||
|
||||
if (repl->r_codegen) {
|
||||
ivy_codegen_destroy(repl->r_codegen);
|
||||
}
|
||||
@@ -115,6 +136,14 @@ static enum ivy_status repl_create(struct repl **out)
|
||||
return status;
|
||||
}
|
||||
|
||||
repl->r_converter = mie_ir_converter_create(MIE_IR_MEM, MIE_IR_TEXT);
|
||||
if (!repl->r_converter) {
|
||||
repl_destroy(repl);
|
||||
return IVY_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
mie_ir_converter_set_dest_file(repl->r_converter, stdout);
|
||||
|
||||
*out = repl;
|
||||
return IVY_OK;
|
||||
}
|
||||
@@ -154,6 +183,8 @@ int repl(const b_command *cmd, const b_arglist *args, const b_array *_)
|
||||
args, OPT_SHOW_AST_NODES, B_COMMAND_INVALID_ID)
|
||||
> 0;
|
||||
|
||||
ivy_codegen_start_module(repl->r_codegen);
|
||||
|
||||
while (true) {
|
||||
struct ivy_token *tok = ivy_lexer_read(repl->r_lex);
|
||||
status = ivy_lexer_get_status(repl->r_lex);
|
||||
|
||||
Reference in New Issue
Block a user