Simple exceptions (#6)
[fur] / templates / program.c
index db1abdf..18d8616 100644 (file)
@@ -1,5 +1,6 @@
 #include <assert.h>
 #include <inttypes.h>
+#include <setjmp.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
@@ -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);
@@ -585,6 +586,14 @@ Object operator${{ id.name }}(Stack* stack)
   assert(left.type == {{ id.in_type.upper() }});
   assert(right.type == {{ id.in_type.upper() }});
 
+  {% if id.name == 'integerDivide' or id.name == 'modularDivide' %}
+  if(right.instance.integer == 0)
+  {
+    fprintf(stderr, "DivisionByZeroError\n");
+    longjmp(parent_jump, 1);
+  }
+  {% endif %}
+
   Object result;
   result.type = {{ id.out_type.upper() }};
   result.instance.{{ id.out_type.lower() }} = left.instance.{{ id.in_type.lower() }} {{ id.operator }} right.instance.{{ id.in_type.lower() }};
@@ -593,7 +602,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 +621,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 +651,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 +691,17 @@ int main(int argc, char** argv)
   Stack* stack = &stackMemory;
   Stack_initialize(stack);
 
+  jmp_buf jump;
+  if(setjmp(jump) != 0)
+  {
+    fprintf(stderr, "\tin __main__\n");
+    Environment_setLive(environment, false);
+    EnvironmentPool_destruct(environmentPool);
+
+    // TODO We would like to return something nonzero here, but that messes up Valgrind so we couldn't catch memory leaks
+    return 0;
+  }
+
   // TODO Use the symbol from SYMBOL_LIST
   {% for builtin in builtins %}
   Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});