Simple exceptions (#6)
authorDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 22 Sep 2017 03:38:34 +0000 (23:38 -0400)
committerGitHub <noreply@github.com>
Fri, 22 Sep 2017 03:38:34 +0000 (23:38 -0400)
* Added basic exceptions

examples/30_division_by_zero.fur [new file with mode: 0644]
examples/30_division_by_zero.fur.stderr.txt [new file with mode: 0644]
integration_tests.py
templates/function_definition.c
templates/program.c

diff --git a/examples/30_division_by_zero.fur b/examples/30_division_by_zero.fur
new file mode 100644 (file)
index 0000000..7d504cd
--- /dev/null
@@ -0,0 +1,5 @@
+def get_divided_answer() do
+  42 // 0
+end
+
+print(get_divided_answer())
diff --git a/examples/30_division_by_zero.fur.stderr.txt b/examples/30_division_by_zero.fur.stderr.txt
new file mode 100644 (file)
index 0000000..b1f79c7
--- /dev/null
@@ -0,0 +1,3 @@
+DivisionByZeroError
+       in get_divided_answer
+       in __main__
index 7669aea..a4d7c67 100644 (file)
@@ -95,7 +95,7 @@ def add_example_memory_leak_test(filename):
                         '--show-reachable=yes',
                         '--num-callers=20',
                         '--track-fds=yes',
-                        '--error-exitcode=666',
+                        '--error-exitcode=42',
                         '-q',
                         './a.out',
                     ],
index fb7a090..72ce6f0 100644 (file)
@@ -8,7 +8,7 @@ Object user${{name}}$implementation(EnvironmentPool* environmentPool, Environmen
   if(setjmp(jump) != 0)
   {
     Environment_setLive(environment, false);
-    fprintf(stderr, "Error in {{name}}\n");
+    fprintf(stderr, "\tin {{name}}\n");
     longjmp(parent_jump, 1);
   }
 
index a599881..18d8616 100644 (file)
@@ -586,6 +586,14 @@ Object operator${{ id.name }}(Stack* stack, jmp_buf parent_jump)
   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() }};
@@ -686,10 +694,12 @@ int main(int argc, char** argv)
   jmp_buf jump;
   if(setjmp(jump) != 0)
   {
-    fprintf(stderr, "Error in __main__\n");
+    fprintf(stderr, "\tin __main__\n");
     Environment_setLive(environment, false);
     EnvironmentPool_destruct(environmentPool);
-    return 1;
+
+    // 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