lang: codegen: implement return generation
This commit is contained in:
@@ -40,6 +40,24 @@ static struct code_generator_result gen_var_declaration(
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
||||
}
|
||||
|
||||
static struct code_generator_result gen_return(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
struct block_codegen_state *block = (struct block_codegen_state *)state;
|
||||
struct mie_builder *builder = gen->c_builder;
|
||||
struct mie_func *current = mie_builder_get_current_func(builder);
|
||||
|
||||
enum ivy_status status = IVY_OK;
|
||||
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
}
|
||||
|
||||
codegen_push_generator(gen, CODE_GENERATOR_RETURN, 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)
|
||||
@@ -62,6 +80,7 @@ struct code_generator block_generator = {
|
||||
.g_state_fini = state_fini,
|
||||
.g_node_generators = {
|
||||
NODE_CODEGEN(VAR, gen_var_declaration),
|
||||
NODE_CODEGEN(RETURN, gen_return),
|
||||
},
|
||||
.g_expr_generator = gen_expr,
|
||||
};
|
||||
|
||||
@@ -44,6 +44,7 @@ enum code_generator_type {
|
||||
CODE_GENERATOR_MSG,
|
||||
CODE_GENERATOR_FSTRING,
|
||||
CODE_GENERATOR_LAMBDA,
|
||||
CODE_GENERATOR_RETURN,
|
||||
};
|
||||
|
||||
enum code_generator_scope_type {
|
||||
|
||||
@@ -9,6 +9,7 @@ extern const struct code_generator var_generator;
|
||||
extern const struct code_generator msg_generator;
|
||||
extern const struct code_generator fstring_generator;
|
||||
extern const struct code_generator lambda_generator;
|
||||
extern const struct code_generator return_generator;
|
||||
|
||||
static const struct code_generator *code_generators[] = {
|
||||
[CODE_GENERATOR_UNIT] = &unit_generator,
|
||||
@@ -18,6 +19,7 @@ static const struct code_generator *code_generators[] = {
|
||||
[CODE_GENERATOR_MSG] = &msg_generator,
|
||||
[CODE_GENERATOR_FSTRING] = &fstring_generator,
|
||||
[CODE_GENERATOR_LAMBDA] = &lambda_generator,
|
||||
[CODE_GENERATOR_RETURN] = &return_generator,
|
||||
};
|
||||
static const size_t nr_code_generators
|
||||
= sizeof code_generators / sizeof code_generators[0];
|
||||
|
||||
88
lang/codegen/return.c
Normal file
88
lang/codegen/return.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "../debug.h"
|
||||
#include "codegen.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct return_codegen_state {
|
||||
struct code_generator_state s_base;
|
||||
unsigned int s_prev_node;
|
||||
struct mie_value *s_value;
|
||||
};
|
||||
|
||||
static struct code_generator_result gen_return(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
struct return_codegen_state *ret = (struct return_codegen_state *)state;
|
||||
if (ret->s_prev_node != 0) {
|
||||
return CODEGEN_RESULT_ERR(IVY_ERR_BAD_SYNTAX);
|
||||
}
|
||||
|
||||
ret->s_prev_node = IVY_AST_RETURN;
|
||||
return CODEGEN_RESULT_OK(0);
|
||||
}
|
||||
|
||||
static struct code_generator_result gen_value_expr(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct ivy_ast_node *node, size_t depth)
|
||||
{
|
||||
struct return_codegen_state *ret = (struct return_codegen_state *)state;
|
||||
if (ret->s_prev_node != IVY_AST_RETURN) {
|
||||
return CODEGEN_RESULT_ERR(IVY_ERR_BAD_SYNTAX);
|
||||
}
|
||||
|
||||
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 return\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 return\n");
|
||||
struct return_codegen_state *ret = (struct return_codegen_state *)state;
|
||||
|
||||
mie_builder_ret(gen->c_builder, ret->s_value);
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static struct code_generator_result value_received(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
struct mie_value *value)
|
||||
{
|
||||
debug_printf("codegen: return value received\n");
|
||||
|
||||
struct return_codegen_state *ret = (struct return_codegen_state *)state;
|
||||
if (ret->s_value || ret->s_prev_node != IVY_AST_RETURN) {
|
||||
return CODEGEN_RESULT_ERR(IVY_ERR_BAD_SYNTAX);
|
||||
}
|
||||
|
||||
ret->s_value = value;
|
||||
ret->s_prev_node = IVY_AST_EXPR;
|
||||
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_POP_GENERATOR);
|
||||
}
|
||||
|
||||
struct code_generator return_generator = {
|
||||
.g_type = CODE_GENERATOR_VAR,
|
||||
.g_state_size = sizeof(struct return_codegen_state),
|
||||
.g_state_init = state_init,
|
||||
.g_state_fini = state_fini,
|
||||
.g_value_received = value_received,
|
||||
.g_expr_generator = gen_value_expr,
|
||||
.g_node_generators = {
|
||||
[IVY_AST_RETURN] = gen_return,
|
||||
},
|
||||
};
|
||||
@@ -183,6 +183,27 @@ static struct code_generator_result gen_var_declaration(
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
||||
}
|
||||
|
||||
static struct code_generator_result gen_return(
|
||||
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 mie_builder *builder = gen->c_builder;
|
||||
struct mie_func *current = mie_builder_get_current_func(builder);
|
||||
|
||||
enum ivy_status status = IVY_OK;
|
||||
if (!current || current != unit->s_immediate) {
|
||||
status = switch_to_immediate_func(gen, unit);
|
||||
}
|
||||
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
}
|
||||
|
||||
codegen_push_generator(gen, CODE_GENERATOR_RETURN, 0, NULL);
|
||||
return CODEGEN_RESULT_OK(CODEGEN_R_REPEAT_NODE);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static struct code_generator_result gen_var_declaration(
|
||||
struct ivy_codegen *gen, struct code_generator_state *state,
|
||||
@@ -272,6 +293,7 @@ struct code_generator unit_generator = {
|
||||
.g_state_fini = state_fini,
|
||||
.g_node_generators = {
|
||||
NODE_CODEGEN(VAR, gen_var_declaration),
|
||||
NODE_CODEGEN(RETURN, gen_return),
|
||||
NODE_CODEGEN(UNIT_PACKAGE, gen_unit_package),
|
||||
NODE_CODEGEN(UNIT_IMPORT, gen_unit_import),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user