Ignore:
Timestamp:
Jun 24, 2019, 11:12:01 AM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b58affe7, c1398e4
Parents:
18e683b (diff), 9e0a360 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/examples/Fib.cfa

    r18e683b rc1ea11b  
    1313}
    1414
    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 }
     16typedef struct { int fn1, fn; } Fib;
     17int fib_state( Fib & f ) with(f) {
     18        int ret = fn; fn = fn1; fn1 = fn + ret;
     19        return ret;
    2020}
    2121
     
    3232coroutine Fib2 { int fn; };                                             // used for communication
    3333void 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];
    3536        for () {
    36                 fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;  // general case
    3737                suspend();                                                              // restart last resume
     38                [fn1, fn] = [fn, fn + fn1];
    3839        }
    3940}
    40 int ?()( Fib2 & fib ) with( fib ) {
     41int ?()( Fib2 & fib ) {                                                 // function-call interface
    4142        return resume( fib ).fn;                                        // restart last suspend
    4243}
    43 int ?()( Fib2 & fib, int N ) with( fib ) {
    44         for ( N - 1 ) fib();
     44int ?()( Fib2 & fib, int N ) {                                  // skip N values
     45        for ( N - 1 ) fib();                                            // use function-call interface
    4546        return fib();
    4647}
    47 double ?()( Fib2 & fib ) with( fib ) {
    48         return (int)(fib()) / 3.14159;                                          // restart last suspend
     48double ?()( Fib2 & fib ) {                                              // different return type
     49        return (int)(fib()) / 3.14159;                          // cast prevents recursive call
    4950}
    5051
Note: See TracChangeset for help on using the changeset viewer.