source: src/tests/coroutine.c@ fa4c094

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since fa4c094 was f802e46, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

cleanup code

  • Property mode set to 100644
File size: 1.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 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//
7// fibonacci.c --
8//
9// Author : Thierry Delisle
10// Created On : Thu Jun 8 07:29:37 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Sep 17 21:38:15 2017
13// Update Count : 7
14//
15
16#include <fstream>
17#include <coroutine>
18
19coroutine Fibonacci {
20 int fn; // used for communication
21};
22
23void ?{}( Fibonacci & this ) {
24 this.fn = 0;
25}
26
27void main( Fibonacci & this ) {
28 int fn1, fn2; // retained between resumes
29 this.fn = 0; // case 0
30 fn1 = this.fn;
31 suspend(); // restart last resume
32
33 this.fn = 1; // case 1
34 fn2 = fn1; fn1 = this.fn;
35 suspend(); // restart last resume
36
37 for ( ;; ) { // general case
38 this.fn = fn1 + fn2;
39 fn2 = fn1; fn1 = this.fn;
40 suspend(); // restart last resume
41 } // for
42}
43
44int next( Fibonacci & this ) {
45 resume( this ); // restart last suspend
46 return this.fn;
47}
48
49int main() {
50 Fibonacci f1, f2;
51 for ( int i = 1; i <= 10; i += 1 ) {
52 sout | next( f1 ) | next( f2 ) | endl;
53 } // for
54}
55
56// Local Variables: //
57// tab-width: 4 //
58// compile-command: "cfa fibonacci.c" //
59// End: //
Note: See TracBrowser for help on using the repository browser.