X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=templates%2Ffunction_definition.c;h=ccf3e7596923a252cb4142c2906b350b958781aa;hb=379bdcf727cc66ee58d36a112e306b81e427fcf1;hp=3120fb96d9a17e6d26866ed40f712e3fe7e69695;hpb=3da330f045ed7fcb66ee9d9447de320680263699;p=fur diff --git a/templates/function_definition.c b/templates/function_definition.c index 3120fb9..ccf3e75 100644 --- a/templates/function_definition.c +++ b/templates/function_definition.c @@ -1,21 +1,38 @@ -Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args) +Object user${{name}}${{index}}$implementation( + EnvironmentPool* environmentPool, + Environment* environment, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) { - assert(argc == {{ argument_name_list|length }}); + environment = Environment_construct(environmentPool, environment); - Environment* environment = EnvironmentPool_allocate(environmentPool); - Environment_initialize(environment, parent); + StackSnapshot stackSnapshot = Stack_takeSnapshot(stack); - {% for argument_name in argument_name_list %} - Environment_set(environment, "{{ argument_name }}", args[{{ loop.index0 }}]); + jmp_buf jump; + if(setjmp(jump) != 0) + { + fprintf(stderr, "\tin {{name}} on line %zu\n", line); + + Stack_rewind(stack, stackSnapshot); + Environment_setLive(environment, false); + + longjmp(parentJump, 1); + } + + Object result = builtin$nil; + + {% for argument_name in argument_name_list|reverse %} + Environment_set(environment, "{{ argument_name }}", Stack_pop(stack)); {% endfor %} - {% for statement in statement_list[:-1] %} + {% for statement in statement_list %} {{ statement }} {% endfor %} - Object result = {{ statement_list[-1] }} - + // TODO Set the environment back to the parent environment Environment_setLive(environment, false); return result; }