1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
implement Exceptions;
include "sys.m";
include "draw.m";
sys: 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);
}
}
|