forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calc.Mod
433 lines (371 loc) · 11.9 KB
/
Calc.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE Calc; (** portable *) (* CAS 28.9.93 / tk adapted for System 3 1.6.95*)
IMPORT
MathL, Texts, Oberon;
CONST
Version = "Calc / cas 28.9.93";
End = 7; (*new scanner symbol*)
Eps = 1.0D-9; Eps0 = 0.5D-9;
TYPE
Symbol = POINTER TO SymbolDesc;
SymbolDesc = RECORD
name: ARRAY 32 OF CHAR;
funct: BOOLEAN;
val: LONGREAL;
next: Symbol
END;
VAR
lastTime: LONGINT;
W: Texts.Writer;
S: Texts.Scanner;
syms: Symbol;
(** expression syntax:
Expr = Term {AddOp Term}.
Term = Factor {MulOp Factor}.
Factor = Atom {PowOp Atom}.
Atom = Number | Functor Atom | ident | "(" Expr ")".
PowOp = "^".
MulOp = "*" | "/" | "%" | "<" | ">". -- % modulo, < shift left, > shift right
AddOp = ["+" | "-"]. -- no add op: addition(!)
Number = (digit {digit}) | (digit {hexDigit} "H") | (digit {hexDigit} "X") | (""" char """).
Functor = "arccos" | "arcsin" | "arctan" | "cos" | "entier" | "exp" | "ln" | "short" | "sign" | "sin" | "sqrt" | "tan".
**)
PROCEDURE err;
BEGIN S.class := Texts.Inval
END err;
PROCEDURE sign (n: LONGREAL): LONGREAL;
BEGIN
IF n < 0 THEN RETURN -1
ELSIF n = 0 THEN RETURN 0
ELSE RETURN 1 END
END sign;
PROCEDURE short (n: LONGREAL): REAL;
BEGIN RETURN SHORT(n + Eps0)
END short;
PROCEDURE entier (n: LONGREAL): LONGINT;
BEGIN RETURN ENTIER(n + Eps0)
END entier;
PROCEDURE sin (n: LONGREAL): LONGREAL;
BEGIN RETURN MathL.sin(n)
END sin;
PROCEDURE cos (n: LONGREAL): LONGREAL;
BEGIN RETURN MathL.cos(n)
END cos;
PROCEDURE tan (n: LONGREAL): LONGREAL;
VAR x: LONGREAL;
BEGIN x := MathL.cos(n);
IF x # 0 THEN RETURN MathL.sin(n) / x ELSE err; RETURN 1 END
END tan;
PROCEDURE arcsin (n: LONGREAL): LONGREAL;
VAR x: LONGREAL;
BEGIN x := MathL.sqrt(1 - n * n);
IF x # 0 THEN RETURN MathL.arctan(n / x) ELSE err; RETURN 1 END
END arcsin;
PROCEDURE arccos (n: LONGREAL): LONGREAL;
BEGIN RETURN MathL.pi / 2 - arcsin(n)
END arccos;
PROCEDURE arctan (n: LONGREAL): LONGREAL;
BEGIN RETURN MathL.arctan(n)
END arctan;
PROCEDURE exp (n: LONGREAL): LONGREAL;
BEGIN RETURN MathL.exp(n)
END exp;
PROCEDURE ln (n: LONGREAL): LONGREAL;
BEGIN
IF n > 0 THEN RETURN MathL.ln(n) ELSE err; RETURN 1 END
END ln;
PROCEDURE sqrt (n: LONGREAL): LONGREAL;
BEGIN
IF n >= 0 THEN RETURN MathL.sqrt(n) ELSE err; RETURN 1 END
END sqrt;
PROCEDURE Ch (ch: CHAR);
BEGIN Texts.Write(W, ch);
END Ch;
PROCEDURE Str (s: ARRAY OF CHAR);
BEGIN Texts.WriteString(W, s);
END Str;
PROCEDURE WrHex (n: LONGREAL);
VAR x, y: LONGINT; i: INTEGER;
a: ARRAY 10 OF CHAR;
BEGIN x := entier(n);
i := 0; Texts.Write(W, " ");
REPEAT y := x MOD 10H;
IF y < 10 THEN a[i] := CHR(y + 30H) ELSE a[i] := CHR(y + 37H) END;
x := x DIV 10H; INC(i)
UNTIL i = 8;
REPEAT DEC(i) UNTIL (i = 0) OR (a[i] # "0");
IF a[i] >= "A" THEN Texts.Write(W, "0") END;
WHILE i >= 0 DO Texts.Write(W, a[i]); DEC(i) END;
Texts.Write(W, "H")
END WrHex;
PROCEDURE WrInt (n: LONGREAL);
BEGIN Texts.Write(W, " "); Texts.WriteInt(W, entier(n), 0)
END WrInt;
PROCEDURE WrChar (n: LONGREAL);
VAR ch: CHAR;
BEGIN ch := CHR(entier(n));
IF (" " <= ch) & (ch < 7FX) OR (80X <= ch) & (ch < 0A0X) THEN Ch(" "); Ch(22X); Ch(ch); Ch(22X)
ELSE WrHex(ORD(ch))
END
END WrChar;
PROCEDURE WrReal (n: LONGREAL);
VAR x: LONGREAL;
BEGIN
IF (MIN(LONGINT) <= n) & (n <= MAX(LONGINT)) THEN x := ABS(n - ENTIER(SHORT(n)));
IF x < Eps THEN WrInt(n); RETURN END
END;
IF (MIN(REAL) <= n) & (n <= MAX(REAL)) THEN x := ABS(n - SHORT(n));
IF x < Eps THEN
IF (-10000 < n) & (n < 10000) THEN Texts.WriteLongRealFix(W, n, 30, 15, 0)
ELSE Texts.WriteReal(W, short(n), 14)
END;
RETURN
END
END;
Texts.WriteLongReal(W, n, 23)
END WrReal;
PROCEDURE WrValue (n: LONGREAL);
VAR x: LONGREAL;
BEGIN
Str(" ="); WrReal(n);
IF (MIN(LONGINT) <= n) & (n <= MAX(LONGINT)) THEN x := ABS(n - ENTIER(SHORT(n)));
IF x < Eps THEN Str(" ="); WrHex(n); Str(" ="); WrInt(n);
IF (0 <= n) & (n < 256) & (entier(n) = n) THEN Str(" ="); WrChar(n) END
END
END
END WrValue;
PROCEDURE Ln;
BEGIN Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
END Ln;
PROCEDURE Scan (VAR S: Texts.Scanner);
PROCEDURE hex (n: LONGINT): LONGINT;
VAR x, i: LONGINT; d: ARRAY 8 OF LONGINT;
BEGIN x := 0; i := 0;
REPEAT d[i] := n MOD 10; n := n DIV 10; INC(i) UNTIL n = 0;
WHILE i > 0 DO DEC(i); x := 16*x + d[i] END;
RETURN x
END hex;
BEGIN
IF S.eot THEN S.class := End
ELSIF S.nextCh = "/" THEN S.class := Texts.Char; S.c := "/"; Texts.Read(S, S.nextCh)
ELSE Texts.Scan(S)
END;
IF S.line # 0 THEN S.class := End END;
IF (S.class = Texts.Char) & (S.c = " ") THEN S.c := "-"
ELSIF (S.class = Texts.String) & (S.len = 2) THEN S.i := ORD(S.s[0]); S.class := Texts.Int
ELSIF (S.class = Texts.Int) & (S.nextCh = "X") THEN S.i := hex(S.i);
Texts.Read(S, S.nextCh)
END
END Scan;
PROCEDURE OpenScanner (VAR S: Texts.Scanner);
VAR text: Texts.Text; beg, end, time: LONGINT;
BEGIN Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos); Scan(S);
IF (S.class = Texts.Char) & (S.c = "^") & (S.line = 0) THEN
Oberon.GetSelection(text, beg, end, time);
IF time >= lastTime THEN lastTime := time;
Texts.OpenScanner(S, text, beg); Scan(S)
END
END;
IF S.line # 0 THEN S.class := Texts.Inval END
END OpenScanner;
PROCEDURE FindIdent (name: ARRAY OF CHAR; insert: BOOLEAN; VAR val: LONGREAL);
VAR s: Symbol;
BEGIN s := syms;
WHILE (s # NIL) & ((s.name # name) OR s.funct) DO s := s.next END;
IF insert THEN
IF s = NIL THEN NEW(s); s.next := syms; syms := s END;
s.name := name; s.funct := FALSE; s.val := val
ELSIF s # NIL THEN val := s.val
ELSE S.class := Texts.Inval
END
END FindIdent;
PROCEDURE FindFunct (name: ARRAY OF CHAR; insert: BOOLEAN; VAR sym: Symbol);
VAR s: Symbol;
BEGIN s := syms;
WHILE (s # NIL) & ((s.name # name) OR ~s.funct) DO s := s.next END;
IF insert THEN
IF s = NIL THEN s := sym; s.next := syms; syms := sym END;
s.name := name; s.funct := TRUE; s.val := 0
ELSIF s # NIL THEN sym := s
ELSE sym := NIL
END
END FindFunct;
PROCEDURE InitSyms;
VAR s: Symbol; n: LONGREAL; name: ARRAY 2 OF CHAR;
BEGIN name[1] := 0X;
name[0] := "e"; n := MathL.e; FindIdent(name, TRUE, n);
n := MathL.pi; FindIdent("pi", TRUE, n);
n := 0;
NEW(s); FindFunct("arctan", TRUE, s);
NEW(s); FindFunct("arccos", TRUE, s);
NEW(s); FindFunct("arcsin", TRUE, s);
NEW(s); FindFunct("cos", TRUE, s);
NEW(s); FindFunct("entier", TRUE, s);
NEW(s); FindFunct("exp", TRUE, s);
NEW(s); FindFunct("ln", TRUE, s);
NEW(s); FindFunct("short", TRUE, s);
NEW(s); FindFunct("sign", TRUE, s);
NEW(s); FindFunct("sin", TRUE, s);
NEW(s); FindFunct("sqrt", TRUE, s);
NEW(s); FindFunct("tan", TRUE, s)
END InitSyms;
PROCEDURE^ Expr (VAR n: LONGREAL);
PROCEDURE Functor (sym: Symbol; VAR n: LONGREAL);
BEGIN
IF sym.name = "arcsin" THEN n := arcsin(n)
ELSIF sym.name = "arccos" THEN n := arccos(n)
ELSIF sym.name = "arctan" THEN n := arctan(n)
ELSIF sym.name = "cos" THEN n := cos(n)
ELSIF sym.name = "exp" THEN n := exp(n)
ELSIF sym.name = "entier" THEN n := entier(n)
ELSIF sym.name = "ln" THEN n := ln(n)
ELSIF sym.name = "short" THEN n := short(n)
ELSIF sym.name = "sign" THEN n := sign(n)
ELSIF sym.name = "sin" THEN n := sin(n)
ELSIF sym.name = "sqrt" THEN n := sqrt(n)
ELSIF sym.name = "tan" THEN n := tan(n)
END
END Functor;
PROCEDURE Atom (VAR n: LONGREAL);
VAR sym: Symbol;
BEGIN
IF S.class = Texts.Int THEN n := S.i; Scan(S)
ELSIF S.class = Texts.Real THEN n := S.x; Scan(S)
ELSIF S.class = Texts.LongReal THEN n := S.y; Scan(S)
ELSIF (S.class = Texts.String) & (S.s[0] # 0X) & (S.s[1] = 0X) THEN n := ORD(S.s[0]); Scan(S)
ELSIF S.class = Texts.Name THEN FindFunct(S.s, FALSE, sym);
IF sym # NIL THEN Scan(S); Atom(n);
IF S.class # Texts.Inval THEN Functor(sym, n) END
ELSE FindIdent(S.s, FALSE, n);
IF S.class # Texts.Inval THEN Scan(S) END
END
ELSIF (S.class = Texts.Char) & (S.c = "(") THEN Scan(S);
Expr(n);
IF (S.class = Texts.Char) & (S.c = ")") THEN Scan(S)
ELSE S.class := Texts.Inval
END
ELSE S.class := Texts.Inval
END
END Atom;
PROCEDURE Factor (VAR n: LONGREAL);
VAR x: LONGREAL;
BEGIN Atom(n);
WHILE (S.class = Texts.Char) & (S.c = "^") DO
Scan(S); Factor(x);
n := sign(n) * MathL.exp(MathL.ln(ABS(n)) * x)
END
END Factor;
PROCEDURE Term (VAR n: LONGREAL);
VAR x: LONGREAL; op: CHAR;
BEGIN Factor(n);
WHILE (S.class = Texts.Char)
& ((S.c = "*") OR (S.c = "/") OR (S.c = "%") OR (S.c = ">") OR (S.c = "<")) DO
op := S.c; Scan(S); Factor(x);
CASE op OF
"*": n := n * x
| "/": IF x # 0 THEN n := n / x ELSE err END
| "%": IF x # 0 THEN n := entier(n) MOD entier(x) ELSE err END
| "<": n := ASH(entier(n), entier(x))
| ">": n := ASH(entier(n), -entier(x))
END
END
END Term;
PROCEDURE Expr (VAR n: LONGREAL);
VAR x: LONGREAL; op: CHAR;
BEGIN Term(n);
WHILE (S.class = Texts.Char) & ((S.c = "+") OR (S.c = "-")) OR (S.class = Texts.Int) DO
IF S.class = Texts.Char THEN op := S.c; Scan(S) ELSE op := "+" END;
Term(x);
CASE op OF
"+": n := n + x
| "-": n := n - x
END
END
END Expr;
PROCEDURE Hex*; (** expr **)
VAR n: LONGREAL;
BEGIN Str("Calc.Hex"); OpenScanner(S); Expr(n);
IF S.class # Texts.Inval THEN WrHex(n) ELSE Str(" failed: bad argument") END;
Ln
END Hex;
PROCEDURE Dec*; (** expr **)
VAR n: LONGREAL;
BEGIN Str("Calc.Dec"); OpenScanner(S); Expr(n);
IF S.class # Texts.Inval THEN WrInt(n) ELSE Str(" failed: bad argument") END;
Ln
END Dec;
PROCEDURE Real*; (** expr **)
VAR n: LONGREAL;
BEGIN Str("Calc.Real"); OpenScanner(S); Expr(n);
IF S.class # Texts.Inval THEN WrReal(n) ELSE Str(" failed: bad argument") END;
Ln
END Real;
PROCEDURE Char*; (** expr **)
VAR n: LONGREAL;
BEGIN Str("Calc.Char"); OpenScanner(S); Expr(n);
IF S.class # Texts.Inval THEN
IF (0 <= n) & (n < 256) THEN WrChar(n)
ELSE Str(" failed: not a character code")
END
ELSE Str(" failed: bad argument")
END;
Ln
END Char;
PROCEDURE Set*; (** {name ":=" expr} "~" **)
VAR n: LONGREAL; name: ARRAY 32 OF CHAR;
BEGIN Str("Calc.Set"); Ln; OpenScanner(S);
WHILE S.class = Texts.Name DO name := S.s; Scan(S);
IF (S.class = Texts.Char) & (S.c = ":") THEN
Texts.Scan(S);
IF (S.class = Texts.Char) & (S.c = "=") THEN
Scan(S); Expr(n);
ELSE S.class:=Texts.Inval
END;
ELSE S.class := Texts.Inval
END;
IF S.class # Texts.Inval THEN FindIdent(name, TRUE, n);
IF S.class # Texts.Inval THEN Str(" "); Str(name); WrValue(n); Ln END
END
END;
IF S.class = Texts.Inval THEN Str(" failed: bad argument"); Ln END
END Set;
PROCEDURE List*;
VAR s: Symbol;
BEGIN Str("Calc.List"); Ln;
s := syms;
WHILE s # NIL DO
IF s.funct THEN Str(" "); Str(s.name) END;
s := s.next
END;
Ln;
s := syms;
WHILE s # NIL DO
IF ~s.funct THEN Str(" "); Str(s.name); WrValue(s.val); Ln END;
s := s.next
END
END List;
PROCEDURE Reset*;
BEGIN Str("Calc.Reset"); Ln; syms := NIL; InitSyms
END Reset;
BEGIN Texts.OpenWriter(W); Texts.WriteString(W, Version); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf);
lastTime := 0; syms := NIL; InitSyms
END Calc.
(*
Edit.Open Calc.Tool
Calc.Reset
Calc.Set cos := 33H otto := 1000H ~
Calc.List
Calc.Hex egon + otto
Calc.Dec egon * 2
Calc.Char "j" + 7
Calc.Real cos (193 * pi)
*)