b43b539bd2beebe3747961b7bd2aa1acb037a02e
3 IMPORT Strings
, DevCPM
, DevCPT
;
6 MaxIdLen
= LEN(DevCPT
.Name
);
10 if
= 1; then
= 2; else
= 3; elsif
= 4; end
= 5; new
= 6;
11 ident
= 7; plus
= 8; minus
= 9;
12 not
= 10; and
= 11; or
= 12; rpar
= 13; lpar
= 14;
13 endcom
= 15; eof
= 16;
19 Context
= POINTER TO RECORD
20 next
: Context
; (* upper level block *)
21 alt
: BOOLEAN; (* else branch *)
22 val
: BOOLEAN; (* condition value, inverted if alt *)
23 ref
: INTEGER (* ELSIF count *)
26 Selector
= POINTER TO RECORD
34 ch
: CHAR; (* current character *)
35 name
: DevCPT
.Name
; (* ident *)
38 sym
: BYTE; (* parser symbol *)
39 fold
: INTEGER; (* condition folding *)
43 PROCEDURE err (n
: SHORTINT);
47 PROCEDURE Identifier (VAR sym
: BYTE);
48 VAR i
, res
: INTEGER; n
: ARRAY MaxIdLen
OF CHAR;
51 n
[i
] := ch
; INC(i
); DevCPM
.Get(ch
)
52 UNTIL ~Strings
.IsIdent(ch
) OR (i
= MaxIdLen
);
53 IF i
= MaxIdLen
THEN err(240); DEC(i
) END ;
54 n
[i
] := 0X
; Strings
.StringToUtf8(n
, name
, res
); sym
:= ident
;
55 IF res
= 1 (*truncated*) THEN err(240) END
58 PROCEDURE Get (VAR sym
: BYTE);
60 DevCPM
.errpos
:= DevCPM
.curpos
- 1;
61 WHILE (ch
# DevCPM
.Eot
) & ((ch
<= " ") OR (ch
= 0A0X
)) DO DevCPM
.Get(ch
) END;
62 DevCPM
.startpos
:= DevCPM
.curpos
- 1;
64 | DevCPM
.Eot
: sym
:= eof
65 |
"&": sym
:= and
; DevCPM
.Get(ch
)
66 |
"(": sym
:= lpar
; DevCPM
.Get(ch
)
67 |
")": sym
:= rpar
; DevCPM
.Get(ch
)
69 sym
:= null
; DevCPM
.Get(ch
);
70 IF ch
= ">" THEN sym
:= endcom
; DevCPM
.Get(ch
) END
71 |
"+": sym
:= plus
; DevCPM
.Get(ch
)
72 |
"-": sym
:= minus
; DevCPM
.Get(ch
)
73 |
"E": Identifier(sym
);
74 IF name
= "END" THEN sym
:= end
75 ELSIF name
= "ELSE" THEN sym
:= else
76 ELSIF name
= "ELSIF" THEN sym
:= elsif
78 |
"I": Identifier(sym
); IF name
= "IF" THEN sym
:= if
END
79 |
"N": Identifier(sym
); IF name
= "NEW" THEN sym
:= new
END
80 |
"O": Identifier(sym
); IF name
= "OR" THEN sym
:= or
END
81 |
"T": Identifier(sym
); IF name
= "THEN" THEN sym
:= then
END
82 |
"A".."D", "J".."M", "P".."S", "U".."Z", "a".."z", "_": Identifier(sym
)
83 |
"~": sym
:= not
; DevCPM
.Get(ch
)
85 IF Strings
.IsIdent(ch
) THEN Identifier(sym
) ELSE sym
:= null
; DevCPM
.Get(ch
) END
89 PROCEDURE New (IN name
: DevCPT
.Name
; val
: BOOLEAN): Selector
;
93 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
95 NEW(s
.next
); s
.next
.name
:= name$
; s
.next
.num
:= var
; s
.next
.val
:= val
101 PROCEDURE Old (IN name
: DevCPT
.Name
): Selector
;
105 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
107 err(0); NEW(s
.next
); s
.next
.name
:= name$
; s
.next
.num
:= var
; s
.next
.val
:= FALSE
112 PROCEDURE Find (IN name
: DevCPT
.Name
): Selector
;
116 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
120 PROCEDURE Set
* (IN name
: DevCPT
.Name
; val
: BOOLEAN);
124 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
125 IF s
.next
= NIL THEN NEW(s
.next
) END;
126 s
.next
.name
:= name$
; s
.next
.num
:= var
; s
.next
.val
:= val
129 PROCEDURE ^
Expression (VAR x
: BOOLEAN);
131 PROCEDURE Factor (VAR x
: BOOLEAN);
135 s
:= Old(name
); Get(sym
);
138 ELSIF sym
= lpar
THEN
140 ASSERT(s
.num
= defined
);
142 x
:= Find(name
) # NIL; Get(sym
)
145 IF sym
# rpar
THEN err(23)
151 ELSIF sym
= lpar
THEN
152 Get(sym
); Expression(x
);
153 IF sym
# rpar
THEN err(23)
157 Get(sym
); Factor(x
); x
:= ~x
163 PROCEDURE Term (VAR x
: BOOLEAN);
168 Get(sym
); Factor(y
); x
:= x
& y
172 PROCEDURE Expression (VAR x
: BOOLEAN);
177 Get(sym
); Term(y
); x
:= x
OR y
181 PROCEDURE If (cond
: BOOLEAN);
184 NEW(c
); c
.next
:= top
; c
.alt
:= FALSE
; c
.val
:= cond
; c
.ref
:= 0; top
:= c
;
190 IF top
.alt
THEN err(14) (* double ELSE *)
191 ELSE top
.alt
:= TRUE
; top
.val
:= ~top
.val
;
198 i
:= 0; ref
:= top
.ref
; DEC(fold
, ref
+ 1);
199 WHILE (top
# NIL) & (i
<= ref
) DO top
:= top
.next
; INC(i
) END;
200 IF top
= NIL THEN err(51); fold
:= 0; If(TRUE
) END
204 VAR val
: BOOLEAN; s
: Selector
;
209 IF sym
= ident
THEN s
:= New(name
, FALSE
); Get(sym
)
212 ELSIF sym
= ident
THEN
214 IF sym
= plus
THEN Old(name
).val
:= TRUE
; Get(sym
)
215 ELSIF sym
= minus
THEN Old(name
).val
:= FALSE
; Get(sym
)
219 Get(sym
); Expression(val
); If(val
);
220 IF sym
= then
THEN Get(sym
)
223 ELSIF sym
= elsif
THEN
224 IF fold
<= 1 THEN err(14) END; (* ELSIF without IF *)
225 Else
; Get(sym
); Expression(val
); If(val
); INC(top
.ref
);
226 IF sym
= then
THEN Get(sym
)
229 ELSIF sym
= else
THEN
230 IF fold
<= 1 THEN err(14) END; (* ELSE without IF *)
233 IF fold
<= 1 THEN err(14) END; (* END without IF *)
238 IF sym
# endcom
THEN err(5) ELSE DevCPM
.errpos
:= DevCPM
.curpos
- 1 END
241 PROCEDURE Printable
* (): BOOLEAN;
245 WHILE (c
# NIL) & c
.val
DO c
:= c
.next
END;
251 IF fold
# 1 THEN err(14) END
256 ch
:= " "; sym
:= eof
; name
:= "";
257 fold
:= 0; top
:= NIL; scope
:= NIL
266 s
:= New("TRUE", TRUE
);
267 s
:= New("FALSE", FALSE
);
268 s
:= New("DEFINED", FALSE
); s
.num
:= defined