-
Notifications
You must be signed in to change notification settings - Fork 5
/
D16Parser.pas
1435 lines (1356 loc) · 43 KB
/
D16Parser.pas
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
unit D16Parser;
interface
uses
Classes, Types, Generics.Collections, SysUtils, UncountedInterfacedObject, Lexer, Token, CodeElement, DataType,
VarDeclaration, PascalUnit, ProcDeclaration, ASMBlock, Operation, Operations,
Factor, CompilerDefines, LineMapping, UnitCache, WriterIntf, DUmmyCollection;
type
TSectionType = (tsInterface, tsImplementation);
TD16Parser = class(TUncountedInterfacedObject, IOperations)
private
FLexer: TLexer;
FUnits: TUnitCache;
FOperations: TObjectList<TOperation>;
FCurrentUnit: TPascalUnit;
FCurrentProc: TProcDeclaration;
FSearchPath: TStringlist;
FOnMessage: TOnMessage;
FWarning: Integer;
FErrors: Integer;
FFatals: Integer;
FHints: Integer;
FPeekMode: Boolean;
FDummies: TDummyCollection;
FCurrentSectionType: TSectionType;
procedure ParsePascalUnit(AUnit: TPascalUnit; ACatchException: Boolean = False;
AParseAsProgramm: Boolean = False);
procedure RegisterType(AName: string; ASize: Integer = 2; APrimitive: TRawType = rtUInteger;
ABaseType: TDataType = nil);
procedure RegisterOperation(AOpName: string; ALeftType, ARightType: TRawType; ALeftSize, ARightSize: Integer;
AResultType:TDataType; AAssembler: string);
procedure RegisterBasicTypes();
procedure RegisterOperations();
function GetElement(AName: string; AType: TCodeElementClass): TCodeElement;
function ExpectElement(AName: string; AType: TCodeElementClass): TCodeElement;
function GetDataType(AName: string): TDataType;
function GetVar(AName: string): TVarDeclaration;
function GetOperation(AOperation: string; ALeftType,
ARightType: TDataType): TOperation;
//parse functions
procedure ParseUnit();
procedure ParseUnitHeader();
procedure ParseUnitFooter();
procedure ParseUnitSectionContent(AScope: TObjectList<TCodeElement>);
procedure ParseProgram();
procedure ParseProgramHeader();
procedure ParseUses(AScope: TObjectList<TCodeElement>);
procedure ParseTypes(AScope: TObjectList<TCodeElement>);
procedure ParseTypeDeclaration(AScope: TObjectList<TCodeElement>);
procedure ParseVars(AScope: TObjectList<TCodeElement>);
procedure ParseVarDeclaration(AScope: TObjectList<TCodeElement>; AIncludeEndMark: Boolean = True;
AAsParameter: Boolean = False; AAsLocal: Boolean = False; AAsConst: Boolean = False; AIndexOffset: Integer = 0);
procedure ParseConsts(AScope: TObjectList<TCodeElement>);
procedure ParseRoutineDeclaration(AScope: TObjectList<TCodeElement>);
procedure ParseRoutineParameters(AScope: TObjectList<TCodeElement>);
procedure ParseRoutineLocals(AProc: TProcDeclaration);
procedure ParseRoutineContent(AScope: TObjectList<TCodeElement>);
function ParseRoutineCall(AScope: TObjectList<TCodeElement>; AIncludeEndMark: Boolean = True): TDataType;
function ParseAssignment(AScope: TObjectList<TCodeElement>; AIncludeEndmark: Boolean = True): TDataType;
procedure ParseCondition(AScope: TObjectList<TCodeElement>);
procedure ParseWhileLoop(AScope: TObjectList<TCodeElement>);
procedure ParseRepeatLoop(AScope: TObjectList<TCodeElement>);
procedure ParseForLoop(AScope: TObjectList<TCodeElement>);
procedure ParseCaseStatement(AScope: TObjectList<TCodeElement>);
procedure ParseCase(AScope: TObjectList<TCodeElement>);
function ParseRelation(AScope: TObjectList<TCodeElement>; ATryInverse: Boolean = False): TDataType;
function ParseExpression(AScope: TObjectList<TCodeElement>): TDataType;
function ParseTerm(AScope: TObjectList<TCodeElement>): TDataType;
function ParseFactor(AScope: TObjectList<TCodeElement>): TDataType;
function ParseConstantFactor(AFactor: TFactor): TDataType;
function ParseArrayModifiers(AType: TDataType; AScope: TObjectList<TCodeElement>;
AMaxList: TList<Integer>):TDataType;
function ParsePostExpression(AType: TDataType; AScope: TObjectList<TCodeElement>;
AMaxList: TList<Integer>):TDataType;
procedure ParseASMBlock(AScope: TObjectList<TCodeElement>);
procedure Fatal(AMessage: string);
procedure Error(AMessage: string);
procedure DoMessage(AMessage, AUnit: string; ALine: Integer; ALevel: TMessageLevel);
function GetPathForFile(AFile: string): string;
function GetCurrentLine(): Integer;
procedure RegisterSysUnit();
procedure Initialize();
public
constructor Create(AUnitCache: TUnitCache);
destructor Destroy(); override;
procedure ParseFile(AFile: string);
procedure ParseSource(ASource: string; AAsProgram: Boolean);
procedure Reset();
procedure ResetResults();
property SearchPath: TStringlist read FSearchPath write FSearchPath;
property OnMessage: TOnMessage read FOnMessage write FOnMessage;
property Errors: Integer read FErrors;
property Warnings: Integer read FWarning;
property Fatals: Integer read FFatals;
property Hints: Integer read FHints;
property PeekMode: Boolean read FPeekMode write FPeekMode;
property Units: TUnitCache read FUnits;
end;
const
CRegA = 'A';
CRegB = 'B';
CRegC = 'C';
CRegI = 'I';
CRegJ = 'J';
CRegSP = 'SP';
CRegPC = 'PC';
implementation
uses
Math, StrUtils, Relation, Expression, Term, Assignment, Condition, Loops, ProcCall, CaseState, Optimizer;
const
CSysUnit = 'System';
{ TCompiler }
procedure TD16Parser.ParseFile(AFile: string);
var
LUnit: TPascalUnit;
begin
DoMessage('Compiling ' + QuotedStr(AFile), '', 0, mlNone);
LUnit := TPascalUnit.Create('');
try
if not FileExists(GetPathForFile(AFile)) then
begin
raise EAbort.Create('File not found:' + QuotedStr(AFile));
end;
LUnit.Lexer.LoadFromFile(GetPathForFile(AFile));
ParsePascalUnit(LUnit, false, SameText(ExtractFileExt(AFile), '.d16r'));
except
on E: Exception do
begin
DoMessage(E.Message, ExtractFileName(AFile), 0, mlFatal);
Inc(FFatals);
end;
end;
end;
procedure TD16Parser.ParseSource(ASource: string; AAsProgram: Boolean);
var
LUnit: TPascalUnit;
begin
LUnit := TPascalUnit.Create('');
try
LUnit.Lexer.LoadFromString(ASource);
ParsePascalUnit(LUnit, False, AAsProgram);
except
on E: Exception do
begin
DoMessage(E.Message, '', 0, mlFatal);
Inc(FFatals);
end;
end;
end;
procedure TD16Parser.ParsePascalUnit;
var
LLastUnit: TPascalUnit;
LLastLexer: TLexer;
begin
LLastUnit := FCurrentUnit;
LLastLexer := FLexer;
FUnits.Add(AUnit);
FCurrentUnit := AUnit;
FCurrentUnit.UsedUnits.Add(CSysUnit);
FLexer := AUnit.Lexer;
try
try
if not AParseAsProgramm then
begin
ParseUnit();
end
else
begin
ParseProgram();
end;
except
on E: Exception do
begin
DoMessage(E.Message, AUnit.Name, AUnit.Lexer.PeekToken.FoundInLine, mlFatal);
if AUnit.Name = '' then
begin
FUnits.Delete(FUnits.IndexOf(AUnit));
end;
end;
end;
finally
FCurrentUnit := LLastUnit;
FLexer := LLastLexer;
end;
end;
function TD16Parser.ParsePostExpression(AType: TDataType;
AScope: TObjectList<TCodeElement>; AMaxList: TList<Integer>): TDataType;
begin
Result := AType;
if FLexer.PeekToken.IsContent('[') then
begin
if (Result.RawType = rtPointer) and (Result.BaseType.RawType = rtArray) then
begin
Result := Result.BaseType;
end;
if (Result.RawType = rtArray) or (Result.RawType = rtString) then
begin
Result := ParseArrayModifiers(Result, AScope, AMaxList);
end;
end;
end;
constructor TD16Parser.Create;
begin
inherited Create();
FUnits := AUnitCache;
FOperations := TObjectList<TOperation>.Create();
FSearchPath := TStringList.Create();
FPeekMode := False;
FDummies := TDummyCollection.Create();
Initialize();
end;
destructor TD16Parser.Destroy;
begin
FSearchPath.Free;
FOperations.Free;
FDummies.Free;
inherited;
end;
procedure TD16Parser.DoMessage(AMessage, AUnit: string; ALine: Integer;
ALevel: TMessageLevel);
begin
case ALevel of
mlError:
begin
Inc(FErrors);
end;
mlFatal:
begin
Inc(FFatals);
end;
end;
if Assigned(FOnMessage) then
begin
FOnMessage(AMessage, AUnit, ALine, ALevel);
end;
end;
procedure TD16Parser.Error(AMessage: string);
begin
DoMessage(AMessage, FCurrentUnit.Name, FLexer.PeekToken.FoundInLine, mlError);
end;
function TD16Parser.ExpectElement(AName: string;
AType: TCodeElementClass): TCodeElement;
begin
Result := GetElement(AName, AType);
if not Assigned(Result) then
begin
Fatal('Undeclared identifier ' + QuotedStr(AName));
end;
end;
procedure TD16Parser.Fatal(AMessage: string);
begin
raise EAbort.Create(AMessage);
end;
function TD16Parser.GetCurrentLine: Integer;
begin
Result := FLexer.PeekToken.FoundInLine;
end;
function TD16Parser.GetDataType(AName: string): TDataType;
begin
Result := TDataType(GetElement(AName, TDataType));
if not Assigned(Result) then
begin
Result := FDummies.DataType;
Error(QuotedStr(AName) + ' is not a declared datatype');
end;
end;
function TD16Parser.GetElement(AName: string;
AType: TCodeElementClass): TCodeElement;
var
LUnit: TPascalUnit;
LElement: TCodeElement;
begin
Result := nil;
if Assigned(FCurrentProc) then
begin
Result := TVarDeclaration(FCurrentProc.GetElement(AName, AType));
if Assigned(Result) then
begin
Exit;
end;
end;
Result := FCurrentUnit.GetElementFromAll(AName, AType);
if Assigned(Result) then Exit;
for LUnit in FUnits do
begin
if (LUnit <> FCurrentUnit) and (FCurrentUnit.UsedUnits.IndexOf(LUnit.Name) < 0) then Continue;
LElement := LUnit.GetElement(AName, AType);
if Assigned(LElement) then
begin
Result := LElement;
Break;
end;
end;
end;
function TD16Parser.GetOperation(AOperation: string; ALeftType,
ARightType: TDataType): TOperation;
var
LOperation: TOperation;
begin
Result := nil;
for LOperation in FOperations do
begin
if SameText(LOperation.OpName, AOperation) and (LOperation.LeftRawType = ALeftType.RawType) and (LOperation.RightRawType = ARightType.RawType)
and (LOperation.LeftSize = ALeftType.Size) and (LOperation.RightSize = ARightType.Size) then
begin
Result := LOperation;
Break;
end;
end;
if not Assigned(Result) then
begin
Result := FDummies.Operation;
if not (SameText(ALeftType.Name, FDummies.DataType.Name)
or SameText(ARightType.Name, FDummies.DataType.Name)) then
begin//no error if we tried to handle a dummytype. This means something else went wrong before anyway!
Error('Operator ' + QuotedStr(AOperation) + ' not applicable to ' +
QuotedStr(ALeftType.Name) + ' and ' + QuotedStr(ARightType.Name));
end;
end;
end;
function TD16Parser.GetPathForFile(AFile: string): string;
var
LPath: string;
begin
Result := AFile;
if FileExists(AFile) then
begin
Exit;
end;
for LPath in FSearchPath do
begin
if FileExists(LPath + '\' + AFile) then
begin
Result := LPath + '\' + AFile;
Break;
end;
end;
end;
function TD16Parser.GetVar(AName: string): TVarDeclaration;
begin
Result := TVarDeclaration(GetElement(AName, TVarDeclaration));
if not Assigned(Result) then
begin
Result := FDummies.Variable;
Error(QuotedStr(AName) + ' is not a declared Var');
end;
end;
procedure TD16Parser.Initialize;
begin
FErrors := 0;
FFatals := 0;
FWarning := 0;
FHints := 0;
RegisterSysUnit();
end;
function TD16Parser.ParseArrayModifiers(AType: TDataType;
AScope: TObjectList<TCodeElement>; AMaxList: TList<Integer>): TDataType;
var
i: Integer;
LResult: TDataType;
begin
Result := AType;
while (Result.RawType = rtArray) or (Result.RawType = rtString) do
begin
for i := 0 to Result.Dimensions.Count - 1 do
begin
FLexer.GetToken('[');
LResult := ParseRelation(AScope, false);
AMaxList.Add(Result.Dimensions.Items[i]);
if LResult.RawType <> rtUInteger then
begin
Error('The result of a relation in an array modifier must be an unsigned integer');
end;
FLexer.GetToken(']');
end;
Result := Result.BaseType;
end;
end;
procedure TD16Parser.ParseASMBlock(AScope: TObjectList<TCodeElement>);
var
LToken: TToken;
LBlock: TASMBlock;
LContent, LLine: string;
LVar: TVarDeclaration;
LLineNumber: Integer;
i: Integer;
begin
LBlock := TASMBlock.Create('');
LLine := '';
LLineNumber := -1;
AScope.Add(LBlock);
// LBlock.Line := FLexer.PeekToken.FoundInLine;
FLexer.GetToken('asm');
while not ((FLexer.PeekToken.IsContent('end') and (not FLexer.PeekToken.FollowedByNewLine) and FLexer.AHeadToken.IsContent(';'))) do
begin
if FLexer.PeekToken.IsContent(';') then
begin
while not FLexer.EOF do
begin
LToken := FLexer.GetToken();
if LToken.FollowedByNewLine then
begin
Break;
end;
end;
if FLexer.EOF then
begin
Break;
end;
Continue;
end;
if LBlock.Line = -1 then
begin
LBlock.Line := FLexer.PeekToken.FoundInLine-1;
end;
if LLineNumber = -1 then
begin
LLineNumber := FLexer.PeekToken.FoundInLine - LBlock.Line;
end;
LToken := FLexer.GetToken();
LContent := LToken.Content;
if LToken.IsType(ttIdentifier) and (AnsiIndexText(LContent, ['a', 'b', 'c', 'x', 'y', 'z', 'i', 'j']) < 0) then
begin
LVar := TVarDeclaration(GetElement(LContent, TVarDeclaration));
if Assigned(LVar) then
begin
LContent := LVar.GetAccessIdentifier();
if ((LVar.ParamIndex < 1) or (LVar.ParamIndex > 3)) and (LVar.IsParameter or LVar.IsLocal)
and (not FLexer.PeekToken.IsContent(']')) then
begin
LContent := '[' + LContent + ']';
end;
end;
end
else
begin
if LToken.IsType(ttCharLiteral) then
begin
LContent := '"' + LContent + '"';
end;
end;
LLine := LLine + LContent + ' ';
if LToken.FollowedByNewLine or ((not FLexer.EOF) and FLexer.PeekToken.IsContent(';')) then
begin
if LBlock.Source.Count > 0 then
begin
for i := 1 to LLineNumber - (LBlock.Source.Count + 1) do
begin
LBlock.Source.Add('');
end;
end;
LBlock.Source.Add(LLine);
LLine := '';
LLineNumber := -1;
end;
end;
if LLine <> '' then
begin
for i := 1 to LLineNumber - (LBlock.Source.Count + 1) do
begin
LBlock.Source.Add('');
end;
LBlock.Source.Add(LLine);
end;
FLexer.GetToken('end');
FLexer.GetToken(';');
end;
function TD16Parser.ParseAssignment(AScope: TObjectList<TCodeElement>; AIncludeEndmark: Boolean = True): TDataType;
var
LAssignment: TAssignment;
LRelType: TDataType;
LOverride: TDataType;
begin
LOverride := nil;
LAssignment := TAssignment.Create();
LAssignment.Line := FLexer.PeekToken.FoundInLine;
if FLexer.PeekToken.IsType(ttIdentifier) and FLexer.AHeadToken.IsContent('(') then
begin
LOverride := GetDataType(FLexer.GetToken('', ttIdentifier).Content);
FLexer.GetToken('(');
end;
LAssignment.TargetVar := GetVar(FLexer.GetToken('', ttIdentifier).Content);
if LAssignment.TargetVar.IsConst then
begin
Error('Cannot assign to a const value');
end;
AScope.Add(LAssignment);
if FLexer.PeekToken.IsContent('^') then
begin
LAssignment.Dereference := True;
FLexer.GetToken();
end;
Result := LAssignment.TargetVar.DataType;
if Assigned(LOverride) then
begin
FLexer.GetToken(')');
Result := LOverride;
end;
if LAssignment.Dereference then
begin
Result := Result.BaseType;
end;
Result := ParsePostExpression(Result, LAssignment.Modifiers, LAssignment.ModifierMax);
LAssignment.TargetType := Result;
FLexer.GetToken(':=');
LRelType := ParseRelation(LAssignment.SubElements);
LAssignment.SourceType := LRelType;
if AIncludeEndmark then
begin
FLexer.GetToken(';');
end;
if (LRelType.RawType <> Result.RawType) or ((Result.RawType = rtArray) and (not SameText(Result.Name, LRelType.Name))) then
begin
if not ((LRelType = FDummies.DataType) or (Result = FDummies.DataType)) then
begin
Error('Cannot assign ' + QuotedStr(LRelType.Name) + ' to ' + QuotedStr(Result.Name));
end;
end;
end;
procedure TD16Parser.ParseCase(AScope: TObjectList<TCodeElement>);
var
LCase: TCase;
LFactor: TFactor;
LRepeat: Boolean;
begin
LCase := TCase.Create();
AScope.Add(LCase);
LRepeat := False;
while (not FLexer.PeekToken.IsContent(':')) or LRepeat do
begin
LRepeat := False;
if ParseFactor(LCase.ConstValues).RawType <> rtUInteger then
begin
Error('value must be of type unsigned Integer');
end;
LFactor := TFactor(LCase.ConstValues.Items[LCase.ConstValues.Count-1]);
if (not LFactor.IsConstant) and ((Assigned(LFactor.VarDeclaration) and (not LFactor.VarDeclaration.IsConst))) then
begin
Error('Value must be of type const');
end;
if (LFactor.SubElements.Count > 0) or LFactor.Inverse or LFactor.GetAdress or LFactor.Dereference then
begin
Error('illegal statement');
end;
if FLexer.PeekToken.IsContent(',') then
begin
FLexer.GetToken(',');
LRepeat := True;
end;
end;
FLexer.GetToken(':');
FLexer.GetToken('begin');
ParseRoutineContent(LCase.SubElements);
FLexer.GetToken('end');
FLexer.GetToken(';');
end;
procedure TD16Parser.ParseCaseStatement(AScope: TObjectList<TCodeElement>);
var
LStatement: TCaseStatement;
begin
LStatement := TCaseStatement.Create();
AScope.Add(LStatement);
LStatement.Line := FLexer.PeekToken.FoundInLine;
FLexer.GetToken('case');
if ParseRelation(LStatement.Relation).RawType <> rtUInteger then
begin
Error('Relation of Case requires returntype of unsigned integer');
end;
FLexer.GetToken('of');
while (not FLexer.PeekToken.IsContent('end')) and (not FLexer.PeekToken.IsContent('else')) do
begin
ParseCase(LStatement.Cases);
end;
if FLexer.PeekToken.IsContent('else') then
begin
FLexer.GetToken('else');
FLexer.GetToken('begin');
ParseRoutineContent(LStatement.ElseCase);
FLexer.GetToken('end');
FLexer.GetToken(';');
end;
FLexer.GetToken('end');
FLexer.GetToken(';');
end;
procedure TD16Parser.ParseCondition(AScope: TObjectList<TCodeElement>);
var
LCondition: TCondition;
begin
LCondition := TCondition.Create();
AScope.Add(LCondition);
LCondition.Line := FLexer.PeekToken.FoundInLine;
FLexer.GetToken('if');
ParseRelation(LCondition.Relation);
FLexer.GetToken('then');
FLexer.GetToken('begin');
ParseRoutineContent(LCondition.SubElements);
FLexer.GetToken('end');
if FLexer.PeekToken.IsContent('else') then
begin
FLexer.GetToken('else');
FLexer.GetToken('begin');
ParseRoutineContent(LCondition.ElseElements);
FLexer.GetToken('end');
end;
FLexer.GetToken(';');
end;
function TD16Parser.ParseConstantFactor(AFactor: TFactor): TDataType;
var
LTempID, LContent, LString, LData: string;
begin
if FLexer.PeekToken.IsType(ttCharLiteral) then
begin
Result := GetDataType('string');
LTempID := FCurrentUnit.GetUniqueID('str');
AFactor.Value := LTempID;
LContent := FLexer.GetToken().Content;
LString := StringReplace(LContent,'\n', '",10,"',[]);
LData := ':' + LTempID + ' dat ' + IntToStr(Length(LString)) + ', ' + '"' + LString + '"';
FCurrentUnit.FooterSource.Add(LData);
end
else
begin
AFactor.Value := FLexer.GetToken().Content;
Result := GetDataType('word');
end;
end;
procedure TD16Parser.ParseConsts;
begin
FLexer.GetToken('const');
while not FLexer.PeekToken.IsType(ttReserved) do
begin
ParseVarDeclaration(AScope, True, False, False, True);
end;
end;
function TD16Parser.ParseExpression(AScope: TObjectList<TCodeElement>): TDataType;
var
LExpression: TExpression;
LLastResult: TDataType;
LOperation: TOperation;
LOperator: string;
begin
LExpression := TExpression.Create();
AScope.Add(LExpression);
Result := ParseTerm(LExpression.SubElements);
while FLexer.PeekToken.IsType(ttTermOp) do
begin
LOperator := FLexer.GetToken().Content;
//LExpression.Operators.Add(LOperation);
LLastResult := Result;
Result := ParseTerm(LExpression.SubElements);
LOperation := GetOperation(LOperator, LLastResult, Result);
Result := LOperation.ResultType;
LExpression.Operations.Add(LOperation);
end;
end;
function TD16Parser.ParseFactor(AScope: TObjectList<TCodeElement>): TDataType;
var
LFactor: TFactor;
LInverse: Boolean;
LOverride: TDataType;
begin
LInverse := False;
LOverride := nil;
LFactor := TFactor.Create();
AScope.Add(LFactor);
while FLexer.PeekToken.IsContent('not') do
begin
FLexer.GetToken('not');
LInverse := not LInverse;
end;
if FLexer.PeekToken.IsType(ttIdentifier)
and FLexer.AHeadToken.IsContent('(')
and not Assigned(GetElement(FLexer.PeekToken.Content, TProcDeclaration))
then
begin
LOverride := GetDataType(FLexer.GetToken('', ttIdentifier).Content);
end;
if FLexer.PeekToken.IsContent('(') then
begin
FLexer.GetToken('(');
Result := ParseRelation(LFactor.SubElements, LInverse);
if Result.RawType = rtBoolean then
begin
LInverse := False; //in case it was true when parsing the relation, the value of the relation is already inversed
end;
if Assigned(LOverride) then
begin
Result := LOverride;
end;
FLexer.GetToken(')');
end
else
begin
if Assigned(GetElement(FLexer.PeekToken.Content, TProcDeclaration)) then
begin
Result := ParseRoutineCall(LFactor.SubElements, False);
end
else
begin
if FLexer.PeekToken.IsType(ttNumber) or FLexer.PeekToken.IsType(ttCharLiteral) then
begin
Result := ParseConstantFactor(LFactor);
end
else
begin
if FLexer.PeekToken.IsContent('@') then
begin
FLexer.GetToken('@');
LFactor.GetAdress := True;
end;
LFactor.VarDeclaration := GetVar(FLexer.GetToken('', ttIdentifier).Content);
Result := LFactor.VarDeclaration.DataType;
if LFactor.GetAdress and LFactor.VarDeclaration.IsParameter then
begin
Error('Cannot receive adress of Paremeter ' + QuotedStr(LFactor.VarDeclaration.Name));
end;
end;
end;
end;
LFactor.Inverse := LInverse;
if FLexer.PeekToken.IsContent('^') then
begin
FLexer.GetToken();
LFactor.Dereference := True;
if LFactor.GetAdress then
begin
Error('Cannot get the adress of ' + QuotedStr(LFactor.VarDeclaration.Name) + ' and dereference it at the same time');
end;
end;
if LFactor.Dereference then
begin
Result := Result.BaseType;
end;
Result := ParsePostExpression(Result, LFactor.Modifiers, LFactor.ModifierMax);
if LFactor.GetAdress then
begin
Result := GetDataType('pointer');
end;
end;
procedure TD16Parser.ParseForLoop(AScope: TObjectList<TCodeElement>);
var
LFor: TForLoop;
begin
LFor := TForLoop.Create();
AScope.Add(LFor);
LFor.Line := FLexer.PeekToken.FoundInLine;
FLexer.GetToken('for');
if ParseAssignment(LFor.Assignment, False).RawType <> rtUInteger then
begin
Error('assignment must be of type unsigned integer');
end;
FLexer.GetToken('to');
if ParseRelation(LFor.Relation).RawType <> rtUInteger then
begin
Error('Result of relation must be of type unsigned integer');
end;
FLexer.GetToken('do');
FLexer.GetToken('begin');
ParseRoutineContent(LFor.SubElements);
FLexer.GetToken('end');
FLexer.GetToken(';');
end;
procedure TD16Parser.ParseProgram;
begin
FCurrentSectionType := tsImplementation;
ParseProgramHeader();
if Units.CountUnitName(FCurrentUnit.Name) > 1 then
begin
FUnits.Delete(FUnits.IndexOf(FCurrentUnit));
Exit();
end;
ParseUnitSectionContent(FCurrentUnit.ImplementationSection);
FLexer.GetToken('begin');
ParseRoutineContent(FCurrentUnit.InitSection);
ParseUnitFooter();
end;
procedure TD16Parser.ParseProgramHeader;
begin
FLexer.GetToken('program', ttReserved);
FCurrentUnit.Name := FLexer.GetToken('', ttIdentifier).Content;
FLexer.GetToken.MatchContent(';');
end;
function TD16Parser.ParseRelation(AScope: TObjectList<TCodeElement>; ATryInverse: Boolean = False): TDataType;
var
LRelation: TRelation;
LLastResult: TDataType;
LOperator: string;
LOperation: TOperation;
begin
//tryinverse indicates, that wem may already inverse the result if we can
//we can inverse the result, when the result is of type bool
//if its not bool, it might be, that the result is going to be dereferenced too
//in this case we dont inverse here, which is then handled by the top layer(parsefactor)
//this is required to not mix up the order.
//first dereference THEN inverse it. Always inversing at this position might screw this
//as we dereference AFTER inversing if this is not a bool
LRelation := TRelation.Create();
AScope.Add(LRelation);
Result := ParseExpression(LRelation.SubElements);
if FLexer.PeekToken.IsType(ttRelOp) then
begin
LOperator := FLexer.GetToken().Content;
LLastResult := Result;
Result := ParseExpression(LRelation.SubElements);
LOperation := GetOperation(LOperator, LLastResult, Result);
LRelation.Operations.Add(LOperation);
Result := LOperation.ResultType;
LRelation.Inverse := ATryInverse;//we may inverse here already, since since its not possible to dereference a bool value
end;
end;
procedure TD16Parser.ParseRepeatLoop(AScope: TObjectList<TCodeElement>);
var
LLoop: TRepeatLoop;
begin
LLoop := TRepeatLoop.Create();
AScope.Add(LLoop);
LLoop.Line := FLexer.PeekToken.FoundInLine;
FLexer.GetToken('repeat');
ParseRoutineContent(LLoop.SubElements);
FLexer.GetToken('until');
ParseRelation(LLoop.Relation);
FLexer.GetToken(';');
end;
function TD16Parser.ParseRoutineCall(AScope: TObjectList<TCodeElement>; AIncludeEndMark: Boolean = True): TDataType;
var
LCall: TProcCall;
i: Integer;
LParamType: TDataType;
begin
Result := FDummies.DataType;
LCall := TProcCall.Create();
AScope.Add(LCall);
LCall.Line := FLexer.PeekToken.FoundInLine;
LCall.ProcDeclaration := TProcDeclaration(ExpectElement(FLexer.GetToken().Content, TProcDeclaration));
if LCall.ProcDeclaration.IsFunction then
begin
Result := LCall.ProcDeclaration.ResultType;
LCall.IgnoreResult := AIncludeEndMark;//if we are not in an expression, returnvalue must be ignored or we screw the stack
end
else
begin
if not AIncludeEndMark then //we are inside an expression, no procedure allowed
begin
Fatal('Procedure not allowed inside an expression');
end;
end;
FLexer.GetToken('(');
for i := 0 to LCall.ProcDeclaration.Parameters.Count - 1 do
begin
LParamType := ParseRelation(LCall.Parameters);
if LParamType.RawType <> TVarDeclaration(LCall.ProcDeclaration.Parameters.Items[i]).DataType.RawType then
begin
if not ((LParamType = FDummies.DataType)
or (TVarDeclaration(LCall.ProcDeclaration.Parameters.Items[i]).DataType = FDummies.DataType))then
begin
Error('Cannot assign ' + QuotedStr(LParamType.Name) +
' to ' + QuotedStr(TVarDeclaration(LCall.ProcDeclaration.Parameters.Items[i]).Name));
end;
end;
if i < LCall.ProcDeclaration.Parameters.Count - 1 then
begin
FLexer.GetToken(',');
end;
end;
FLexer.GetToken(')');
if AIncludeEndMark then
begin
FLexer.GetToken(';');
end;
end;
procedure TD16Parser.ParseRoutineContent(AScope: TObjectList<TCodeElement>);
var
LElement: TCodeElement;
begin
while not FLexer.PeekToken.IsContent('end') do
begin
case FLexer.PeekToken.TokenType of
ttIdentifier, ttReserved:
begin
case AnsiIndexText(FLexer.PeekToken.Content, ['if', 'while', 'repeat', 'asm', 'for', 'case']) of
0:
begin
ParseCondition(AScope);
end;
1:
begin
ParseWhileLoop(AScope);
end;
2:
begin
ParseRepeatLoop(AScope);
end;
3:
begin
ParseASMBlock(AScope);
end;
4:
begin
ParseForLoop(AScope);
end;
5:
begin
ParseCaseStatement(AScope);
end
else
LElement := ExpectElement(FLexer.PeekToken.Content, TCodeElement);
if LElement is TProcDeclaration then
begin
ParseRoutineCall(AScope);
end
else
begin
ParseAssignment(AScope);
end;
end;
end;
else
Break;
end;
end;
end;
procedure TD16Parser.ParseRoutineDeclaration;
var
LProc, LDummy: TProcDeclaration;
LIsFunction: Boolean;
begin
if FLexer.PeekToken.IsContent('function') then
begin
LIsFunction := True;
FLexer.GetToken('function');
end
else
begin
LIsFunction := False;
FLexer.GetToken('procedure');
end;
LProc := TProcDeclaration.Create(FLexer.GetToken('', ttIdentifier).Content);
LProc.Line := GetCurrentLine();
FCurrentProc := LProc;
AScope.Add(LProc);
ParseRoutineParameters(LProc.Parameters);
if LIsFunction then
begin
FLexer.GetToken(':');
LProc.ResultType := GetDataType(FLexer.GetToken('', ttIdentifier).Content);
if LProc.ResultType.RawType = rtArray then
begin
Error('You can not use a static array as resulttype');
end;