| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- Listing:
- 1 MODULE prog8;
- 2 (* This program implements a simple interactive calculator *)
- 3 IMPORT IO,Str,Storage;
- 4
- 5 TYPE
- 6 TokenType = (number, add, sub, mul, div, LeftParen,
- 7 RightParen, end);
- 8 SyntacticType = (main, exp, term, factor);
- 9 TreeType = POINTER TO TreeRecordType;
- ***** ^ undeclared identifier
- 10
- 11 TreeRecordType = RECORD
- 12 CASE kind:TokenType OF
- ***** ^ not supported yet
- 13 | number :
- ***** ^ not supported yet
- 14 NumberValue : LONGREAL;
- 15 | add,sub,mul,div :
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 16 left : TreeType;
- 17 right : TreeType;
- 18 END;
- 19 END;
- ***** ^ not supported yet
- 20
- 21 VAR
- 22 c:CHAR;
- 23 token:TokenType;
- ***** ^ not supported yet
- 24 TokenNumberValue:LONGREAL;
- 25
- 26 PROCEDURE error(s:ARRAY OF CHAR);
- ***** ^ not supported yet
- 27 BEGIN
- 28 IO.WrStr(s);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 29 IO.WrLn;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 30 HALT;
- ***** ^ undeclared identifier
- 31 END error;
- ***** ^ not supported yet
- 32
- 33 PROCEDURE readtoken;
- 34 VAR
- 35 s:ARRAY[0..99] OF CHAR;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 36 i:[0..99];
- 37 done:BOOLEAN;
- 38 oldc:CHAR;
- 39 BEGIN
- 40 LOOP
- 41 oldc := c;
- 42 c := IO.RdChar();
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 43 CASE oldc OF
- 44 | ' ' :
- 45 | '+' : token := add; EXIT;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 46 | '-' : token := sub; EXIT;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 47 | '*' : token := mul; EXIT;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 48 | '/' : token := div; EXIT;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 49 | '(' : token := LeftParen; EXIT;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 50 | ')' : token := RightParen; EXIT;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 51 | CHAR(10),CHAR(13),CHAR(26) : token := end; EXIT;
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 52
- 53 | '0'..'9' : (* read a real number *)
- 54 i := 1; s[0] := oldc;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 55 WHILE (c >= '0') AND (c <= '9') DO
- 56 s[i] := c;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 57 INC(i);
- ***** ^ undeclared identifier
- ***** ^ not supported yet
- 58 c := IO.RdChar();
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 59 END;
- 60 IF c<>'.' THEN (* add decimal point if none in input *)
- 61 s[i] := '.';
- ***** ^ not supported yet
- ***** ^ not supported yet
- 62 INC(i);
- ***** ^ undeclared identifier
- ***** ^ not supported yet
- 63 ELSE
- 64 REPEAT (* read fraction part *)
- 65 s[i] := c;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 66 INC(i);
- ***** ^ undeclared identifier
- ***** ^ not supported yet
- 67 c := IO.RdChar();
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 68 UNTIL (c < '0') OR (c > '9');
- 69 END;
- 70 s[i] := CHAR(0);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 71 TokenNumberValue := Str.StrToReal(s, done);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 72 IF NOT done THEN
- 73 error('Bad number?');
- ***** ^ not supported yet
- ***** ^ not supported yet
- 74 END;
- 75 token := number;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 76 EXIT;
- 77 ELSE
- 78 error('Bad character');
- ***** ^ not supported yet
- ***** ^ not supported yet
- 79 END;
- 80 END;
- 81 END readtoken;
- ***** ^ not supported yet
- 82
- 83 PROCEDURE read(what:SyntacticType):TreeType;
- 84 VAR t,t1:TreeType;
- ***** ^ not supported yet
- 85 BEGIN
- 86 CASE what OF
- ***** ^ not supported yet
- 87 | factor :
- ***** ^ not supported yet
- 88 IF token = LeftParen THEN
- ***** ^ not supported yet
- ***** ^ not supported yet
- 89 readtoken;
- ***** ^ not supported yet
- 90 t := read(exp);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 91 IF token = RightParen THEN
- ***** ^ not supported yet
- ***** ^ not supported yet
- 92 readtoken;
- ***** ^ not supported yet
- 93 ELSE
- 94 error("Missing ')'");
- ***** ^ not supported yet
- ***** ^ not supported yet
- 95 END;
- 96 ELSIF token = number THEN
- ***** ^ not supported yet
- ***** ^ not supported yet
- 97 Storage.ALLOCATE(t, SIZE(t^));
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ undeclared identifier
- ***** ^ not supported yet
- 98 t^.kind := number;
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 99 t^.NumberValue := TokenNumberValue;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 100 readtoken;
- ***** ^ not supported yet
- 101 ELSE
- 102 error('Missing number?');
- ***** ^ not supported yet
- ***** ^ not supported yet
- 103 END;
- 104 | term:
- ***** ^ not supported yet
- 105 t := read(factor);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 106 WHILE (token = mul) OR (token = div) DO
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 107 t1 := t;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 108 Storage.ALLOCATE(t, SIZE(t^));
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ undeclared identifier
- ***** ^ not supported yet
- 109 t^.kind := token;
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 110 readtoken;
- ***** ^ not supported yet
- 111 t^.left := t1;
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 112 t^.right := read(factor);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 113 END;
- 114 | exp:
- ***** ^ not supported yet
- 115 t := read(term);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 116 WHILE (token = add) OR (token = sub) DO
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 117 t1 := t;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 118 Storage.ALLOCATE(t, SIZE(t^));
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ undeclared identifier
- ***** ^ not supported yet
- 119 t^.kind := token;
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 120 readtoken;
- ***** ^ not supported yet
- 121 t^.left := t1;
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 122 t^.right := read(term);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 123 END;
- 124 | main :
- ***** ^ not supported yet
- 125 c := IO.RdChar();
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 126 readtoken;
- ***** ^ not supported yet
- 127 t := read(exp);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 128 IF (token <> end) THEN
- ***** ^ not supported yet
- ***** ^ not supported yet
- 129 error('Missing operator?');
- ***** ^ not supported yet
- ***** ^ not supported yet
- 130 END;
- 131 END;
- 132 RETURN t;
- ***** ^ not supported yet
- 133 END read;
- ***** ^ not supported yet
- 134
- 135 PROCEDURE eval(t:TreeType):LONGREAL;
- 136 BEGIN
- 137 CASE t^.kind OF
- ***** ^ not supported yet
- ***** ^ not supported yet
- 138 | number : RETURN t^.NumberValue;
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 139 | add : RETURN eval(t^.left) + eval(t^.right);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 140 | sub : RETURN eval(t^.left) - eval(t^.right);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 141 | mul : RETURN eval(t^.left) * eval(t^.right);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 142 | div : RETURN eval(t^.left) / eval(t^.right);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 143 END;
- 144 END eval;
- ***** ^ not supported yet
- 145
- 146 VAR
- 147 t:TreeType;
- ***** ^ not supported yet
- 148 result:LONGREAL;
- 149 BEGIN
- 150 LOOP
- 151 IO.WrStr('Enter expression : ');
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 152 t := read(main);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 153 result := eval(t);
- ***** ^ not supported yet
- ***** ^ not supported yet
- 154 IO.WrStr(' = ');
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 155 IO.WrLngReal(result, 4, 0);
- ***** ^ not supported yet
- ***** ^ not supported yet
- ***** ^ not supported yet
- 156 IO.WrLn;
- ***** ^ not supported yet
- ***** ^ not supported yet
- 157 END;
- 158 END prog8.
- 219 errors
|