-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.a68
37 lines (33 loc) · 948 Bytes
/
read.a68
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
CO data type and some operations for snailfish numbers CO
MODE PAIR = STRUCT (NUMBER left, right);
MODE NUMBER = UNION(INT, REF PAIR);
PROC make pair = (NUMBER x, y)REF PAIR:
HEAP PAIR := (x,y);
PROC dump = (NUMBER data)VOID:
CASE data IN
(INT n): printf(($g(0)$, n)),
(REF PAIR p): (print("["); dump(left OF p); print(","); dump(right OF p); print("]"))
ESAC;
PROC parse = (REF STRING s)NUMBER:
BEGIN
CHAR c = s[1]; s := s[2:];
IF c = "[" THEN
NUMBER lt = parse (s); s := s[2:];
NUMBER rt = parse (s); s := s[2:];
make pair (lt, rt)
ELSE
INT p; char in string(c, p, "1234567890");
p MOD 10
FI
END;
[]NUMBER homework = BEGIN
FLEX[0]NUMBER lines;
on logical file end(stand in, (REF FILE f)BOOL: done reading);
DO
STRING s; read((s)); read(new line);
([UPB lines+1]NUMBER tmp; tmp[1:UPB lines] := lines; lines := tmp);
lines[UPB lines] := parse(s)
OD;
done reading:
lines
END;