ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change
on this file since b408364 was
600d7be,
checked in by Peter A. Buhr <pabuhr@…>, 5 years ago
|
example updates
|
-
Property mode set to
100644
|
File size:
1.7 KB
|
Rev | Line | |
---|
[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 | |
---|
[600d7be] | 15 | #define FibCtor { 0, 1 } |
---|
| 16 | typedef struct { int fn, fn1; } Fib; |
---|
[2e041e27] | 17 | int fib_state( Fib & f ) with( f ) { |
---|
[600d7be] | 18 | int fn0 = fn1 + fn2; fn2 = fn1; fn = fn0; |
---|
| 19 | return fn1; |
---|
[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 |
---|
[600d7be] | 34 | int fn1 = 1, fn2 = 0; // precompute first two states |
---|
[2e041e27] | 35 | for () { |
---|
| 36 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case |
---|
| 37 | suspend(); // restart last resume |
---|
| 38 | } |
---|
| 39 | } |
---|
[600d7be] | 40 | int ?()( Fib2 & fib ) with( fib ) { |
---|
| 41 | return resume( fib ).fn; // restart last suspend |
---|
| 42 | } |
---|
| 43 | int ?()( Fib2 & fib, int N ) with( fib ) { |
---|
| 44 | for ( N - 1 ) fib(); |
---|
| 45 | return fib(); |
---|
| 46 | } |
---|
| 47 | double ?()( Fib2 & fib ) with( fib ) { |
---|
| 48 | return (int)(fib()) / 3.14159; // restart last suspend |
---|
[2e041e27] | 49 | } |
---|
| 50 | |
---|
| 51 | int main() { |
---|
| 52 | for ( 10 ) |
---|
| 53 | sout | fib_gvar(); |
---|
| 54 | sout | nl; |
---|
| 55 | |
---|
[600d7be] | 56 | Fib f1 = FibCtor, f2 = FibCtor; |
---|
[2e041e27] | 57 | for ( 10 ) |
---|
| 58 | sout | fib_state( f1 ) | fib_state( f2 ); |
---|
| 59 | sout | nl; |
---|
| 60 | |
---|
| 61 | Fib1 f1, f2; |
---|
| 62 | for ( 10 ) |
---|
[600d7be] | 63 | sout | f1() | f2(); |
---|
[2e041e27] | 64 | sout | nl; |
---|
| 65 | |
---|
[600d7be] | 66 | Fib2 f12, f22; |
---|
[2e041e27] | 67 | for ( 10 ) |
---|
[600d7be] | 68 | sout | (int)f12() | (double)f12() | f22( 2 ); |
---|
[2e041e27] | 69 | } |
---|
| 70 | |
---|
| 71 | // Local Variables: // |
---|
| 72 | // tab-width: 4 // |
---|
| 73 | // fill-column: 120 // |
---|
| 74 | // compile-command: "cfa Fib.cfa" // |
---|
| 75 | // End: // |
---|
Note: See
TracBrowser
for help on using the repository browser.