source: tests/coroutine/fmtLines.cfa @ 427854b

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 427854b was 427854b, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

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

  • Property mode set to 100644
File size: 1.7 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// fmtLines.cc -- format characters into blocks of 4 and groups of 5 blocks per line
8//
9// Author           : Peter A. Buhr
10// Created On       : Sun Sep 17 21:56:15 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 22 13:41:16 2019
13// Update Count     : 58
14//
15
16#include <fstream.hfa>
17#include <coroutine.hfa>
18
19coroutine Format {
20        char ch;                                                                                        // used for communication
21        int g, b;                                                                                       // global because used in destructor
22};
23
24void main( Format & fmt ) with( fmt ) {
25        for () {                                                                                        // for as many characters
26                for ( g = 0; g < 5; g += 1 ) {                                  // groups of 5 blocks
27                        for ( b = 0; b < 4; b += 1 ) {                          // blocks of 4 characters
28                                for () {                                                                // for newline characters
29                                        suspend;
30                                  if ( ch != '\n' ) break;                              // ignore newline
31                                } // for
32                                sout | ch;                                                              // print character
33                        } // for
34                        sout | "  ";                                                            // print block separator
35                } // for
36                sout | nl;                                                                              // print group separator
37        } // for
38} // main
39
40void ?{}( Format & fmt ) {
41        resume( fmt );                                                                          // prime (start) coroutine
42}
43
44void ^?{}( Format & fmt ) with( fmt ) {
45        if ( g != 0 || b != 0 ) sout | nl;
46}
47
48void format( Format & fmt ) {
49        resume( fmt );
50} // format
51
52int main() {
53        Format fmt;
54        sout | nlOff;                                                                           // turn off auto newline
55
56  eof: for () {                                                                                 // read until end of file
57                sin | fmt.ch;                                                                   // read one character
58          if ( eof( sin ) ) break eof;                                          // eof ?
59                format( fmt );                                                                  // push character for formatting
60        } // for
61} // main
62
63// Local Variables: //
64// tab-width: 4 //
65// compile-command: "cfa fmtLines.cfa" //
66// End: //
Note: See TracBrowser for help on using the repository browser.