lang: codegen: redesign variable definition/resolution system to support capturing
the codegen scope system has been removed. instead, each generator state in the stack, from the current state backwards, is informed when a variable is defined, resolved, or captured. when a variable is defined, the state stack is traversed back-to-front (current generator first). each state has a chance to record the variable definition. once one state has signalled that it has recorded the variable definition, the traversal ends. when a variable is resolved, the state stack is traversed back-to-front (current generator first). each state is asked whether or not it recognises the variable identifier being resolved. if a state has the variable in question defined, it returns information about the variable definition, and the traversal stops. once a variable has been resolved, the state stack is traversed front-to-back (current generator last), starting from the generator /after/ the one that provided the variable definition. each generator in the iteration is given the chance to adjust the variable information, or generate IR in response to the variable being accessed. this is used to implement variable capture, where the state of a variable in the enclosing context is captured for later use.
This commit is contained in:
@@ -178,8 +178,14 @@ static struct code_generator_result gen_var_reference(
|
||||
struct mie_value *var_ptr = NULL;
|
||||
|
||||
struct ivy_ast_ident_node *ident = (struct ivy_ast_ident_node *)node;
|
||||
struct codegen_var *var
|
||||
= codegen_resolve_variable(gen, ident->n_content->t_str);
|
||||
struct codegen_var var = {};
|
||||
enum ivy_status status
|
||||
= codegen_resolve_variable(gen, ident->n_content->t_str, &var);
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (var) {
|
||||
/* this is a local variable */
|
||||
var_ptr = var->v_ptr;
|
||||
@@ -192,8 +198,13 @@ static struct code_generator_result gen_var_reference(
|
||||
struct mie_type *id = mie_ctx_get_type(gen->c_ctx, MIE_TYPE_ID);
|
||||
struct mie_value *var_value
|
||||
= mie_builder_load(gen->c_builder, id, var_ptr, NULL);
|
||||
#endif
|
||||
struct mie_value *var_value = codegen_load_variable(gen, &var);
|
||||
if (!var_value) {
|
||||
return CODEGEN_RESULT_ERR(IVY_ERR_INTERNAL_FAILURE);
|
||||
}
|
||||
|
||||
enum ivy_status status = push_operand(expr, node, var_value);
|
||||
status = push_operand(expr, node, var_value);
|
||||
|
||||
if (status != IVY_OK) {
|
||||
return CODEGEN_RESULT_ERR(status);
|
||||
|
||||
Reference in New Issue
Block a user