From e5e0dba41edce39475f839e29650007b37641be9 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Wed, 9 Aug 2017 14:37:41 -0400 Subject: [PATCH] Go back to allocating environments on the heap --- generation.py | 8 ++++---- templates/program.c | 15 ++++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/generation.py b/generation.py index b48342a..fa25718 100644 --- a/generation.py +++ b/generation.py @@ -23,7 +23,7 @@ def generate_constant_expression(c_constant_expression): return CONSTANT_EXPRESSION_MAPPING[c_constant_expression.value] def generate_symbol_expression(c_symbol_expression): - return 'Environment_get(&environment, SYMBOL_LIST[{}] /* symbol: {} */)'.format( + return 'Environment_get(environment, SYMBOL_LIST[{}] /* symbol: {} */)'.format( c_symbol_expression.symbol_list_index, c_symbol_expression.symbol, ) @@ -65,7 +65,7 @@ def generate_negation_expression(c_negation_expression): ) def generate_function_call(function_call): - return 'Environment_get(&environment, "{}").instance.closure(&environment, {}, {})'.format( + return 'Environment_get(environment, "{}").instance.closure(environment, {}, {})'.format( function_call.name, function_call.argument_count, # TODO This is just a single item containing a reference to the items list--make that clearer @@ -81,7 +81,7 @@ def generate_expression_statement(statement): return '{};'.format(generate_expression(statement.expression)) def generate_symbol_assignment_statement(c_assignment_statement): - return 'Environment_set(&environment, SYMBOL_LIST[{}] /* symbol: {} */, {});'.format( + return 'Environment_set(environment, SYMBOL_LIST[{}] /* symbol: {} */, {});'.format( c_assignment_statement.target_symbol_list_index, c_assignment_statement.target, generate_expression(c_assignment_statement.expression), @@ -142,7 +142,7 @@ def generate_if_else_statement(statement): return generated_if_clause + generated_if_statements + generated_else_statements def generate_function_declaration(statement): - return 'Environment_set(&environment, "{}", user${});'.format(statement.name, statement.name) + return 'Environment_set(environment, "{}", user${});'.format(statement.name, statement.name) def generate_statement(statement): return { diff --git a/templates/program.c b/templates/program.c index 0cab0c8..aba8382 100644 --- a/templates/program.c +++ b/templates/program.c @@ -333,15 +333,15 @@ Object builtin$print = { CLOSURE, (Instance)builtin$print$implementation }; {% for function_definition in function_definition_list %} Object user${{function_definition.name}}$implementation(Environment* parent, size_t argc, Object* args) { - Environment environment; - Environment_initialize(&environment, parent); + Environment* environment = malloc(sizeof(Environment));; + Environment_initialize(environment, parent); {% for statement in function_definition.statement_list[:-1] %} {{ generate_statement(statement) }} {% endfor %} Object result = {{ generate_statement(function_definition.statement_list[-1]) }} - Environment_deinitialize(&environment); + Environment_deinitialize(environment); return result; } @@ -350,19 +350,20 @@ Object user${{function_definition.name}} = { CLOSURE, (Instance)user${{function_ int main(int argc, char** argv) { - Environment environment; - Environment_initialize(&environment, NULL); + Environment* environment = malloc(sizeof(Environment)); + Environment_initialize(environment, NULL); // TODO Use the symbol from SYMBOL_LIST {% for builtin in builtins %} - Environment_set(&environment, "{{ builtin }}", builtin${{ builtin }}); + Environment_set(environment, "{{ builtin }}", builtin${{ builtin }}); {% endfor %} {% for statement in statements %} {{ generate_statement(statement) }} {% endfor %} - Environment_deinitialize(&environment); + Environment_deinitialize(environment); + free(environment); return 0; } -- 2.20.1