source:
tests/coroutine/fibonacci.cfa
@
fbfd97bd
Last change on this file since fbfd97bd was 427854b, checked in by , 5 years ago | |
---|---|
|
|
File size: 1.3 KB |
Rev | Line | |
---|---|---|
[242a902] | 1 | // |
[ec95d11] | 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. | |
[242a902] | 6 | // |
[71b50f36] | 7 | // fibonacci.c -- 3-state finite-state machine |
[242a902] | 8 | // |
[ec95d11] | 9 | // Author : Thierry Delisle |
10 | // Created On : Thu Jun 8 07:29:37 2017 | |
11 | // Last Modified By : Peter A. Buhr | |
[f8cd310] | 12 | // Last Modified On : Fri Mar 22 13:40:35 2019 |
13 | // Update Count : 26 | |
[242a902] | 14 | // |
[ec95d11] | 15 | |
[73abe95] | 16 | #include <fstream.hfa> |
17 | #include <coroutine.hfa> | |
[c15b805] | 18 | |
[971d9f2] | 19 | coroutine Fibonacci { int fn; }; // used for communication |
[c15b805] | 20 | |
[71b50f36] | 21 | void main( Fibonacci & fib ) with( fib ) { // called on first resume |
[f802e46] | 22 | int fn1, fn2; // retained between resumes |
[af1ed1ad] | 23 | fn = 0; fn1 = fn; // 1st case |
[427854b] | 24 | suspend; // restart last resume |
[af1ed1ad] | 25 | fn = 1; fn2 = fn1; fn1 = fn; // 2nd case |
[427854b] | 26 | suspend; // restart last resume |
[adb6b30f] | 27 | for () { |
[71b50f36] | 28 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case |
[427854b] | 29 | suspend; // restart last resume |
[ec95d11] | 30 | } // for |
[c15b805] | 31 | } |
32 | ||
[971d9f2] | 33 | int next( Fibonacci & fib ) with( fib ) { |
34 | resume( fib ); // restart last suspend | |
35 | return fn; | |
[c15b805] | 36 | } |
37 | ||
[9129a84] | 38 | int main() { |
[ec95d11] | 39 | Fibonacci f1, f2; |
[ae4af81] | 40 | for ( 10 ) { // print N Fibonacci values |
[200fcb3] | 41 | sout | next( f1 ) | next( f2 ); |
[ec95d11] | 42 | } // for |
[0e76cf4f] | 43 | } |
[ec95d11] | 44 | |
45 | // Local Variables: // | |
46 | // tab-width: 4 // | |
[f8cd310] | 47 | // compile-command: "cfa fibonacci.cfa" // |
[ec95d11] | 48 | // End: // |
Note: See TracBrowser
for help on using the repository browser.