Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (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' into cleanup-dtors

File:
1 moved

Legend:

Unmodified
Added
Removed
  • tests/coroutine/fibonacci.c

    rf9feab8 r90152a4  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // fibonacci.c --
     7// fibonacci.c -- 3-state finite-state machine
    88//
    99// Author           : Thierry Delisle
    1010// Created On       : Thu Jun  8 07:29:37 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Sep 17 21:38:15 2017
    13 // Update Count     : 7
     12// Last Modified On : Sat Aug 18 11:21:58 2018
     13// Update Count     : 24
    1414//
    1515
    16 #include <fstream>
    17 #include <coroutine>
     16#include <fstream.hfa>
     17#include <coroutine.hfa>
    1818
    19 coroutine Fibonacci {
    20         int fn;                                                                                         // used for communication
    21 };
     19coroutine Fibonacci { int fn; };                                                // used for communication
    2220
    23 void ?{}( Fibonacci & this ) {
    24         this.fn = 0;
    25 }
    26 
    27 void main( Fibonacci & this ) {
     21void main( Fibonacci & fib ) with( fib ) {                              // called on first resume
    2822        int fn1, fn2;                                                                           // retained between resumes
    29         this.fn = 0;                                                                            // case 0
    30         fn1 = this.fn;
     23        fn = 0;  fn1 = fn;                                                                      // 1st case
    3124        suspend();                                                                                      // restart last resume
    32 
    33         this.fn = 1;                                                                            // case 1
    34         fn2 = fn1;  fn1 = this.fn;
     25        fn = 1;  fn2 = fn1;  fn1 = fn;                                          // 2nd case
    3526        suspend();                                                                                      // restart last resume
    36 
    37         for ( ;; ) {                                                                            // general case
    38                 this.fn = fn1 + fn2;
    39                 fn2 = fn1;  fn1 = this.fn;
     27        for () {
     28                fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;                  // general case
    4029                suspend();                                                                              // restart last resume
    4130        } // for
    4231}
    4332
    44 int next( Fibonacci & this ) {
    45         resume( this );                                                                         // restart last suspend
    46         return this.fn;
     33int next( Fibonacci & fib ) with( fib ) {
     34        resume( fib );                                                                          // restart last suspend
     35        return fn;
    4736}
    4837
    4938int main() {
    5039        Fibonacci f1, f2;
    51         for ( int i = 1; i <= 10; i += 1 ) {
     40        for ( 10 ) {                                                                            // print N Fibonacci values
    5241                sout | next( f1 ) | next( f2 ) | endl;
    5342        } // for
Note: See TracChangeset for help on using the changeset viewer.