Changeset b60ed54 for doc/papers/concurrency/examples/Fib.cfa
 Timestamp:
 Jun 23, 2019, 11:42:59 PM (4 years ago)
 Branches:
 armeh, enum, forallpointerdecay, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr, pthreademulation, qualifiedEnum
 Children:
 e764ee1
 Parents:
 8f079f0
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

doc/papers/concurrency/examples/Fib.cfa
r8f079f0 rb60ed54 13 13 } 14 14 15 #define FibCtor { 0, 1}16 typedef struct { int fn , fn1; } Fib;17 int fib_state( Fib & f ) with( f) {18 int fn0 = fn1 + fn2; fn2 = fn1; fn = fn0;19 return fn1;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; 20 20 } 21 21 … … 32 32 coroutine Fib2 { int fn; }; // used for communication 33 33 void main( Fib2 & fib ) with( fib ) { // called on first resume 34 int fn1 = 1, fn2 = 0; // precompute first two states 34 int fn1; // precompute first two states 35 [fn1, fn] = [1, 0]; 35 36 for () { 36 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case37 37 suspend(); // restart last resume 38 [fn1, fn] = [fn, fn + fn1]; 38 39 } 39 40 } 40 int ?()( Fib2 & fib ) with( fib ) {41 int ?()( Fib2 & fib ) { // functioncall interface 41 42 return resume( fib ).fn; // restart last suspend 42 43 } 43 int ?()( Fib2 & fib, int N ) with( fib ) {44 for ( N  1 ) fib(); 44 int ?()( Fib2 & fib, int N ) { // skip N values 45 for ( N  1 ) fib(); // use functioncall interface 45 46 return fib(); 46 47 } 47 double ?()( Fib2 & fib ) with( fib ) {48 return (int)(fib()) / 3.14159; // restart last suspend48 double ?()( Fib2 & fib ) { // different return type 49 return (int)(fib()) / 3.14159; // cast prevents recursive call 49 50 } 50 51
Note: See TracChangeset
for help on using the changeset viewer.