source: tests/coroutine/devicedriver.cfa @ 083203b

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

coroutine device-driver example

  • Property mode set to 100644
File size: 2.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// devicedriver.cfa --
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Mar 16 15:30:34 2019
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Mar 18 08:29:20 2019
13// Update Count     : 79
14//
15
16#include <fstream.hfa>
17#include <coroutine.hfa>
18
19enum Status { CONT, MSG, ESTX, ELNTH, ECRC };
20coroutine Driver {
21        Status status;
22        char * msg, byte;
23}; // Driver
24
25void ?{}( Driver & d, char * m ) { d.msg = m; }
26Status next( Driver & d, char b ) with( d ) {
27        byte = b; resume( d ); return status;
28} // next
29
30void main( Driver & d ) with( d ) {
31        enum { STX = '\002', ESC = '\033', ETX = '\003', MaxMsg = 64 };
32        unsigned short int crc;                                                         // error checking
33  msg: for () {                                                                                 // parse message
34                status = CONT;
35                unsigned int lnth = 0, sum = 0;
36                while ( byte != STX ) suspend();
37          emsg: for () {
38                        suspend();
39                        choose ( byte ) {                                                       // process byte
40                          case STX:
41                                status = ESTX; suspend(); continue msg;
42                          case ETX:
43                                break emsg;
44                          case ESC:
45                                suspend();
46                        } // choose
47                        if ( lnth >= MaxMsg ) {                                         // buffer full ?
48                                status = ELNTH; suspend(); continue msg;
49                        } // if
50                        msg[lnth++] = byte;
51                        sum += byte;
52                } // for
53                msg[lnth] = '\0';                                                               // terminate string
54                suspend();
55                crc = (unsigned char)byte << 8; // prevent sign extension for signed char
56                suspend();
57                status = (crc | (unsigned char)byte) == sum ? MSG : ECRC;
58                suspend();
59        } // for
60} // main
61
62int main() {
63        char msg[65], byte;
64        Driver driver = { msg };
65  eof: for () {                                                                                 // read until end of file
66                sin | byte;                                                                             // read one character
67          if ( eof( sin ) ) break eof;                                          // eof ?
68                choose( next( driver, byte ) ) {                                // analyse character
69                  case MSG:
70                        sout | "msg:" | msg;
71                  case ESTX:
72                        sout | "STX in message";
73                  case ELNTH:
74                        sout | "message too long";
75                  case ECRC:
76                        sout | "CRC failure";
77                  default: ;
78                } // choose
79        } // for
80} // main
81
82// Local Variables: //
83// tab-width: 4 //
84// compile-command: "cfa -g -Wall -Wextra devicedriver.cfa" //
85// End: //
Note: See TracBrowser for help on using the repository browser.