source: tests/coroutine/runningTotal.cfa @ ba068c0

Last change on this file since ba068c0 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
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// runningTotal.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Wed Dec  6 08:05:27 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 22 13:40:49 2019
13// Update Count     : 5
14//
15
16#include <fstream.hfa>
17#include <coroutine.hfa>
18
19coroutine RunTotal {                                                                    // input numbers and return running total
20        int input, total;                                                                       // communication
21};
22
23void ?{}( RunTotal & rntl ) { rntl.total = 0; }
24
25void update( RunTotal & rntl, int input ) with( rntl ) { // helper
26        total += input;                                                                         // remember between activations
27        suspend;                                                                                        // inactivate on stack
28}
29
30void main( RunTotal & rntl ) with( rntl ) {
31        for () {
32                update( rntl, input );
33        } // for
34}
35
36int add( RunTotal & rntl, int input ) {
37        rntl.input = input;                                                                     // pass input to coroutine
38        resume( rntl );
39        return rntl.total;                                                                      // return total from coroutine
40}
41int main() {
42        RunTotal rntl;
43        for ( i; 10 ) {
44                sout | i | add( rntl, i );
45        } // for
46}
47
48// Local Variables: //
49// tab-width: 4 //
50// compile-command: "cfa runningTotal.cfa" //
51// End: //
Note: See TracBrowser for help on using the repository browser.