2020-05-25 22:48:33 +02:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
Copyright (c) 2018-2021, Anton Krotov
|
2019-03-11 09:59:55 +01:00
|
|
|
All rights reserved.
|
2016-10-24 01:30:27 +02:00
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE SCAN;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
IMPORT TXT := TEXTDRV, ARITH, S := STRINGS, ERRORS, LISTS;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
CONST
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
NUMLEN = 256;
|
|
|
|
IDLEN = 256;
|
|
|
|
TEXTLEN = 512;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxUNDEF* = 0; lxIDENT* = 1; lxINTEGER* = 2; lxHEX* = 3;
|
|
|
|
lxCHAR* = 4; lxFLOAT* = 5; lxSTRING* = 6; lxCOMMENT* = 7;
|
|
|
|
lxEOF* = 8;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxPLUS* = 21; lxMINUS* = 22; lxMUL* = 23; lxSLASH* = 24;
|
|
|
|
lxNOT* = 25; lxAND* = 26; lxPOINT* = 27; lxCOMMA* = 28;
|
|
|
|
lxSEMI* = 29; lxBAR* = 30; lxLROUND* = 31; lxLSQUARE* = 32;
|
|
|
|
lxLCURLY* = 33; lxCARET* = 34; lxEQ* = 35; lxNE* = 36;
|
|
|
|
lxLT* = 37; lxGT* = 38; lxCOLON* = 39; lxRROUND* = 40;
|
|
|
|
lxRSQUARE* = 41; lxRCURLY* = 42; lxLE* = 43; lxGE* = 44;
|
|
|
|
lxASSIGN* = 45; lxRANGE* = 46;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxKW = 51;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxARRAY* = 51; lxBEGIN* = 52; lxBY* = 53; lxCASE* = 54;
|
|
|
|
lxCONST* = 55; lxDIV* = 56; lxDO* = 57; lxELSE* = 58;
|
|
|
|
lxELSIF* = 59; lxEND* = 60; lxFALSE* = 61; lxFOR* = 62;
|
|
|
|
lxIF* = 63; lxIMPORT* = 64; lxIN* = 65; lxIS* = 66;
|
|
|
|
lxMOD* = 67; lxMODULE* = 68; lxNIL* = 69; lxOF* = 70;
|
|
|
|
lxOR* = 71; lxPOINTER* = 72; lxPROCEDURE* = 73; lxRECORD* = 74;
|
|
|
|
lxREPEAT* = 75; lxRETURN* = 76; lxTHEN* = 77; lxTO* = 78;
|
|
|
|
lxTRUE* = 79; lxTYPE* = 80; lxUNTIL* = 81; lxVAR* = 82;
|
|
|
|
lxWHILE* = 83;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxERROR01* = -1; lxERROR02* = -2; lxERROR03* = -3; lxERROR04* = -4;
|
2021-06-15 19:33:16 +02:00
|
|
|
lxERROR05* = -5; (*lxERROR06* = -6;*) lxERROR07* = -7; lxERROR08* = -8;
|
2019-09-26 22:23:06 +02:00
|
|
|
lxERROR09* = -9; lxERROR10* = -10; lxERROR11* = -11; lxERROR12* = -12;
|
|
|
|
lxERROR13* = -13;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
TEXTSTR* = ARRAY TEXTLEN OF CHAR;
|
|
|
|
IDSTR* = ARRAY IDLEN OF CHAR;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
DEF = POINTER TO RECORD (LISTS.ITEM)
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
ident: IDSTR
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
STRING* = POINTER TO RECORD (LISTS.ITEM)
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
s*: TEXTSTR;
|
|
|
|
offset*, offsetW*, hash: INTEGER
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
IDENT* = RECORD
|
|
|
|
|
|
|
|
s*: IDSTR;
|
|
|
|
hash*: INTEGER
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
POSITION* = RECORD
|
|
|
|
|
|
|
|
line*, col*: INTEGER
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
LEX* = RECORD
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
sym*: INTEGER;
|
|
|
|
pos*: POSITION;
|
|
|
|
ident*: IDENT;
|
2021-06-15 19:33:16 +02:00
|
|
|
string*: STRING;
|
2019-09-26 22:23:06 +02:00
|
|
|
value*: ARITH.VALUE;
|
2021-06-15 19:33:16 +02:00
|
|
|
error*: INTEGER
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
SCANNER* = TXT.TEXT;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
KEYWORD = ARRAY 10 OF CHAR;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
VAR
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
delimiters: ARRAY 256 OF BOOLEAN;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
upto, LowerCase, _if: BOOLEAN;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
strings, def: LISTS.LIST;
|
|
|
|
|
|
|
|
KW: ARRAY 33 OF RECORD upper, lower: KEYWORD; uhash, lhash: INTEGER END;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE enterKW (s: KEYWORD; idx: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
KW[idx].lower := s;
|
|
|
|
KW[idx].upper := s;
|
|
|
|
S.UpCase(KW[idx].upper);
|
|
|
|
KW[idx].uhash := S.HashStr(KW[idx].upper);
|
|
|
|
KW[idx].lhash := S.HashStr(KW[idx].lower);
|
|
|
|
END enterKW;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE checkKW (ident: IDENT): INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
2021-06-15 19:33:16 +02:00
|
|
|
i, res: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
res := lxIDENT;
|
|
|
|
i := 0;
|
|
|
|
WHILE i < LEN(KW) DO
|
|
|
|
IF (KW[i].uhash = ident.hash) & (KW[i].upper = ident.s)
|
|
|
|
OR LowerCase & (KW[i].lhash = ident.hash) & (KW[i].lower = ident.s) THEN
|
|
|
|
res := i + lxKW;
|
|
|
|
i := LEN(KW)
|
|
|
|
END;
|
|
|
|
INC(i)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
RETURN res
|
|
|
|
END checkKW;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE enterStr* (s: TEXTSTR): STRING;
|
|
|
|
VAR
|
|
|
|
str, res: STRING;
|
|
|
|
hash: INTEGER;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
hash := S.HashStr(s);
|
|
|
|
str := strings.first(STRING);
|
|
|
|
res := NIL;
|
|
|
|
WHILE str # NIL DO
|
|
|
|
IF (str.hash = hash) & (str.s = s) THEN
|
|
|
|
res := str;
|
|
|
|
str := NIL
|
|
|
|
ELSE
|
|
|
|
str := str.next(STRING)
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
IF res = NIL THEN
|
|
|
|
NEW(res);
|
|
|
|
res.s := s;
|
|
|
|
res.offset := -1;
|
|
|
|
res.offsetW := -1;
|
|
|
|
res.hash := hash;
|
|
|
|
LISTS.push(strings, res)
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2021-06-15 19:33:16 +02:00
|
|
|
|
|
|
|
RETURN res
|
|
|
|
END enterStr;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE nextc (text: TXT.TEXT): CHAR;
|
|
|
|
BEGIN
|
|
|
|
TXT.next(text)
|
|
|
|
RETURN text.peak
|
|
|
|
END nextc;
|
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE setIdent* (VAR ident: IDENT; s: IDSTR);
|
|
|
|
BEGIN
|
|
|
|
ident.s := s;
|
|
|
|
ident.hash := S.HashStr(s)
|
|
|
|
END setIdent;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE ident (text: TXT.TEXT; VAR lex: LEX);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
2021-06-15 19:33:16 +02:00
|
|
|
i: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
2019-03-11 09:59:55 +01:00
|
|
|
ASSERT(S.letter(c));
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
i := 0;
|
|
|
|
WHILE (i < IDLEN - 1) & (S.letter(c) OR S.digit(c)) DO
|
|
|
|
lex.ident.s[i] := c;
|
|
|
|
INC(i);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
lex.ident.s[i] := 0X;
|
|
|
|
lex.ident.hash := S.HashStr(lex.ident.s);
|
|
|
|
lex.sym := checkKW(lex.ident);
|
|
|
|
|
|
|
|
IF S.letter(c) OR S.digit(c) THEN
|
|
|
|
ERRORS.WarningMsg(lex.pos.line, lex.pos.col, 2);
|
|
|
|
WHILE S.letter(c) OR S.digit(c) DO
|
|
|
|
c := nextc(text)
|
2019-10-06 19:55:12 +02:00
|
|
|
END
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
END ident;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE number (text: TXT.TEXT; VAR lex: LEX);
|
2021-06-15 19:33:16 +02:00
|
|
|
TYPE
|
|
|
|
NUMSTR = ARRAY NUMLEN OF CHAR;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
|
|
|
hex: BOOLEAN;
|
2021-06-15 19:33:16 +02:00
|
|
|
error, sym, i: INTEGER;
|
|
|
|
num: NUMSTR;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE push (VAR num: NUMSTR; VAR i: INTEGER; c: CHAR);
|
|
|
|
BEGIN
|
|
|
|
IF i < NUMLEN - 1 THEN
|
|
|
|
num[i] := c;
|
|
|
|
INC(i)
|
|
|
|
END
|
|
|
|
END push;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
2019-03-11 09:59:55 +01:00
|
|
|
ASSERT(S.digit(c));
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
i := 0;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
error := 0;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxINTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
hex := FALSE;
|
|
|
|
|
|
|
|
WHILE S.digit(c) DO
|
2021-06-15 19:33:16 +02:00
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
WHILE S.hexdigit(c) OR LowerCase & ("a" <= c) & (c <= "f") DO
|
|
|
|
S.cap(c);
|
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
hex := TRUE
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
IF (c = "H") OR LowerCase & (c = "h") THEN
|
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxHEX
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
ELSIF (c = "X") OR LowerCase & (c = "x") THEN
|
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxCHAR
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
ELSIF c = "." THEN
|
|
|
|
|
|
|
|
IF hex THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxERROR01
|
2016-10-24 01:30:27 +02:00
|
|
|
ELSE
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c # "." THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
push(num, i, ".");
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxFLOAT
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxINTEGER;
|
2019-09-26 22:23:06 +02:00
|
|
|
text.peak := 7FX;
|
|
|
|
upto := TRUE
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
WHILE S.digit(c) DO
|
2021-06-15 19:33:16 +02:00
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
IF (c = "E") OR LowerCase & (c = "e") THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
IF (c = "+") OR (c = "-") THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
IF S.digit(c) THEN
|
|
|
|
WHILE S.digit(c) DO
|
2021-06-15 19:33:16 +02:00
|
|
|
push(num, i, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
ELSE
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxERROR02
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
END
|
|
|
|
|
|
|
|
END
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
ELSIF hex THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxERROR01
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
IF (i = NUMLEN - 1) & (sym >= 0) THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxERROR07
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
num[i] := 0X;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
IF sym = lxINTEGER THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
ARITH.iconv(num, lex.value, error)
|
2020-10-13 09:58:51 +02:00
|
|
|
ELSIF (sym = lxHEX) OR (sym = lxCHAR) THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
ARITH.hconv(num, lex.value, error)
|
2020-10-13 09:58:51 +02:00
|
|
|
ELSIF sym = lxFLOAT THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
ARITH.fconv(num, lex.value, error)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
CASE error OF
|
|
|
|
|0:
|
2020-10-13 09:58:51 +02:00
|
|
|
|1: sym := lxERROR08
|
|
|
|
|2: sym := lxERROR09
|
|
|
|
|3: sym := lxERROR10
|
|
|
|
|4: sym := lxERROR11
|
|
|
|
|5: sym := lxERROR12
|
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
lex.sym := sym
|
2019-03-11 09:59:55 +01:00
|
|
|
END number;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE string (text: TXT.TEXT; VAR lex: LEX; quot: CHAR);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
2019-09-26 22:23:06 +02:00
|
|
|
c: CHAR;
|
2021-06-15 19:33:16 +02:00
|
|
|
i: INTEGER;
|
|
|
|
str: TEXTSTR;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
i := 0;
|
|
|
|
WHILE (i < LEN(str) - 1) & (c # quot) & (c # 0X) & ~text.eol & ~text.eof DO
|
|
|
|
str[i] := c;
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2021-06-15 19:33:16 +02:00
|
|
|
INC(i)
|
|
|
|
END;
|
|
|
|
|
|
|
|
str[i] := 0X;
|
|
|
|
|
|
|
|
IF (i = LEN(str) - 1) & (c # quot) & (c # 0X) & ~text.eol & ~text.eof THEN
|
|
|
|
lex.sym := lxERROR05
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = quot THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2021-06-15 19:33:16 +02:00
|
|
|
IF i # 1 THEN
|
|
|
|
lex.sym := lxSTRING
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
2021-06-15 19:33:16 +02:00
|
|
|
lex.sym := lxCHAR;
|
|
|
|
ARITH.setChar(lex.value, ORD(str[0]))
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
2021-06-15 19:33:16 +02:00
|
|
|
ELSIF lex.sym # lxERROR05 THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.sym := lxERROR03
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF lex.sym = lxSTRING THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
lex.string := enterStr(str);
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.value.typ := ARITH.tSTRING;
|
|
|
|
lex.value.string := lex.string
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END string;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE comment (text: TXT.TEXT);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
|
|
|
cond, depth: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
cond := 0;
|
2019-03-11 09:59:55 +01:00
|
|
|
depth := 1;
|
|
|
|
|
|
|
|
REPEAT
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = "*" THEN
|
|
|
|
IF cond = 1 THEN
|
|
|
|
cond := 0;
|
|
|
|
INC(depth)
|
|
|
|
ELSE
|
|
|
|
cond := 2
|
|
|
|
END
|
|
|
|
ELSIF c = ")" THEN
|
|
|
|
IF cond = 2 THEN
|
|
|
|
DEC(depth)
|
|
|
|
END;
|
|
|
|
cond := 0
|
|
|
|
ELSIF c = "(" THEN
|
|
|
|
cond := 1
|
|
|
|
ELSE
|
|
|
|
cond := 0
|
|
|
|
END
|
|
|
|
|
|
|
|
UNTIL (depth = 0) OR text.eof
|
|
|
|
|
|
|
|
END comment;
|
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE delimiter (text: TXT.TEXT; c: CHAR): INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
VAR
|
|
|
|
sym: INTEGER;
|
2021-06-15 19:33:16 +02:00
|
|
|
c0: CHAR;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
c0 := c;
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
CASE c0 OF
|
2019-03-11 09:59:55 +01:00
|
|
|
|"+":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxPLUS
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"-":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxMINUS
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"*":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxMUL
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"/":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxSLASH;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
IF c = "/" THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxCOMMENT;
|
2019-03-11 09:59:55 +01:00
|
|
|
REPEAT
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
UNTIL text.eol OR text.eof
|
|
|
|
END
|
|
|
|
|
|
|
|
|"~":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxNOT
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"&":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxAND
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|".":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxPOINT;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
IF c = "." THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxRANGE;
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
|",":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxCOMMA
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|";":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxSEMI
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"|":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxBAR
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"(":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxLROUND;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = "*" THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxCOMMENT;
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
comment(text)
|
|
|
|
END
|
|
|
|
|
|
|
|
|"[":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxLSQUARE
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"{":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxLCURLY
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"^":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxCARET
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"=":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxEQ
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"#":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxNE
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"<":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxLT;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = "=" THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxLE;
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|">":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxGT;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = "=" THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxGE;
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
|":":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxCOLON;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = "=" THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxASSIGN;
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
|")":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxRROUND
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"]":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxRSQUARE
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|"}":
|
2020-10-13 09:58:51 +02:00
|
|
|
sym := lxRCURLY
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
END
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
RETURN sym
|
2019-03-11 09:59:55 +01:00
|
|
|
END delimiter;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE Next* (text: SCANNER; VAR lex: LEX);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
PROCEDURE check (cond: BOOLEAN; text: SCANNER; lex: LEX; errno: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
IF ~cond THEN
|
|
|
|
ERRORS.ErrorMsg(text.fname, lex.pos.line, lex.pos.col, errno)
|
|
|
|
END
|
|
|
|
END check;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE IsDef (str: ARRAY OF CHAR): BOOLEAN;
|
|
|
|
VAR
|
|
|
|
cur: DEF;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
cur := def.first(DEF);
|
|
|
|
WHILE (cur # NIL) & (cur.ident # str) DO
|
|
|
|
cur := cur.next(DEF)
|
|
|
|
END
|
|
|
|
|
|
|
|
RETURN cur # NIL
|
|
|
|
END IsDef;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Skip (text: SCANNER);
|
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
i := 0;
|
|
|
|
WHILE (i <= text.ifc) & ~text._skip[i] DO
|
|
|
|
INC(i)
|
|
|
|
END;
|
|
|
|
text.skip := i <= text.ifc
|
|
|
|
END Skip;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE prep_if (text: SCANNER; VAR lex: LEX);
|
|
|
|
VAR
|
|
|
|
skip: BOOLEAN;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
INC(text.ifc);
|
|
|
|
text._elsif[text.ifc] := lex.sym = lxELSIF;
|
|
|
|
IF lex.sym = lxIF THEN
|
|
|
|
INC(text.elsec);
|
|
|
|
text._else[text.elsec] := FALSE
|
|
|
|
END;
|
|
|
|
_if := TRUE;
|
|
|
|
skip := TRUE;
|
|
|
|
text.skip := FALSE;
|
|
|
|
|
|
|
|
Next(text, lex);
|
|
|
|
check(lex.sym = lxLROUND, text, lex, 64);
|
|
|
|
|
|
|
|
Next(text, lex);
|
|
|
|
check(lex.sym = lxIDENT, text, lex, 22);
|
|
|
|
|
|
|
|
REPEAT
|
2021-06-15 19:33:16 +02:00
|
|
|
IF IsDef(lex.ident.s) THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
skip := FALSE
|
|
|
|
END;
|
|
|
|
|
|
|
|
Next(text, lex);
|
|
|
|
IF lex.sym = lxBAR THEN
|
|
|
|
Next(text, lex);
|
|
|
|
check(lex.sym = lxIDENT, text, lex, 22)
|
|
|
|
ELSE
|
|
|
|
check(lex.sym = lxRROUND, text, lex, 33)
|
|
|
|
END
|
|
|
|
UNTIL lex.sym = lxRROUND;
|
|
|
|
|
|
|
|
_if := FALSE;
|
|
|
|
text._skip[text.ifc] := skip;
|
|
|
|
Skip(text);
|
|
|
|
Next(text, lex)
|
|
|
|
END prep_if;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE prep_end (text: SCANNER; VAR lex: LEX);
|
|
|
|
BEGIN
|
|
|
|
check(text.ifc > 0, text, lex, 118);
|
|
|
|
IF lex.sym = lxEND THEN
|
|
|
|
WHILE text._elsif[text.ifc] DO
|
|
|
|
DEC(text.ifc)
|
|
|
|
END;
|
|
|
|
DEC(text.ifc);
|
|
|
|
DEC(text.elsec)
|
|
|
|
ELSIF (lex.sym = lxELSE) OR (lex.sym = lxELSIF) THEN
|
|
|
|
check(~text._else[text.elsec], text, lex, 118);
|
|
|
|
text._skip[text.ifc] := ~text._skip[text.ifc];
|
|
|
|
text._else[text.elsec] := lex.sym = lxELSE
|
|
|
|
END;
|
|
|
|
Skip(text);
|
|
|
|
IF lex.sym = lxELSIF THEN
|
|
|
|
prep_if(text, lex)
|
|
|
|
ELSE
|
|
|
|
Next(text, lex)
|
|
|
|
END
|
|
|
|
END prep_end;
|
|
|
|
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
REPEAT
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
WHILE S.space(c) DO
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
lex.pos.line := text.line;
|
|
|
|
lex.pos.col := text.col;
|
|
|
|
|
|
|
|
IF S.letter(c) THEN
|
|
|
|
ident(text, lex)
|
|
|
|
ELSIF S.digit(c) THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
number(text, lex)
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSIF (c = '"') OR (c = "'") THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
string(text, lex, c)
|
2019-10-06 19:55:12 +02:00
|
|
|
ELSIF delimiters[ORD(c)] THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
lex.sym := delimiter(text, c)
|
2020-10-13 09:58:51 +02:00
|
|
|
ELSIF c = "$" THEN
|
|
|
|
IF S.letter(nextc(text)) THEN
|
|
|
|
ident(text, lex);
|
|
|
|
IF lex.sym = lxIF THEN
|
|
|
|
IF ~_if THEN
|
|
|
|
prep_if(text, lex)
|
|
|
|
END
|
|
|
|
ELSIF (lex.sym = lxEND) OR (lex.sym = lxELSE) OR (lex.sym = lxELSIF) THEN
|
|
|
|
IF ~_if THEN
|
|
|
|
prep_end(text, lex)
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
check(FALSE, text, lex, 119)
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
check(FALSE, text, lex, 119)
|
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSIF c = 0X THEN
|
|
|
|
lex.sym := lxEOF;
|
2020-10-13 09:58:51 +02:00
|
|
|
text.skip := FALSE;
|
2019-03-11 09:59:55 +01:00
|
|
|
IF text.eof THEN
|
|
|
|
INC(lex.pos.col)
|
|
|
|
END
|
2019-09-26 22:23:06 +02:00
|
|
|
ELSIF (c = 7FX) & upto THEN
|
|
|
|
upto := FALSE;
|
|
|
|
lex.sym := lxRANGE;
|
|
|
|
DEC(lex.pos.col);
|
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.sym := lxERROR04
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF lex.sym < 0 THEN
|
|
|
|
lex.error := -lex.sym
|
|
|
|
ELSE
|
|
|
|
lex.error := 0
|
|
|
|
END
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
UNTIL (lex.sym # lxCOMMENT) & ~text.skip
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END Next;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE open* (name: ARRAY OF CHAR): SCANNER;
|
2019-09-26 22:23:06 +02:00
|
|
|
RETURN TXT.open(name)
|
2019-03-11 09:59:55 +01:00
|
|
|
END open;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE close* (VAR scanner: SCANNER);
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.close(scanner)
|
2019-03-11 09:59:55 +01:00
|
|
|
END close;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE init* (lower: BOOLEAN);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
delim: ARRAY 23 OF CHAR;
|
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
upto := FALSE;
|
2020-10-13 09:58:51 +02:00
|
|
|
LowerCase := lower;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
FOR i := 0 TO 255 DO
|
2019-10-06 19:55:12 +02:00
|
|
|
delimiters[i] := FALSE
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
delim := "+-*/~&.,;|([{^=#<>:)]}";
|
|
|
|
|
|
|
|
FOR i := 0 TO LEN(delim) - 2 DO
|
2019-10-06 19:55:12 +02:00
|
|
|
delimiters[ORD(delim[i])] := TRUE
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
enterKW("array", 0);
|
|
|
|
enterKW("begin", 1);
|
|
|
|
enterKW("by", 2);
|
|
|
|
enterKW("case", 3);
|
|
|
|
enterKW("const", 4);
|
|
|
|
enterKW("div", 5);
|
|
|
|
enterKW("do", 6);
|
|
|
|
enterKW("else", 7);
|
|
|
|
enterKW("elsif", 8);
|
|
|
|
enterKW("end", 9);
|
|
|
|
enterKW("false", 10);
|
|
|
|
enterKW("for", 11);
|
|
|
|
enterKW("if", 12);
|
|
|
|
enterKW("import", 13);
|
|
|
|
enterKW("in", 14);
|
|
|
|
enterKW("is", 15);
|
|
|
|
enterKW("mod", 16);
|
|
|
|
enterKW("module", 17);
|
|
|
|
enterKW("nil", 18);
|
|
|
|
enterKW("of", 19);
|
|
|
|
enterKW("or", 20);
|
|
|
|
enterKW("pointer", 21);
|
|
|
|
enterKW("procedure", 22);
|
|
|
|
enterKW("record", 23);
|
|
|
|
enterKW("repeat", 24);
|
|
|
|
enterKW("return", 25);
|
|
|
|
enterKW("then", 26);
|
|
|
|
enterKW("to", 27);
|
|
|
|
enterKW("true", 28);
|
|
|
|
enterKW("type", 29);
|
|
|
|
enterKW("until", 30);
|
|
|
|
enterKW("var", 31);
|
|
|
|
enterKW("while", 32)
|
2019-03-11 09:59:55 +01:00
|
|
|
END init;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE NewDef* (str: ARRAY OF CHAR);
|
|
|
|
VAR
|
|
|
|
item: DEF;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
NEW(item);
|
|
|
|
COPY(str, item.ident);
|
|
|
|
LISTS.push(def, item)
|
|
|
|
END NewDef;
|
|
|
|
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
def := LISTS.create(NIL);
|
|
|
|
strings := LISTS.create(NIL)
|
2020-05-25 22:48:33 +02:00
|
|
|
END SCAN.
|