source: tests/coroutine/fibonacci_1.cfa @ ecaedf35

Last change on this file since ecaedf35 was 427854b, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

First draft implementation of generators, still missing error checking, testing and clean-up

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[69efc321]1//
2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[386e710]7// fibonacci_1.cfa -- 1-state finite-state machine: precomputed first two states returning f(n - 1)
[69efc321]8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Apr 26 23:20:08 2018
11// Last Modified By : Peter A. Buhr
[386e710]12// Last Modified On : Thu Mar 21 08:10:45 2019
13// Update Count     : 25
[427854b]14//
[69efc321]15
16#include <fstream.hfa>
17#include <coroutine.hfa>
18
[386e710]19coroutine Fibonacci { int fn1; };                                               // used for communication
[69efc321]20
21void main( Fibonacci & fib ) with( fib ) {                              // called on first resume
[386e710]22        int fn;
23        [fn1, fn] = [0, 1];                                                                     // precompute first two states
[69efc321]24        for () {
[427854b]25                suspend;                                                                                // restart last resume
[386e710]26                [fn1, fn] = [fn, fn1 + fn];                                             // general case
[69efc321]27        } // for
28}
29
30int next( Fibonacci & fib ) with( fib ) {
31        resume( fib );                                                                          // restart last suspend
[386e710]32        return fn1;
[69efc321]33}
34
35int main() {
36        Fibonacci f1, f2;
37        for ( 10 ) {                                                                            // print N Fibonacci values
[200fcb3]38                sout | next( f1 ) | next( f2 );
[69efc321]39        } // for
40}
41
42// Local Variables: //
43// tab-width: 4 //
[386e710]44// compile-command: "cfa fibonacci_1.cfa" //
[69efc321]45// End: //
Note: See TracBrowser for help on using the repository browser.