-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathxdp.dpr
More file actions
4433 lines (3417 loc) · 125 KB
/
xdp.dpr
File metadata and controls
4433 lines (3417 loc) · 125 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// XD Pascal - a 32-bit compiler for MS-DOS (real CPU mode)
// Developed by Vasiliy Tereshkov, 2009-2010
{$APPTYPE CONSOLE}
{$I-}
program XDP;
// uses SysUtils; // For debug purposes only
const
VERSION = '0.7.12';
NUMDELIMITERS = 22;
NUMKEYWORDS = 31;
// Standard token codes
OPARTOK = 1;
CPARTOK = 2;
MULTOK = 3;
PLUSTOK = 4;
COMMATOK = 5;
MINUSTOK = 6;
PERIODTOK = 7;
RANGETOK = 8;
DIVTOK = 9;
COLONTOK = 10;
ASSIGNTOK = 11;
SEMICOLONTOK = 12;
LTTOK = 13;
LETOK = 14;
NETOK = 15;
EQTOK = 16;
GTTOK = 17;
GETOK = 18;
ADDRESSTOK = 19;
OBRACKETTOK = 20;
CBRACKETTOK = 21;
DEREFERENCETOK = 22;
ANDTOK = 23;
ARRAYTOK = 24;
BEGINTOK = 25;
CASETOK = 26;
CONSTTOK = 27;
IDIVTOK = 28;
DOTOK = 29;
DOWNTOTOK = 30;
ELSETOK = 31;
ENDTOK = 32;
FORTOK = 33;
FUNCTIONTOK = 34;
IFTOK = 35;
MODTOK = 36;
NILTOK = 37;
NOTTOK = 38;
OFTOK = 39;
ORTOK = 40;
PROCEDURETOK = 41;
PROGRAMTOK = 42;
RECORDTOK = 43;
REPEATTOK = 44;
SHLTOK = 45;
SHRTOK = 46;
THENTOK = 47;
TOTOK = 48;
TYPETOK = 49;
UNTILTOK = 50;
VARTOK = 51;
WHILETOK = 52;
XORTOK = 53;
// Non-standard token codes
IDENTTOK = 101;
INTNUMBERTOK = 102;
FRACNUMBERTOK = 103;
CHARLITERALTOK = 104;
STRINGLITERALTOK = 105;
// Identifier kind codes
CONSTANT = 1;
USERTYPE = 2;
VARIABLE = 3;
PROC = 4;
FUNC = 5;
// Type kinds
ANYTYPE = 1;
INTEGERTYPE = 2;
SMALLINTTYPE = 3;
SHORTINTTYPE = 4;
CHARTYPE = 5;
BOOLEANTYPE = 6;
REALTYPE = 7;
POINTERTYPE = 8;
TEXTTYPE = 9;
ARRAYTYPE = 10;
RECORDTYPE = 11;
SUBRANGETYPE = 12;
FORWARDTYPE = 101;
IntegerTypes = [INTEGERTYPE, SMALLINTTYPE, SHORTINTTYPE];
OrdinalTypes = IntegerTypes + [CHARTYPE, BOOLEANTYPE, SUBRANGETYPE];
// Type indices
ANYTYPEINDEX = 1; // Base type for untyped pointers
INTEGERTYPEINDEX = 2;
SMALLINTTYPEINDEX = 3;
SHORTINTTYPEINDEX = 4;
CHARTYPEINDEX = 5;
BOOLEANTYPEINDEX = 6;
REALTYPEINDEX = 7;
POINTERTYPEINDEX = 8; // Untyped pointer, compatible with any other
TEXTTYPEINDEX = 9; // Universal file type
STRINGTYPEINDEX = 10;
// Predefined routine codes
INCPROC = 1;
DECPROC = 2;
READPROC = 3;
WRITEPROC = 4;
READLNPROC = 5;
WRITELNPROC = 6;
INPPROC = 7; // Read from a port
OUTPPROC = 8; // Write to a port
NEWPROC = 9;
DISPOSEPROC = 10;
HALTPROC = 11;
INTRPROC = 12;
SIZEOFFUNC = 15;
ORDFUNC = 16;
CHRFUNC = 17;
PREDFUNC = 18;
SUCCFUNC = 19;
ROUNDFUNC = 20;
TRUNCFUNC = 21;
ABSFUNC = 22;
SQRFUNC = 23;
SINFUNC = 24;
COSFUNC = 25;
ARCTANFUNC = 26;
EXPFUNC = 27;
LNFUNC = 28;
SQRTFUNC = 29;
// Compiler parameters
MAXSTRLENGTH = 80;
MAXSTDTOKENLENGTH = 9;
MAXNAMELENGTH = 32;
MAXIDENTS = 1000;
MAXTYPES = 1000;
MAXBLOCKS = 128; // Must be a multiple of 8
MAXNESTING = 10;
MAXPARAMS = 20;
MAXUNITNESTING = 5;
MAXFIELDS = 100;
PSPSIZE = $100;
SEGMENTSIZE = $10000;
MAXSTATICSTRDATASIZE = $4000;
// Compilation pass codes
CALLDETERMPASS = 1;
CODEGENERATIONPASS = 2;
// Scope levels
GLOBAL = 1;
LOCAL = 2;
// Parameter passing
VALPASSING = 1;
CONSTPASSING = 2;
VARPASSING = 3;
type
TString = string [MAXSTRLENGTH];
TKeyName = string [MAXSTDTOKENLENGTH];
TName = string [MAXNAMELENGTH];
TUnit = record
FileName: TString;
Pos, Line: Integer;
end;
TParam = record
Name: TName;
DataType: Byte;
PassMethod: Byte;
end;
PParam = ^TParam;
TField = record
Name: TName;
DataType: Byte;
Offset: Integer;
end;
TType = record
Block: Byte;
case TypeKind: Byte of
SUBRANGETYPE:
(HostType: Byte;
Low, High: Integer);
POINTERTYPE, ARRAYTYPE:
(BaseType, IndexType: Byte);
RECORDTYPE:
(NumFields: Integer;
Field: array [1..MAXFIELDS] of ^TField);
FORWARDTYPE:
(TypeIdentName: TName);
end;
TConst = record
case Kind: Byte of
INTNUMBERTOK:
(Value: LongInt);
FRACNUMBERTOK:
(FracValue: Single);
end;
TToken = record
Kind: Byte;
Name: TName;
Value: LongInt;
FracValue: Single;
StrAddress: Integer;
StrLength: Integer;
end;
TIdentifier = record
Kind: Byte;
Name: TName;
Value: LongInt; // Value for a constant, address for a variable, procedure or function
FracValue: Single;
Block: Byte; // Index of a block in which the identifier is defined
NestingLevel: Byte;
DataType: Byte;
RecType: Byte; // Parent record type code for a field
Scope: Byte;
PassMethod: Byte; // Value, CONST or VAR parameter status
NumParams: Integer;
Param: array [1..MAXPARAMS] of PParam;
ProcAsBlock: Byte;
PredefIndex: Byte;
IsUnresolvedForward: Boolean;
end;
const
Keyword: array [1..NUMKEYWORDS] of TKeyName =
(
'AND',
'ARRAY',
'BEGIN',
'CASE',
'CONST',
'DIV',
'DO',
'DOWNTO',
'ELSE',
'END',
'FOR',
'FUNCTION',
'IF',
'MOD',
'NIL',
'NOT',
'OF',
'OR',
'PROCEDURE',
'PROGRAM',
'RECORD',
'REPEAT',
'SHL',
'SHR',
'THEN',
'TO',
'TYPE',
'UNTIL',
'VAR',
'WHILE',
'XOR'
);
var
Ident: array [1..MAXIDENTS] of TIdentifier;
Types: array [1..MAXTYPES] of TType;
UnitStack: array [1..MAXUNITNESTING] of TUnit;
StaticStringData: array [0..MAXSTATICSTRDATASIZE - 1] of Char;
CodePosStack: array [0..1023] of Integer;
BlockStack: array [1..MAXNESTING] of Byte;
CallGraph: array [0..MAXBLOCKS - 1, 0..MAXBLOCKS div 8 - 1] of Byte; // Rows are callers, columns are callees
BlockIsNotDead: array [1..MAXBLOCKS] of Boolean;
Tok: TToken;
NumIdent, NumTypes, NumStaticStrChars, VarDataOrigin, NumBlocks, BlockStackTop,
CodeSize, CodePosStackTop, GlobalDataSize,
Pass, UnitStackTop, Line: Integer;
ProgramName, ExeName: TString;
InFile: file of Char;
OutFile: file of Byte;
EndOfProgram: Boolean;
ch, ch2: Char;
// ----- GENERAL ROUTINES -----
procedure DisposeAll;
var
i, j: Integer;
begin
// Dispose dynamically allocated parameter data
for i := 1 to NumIdent do
if Ident[i].Kind in [PROC, FUNC] then
for j := 1 to Ident[i].NumParams do
Dispose(Ident[i].Param[j]);
// Dispose dynamically allocated field data
for i := 1 to NumTypes do
if Types[i].TypeKind = RECORDTYPE then
for j := 1 to Types[i].NumFields do
Dispose(Types[i].Field[j]);
end;
procedure Error(const Msg: string);
begin
WriteLn('Error ', UnitStack[UnitStackTop].FileName, ' ', Line, ': ', Msg);
WriteLn;
DisposeAll;
Close(InFile);
Close(OutFile);
Halt(1);
end;
function GetKeyword(const S: TKeyName): Integer;
var
Max, Mid, Min: Integer;
Found: Boolean;
begin
Result := 0;
// Binary search
Min := 1;
Max := NUMKEYWORDS;
repeat
Mid := (Min + Max) div 2;
if S > Keyword[Mid] then
Min := Mid + 1
else
Max := Mid - 1;
Found := S = Keyword[Mid];
until Found or (Min > Max);
if Found then Result := NUMDELIMITERS + Mid;
end;
function GetIdentUnsafe(const S: TName): Integer;
var
IdentIndex, BlockStackIndex: Integer;
begin
Result := 0;
BlockStackIndex := BlockStackTop;
while (BlockStackIndex > 0) and (Result = 0) do
begin
IdentIndex := NumIdent;
while (IdentIndex > 0) and (Result = 0) do
begin
if (Ident[IdentIndex].Name = S) and (Ident[IdentIndex].Block = BlockStack[BlockStackIndex]) then Result := IdentIndex;
Dec(IdentIndex);
end;// while
Dec(BlockStackIndex);
end;// while
end;
function GetIdent(const S: TName): Integer;
begin
Result := GetIdentUnsafe(S);
if Result = 0 then
Error('Unknown identifier: ' + S);
end;
function GetField(RecType: Byte; const S: TName): Integer;
var
FieldIndex: Integer;
begin
Result := 0;
FieldIndex := 1;
while (FieldIndex <= Types[RecType].NumFields) and (Result = 0) do
begin
if Types[RecType].Field[FieldIndex]^.Name = S then Result := FieldIndex;
Inc(FieldIndex);
end;// while
if Result = 0 then
Error('Unknown field: ' + S);
end;
function GetSpelling(var Tok: TToken): TString;
begin
if Tok.Kind = 0 then
Result := 'no token'
else if Tok.Kind <= NUMDELIMITERS then
case Tok.Kind of
OPARTOK: Result := '(';
CPARTOK: Result := ')';
MULTOK: Result := '*';
PLUSTOK: Result := '+';
COMMATOK: Result := ',';
MINUSTOK: Result := '-';
PERIODTOK: Result := '.';
RANGETOK: Result := '..';
DIVTOK: Result := '/';
COLONTOK: Result := ':';
ASSIGNTOK: Result := ':=';
SEMICOLONTOK: Result := ';';
LTTOK: Result := '<';
LETOK: Result := '<=';
NETOK: Result := '<>';
EQTOK: Result := '=';
GTTOK: Result := '>';
GETOK: Result := '>=';
ADDRESSTOK: Result := '@';
OBRACKETTOK: Result := '[';
CBRACKETTOK: Result := ']';
DEREFERENCETOK: Result := '^';
end // case
else if Tok.Kind <= NUMDELIMITERS + NUMKEYWORDS then
Result := Keyword[Tok.Kind - NUMDELIMITERS]
else if Tok.Kind = IDENTTOK then
Result := 'identifier'
else if (Tok.Kind = INTNUMBERTOK) or (Tok.Kind = FRACNUMBERTOK) then
Result := 'number'
else if (Tok.Kind = CHARLITERALTOK) or (Tok.Kind = STRINGLITERALTOK) then
Result := 'literal'
else
Result := 'unknown token';
end;
procedure DefineStaticString(var Tok: TToken; const StrValue: TString);
var
i: Integer;
begin
Tok.StrAddress := NumStaticStrChars;
Tok.StrLength := Length(StrValue);
for i := 1 to Length(StrValue) do
begin
StaticStringData[NumStaticStrChars] := StrValue[i];
Inc(NumStaticStrChars);
if NumStaticStrChars > MAXSTATICSTRDATASIZE - 1 then
Error('Maximum string data size exceeded');
end;
// Add string termination character
StaticStringData[NumStaticStrChars] := #0;
Inc(NumStaticStrChars);
end;
function LowBound(DataType: Byte): Integer;
begin
Result := 0;
case Types[DataType].TypeKind of
INTEGERTYPE: Result := Low(Integer);
SMALLINTTYPE: Result := Low(SmallInt);
SHORTINTTYPE: Result := Low(ShortInt);
CHARTYPE: Result := 0;
BOOLEANTYPE: Result := -1;
SUBRANGETYPE: Result := Types[DataType].Low;
else
Error('Ordinal type expected');
end;// case
end;
function HighBound(DataType: Byte): Integer;
begin
Result := 0;
case Types[DataType].TypeKind of
INTEGERTYPE: Result := High(Integer);
SMALLINTTYPE: Result := High(SmallInt);
SHORTINTTYPE: Result := High(ShortInt);
CHARTYPE: Result := 255;
BOOLEANTYPE: Result := 0;
SUBRANGETYPE: Result := Types[DataType].High;
else
Error('Ordinal type expected');
end;// case
end;
function TypeSize(DataType: Byte): Integer;
var
i: Integer;
begin
Result := 0;
case Types[DataType].TypeKind of
INTEGERTYPE: Result := SizeOf(Integer);
SMALLINTTYPE: Result := SizeOf(SmallInt);
SHORTINTTYPE: Result := SizeOf(ShortInt);
CHARTYPE: Result := SizeOf(Char);
BOOLEANTYPE: Result := SizeOf(Boolean);
REALTYPE: Result := SizeOf(Single);
POINTERTYPE: Result := SizeOf(Pointer);
TEXTTYPE: Result := SizeOf(Integer);
ARRAYTYPE: Result := (HighBound(Types[DataType].IndexType) - LowBound(Types[DataType].IndexType) + 1) * TypeSize(Types[DataType].BaseType);
RECORDTYPE: begin
Result := 0;
for i := 1 to Types[DataType].NumFields do
Result := Result + TypeSize(Types[DataType].Field[i]^.DataType);
end;
SUBRANGETYPE: Result := SizeOf(Integer);
else
Error('Illegal type');
end;// case
end;
function GetCompatibleType(LeftType, RightType: Byte): Byte;
begin
Result := 0;
if LeftType = RightType then // General rule
Result := LeftType
else // Special cases
begin
// Untyped pointers compatible with any other pointers
if (Types[LeftType].TypeKind = POINTERTYPE) and (Types[RightType].TypeKind = POINTERTYPE) and
((Types[LeftType].BaseType = ANYTYPE) or (Types[RightType].BaseType = ANYTYPE)) then
Result := LeftType;
// Subranges compatible with their host types
if Types[LeftType].TypeKind = SUBRANGETYPE then
Result := GetCompatibleType(Types[LeftType].HostType, RightType);
if Types[RightType].TypeKind = SUBRANGETYPE then
Result := GetCompatibleType(LeftType, Types[RightType].HostType);
// Integers
if (Types[LeftType].TypeKind in IntegerTypes) and
(Types[RightType].TypeKind in IntegerTypes) then
Result := LeftType;
// Booleans
if (Types[LeftType].TypeKind = BOOLEANTYPE) and
(Types[RightType].TypeKind = BOOLEANTYPE) then
Result := LeftType;
// Characters
if (Types[LeftType].TypeKind = CHARTYPE) and
(Types[RightType].TypeKind = CHARTYPE) then
Result := LeftType;
end;// if
if Result = 0 then
Error('Incompatible types');
end;
function ConversionIsPossible(SrcType, DestType: Byte): Boolean;
begin
// Implicit type conversion is possible if DestType is real and SrcType is integer or a subrange of integer
Result := (Types[DestType].TypeKind = REALTYPE) and
((Types[SrcType].TypeKind in IntegerTypes) or
((Types[SrcType].TypeKind = SUBRANGETYPE) and (Types[Types[SrcType].HostType].TypeKind in IntegerTypes)));
end;
procedure AssertIdent;
begin
if Tok.Kind <> IDENTTOK then
Error('Identifier expected but ' + GetSpelling(Tok) + ' found');
end;
procedure CheckOperator(op: Byte; DataType: Byte);
begin
if Types[DataType].TypeKind = SUBRANGETYPE then
CheckOperator(op, Types[DataType].HostType)
else if (not (Types[DataType].TypeKind in (OrdinalTypes + [REALTYPE, POINTERTYPE]))) or
((Types[DataType].TypeKind = REALTYPE) and
not (op in [MULTOK, DIVTOK, PLUSTOK, MINUSTOK, GTTOK, GETOK, EQTOK, NETOK, LETOK, LTTOK])) or
((Types[DataType].TypeKind in IntegerTypes) and
not (op in [MULTOK, IDIVTOK, MODTOK, SHLTOK, SHRTOK, ANDTOK, PLUSTOK, MINUSTOK, ORTOK, XORTOK, NOTTOK, GTTOK, GETOK, EQTOK, NETOK, LETOK, LTTOK])) or
((Types[DataType].TypeKind = CHARTYPE) and
not (op in [GTTOK, GETOK, EQTOK, NETOK, LETOK, LTTOK])) or
((Types[DataType].TypeKind = BOOLEANTYPE) and
not (op in [ANDTOK, ORTOK, XORTOK, NOTTOK, GTTOK, GETOK, EQTOK, NETOK, LETOK, LTTOK])) or
((Types[DataType].TypeKind = POINTERTYPE) and
not (op in [GTTOK, GETOK, EQTOK, NETOK, LETOK, LTTOK]))
then
Error('Operator is not applicable');
end;
procedure AddCallGraphChild(ParentBlock, ChildBlock: Integer);
begin
// Set bit at ParentBlock row, ChildBlock column
CallGraph[ParentBlock, ChildBlock div 8] := CallGraph[ParentBlock, ChildBlock div 8] or (1 shl (ChildBlock mod 8));
end;
// ----- SCANNER -----
procedure InitScanner;
begin
EndOfProgram := FALSE;
UnitStackTop := 1;
UnitStack[UnitStackTop].FileName := ProgramName;
Assign(InFile, ProgramName);
Reset(InFile);
if IOResult <> 0 then
Error('Unable to open source file ' + ProgramName);
Line := 1;
ch := ' ';
ch2 := ' ';
end;
procedure EnterIncludedFile(const Name: TString);
begin
UnitStack[UnitStackTop].Pos := FilePos(InFile);
UnitStack[UnitStackTop].Line := Line;
Close(InFile);
Assign(InFile, Name);
Reset(InFile);
if IOResult <> 0 then
Error('Unable to open source file ' + Name);
Inc(UnitStackTop);
UnitStack[UnitStackTop].FileName := Name;
Line := 1;
end;
procedure LeaveIncludedFile(var ch: Char);
begin
if UnitStackTop > 1 then
begin
Dec(UnitStackTop);
Assign(InFile, UnitStack[UnitStackTop].FileName);
Reset(InFile);
Seek(InFile, UnitStack[UnitStackTop].Pos);
Line := UnitStack[UnitStackTop].Line;
Read(InFile, ch);
end
else
begin
EndOfProgram := TRUE;
ch := #0;
end;
end;
procedure ReadChar(var ch: Char);
begin
if EndOfProgram then
ch := #0
else
if EOF(InFile) then
begin
Close(InFile);
LeaveIncludedFile(ch);
end
else
Read(InFile, ch);
if ch = #10 then Inc(Line); // End of line found
end;
procedure ReadValidChar(var ch: Char);
begin
ReadChar(ch);
ch := UpCase(ch);
end;
procedure ReadLiteralChar(var ch: Char);
begin
ReadChar(ch);
if (ch = #0) or (ch = #10) then
Error('Unterminated string');
end;
procedure ReadSingleLineComment;
begin
while (ch <> #10) and not EndOfProgram do
ReadChar(ch);
end;
procedure ReadMultiLineComment;
begin
while (ch <> '}') and not EndOfProgram do
ReadChar(ch);
end;
procedure ReadDirective;
var
Text: TString;
begin
ReadChar(ch);
if UpCase(ch) = 'I' then // Include directive found
begin
Text := '';
ReadChar(ch);
while (ch <> '}') and not EndOfProgram do
begin
if not (ch in [#1..#31, ' ']) then Text := Text + ch;
ReadChar(ch);
end;
EnterIncludedFile(Text);
end
else
Error('Unknown compiler directive');
end;
procedure ReadHexadecimalNumber;
var
Num: Integer;
NumFound: Boolean;
begin
Num := 0;
NumFound := FALSE;
while ch in ['0'..'9', 'A'..'F'] do
begin
if ch in ['0'..'9'] then
Num := 16 * Num + Ord(ch) - Ord('0')
else
Num := 16 * Num + Ord(ch) - Ord('A') + 10;
NumFound := TRUE;
ReadValidChar(ch);
end;
if not NumFound then
Error('Hexadecimal constant is not found');
Tok.Kind := INTNUMBERTOK;
Tok.Value := Num;
end;
procedure ReadDecimalNumber;
var
Num, Expon: Integer;
Frac, FracWeight: Single;
NegExpon, RangeFound, ExponFound: Boolean;
begin
Num := 0;
Frac := 0;
Expon := 0;
NegExpon := FALSE;
while ch in ['0'..'9'] do
begin
Num := 10 * Num + Ord(ch) - Ord('0');
ReadValidChar(ch);
end;
if (ch <> '.') and (ch <> 'E') then // Integer number
begin
Tok.Kind := INTNUMBERTOK;
Tok.Value := Num;
end
else
begin
// Check for '..' token
RangeFound := FALSE;
if ch = '.' then
begin
ReadValidChar(ch2);
if ch2 = '.' then // Integer number followed by '..' token
begin
Tok.Kind := INTNUMBERTOK;
Tok.Value := Num;
RangeFound := TRUE;
end;
if not EndOfProgram then Seek(InFile, FilePos(InFile) - 1);
end; // if ch = '.'
if not RangeFound then // Fractional number
begin
// Check for fractional part
if ch = '.' then
begin
FracWeight := 0.1;
ReadValidChar(ch);
while ch in ['0'..'9'] do
begin
Frac := Frac + FracWeight * (Ord(ch) - Ord('0'));
FracWeight := FracWeight / 10;
ReadValidChar(ch);
end;
end; // if ch = '.'
// Check for exponent
if ch = 'E' then
begin
ReadValidChar(ch);
// Check for exponent sign
if ch = '+' then
ReadValidChar(ch)
else if ch = '-' then
begin
NegExpon := TRUE;
ReadValidChar(ch);
end;
ExponFound := FALSE;
while ch in ['0'..'9'] do
begin
Expon := 10 * Expon + Ord(ch) - Ord('0');
ReadValidChar(ch);
ExponFound := TRUE;
end;
if not ExponFound then
Error('Exponent is not found');
if NegExpon then Expon := -Expon;
end; // if ch = 'E'
Tok.Kind := FRACNUMBERTOK;
Tok.FracValue := (Num + Frac) * exp(Expon * ln(10));
end; // if not RangeFound
end; // else
end;
procedure ReadNumber;
begin
if ch = '$' then
begin
ReadValidChar(ch);
ReadHexadecimalNumber;
end
else
ReadDecimalNumber;
end;
procedure ReadCharCode;
begin
ReadValidChar(ch);
if not (ch in ['0'..'9', '$']) then
Error('Character code is not found');
ReadNumber;
if Tok.Kind = FRACNUMBERTOK then
Error('Integer character code expected');
Tok.Kind := CHARLITERALTOK;
end;
procedure ReadKeywordOrIdentifier;
var
Text: TString;
CurToken: Integer;
begin
Text := '';
repeat
Text := Text + ch;
ReadValidChar(ch);
until not (ch in ['A'..'Z', '_', '0'..'9']);
CurToken := GetKeyword(Text);
if CurToken <> 0 then // Keyword found
Tok.Kind := CurToken
else
begin // Identifier found