diff options
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); + } +} |
