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