source: doc/papers/concurrency/examples/Format.cfa @ 79b018f3

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 79b018f3 was 2e041e27, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

add examples directory for testing code in the paper

  • Property mode set to 100644
File size: 2.1 KB
Line 
1#if 1
2#include <fstream.hfa>
3#include <coroutine.hfa>
4
5coroutine Fmt {
6        char ch;                                                                // communication variables
7        int g, b;                                                               // needed in destructor
8};
9void main( Fmt & fmt ) with( fmt ) {
10        for () {                                                                // for as many characters
11                for ( g = 0; g < 5; g += 1 ) {          // groups of 5 blocks
12                        for ( b = 0; b < 4; b += 1 ) {  // blocks of 4 characters
13//                              do {
14                                        suspend();
15//                              } while ( ch == '\n' || ch == '\t' );
16                                sout | ch;                                      // print character
17                        }
18                        sout | "  ";                                    // block separator
19                }
20                sout | nl;                                                      // group separator
21        }
22}
23void ?{}( Fmt & fmt ) { resume( fmt ); } // prime (start) coroutine
24void ^?{}( Fmt & fmt ) with( fmt ) { // destructor
25        if ( g != 0 || b != 0 ) // special case
26                sout | nl; }
27void send( Fmt & fmt, char c ) { fmt.ch = c; resume( fmt ); }
28int main() {
29        Fmt fmt;
30        sout | nlOff;                                                   // turn off auto newline
31        for ( 41 )
32                send( fmt, 'a' );
33  //    sout | nlOff;                                                   // turn off auto newline
34  // eof: for () {                                                              // read until end of file
35  //            sin | fmt.ch;                                           // read one character
36  //      if ( eof( sin ) ) break eof;                  // eof ?
37  //            format( fmt );                                          // push character for formatting
38  //    }
39}
40#else
41
42#include <stdio.h>
43
44struct Format {
45        char ch;                                                                // used for communication
46        int g, b;                                                               // global because used in destructor
47};
48
49void format( struct Format * fmt ) {
50        if ( fmt->ch != -1 ) { // not EOF ?
51//              if ( fmt->ch == '\n' || fmt->ch == '\t' ) return;
52//              printf( "%c %d %d", fmt->ch, fmt->g, fmt->b );          // character
53                printf( "%c", fmt->ch );                // character
54                fmt->b += 1;
55                if ( fmt->b == 4 ) {    // block ?
56                        printf( "  " ); // separator
57                        fmt->b = 0;
58                        fmt->g += 1;
59                }
60                if ( fmt->g == 5 ) {    // group ?
61                        printf( "\n" ); // separator
62                        fmt->g = 0;
63                }
64        } else {
65                if ( fmt->g != 0 || fmt->b != 0 ) printf( "\n" );
66        }
67}
68int main() {
69        struct Format fmt = { 0, 0, 0 };
70        for ( ;; ) {
71                scanf( "%c", &fmt.ch );
72          if ( feof( stdin ) ) break;
73                format( &fmt );
74        }
75        fmt.ch = -1;
76        format( &fmt );
77}
78#endif
79
80// Local Variables: //
81// tab-width: 4 //
82// fill-column: 120 //
83// compile-command: "cfa Format.cfa" //
84// End: //
Note: See TracBrowser for help on using the repository browser.