Ignore:
Timestamp:
Jun 4, 2019, 6:28:55 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
7261e3c
Parents:
466fa01
Message:

example updates

File:
1 edited

Legend:

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

    r466fa01 r600d7be  
    1313}
    1414
    15 #define FIB_INIT { 0, 1 }
    16 typedef struct { int fn1, fn2; } Fib;
     15#define FibCtor { 0, 1 }
     16typedef struct { int fn, fn1; } Fib;
    1717int fib_state( Fib & f ) with( f ) {
    18         int ret = fn2;
    19         int fn = fn1 + fn2;
    20         fn2 = fn1; fn1 = fn;
    21         return ret;
     18        int fn0 = fn1 + fn2;  fn2 = fn1;  fn = fn0;
     19        return fn1;
    2220}
    2321
     
    3028        }
    3129}
    32 int next( Fib1 & fib ) with( fib ) { resume( fib ); return fn; }
     30int ?()( Fib1 & fib ) with( fib ) { return resume( fib ).fn; }
    3331
    34 coroutine Fib2 { int ret; };                                    // used for communication
     32coroutine Fib2 { int fn; };                                             // used for communication
    3533void main( Fib2 & fib ) with( fib ) {                   // called on first resume
    36         int fn, fn1 = 1, fn2 = 0;                                       // precompute first two states
     34        int fn1 = 1, fn2 = 0;                                           // precompute first two states
    3735        for () {
    38                 ret = fn2;
    3936                fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;  // general case
    4037                suspend();                                                              // restart last resume
    4138        }
    4239}
    43 int next( Fib2 & fib ) with( fib ) {
    44         resume( fib );                                                          // restart last suspend
    45         return ret;
     40int ?()( Fib2 & fib ) with( fib ) {
     41        return resume( fib ).fn;                                        // restart last suspend
     42}
     43int ?()( Fib2 & fib, int N ) with( fib ) {
     44        for ( N - 1 ) fib();
     45        return fib();
     46}
     47double ?()( Fib2 & fib ) with( fib ) {
     48        return (int)(fib()) / 3.14159;                                          // restart last suspend
    4649}
    4750
     
    5154        sout | nl;
    5255
    53         Fib f1 = FIB_INIT, f2 = FIB_INIT;
     56        Fib f1 = FibCtor, f2 = FibCtor;
    5457        for ( 10 )
    5558                sout | fib_state( f1 ) | fib_state( f2 );
     
    5861        Fib1 f1, f2;
    5962        for ( 10 )
    60                 sout | next( f1 ) | next( f2 );
     63                sout | f1() | f2();
    6164        sout | nl;
    6265
    63         Fib2 f1, f2;
     66        Fib2 f12, f22;
    6467        for ( 10 )
    65                 sout | next( (Fib2 &)f1 ) | next( (Fib2 &)f2 );
     68                sout | (int)f12() | (double)f12() | f22( 2 );
    6669}
    6770
Note: See TracChangeset for help on using the changeset viewer.