[2e041e27] | 1 | #include <thread.hfa> |
---|
| 2 | #include <fstream.hfa> |
---|
| 3 | |
---|
| 4 | int fn1, fn2, state = 1; |
---|
| 5 | int fib_gvar() { |
---|
| 6 | int fn; |
---|
| 7 | choose ( state ) { |
---|
| 8 | case 1: fn = 0; fn1 = fn; state = 2; |
---|
| 9 | case 2: fn = 1; fn2 = fn1; fn1 = fn; state = 3; |
---|
| 10 | case 3: fn = fn1 + fn2; fn2 = fn1; fn1 = fn; |
---|
| 11 | } |
---|
| 12 | return fn; |
---|
| 13 | } |
---|
| 14 | |
---|
[b60ed54] | 15 | #define FibCtor { 1, 0 } |
---|
| 16 | typedef struct { int fn1, fn; } Fib; |
---|
| 17 | int fib_state( Fib & f ) with(f) { |
---|
| 18 | int ret = fn; fn = fn1; fn1 = fn + ret; |
---|
| 19 | return ret; |
---|
[2e041e27] | 20 | } |
---|
| 21 | |
---|
| 22 | coroutine Fib1 { int fn; }; // used for communication |
---|
| 23 | void main( Fib1 & fib ) with( fib ) { // called on first resume |
---|
| 24 | fn = 0; int fn1 = fn; suspend(); |
---|
| 25 | fn = 1; int fn2 = fn1; fn1 = fn; suspend(); |
---|
| 26 | for () { |
---|
| 27 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; suspend(); |
---|
| 28 | } |
---|
| 29 | } |
---|
[600d7be] | 30 | int ?()( Fib1 & fib ) with( fib ) { return resume( fib ).fn; } |
---|
[2e041e27] | 31 | |
---|
[600d7be] | 32 | coroutine Fib2 { int fn; }; // used for communication |
---|
[2e041e27] | 33 | void main( Fib2 & fib ) with( fib ) { // called on first resume |
---|
[b60ed54] | 34 | int fn1; // precompute first two states |
---|
| 35 | [fn1, fn] = [1, 0]; |
---|
[2e041e27] | 36 | for () { |
---|
| 37 | suspend(); // restart last resume |
---|
[b60ed54] | 38 | [fn1, fn] = [fn, fn + fn1]; |
---|
[2e041e27] | 39 | } |
---|
| 40 | } |
---|
[b60ed54] | 41 | int ?()( Fib2 & fib ) { // function-call interface |
---|
[600d7be] | 42 | return resume( fib ).fn; // restart last suspend |
---|
| 43 | } |
---|
[b60ed54] | 44 | int ?()( Fib2 & fib, int N ) { // skip N values |
---|
| 45 | for ( N - 1 ) fib(); // use function-call interface |
---|
[600d7be] | 46 | return fib(); |
---|
| 47 | } |
---|
[b60ed54] | 48 | double ?()( Fib2 & fib ) { // different return type |
---|
| 49 | return (int)(fib()) / 3.14159; // cast prevents recursive call |
---|
[2e041e27] | 50 | } |
---|
| 51 | |
---|
| 52 | int main() { |
---|
| 53 | for ( 10 ) |
---|
| 54 | sout | fib_gvar(); |
---|
| 55 | sout | nl; |
---|
| 56 | |
---|
[600d7be] | 57 | Fib f1 = FibCtor, f2 = FibCtor; |
---|
[2e041e27] | 58 | for ( 10 ) |
---|
| 59 | sout | fib_state( f1 ) | fib_state( f2 ); |
---|
| 60 | sout | nl; |
---|
| 61 | |
---|
| 62 | Fib1 f1, f2; |
---|
| 63 | for ( 10 ) |
---|
[600d7be] | 64 | sout | f1() | f2(); |
---|
[2e041e27] | 65 | sout | nl; |
---|
| 66 | |
---|
[600d7be] | 67 | Fib2 f12, f22; |
---|
[2e041e27] | 68 | for ( 10 ) |
---|
[600d7be] | 69 | sout | (int)f12() | (double)f12() | f22( 2 ); |
---|
[2e041e27] | 70 | } |
---|
| 71 | |
---|
| 72 | // Local Variables: // |
---|
| 73 | // tab-width: 4 // |
---|
| 74 | // fill-column: 120 // |
---|
| 75 | // compile-command: "cfa Fib.cfa" // |
---|
| 76 | // End: // |
---|