source: tests/coroutine/cntparens.cfa@ 5db17077

Last change on this file since 5db17077 was 3ac5fd8, checked in by Peter A. Buhr <pabuhr@…>, 13 months ago

first attempt changing end-of-file to an exception

  • 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 : Thu Aug 15 20:39:34 2024
13// Update Count : 2
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 try {
49 for () { // read until end of file
50 sin | ch; // read one character
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 } catch( end_of_file * ) {
56 sout | "Error";
57 } // try
58} // main
59
60// Local Variables: //
61// tab-width: 4 //
62// compile-command: "cfa -g -Wall -Wextra cntparens.cfa" //
63// End: //
Note: See TracBrowser for help on using the repository browser.