source: tests/coroutine/cntparens.cfa @ 49db841

ADTast-experimental
Last change on this file since 49db841 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.5 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// cntparens.cfa -- match left/right parenthesis
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Apr 20 11:04:45 2019
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Apr 20 11:06:21 2019
13// Update Count     : 1
14//
15
16#include <fstream.hfa>
17#include <coroutine.hfa>
18
19enum Status { Cont, Match, Error };
20coroutine CntParens {
21        char ch;                                                                                        // used for communication
22        Status status;
23        unsigned int cnt;
24}; // CntParens
25
26void main( CntParens & cpns ) with( cpns ) {
27        for ( ; ch == '('; cnt += 1 ) {                                         // left parenthesis
28                suspend;
29        }
30        for ( ; ch == ')' && cnt > 1; cnt -= 1 ) {                      // right parenthesis
31                suspend;
32        }
33        status = ch == ')' ? Match : Error;
34} // main
35
36void ?{}( CntParens & cpns ) with( cpns ) { status = Cont; cnt = 0; }
37
38Status next( CntParens & cpns, char c ) with( cpns ) {
39        ch = c;
40        resume( cpns );
41        return status;
42}
43
44int main() {
45        CntParens cpns;
46        char ch;
47
48        for () {                                                                                        // read until end of file
49                sin | ch;                                                                               // read one character
50          if ( eof( sin ) ) { sout | "Error"; break; }          // eof ?
51                Status ret = next( cpns, ch );                                  // push character for formatting
52          if ( ret == Match ) { sout | "Match"; break; }
53          if ( ret == Error ) { sout | "Error"; break; }
54        } // for
55} // main
56
57// Local Variables: //
58// tab-width: 4 //
59// compile-command: "cfa -g -Wall -Wextra cntparens.cfa" //
60// End: //
Note: See TracBrowser for help on using the repository browser.