lang: codegen: add for-loop generator

This commit is contained in:
2025-05-08 10:53:00 +01:00
parent c31638c3de
commit 475575f76f
4 changed files with 82 additions and 0 deletions

70
lang/codegen/for-loop.c Normal file
View File

@@ -0,0 +1,70 @@
#include "codegen.h"
#include <mie/block.h>
#include <mie/func.h>
#include <mie/module.h>
struct for_codegen_state {
struct code_generator_state s_base;
struct mie_value *s_iterator;
struct mie_value *s_container;
};
static struct code_generator_result gen_expr(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct for_codegen_state *for_loop = (struct for_codegen_state *)state;
codegen_push_generator(gen, CODE_GENERATOR_EXPR, 0, NULL);
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
static struct code_generator_result gen_block(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
codegen_push_generator(gen, CODE_GENERATOR_BLOCK, 0, NULL);
return CODEGEN_RESULT_OK(0);
}
static enum ivy_status state_init(
struct ivy_codegen *gen, struct code_generator_state *state,
uintptr_t argv, void *argp)
{
return IVY_OK;
}
static enum ivy_status state_fini(
struct ivy_codegen *gen, struct code_generator_state *state,
struct mie_value **result)
{
return IVY_OK;
}
static struct code_generator_result value_received(
struct ivy_codegen *gen, struct code_generator_state *state,
struct mie_value *value)
{
struct for_codegen_state *for_loop = (struct for_codegen_state *)state;
if (!for_loop->s_iterator) {
for_loop->s_iterator = value;
} else if (!for_loop->s_container) {
for_loop->s_container = value;
}
return CODEGEN_RESULT_OK(0);
}
struct code_generator for_loop_generator = {
.g_type = CODE_GENERATOR_FOR_LOOP,
.g_state_size = sizeof(struct for_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(BLOCK, gen_block),
},
};