lang: codegen: implement fstring generation
This commit is contained in:
96
lang/codegen/fstring.c
Normal file
96
lang/codegen/fstring.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "../debug.h"
|
||||
#include "codegen.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct fstring_codegen_state {
|
||||
struct code_generator_state s_base;
|
||||
struct mie_value *s_stringbuilder;
|
||||
};
|
||||
|
||||
static struct code_generator_result gen_fstring(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
struct fstring_codegen_state *fstring
|
||||
= (struct fstring_codegen_state *)state;
|
||||
|
||||
struct mie_value *stringbuilder_ptr
|
||||
= mie_builder_get_data_ptr(gen->c_builder, "StringBuilder");
|
||||
|
||||
struct mie_type *id = mie_ctx_get_type(gen->c_ctx, MIE_TYPE_ID);
|
||||
struct mie_value *stringbuilder_class
|
||||
= mie_builder_load(gen->c_builder, id, stringbuilder_ptr, NULL);
|
||||
struct mie_value *sel = mie_ctx_get_selector(gen->c_ctx, "_M3newE");
|
||||
|
||||
fstring->s_stringbuilder = mie_builder_msg(
|
||||
gen->c_builder, id, stringbuilder_class, sel, NULL, 0, 0, NULL);
|
||||
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
static struct code_generator_result gen_expr(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
codegen_push_generator(gen, CODE_GENERATOR_EXPR, 0, NULL);
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
||||
}
|
||||
|
||||
static enum ivy_status state_init(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
uintptr_t argv, void *argp)
|
||||
{
|
||||
debug_printf("codegen: start of fstring\n");
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status state_fini(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct mie_value **result)
|
||||
{
|
||||
debug_printf("codegen: end of fstring\n");
|
||||
struct fstring_codegen_state *fstring
|
||||
= (struct fstring_codegen_state *)state;
|
||||
|
||||
struct mie_type *id = mie_ctx_get_type(gen->c_ctx, MIE_TYPE_ID);
|
||||
struct mie_value *sel = mie_ctx_get_selector(gen->c_ctx, "_M8toStringE");
|
||||
|
||||
struct mie_value *str = mie_builder_msg(
|
||||
gen->c_builder, id, fstring->s_stringbuilder, sel, NULL, 0, 0,
|
||||
NULL);
|
||||
|
||||
*result = str;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static struct code_generator_result value_received(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct mie_value *value)
|
||||
{
|
||||
struct fstring_codegen_state *fstring
|
||||
= (struct fstring_codegen_state *)state;
|
||||
|
||||
struct mie_type *void_type = mie_ctx_get_type(gen->c_ctx, MIE_TYPE_VOID);
|
||||
struct mie_value *sel = mie_ctx_get_selector(gen->c_ctx, "_M06appendE");
|
||||
|
||||
mie_builder_msg(
|
||||
gen->c_builder, void_type, fstring->s_stringbuilder, sel,
|
||||
&value, 1, MIE_BUILDER_IGNORE_RESULT, NULL);
|
||||
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
struct code_generator fstring_generator = {
|
||||
.g_type = CODE_GENERATOR_FSTRING,
|
||||
.g_state_size = sizeof(struct fstring_codegen_state),
|
||||
.g_state_init = state_init,
|
||||
.g_state_fini = state_fini,
|
||||
.g_value_received = value_received,
|
||||
.g_expr_generator = gen_expr,
|
||||
.g_node_generators = {
|
||||
NODE_CODEGEN(FSTRING, gen_fstring),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user