1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // fibonacci.c -- 3-state finite-state machine |
---|
8 | // |
---|
9 | // Author : Colby Parsons |
---|
10 | // Created On : Thu July 6 07:29:37 2023 |
---|
11 | // Last Modified By : Colby Parsons |
---|
12 | // Last Modified On : Thu July 6 07:29:37 2023 |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream.hfa> |
---|
17 | #include <coroutine.hfa> |
---|
18 | #include <stdlib.hfa> |
---|
19 | |
---|
20 | exception fib_num { |
---|
21 | int num; |
---|
22 | }; |
---|
23 | vtable(fib_num) fib_num_vt; |
---|
24 | |
---|
25 | exception fib_2_num { |
---|
26 | int num1, num2; |
---|
27 | }; |
---|
28 | vtable(fib_2_num) fib_2_num_vt; |
---|
29 | |
---|
30 | coroutine Fibonacci { int fn; }; // used for communication |
---|
31 | |
---|
32 | void main( Fibonacci & fib ) with( fib ) { // called on first resume |
---|
33 | int fn1, fn2; // retained between resumes |
---|
34 | try{ |
---|
35 | fn = 0; fn1 = fn; // 1st case |
---|
36 | poll( fib ); |
---|
37 | suspend; // restart last resume |
---|
38 | fn = 1; fn2 = fn1; fn1 = fn; // 2nd case |
---|
39 | poll( fib ); |
---|
40 | suspend; // restart last resume |
---|
41 | for () { |
---|
42 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case |
---|
43 | poll( fib ); |
---|
44 | suspend; // restart last resume |
---|
45 | } // for |
---|
46 | } catchResume ( fib_num * e ) { |
---|
47 | fn1 = e->num; |
---|
48 | fn2 = e->num; |
---|
49 | fn = fn1 + fn2; |
---|
50 | } catchResume ( fib_2_num * e ) { |
---|
51 | fn1 = e->num1; |
---|
52 | fn2 = e->num1; |
---|
53 | fn = fn1 + fn2; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | int main() { |
---|
58 | Fibonacci f1, f2; |
---|
59 | for ( 3 ) { // print N Fibonacci values |
---|
60 | sout | resume( f1 ).fn; |
---|
61 | } // for |
---|
62 | for ( i; 3 ) { |
---|
63 | fib_num except{ &fib_num_vt, i }; |
---|
64 | resumeAt( f1, except ); |
---|
65 | sout | resume( f1 ).fn; |
---|
66 | } |
---|
67 | { |
---|
68 | fib_2_num except{ &fib_2_num_vt, 10, 12 }; |
---|
69 | resumeAt( f1, except ); |
---|
70 | } |
---|
71 | sout | resume( f1 ).fn; |
---|
72 | } |
---|
73 | |
---|
74 | // Local Variables: // |
---|
75 | // tab-width: 4 // |
---|
76 | // compile-command: "cfa fibonacci.cfa" // |
---|
77 | // End: // |
---|