lang: add match-expression parser/generator

This commit is contained in:
2025-11-04 10:35:52 +00:00
parent b444a565a2
commit 58fa746aa1
4 changed files with 141 additions and 0 deletions

View File

@@ -50,6 +50,7 @@ enum code_generator_type {
CODE_GENERATOR_FOR_LOOP,
CODE_GENERATOR_WHILE_LOOP,
CODE_GENERATOR_CASCADE,
CODE_GENERATOR_MATCH,
};
enum code_generator_scope_type {

View File

@@ -277,6 +277,14 @@ static struct code_generator_result gen_for_loop(
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
static struct code_generator_result gen_match(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
codegen_push_generator(gen, CODE_GENERATOR_MATCH, 0, NULL);
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
}
#if 0
static struct code_generator_result gen_var_ref(
struct ivy_codegen *gen, struct code_generator_state *state,
@@ -725,5 +733,6 @@ struct code_generator expr_generator = {
NODE_CODEGEN(IDENT, gen_var_reference),
NODE_CODEGEN(COND_GROUP, gen_cond_group),
NODE_CODEGEN(FOR_LOOP, gen_for_loop),
NODE_CODEGEN(MATCH, gen_match),
},
};

86
lang/codegen/match.c Normal file
View File

@@ -0,0 +1,86 @@
#include "codegen.h"
#include <mie/ir/block.h>
#include <mie/ir/func.h>
#include <mie/ir/module.h>
struct match_codegen_state {
struct code_generator_state s_base;
};
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 match_codegen_state *match = (struct match_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_match(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result gen_block(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct match_codegen_state *match = (struct match_codegen_state *)state;
struct mie_func *func = mie_builder_get_current_func(gen->c_builder);
codegen_push_generator(gen, CODE_GENERATOR_BLOCK, 0, NULL);
return CODEGEN_RESULT_OK(0);
}
static struct code_generator_result gen_discard(
struct ivy_codegen *gen, struct code_generator_state *state,
struct ivy_ast_node *node, size_t depth)
{
struct match_codegen_state *match = (struct match_codegen_state *)state;
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)
{
struct match_codegen_state *match = (struct match_codegen_state *)state;
return IVY_OK;
}
static enum ivy_status state_fini(
struct ivy_codegen *gen, struct code_generator_state *state,
struct code_generator_value *result)
{
struct match_codegen_state *match = (struct match_codegen_state *)state;
return IVY_OK;
}
static struct code_generator_result value_received(
struct ivy_codegen *gen, struct code_generator_state *state,
struct code_generator_value *value)
{
struct match_codegen_state *match = (struct match_codegen_state *)state;
struct mie_func *func = mie_builder_get_current_func(gen->c_builder);
return CODEGEN_RESULT_OK(0);
}
struct code_generator match_generator = {
.g_type = CODE_GENERATOR_FOR_LOOP,
.g_state_size = sizeof(struct match_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(MATCH, gen_match),
NODE_CODEGEN(BLOCK, gen_block),
NODE_CODEGEN(DISCARD, gen_discard),
},
};