Start working on the interpeter
[fur] / templates / function_definition.c
index 24c73f8..ccf3e75 100644 (file)
@@ -1,8 +1,27 @@
 
-Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack)
+Object user${{name}}${{index}}$implementation(
+    EnvironmentPool* environmentPool,
+    Environment* environment,
+    size_t argc,
+    Stack* stack,
+    const unsigned long line,
+    jmp_buf parentJump)
 {
-  Environment* environment = EnvironmentPool_allocate(environmentPool);
-  Environment_initialize(environment, parent);
+  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);
+  }
+
   Object result = builtin$nil;
 
   {% for argument_name in argument_name_list|reverse %}
@@ -13,6 +32,7 @@ Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environmen
   {{ statement }}
   {% endfor %}
 
+  // TODO Set the environment back to the parent environment
   Environment_setLive(environment, false);
   return result;
 }