3 IMPORT Strings
, DevCPM
, DevCPT
;
6 MaxIdLen
= LEN(DevCPT
.Name
);
10 if
= 1; then
= 2; else
= 3; elsif
= 4; end
= 5;
11 new
= 6; error
= 7; ident
= 8; plus
= 9; minus
= 10;
12 not
= 11; and
= 12; or
= 13; rpar
= 14; lpar
= 15;
13 endcom
= 16; eof
= 17;
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
77 ELSIF name
= "ERROR" THEN sym
:= error
79 |
"I": Identifier(sym
); IF name
= "IF" THEN sym
:= if
END
80 |
"N": Identifier(sym
); IF name
= "NEW" THEN sym
:= new
END
81 |
"O": Identifier(sym
); IF name
= "OR" THEN sym
:= or
END
82 |
"T": Identifier(sym
); IF name
= "THEN" THEN sym
:= then
END
83 |
"A".."D", "J".."M", "P".."S", "U".."Z", "a".."z", "_": Identifier(sym
)
84 |
"~": sym
:= not
; DevCPM
.Get(ch
)
86 IF Strings
.IsIdent(ch
) THEN Identifier(sym
) ELSE sym
:= null
; DevCPM
.Get(ch
) END
90 PROCEDURE New (IN name
: DevCPT
.Name
; val
: BOOLEAN): Selector
;
94 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
96 NEW(s
.next
); s
.next
.name
:= name$
; s
.next
.num
:= var
; s
.next
.val
:= val
102 PROCEDURE Old (IN name
: DevCPT
.Name
): Selector
;
106 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
108 err(0); NEW(s
.next
); s
.next
.name
:= name$
; s
.next
.num
:= var
; s
.next
.val
:= FALSE
113 PROCEDURE Find (IN name
: DevCPT
.Name
): Selector
;
117 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
121 PROCEDURE Set
* (IN name
: DevCPT
.Name
; val
: BOOLEAN);
125 WHILE (s
.next
# NIL) & (s
.next
.name$
# name$
) DO s
:= s
.next
END;
126 IF s
.next
= NIL THEN NEW(s
.next
) END;
127 s
.next
.name
:= name$
; s
.next
.num
:= var
; s
.next
.val
:= val
130 PROCEDURE ^
Expression (VAR x
: BOOLEAN);
132 PROCEDURE Factor (VAR x
: BOOLEAN);
136 s
:= Old(name
); Get(sym
);
139 ELSIF sym
= lpar
THEN
141 ASSERT(s
.num
= defined
);
143 x
:= Find(name
) # NIL; Get(sym
)
146 IF sym
# rpar
THEN err(23)
152 ELSIF sym
= lpar
THEN
153 Get(sym
); Expression(x
);
154 IF sym
# rpar
THEN err(23)
158 Get(sym
); Factor(x
); x
:= ~x
164 PROCEDURE Term (VAR x
: BOOLEAN);
169 Get(sym
); Factor(y
); x
:= x
& y
173 PROCEDURE Expression (VAR x
: BOOLEAN);
178 Get(sym
); Term(y
); x
:= x
OR y
182 PROCEDURE If (cond
: BOOLEAN);
185 NEW(c
); c
.next
:= top
; c
.alt
:= FALSE
; c
.val
:= cond
; c
.ref
:= 0; top
:= c
;
191 IF top
.alt
THEN err(14) (* double ELSE *)
192 ELSE top
.alt
:= TRUE
; top
.val
:= ~top
.val
;
199 i
:= 0; ref
:= top
.ref
; DEC(fold
, ref
+ 1);
200 WHILE (top
# NIL) & (i
<= ref
) DO top
:= top
.next
; INC(i
) END;
201 IF top
= NIL THEN err(51); fold
:= 0; If(TRUE
) END
204 PROCEDURE Printable
* (): BOOLEAN;
208 WHILE (c
# NIL) & c
.val
DO c
:= c
.next
END;
213 VAR val
: BOOLEAN; s
: Selector
;
218 IF sym
= ident
THEN s
:= New(name
, FALSE
); Get(sym
)
221 ELSIF sym
= error
THEN
222 IF Printable() THEN err(501) END; Get(sym
)
223 ELSIF sym
= ident
THEN
225 IF sym
= plus
THEN Old(name
).val
:= TRUE
; Get(sym
)
226 ELSIF sym
= minus
THEN Old(name
).val
:= FALSE
; Get(sym
)
230 Get(sym
); Expression(val
); If(val
);
231 IF sym
= then
THEN Get(sym
)
234 ELSIF sym
= elsif
THEN
235 IF fold
<= 1 THEN err(14) END; (* ELSIF without IF *)
236 Else
; Get(sym
); Expression(val
); If(val
); INC(top
.ref
);
237 IF sym
= then
THEN Get(sym
)
240 ELSIF sym
= else
THEN
241 IF fold
<= 1 THEN err(14) END; (* ELSE without IF *)
244 IF fold
<= 1 THEN err(14) END; (* END without IF *)
249 IF sym
# endcom
THEN err(5) ELSE DevCPM
.errpos
:= DevCPM
.curpos
- 1 END
254 IF fold
# 1 THEN err(14) END
259 ch
:= " "; sym
:= eof
; name
:= "";
260 fold
:= 0; top
:= NIL; scope
:= NIL
269 s
:= New("TRUE", TRUE
);
270 s
:= New("FALSE", FALSE
);
271 s
:= New("DEFINED", FALSE
); s
.num
:= defined