From: David Kerkeslager Date: Wed, 20 Sep 2017 22:32:08 +0000 (-0400) Subject: Start passing around a jump buffer to handle errors X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;h=d875a383425b06597d6ddc8a8c7fe67feec77962;p=fur Start passing around a jump buffer to handle errors --- diff --git a/generation.py b/generation.py index d5d64ba..44a8d16 100644 --- a/generation.py +++ b/generation.py @@ -27,7 +27,7 @@ def generate_variable_expression(expression): return expression.variable def generate_function_call_for_fur_infix_operator(expression): - return 'operator${}(stack)'.format( + return 'operator${}(stack, jump)'.format( expression.name, ) @@ -80,7 +80,7 @@ def generate_function_call(function_call): # TODO Check the type of the things being called function_expression = generate_variable_expression(function_call.function_expression) - return '{}.instance.closure.call(environmentPool, {}.instance.closure.closed, {}, stack)'.format( + return '{}.instance.closure.call(environmentPool, {}.instance.closure.closed, {}, stack, jump)'.format( function_expression, function_expression, function_call.argument_count, diff --git a/templates/function_definition.c b/templates/function_definition.c index 24c73f8..fb7a090 100644 --- a/templates/function_definition.c +++ b/templates/function_definition.c @@ -1,8 +1,17 @@ -Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack) +Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent_jump) { Environment* environment = EnvironmentPool_allocate(environmentPool); Environment_initialize(environment, parent); + + jmp_buf jump; + if(setjmp(jump) != 0) + { + Environment_setLive(environment, false); + fprintf(stderr, "Error in {{name}}\n"); + longjmp(parent_jump, 1); + } + Object result = builtin$nil; {% for argument_name in argument_name_list|reverse %} diff --git a/templates/program.c b/templates/program.c index db1abdf..a599881 100644 --- a/templates/program.c +++ b/templates/program.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -66,7 +67,7 @@ typedef struct Closure Closure; struct Closure { Environment* closed; - Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*); + Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*, jmp_buf); }; struct List; @@ -544,7 +545,7 @@ Object operator$negate(Object input) } // TODO Make this conditionally added -Object operator$concatenate(Stack* stack) +Object operator$concatenate(Stack* stack, jmp_buf parent_jump) { Object right = Stack_pop(stack); Object left = Stack_pop(stack); @@ -577,7 +578,7 @@ Object operator$concatenate(Stack* stack) } {% for id in infix_declarations %} -Object operator${{ id.name }}(Stack* stack) +Object operator${{ id.name }}(Stack* stack, jmp_buf parent_jump) { Object right = Stack_pop(stack); Object left = Stack_pop(stack); @@ -593,7 +594,7 @@ Object operator${{ id.name }}(Stack* stack) {% endfor %} {% if 'pow' in builtins %} -Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack) +Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent_jump) { // Must unload items in reverse order Object exponent = Stack_pop(stack); @@ -612,7 +613,7 @@ Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implement {% endif %} {% if 'print' in builtins %} -Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack) +Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent_jump) { Stack reverse_stack; Stack_initialize(&reverse_stack); @@ -642,9 +643,9 @@ Object builtin$print$implementation(EnvironmentPool* environmentPool, Environmen case STRING_CONCATENATION: Stack_push(stack, output.instance.string_concatenation->left); - builtin$print$implementation(NULL, NULL, 1, stack); + builtin$print$implementation(NULL, NULL, 1, stack, parent_jump); Stack_push(stack, output.instance.string_concatenation->right); - builtin$print$implementation(NULL, NULL, 1, stack); + builtin$print$implementation(NULL, NULL, 1, stack, parent_jump); break; case STRING_LITERAL: @@ -682,6 +683,15 @@ int main(int argc, char** argv) Stack* stack = &stackMemory; Stack_initialize(stack); + jmp_buf jump; + if(setjmp(jump) != 0) + { + fprintf(stderr, "Error in __main__\n"); + Environment_setLive(environment, false); + EnvironmentPool_destruct(environmentPool); + return 1; + } + // TODO Use the symbol from SYMBOL_LIST {% for builtin in builtins %} Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});