From bca87656ab93d4b99b4b007bfd54580bdb19f9dc Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Thu, 21 Sep 2017 23:38:34 -0400 Subject: [PATCH] Simple exceptions (#6) * Added basic exceptions --- examples/30_division_by_zero.fur | 5 +++++ examples/30_division_by_zero.fur.stderr.txt | 3 +++ integration_tests.py | 2 +- templates/function_definition.c | 2 +- templates/program.c | 14 ++++++++++++-- 5 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 examples/30_division_by_zero.fur create mode 100644 examples/30_division_by_zero.fur.stderr.txt diff --git a/examples/30_division_by_zero.fur b/examples/30_division_by_zero.fur new file mode 100644 index 0000000..7d504cd --- /dev/null +++ b/examples/30_division_by_zero.fur @@ -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 index 0000000..b1f79c7 --- /dev/null +++ b/examples/30_division_by_zero.fur.stderr.txt @@ -0,0 +1,3 @@ +DivisionByZeroError + in get_divided_answer + in __main__ diff --git a/integration_tests.py b/integration_tests.py index 7669aea..a4d7c67 100644 --- a/integration_tests.py +++ b/integration_tests.py @@ -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', ], diff --git a/templates/function_definition.c b/templates/function_definition.c index fb7a090..72ce6f0 100644 --- a/templates/function_definition.c +++ b/templates/function_definition.c @@ -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); } diff --git a/templates/program.c b/templates/program.c index a599881..18d8616 100644 --- a/templates/program.c +++ b/templates/program.c @@ -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 -- 2.20.1