X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=templates%2Ffunction_definition.c;h=ccf3e7596923a252cb4142c2906b350b958781aa;hb=edc05c8d2d465653c02c350592eff62c542a37ed;hp=0c60f3082eef5c77a1f9659945928bc7a87c6db5;hpb=a96e96f3f783930707122f691cd6a08a90416a74;p=fur diff --git a/templates/function_definition.c b/templates/function_definition.c index 0c60f30..ccf3e75 100644 --- a/templates/function_definition.c +++ b/templates/function_definition.c @@ -1,20 +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); + + StackSnapshot stackSnapshot = Stack_takeSnapshot(stack); + + 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); + } - Environment* environment = EnvironmentPool_allocate(environmentPool); - Environment_initialize(environment, parent); Object result = builtin$nil; - {% for argument_name in argument_name_list %} - Environment_set(environment, "{{ argument_name }}", args[{{ loop.index0 }}]); + {% for argument_name in argument_name_list|reverse %} + Environment_set(environment, "{{ argument_name }}", Stack_pop(stack)); {% endfor %} {% for statement in statement_list %} {{ statement }} {% endfor %} + // TODO Set the environment back to the parent environment Environment_setLive(environment, false); return result; }