2025-04-18 23:17:39 +01:00
|
|
|
#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,
|
2025-09-08 16:17:29 +01:00
|
|
|
struct code_generator_value *result)
|
2025-04-18 23:17:39 +01:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2025-09-08 16:17:29 +01:00
|
|
|
code_generator_value_set_mie_value(result, str);
|
2025-04-18 23:17:39 +01:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct code_generator_result value_received(
|
|
|
|
|
struct ivy_codegen *gen, struct code_generator_state *state,
|
2025-09-08 16:17:29 +01:00
|
|
|
struct code_generator_value *value)
|
2025-04-18 23:17:39 +01:00
|
|
|
{
|
|
|
|
|
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,
|
2025-09-08 16:17:29 +01:00
|
|
|
&value->v_value.mie_value, 1, MIE_BUILDER_IGNORE_RESULT, NULL);
|
2025-04-18 23:17:39 +01:00
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
},
|
|
|
|
|
};
|