lang: codegen: implement unit-package and unit-import generation
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
#include "codegen.h"
|
||||
#include "var-map.h"
|
||||
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/object/list.h>
|
||||
#include <mie/block.h>
|
||||
#include <mie/func.h>
|
||||
#include <mie/module.h>
|
||||
#include <mie/record.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct unit_codegen_state {
|
||||
struct code_generator_state s_base;
|
||||
@@ -90,6 +94,74 @@ static struct code_generator_result gen_expr(
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
||||
}
|
||||
|
||||
static void serialise_package_name(b_queue *parts, b_stringstream *out)
|
||||
{
|
||||
b_stringstream_add(out, "_ZN");
|
||||
|
||||
b_queue_iterator it;
|
||||
b_queue_foreach (&it, parts) {
|
||||
struct ivy_token *tok
|
||||
= b_unbox(struct ivy_token, it.entry, t_entry);
|
||||
size_t len = strlen(tok->t_str);
|
||||
b_stringstream_addf(out, "%zu%s", len, tok->t_str);
|
||||
}
|
||||
|
||||
b_stringstream_add(out, "E");
|
||||
}
|
||||
|
||||
static struct code_generator_result gen_unit_package(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
struct unit_codegen_state *unit = (struct unit_codegen_state *)state;
|
||||
struct ivy_ast_unit_package_node *pkg
|
||||
= (struct ivy_ast_unit_package_node *)node;
|
||||
|
||||
b_stringstream str;
|
||||
b_stringstream_begin_dynamic(&str);
|
||||
serialise_package_name(&pkg->n_ident, &str);
|
||||
char *ident = b_stringstream_end(&str);
|
||||
struct mie_value *ident_val = mie_ctx_get_string(gen->c_ctx, ident);
|
||||
free(ident);
|
||||
|
||||
mie_builder_put_record(
|
||||
gen->c_builder, MIE_CONST(ident_val), "package_scope");
|
||||
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
static struct code_generator_result gen_unit_import(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
struct unit_codegen_state *unit = (struct unit_codegen_state *)state;
|
||||
struct ivy_ast_unit_package_node *pkg
|
||||
= (struct ivy_ast_unit_package_node *)node;
|
||||
|
||||
struct mie_record *rec
|
||||
= mie_builder_get_record(gen->c_builder, "import");
|
||||
struct mie_array *import_list = NULL;
|
||||
|
||||
if (!rec) {
|
||||
import_list = MIE_ARRAY(mie_ctx_create_array(gen->c_ctx));
|
||||
rec = mie_builder_put_record(
|
||||
gen->c_builder, MIE_CONST(import_list), "import");
|
||||
} else {
|
||||
import_list = MIE_ARRAY(rec->r_value);
|
||||
}
|
||||
|
||||
b_stringstream str;
|
||||
b_stringstream_begin_dynamic(&str);
|
||||
serialise_package_name(&pkg->n_ident, &str);
|
||||
char *ident = b_stringstream_end(&str);
|
||||
struct mie_value *ident_val = mie_ctx_get_string(gen->c_ctx, ident);
|
||||
free(ident);
|
||||
|
||||
b_list_push_back(import_list->a_values, ident_val);
|
||||
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
static struct code_generator_result gen_var_declaration(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
@@ -200,6 +272,8 @@ struct code_generator unit_generator = {
|
||||
.g_state_fini = state_fini,
|
||||
.g_node_generators = {
|
||||
NODE_CODEGEN(VAR, gen_var_declaration),
|
||||
NODE_CODEGEN(UNIT_PACKAGE, gen_unit_package),
|
||||
NODE_CODEGEN(UNIT_IMPORT, gen_unit_import),
|
||||
},
|
||||
.g_expr_generator = gen_expr,
|
||||
.g_define_var = define_var,
|
||||
|
||||
Reference in New Issue
Block a user