diff options
| author | henesy <henesy.dev@gmail.com> | 2019-03-11 23:22:22 -0500 |
|---|---|---|
| committer | henesy <henesy.dev@gmail.com> | 2019-03-11 23:22:22 -0500 |
| commit | c818e95dc18d91228358fc9750d29dcd8cc9b488 (patch) | |
| tree | 6c16d4286b607c5ead4a59134be8cae60f9fde75 /Exceptions/exceptions.b | |
| parent | ce87687bbc9586ae0ecb1ea646596a76410ef568 (diff) | |
add exceptions example and explanation
Diffstat (limited to 'Exceptions/exceptions.b')
| -rw-r--r-- | Exceptions/exceptions.b | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/Exceptions/exceptions.b b/Exceptions/exceptions.b index 336c448..b9addaa 100644 --- a/Exceptions/exceptions.b +++ b/Exceptions/exceptions.b @@ -4,18 +4,60 @@ include "sys.m"; include "draw.m"; sys: Sys; -print: import sys; Exceptions: module { init: fn(nil: ref Draw->Context, nil: list of string); }; +FIB: exception(int, int); + init(nil: ref Draw->Context, nil: list of string) { sys = load Sys Sys->PATH; - + for(i := 0; i < 5 ; i++) { + f := fibonacci(i); + + if(f < 0) + break; + + sys->print("F(%d) = %d\n", i, f); + } raise "going down!"; exit; } + +fibonacci(n: int): int { + { + fib(1, n, 1, 1); + } exception e { + FIB => + (x, nil) := e; + return x; + + "*" => + sys->print("unexpected string exception %s raised\n", e); + + * => + sys->print("unexpected exception raised\n"); + } + + return 0; +} + +fib(n, m, x, y: int) raises (FIB) { + if(n >= m) + raise FIB(x, y); + { + fib(n+1, m, x, y); + } exception e { + FIB => + (x, y) = e; + + x = x+y; + y = x-y; + + raise FIB(x, y); + } +} |
