-
Notifications
You must be signed in to change notification settings - Fork 1
/
btpc64.pas
2972 lines (2830 loc) · 84 KB
/
btpc64.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
(******************************************************************************
* BeRoTinyPascal *
******************************************************************************
* A self-hosting capable tiny pascal compiler for the Win32 x86 platform *
******************************************************************************
* Version 2016-06-22-18-07-0000 *
******************************************************************************
* zlib license *
*============================================================================*
* *
* Copyright (C) 2006-2016, Benjamin Rosseaux (benjamin@rosseaux.com) *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement in the product documentation would be *
* appreciated but is not required. *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
* *
******************************************************************************
* General guidelines for code contributors *
*============================================================================*
* *
* 1. Make sure you are legally allowed to make a contribution under the zlib *
* license. *
* 2. The zlib license header goes at the top of each source file, with *
* appropriate copyright notice. *
* 3. After a pull request, check the status of your pull request on *
http://github.com/BeRo1985/berotinypascal . *
* 4. Write code, which is compatible with Delphi 7-XE7 and FreePascal >= 3.0 *
* and even with BeRoTinyPascal itself, so don't use generics/templates, *
* operator overloading and another newer syntax features than Delphi 7 *
* and BeRoTinyPascal have support for that. *
* 5. Don't use any libraries/units except the RTL system unit functions *
* +. Make sure the code compiles with Delphi 7, FreePascal >= 3.0 and with *
* BeRoTinyPascal itself. *
* *
******************************************************************************)
program BTPC; { BeRoTinyPascalCompiler }
{$ifdef fpc}
{$mode delphi}
{$endif}
{$ifdef Win32}
{$define Windows}
{$endif}
{$ifdef Win64}
{$define Windows}
{$endif}
{$ifdef WinCE}
{$define Windows}
{$endif}
{$ifdef Windows}
{$apptype console}
{$endif}
const MaximalCodeSize=262144;
MaximalIdentifiers=512;
MaximalTypes=32;
MaximalList=10;
MaximalAlfa=20;
MaximalStringLength=255;
MaximalCases=256;
OPNone=-1;
OPAdd=0;
OPNeg=1;
OPMul=2;
OPDivD=3;
OPRemD=4;
OPDiv2=5;
OPRem2=6;
OPEqlI=7;
OPNEqI=8;
OPLssI=9;
OPLeqI=10;
OPGtrI=11;
OPGEqI=12;
OPDupl=13;
OPSwap=14;
OPAndB=15;
OPOrB=16;
OPLoad=17;
OPStore=18;
OPHalt=19;
OPWrI=20;
OPWrC=21;
OPWrL=22;
OPRdI=23;
OPRdC=24;
OPRdL=25;
OPEOF=26;
OPEOL=27;
OPLdC=28;
OPLdA=29;
OPLdLA=30;
OPLdL=31;
OPLdG=32;
OPStL=33;
OPStG=34;
OPMove=35;
OPCopy=36;
OPAddC=37;
OPMulC=38;
OPJmp=39;
OPJZ=40;
OPCall=41;
OPAdjS=42;
OPExit=43;
TokIdent=0;
TokNumber=1;
TokStrC=2;
TokPlus=3;
TokMinus=4;
TokMul=5;
TokLBracket=6;
TokRBracket=7;
TokColon=8;
TokEql=9;
TokNEq=10;
TokLss=11;
TokLEq=12;
TokGtr=13;
TokGEq=14;
TokLParent=15;
TokRParent=16;
TokComma=17;
TokSemi=18;
TokPeriod=19;
TokAssign=20;
SymBEGIN=21;
SymEND=22;
SymIF=23;
SymTHEN=24;
SymELSE=25;
SymWHILE=26;
SymDO=27;
SymCASE=28;
SymREPEAT=29;
SymUNTIL=30;
SymFOR=31;
SymTO=32;
SymDOWNTO=33;
SymNOT=34;
SymDIV=35;
SymMOD=36;
SymAND=37;
SymOR=38;
SymCONST=39;
SymVAR=40;
SymTYPE=41;
SymARRAY=42;
SymOF=43;
SymPACKED=44;
SymRECORD=45;
SymPROGRAM=46;
SymFORWARD=47;
SymHALT=48;
SymFUNC=49;
SymPROC=50;
IdCONST=0;
IdVAR=1;
IdFIELD=2;
IdTYPE=3;
IdFUNC=4;
KindSIMPLE=0;
KindARRAY=1;
KindRECORD=2;
TypeINT=1;
TypeBOOL=2;
TypeCHAR=3;
TypeSTR=4;
FunCHR=0;
FunORD=1;
FunWRITE=2;
FunWRITELN=3;
FunREAD=4;
FunREADLN=5;
FunEOF=6;
FunEOFLN=7;
type TAlfa=array[1..MaximalAlfa] of char;
TIdent=record
Name:TAlfa;
Link:integer;
TypeDefinition:integer;
Kind:integer;
Value:integer;
VariableLevel:integer;
VariableAddress:integer;
ReferencedParameter:boolean;
Offset:integer;
FunctionLevel:integer;
FunctionAddress:integer;
LastParameter:integer;
ReturnAddress:integer;
Inside:boolean;
end;
TType=record
Size:integer;
Kind:integer;
StartIndex:integer;
EndIndex:integer;
SubType:integer;
Fields:integer;
end;
var CurrentChar:char;
CurrentColumn:integer;
CurrentLine:integer;
CurrentSymbol:integer;
CurrentIdentifer:TAlfa;
CurrentNumber:integer;
CurrentString:array[1..255] of char;
CurrentStringLength:integer;
FunctionDeclarationIndex:integer;
Keywords:array[SymBEGIN..SymPROC] of TAlfa;
LastOpcode:integer;
CurrentLevel:integer;
IsLabeled:boolean;
SymbolNameList:array[-1..MaximalList] of integer;
IdentifierPosition:integer;
TypePosition:integer;
Identifiers:array[0..MaximalIdentifiers] of TIdent;
Types:array[1..MaximalTypes] of TType;
Code:array[0..MaximalCodeSize] of integer;
CodePosition:integer;
StackPosition:integer;
function StringCompare(var s1,s2:TAlfa):boolean;
var f:boolean;
i:integer;
begin
f:=true;
i:=1;
while f and (i<=MaximalAlfa) do begin
f:=(s1[i]=s2[i]);
i:=i+1;
end;
StringCompare:=f;
end;
procedure StringCopy(var Dest:TAlfa;Src:TAlfa);
begin
Dest:=Src;
end;
procedure Error(n:integer);
begin
Write('Error ',n:1,': ');
case n of
TokIdent:begin
Write('Identifier expected');
end;
TokNumber:begin
Write('Number expected');
end;
TokStrC:begin
Write('String expected');
end;
TokPlus:begin
Write('"+" expected');
end;
TokMinus:begin
Write('"-" expected');
end;
TokMul:begin
Write('"*" expected');
end;
TokLBracket:begin
Write('"[" expected');
end;
TokRBracket:begin
Write('"]" expected');
end;
TokColon:begin
Write('":" expected');
end;
TokEql:begin
Write('"=" expected');
end;
TokNEq:begin
Write('"<>" expected');
end;
TokLss:begin
Write('"<" expected');
end;
TokLEq:begin
Write('"<=" expected');
end;
TokGtr:begin
Write('">" expected');
end;
TokGEq:begin
Write('">=" expected');
end;
TokLParent:begin
Write('"(" expected');
end;
TokRParent:begin
Write('")" expected');
end;
TokComma:begin
Write('"," expected');
end;
TokSemi:begin
Write('";" expected');
end;
TokPeriod:begin
Write('"." expected');
end;
TokAssign:begin
Write('":=" expected');
end;
SymBEGIN:begin
Write('"begin" expected');
end;
SymEND:begin
Write('"end" expected');
end;
SymIF:begin
Write('"if" expected');
end;
SymTHEN:begin
Write('"then" expected');
end;
SymELSE:begin
Write('"else" expected');
end;
SymWHILE:begin
Write('"else" expected');
end;
SymDO:begin
Write('"do" expected');
end;
SymCASE:begin
Write('"case" expected');
end;
SymREPEAT:begin
Write('"repeat" expected');
end;
SymUNTIL:begin
Write('"until" expected');
end;
SymFOR:begin
Write('"for" expected');
end;
SymTO:begin
Write('"to" expected');
end;
SymDOWNTO:begin
Write('"downto" expected');
end;
SymNOT:begin
Write('"not" expected');
end;
SymDIV:begin
Write('"div" expected');
end;
SymMOD:begin
Write('"mod" expected');
end;
SymAND:begin
Write('"and" expected');
end;
SymOR:begin
Write('"or" expected');
end;
SymCONST:begin
Write('"const" expected');
end;
SymVAR:begin
Write('"var" expected');
end;
SymTYPE:begin
Write('"type" expected');
end;
SymARRAY:begin
Write('"array" expected');
end;
SymOF:begin
Write('"of" expected');
end;
SymPACKED:begin
Write('"packed" expected');
end;
SymRECORD:begin
Write('"record" expected');
end;
SymPROGRAM:begin
Write('"program" expected');
end;
SymFORWARD:begin
Write('"forward" expected');
end;
SymHALT:begin
Write('"halt" expected');
end;
SymFUNC:begin
Write('"function" expected');
end;
SymPROC:begin
Write('"procedure" expected');
end;
100:begin
Write('String literal must be closed');
end;
101:begin
Write('String is empty');
end;
102:begin
Write('Bad char');
end;
103:begin
Write('Too many identifiers');
end;
104:begin
Write('Duplicate identifier');
end;
105:begin
Write('Duplicate procedure/function');
end;
106:begin
Write('Unknown identifiers');
end;
107:begin
Write('Invalid type');
end;
108:begin
Write('Record type expected');
end;
109:begin
Write('Unknown field');
end;
110:begin
Write('Array type expected');
end;
111:begin
Write('Non-writeable type');
end;
112:begin
Write('Non-readable type');
end;
113:begin
Write('Too many argumnts');
end;
114:begin
Write('Passing string to var argument isn''t allowed');
end;
115:begin
Write('Passing string to non-array argument isn''t allowed');
end;
116:begin
Write('Passing string to non-char-array argument isn''t allowed');
end;
117:begin
Write('Passing string to wrong sized char-array argument isn''t allowed');
end;
118:begin
Write('Too few argumnts');
end;
119:begin
Write('Procedure calls inside a expression aren''t allowed');
end;
120:begin
Write('Type inside a expression isn''t allowed');
end;
121:begin
Write('Expression expected');
end;
122:begin
Write('Illegal assigning to function');
end;
123:begin
Write('Illegal assigning to constant or type');
end;
124:begin
Write('Case expression must be constant');
end;
125:begin
Write('Case expression expected');
end;
126:begin
Write('":" expected');
end;
127:begin
Write('Variable after "FOR" expected');
end;
128:begin
Write('Incorrect iterator type');
end;
129:begin
Write('"TO" or "DOWNTO" expected');
end;
130:begin
Write('Constant expected');
end;
131:begin
Write('Identifier or number literal expected');
end;
132:begin
Write('First index of array must be less or equal then last');
end;
133:begin
Write('Type expected');
end;
134:begin
Write('Too many types');
end;
135:begin
Write('Too many nested records');
end;
136:begin
Write('Too many nested procedures');
end;
137:begin
Write('Invalid function return type');
end;
138:begin
Write('Too many arguments then at forward declaration');
end;
139:begin
Write('Argument name doesn''t match forward declaration');
end;
140:begin
Write('Argument type doesn''t match forward declaration');
end;
141:begin
Write('Argument var doesn''t match forward declaration');
end;
142:begin
Write('Too less arguments then at forward declaration');
end;
143:begin
Write('Already forward declared');
end;
144:begin
Write('No definition for forward declared');
end;
145:begin
Write('Internal negative retn');
end;
146:begin
Write('Binary is too big');
end;
147:begin
Write('String is too long');
end;
148:begin
Write('Too many cases');
end;
149:begin
Write('Too large code');
end;
150:begin
Write('Incompatible types');
end;
end;
WriteLn(' at line ',CurrentLine:1,' at column ',CurrentColumn:1);
Halt;
end;
procedure ReadChar;
begin
if not EOF then begin
read(CurrentChar);
CurrentColumn:=CurrentColumn+1;
if CurrentChar=#10 then begin
CurrentLine:=CurrentLine+1;
CurrentColumn:=0;
end;
end else begin
CurrentChar:=#0;
end;
end;
function ReadNumber:integer;
var Num:integer;
begin
Num:=0;
if ('0'<=CurrentChar) and (CurrentChar<='9') then begin
while ('0'<=CurrentChar) and (CurrentChar<='9') do begin
Num:=(Num*10)+(ord(CurrentChar)-ord('0'));
ReadChar;
end;
end else if CurrentChar='$' then begin
ReadChar;
while (('0'<=CurrentChar) and (CurrentChar<='9')) or
(('a'<=CurrentChar) and (CurrentChar<='f')) or
(('A'<=CurrentChar) and (CurrentChar<='F')) do begin
if ('0'<=CurrentChar) and (CurrentChar<='9') then begin
Num:=(Num*16)+(ord(CurrentChar)-ord('0'));
end else if ('a'<=CurrentChar) and (CurrentChar<='f') then begin
Num:=(Num*16)+(ord(CurrentChar)-ord('a')+10);
end else if ('A'<=CurrentChar) and (CurrentChar<='F') then begin
Num:=(Num*16)+(ord(CurrentChar)-ord('A')+10);
end;
ReadChar;
end;
end;
ReadNumber:=Num;
end;
procedure GetSymbol;
var k,s:integer;
StrEnd,InStr:boolean;
LastChar:char;
begin
while (CurrentChar>#0) and (CurrentChar<=' ') do begin
ReadChar;
end;
if (('a'<=CurrentChar) and (CurrentChar<='z')) or (('A'<=CurrentChar) and (CurrentChar<='Z')) then begin
k:=0;
while ((('a'<=CurrentChar) and (CurrentChar<='z')) or (('A'<=CurrentChar) and (CurrentChar<='Z')) or (('0'<=CurrentChar) and (CurrentChar<='9'))) or (CurrentChar='_') do begin
if k<>MaximalAlfa then begin
k:=k+1;
if ('a'<=CurrentChar) and (CurrentChar<='z') then begin
CurrentChar:=chr(ord(CurrentChar)-32);
end;
CurrentIdentifer[k]:=CurrentChar;
end;
ReadChar;
end;
while k<>MaximalAlfa do begin
k:=k+1;
CurrentIdentifer[k]:=' ';
end;
CurrentSymbol:=TokIdent;
s:=SymBEGIN;
while s<=SymPROC do begin
if StringCompare(Keywords[s],CurrentIdentifer) then begin
CurrentSymbol:=s;
end;
s:=s+1;
end;
end else if (('0'<=CurrentChar) and (CurrentChar<='9')) or (CurrentChar='$') then begin
CurrentSymbol:=TokNumber;
CurrentNumber:=ReadNumber;
end else if CurrentChar=':' then begin
ReadChar;
if CurrentChar='=' then begin
ReadChar;
CurrentSymbol:=TokAssign;
end else begin
CurrentSymbol:=TokColon;
end;
end else if CurrentChar='>' then begin
ReadChar;
if CurrentChar='=' then begin
ReadChar;
CurrentSymbol:=TokGEq;
end else begin
CurrentSymbol:=TokGtr;
end;
end else if CurrentChar='<' then begin
ReadChar;
if CurrentChar='=' then begin
ReadChar;
CurrentSymbol:=TokLEq;
end else if CurrentChar='>' then begin
ReadChar;
CurrentSymbol:=TokNEq;
end else begin
CurrentSymbol:=TokLss;
end;
end else if CurrentChar='.' then begin
ReadChar;
if CurrentChar='.' then begin
ReadChar;
CurrentSymbol:=TokColon;
end else begin
CurrentSymbol:=TokPeriod
end;
end else if (CurrentChar='''') or (CurrentChar='#') then begin
CurrentStringLength:=0;
StrEnd:=false;
InStr:=false;
CurrentSymbol:=TokStrC;
while not StrEnd do begin
if InStr then begin
if CurrentChar='''' then begin
ReadChar;
if CurrentChar='''' then begin
if CurrentStringLength=MaximalStringLength then begin
Error(147);
end;
CurrentStringLength:=CurrentStringLength+1;
CurrentString[CurrentStringLength]:=CurrentChar;
ReadChar;
end else begin
InStr:=false;
end;
end else if (CurrentChar=#13) or (CurrentChar=#10) then begin
Error(100);
StrEnd:=true;
end else begin
if CurrentStringLength=MaximalStringLength then begin
Error(147);
end;
CurrentStringLength:=CurrentStringLength+1;
CurrentString[CurrentStringLength]:=CurrentChar;
ReadChar;
end;
end else begin
if CurrentChar='''' then begin
InStr:=true;
ReadChar;
end else if CurrentChar='#' then begin
ReadChar;
if CurrentStringLength=MaximalStringLength then begin
Error(147);
end;
CurrentStringLength:=CurrentStringLength+1;
CurrentString[CurrentStringLength]:=chr(ReadNumber);
end else begin
StrEnd:=true;
end;
end;
end;
if CurrentStringLength=0 then begin
Error(101);
end;
end else if CurrentChar='+' then begin
ReadChar;
CurrentSymbol:=TokPlus;
end else if CurrentChar='-' then begin
ReadChar;
CurrentSymbol:=TokMinus;
end else if CurrentChar='*' then begin
ReadChar;
CurrentSymbol:=TokMul;
end else if CurrentChar='(' then begin
ReadChar;
if CurrentChar='*' then begin
ReadChar;
LastChar:='-';
while (CurrentChar<>#0) and not ((CurrentChar=')') and (LastChar='*')) do begin
LastChar:=CurrentChar;
ReadChar;
end;
ReadChar;
GetSymbol;
end else begin
CurrentSymbol:=TokLParent;
end;
end else if CurrentChar=')' then begin
ReadChar;
CurrentSymbol:=TokRParent;
end else if CurrentChar='[' then begin
ReadChar;
CurrentSymbol:=TokLBracket;
end else if CurrentChar=']' then begin
ReadChar;
CurrentSymbol:=TokRBracket;
end else if CurrentChar='=' then begin
ReadChar;
CurrentSymbol:=TokEql;
end else if CurrentChar=',' then begin
ReadChar;
CurrentSymbol:=TokComma;
end else if CurrentChar=';' then begin
ReadChar;
CurrentSymbol:=TokSemi;
end else if CurrentChar='{' then begin
while (CurrentChar<>'}') and (CurrentChar<>#0) do begin
ReadChar;
end;
ReadChar;
GetSymbol;
end else if CurrentChar='/' then begin
ReadChar;
if CurrentChar='/' then begin
repeat
ReadChar;
until (CurrentChar=#10) or (CurrentChar=#0);
GetSymbol;
end else begin
Error(102);
end;
end else begin
Error(102);
end;
end;
procedure Check(s:integer);
begin
if CurrentSymbol<>s then begin
Error(s);
end;
end;
procedure Expect(s:integer);
begin
Check(s);
GetSymbol;
end;
procedure EnterSymbol(CurrentIdentifer:TAlfa;k,t:integer);
var j:integer;
begin
if IdentifierPosition=MaximalIdentifiers then begin
Error(103);
end;
IdentifierPosition:=IdentifierPosition+1;
Identifiers[0].Name:=CurrentIdentifer;
j:=SymbolNameList[CurrentLevel];
while not StringCompare(Identifiers[j].Name,CurrentIdentifer) do begin
j:=Identifiers[j].Link;
end;
if j<>0 then begin
if Identifiers[j].Kind<>IdFUNC then begin
Error(104);
end;
if (Code[Identifiers[j].FunctionAddress]<>OPJmp) or (Code[Identifiers[j].FunctionAddress+1]>0) then begin
Error(105);
end;
Identifiers[j].Name[1]:='$';
Code[Identifiers[j].FunctionAddress+1]:=CodePosition;
FunctionDeclarationIndex:=j;
end;
Identifiers[IdentifierPosition].Name:=CurrentIdentifer;
Identifiers[IdentifierPosition].Link:=SymbolNameList[CurrentLevel];
Identifiers[IdentifierPosition].TypeDefinition:=t;
Identifiers[IdentifierPosition].Kind:=k;
SymbolNameList[CurrentLevel]:=IdentifierPosition;
end;
function Position:integer;
var i,j:integer;
begin
Identifiers[0].Name:=CurrentIdentifer;
i:=CurrentLevel;
repeat
j:=SymbolNameList[i];
while not StringCompare(Identifiers[j].Name,CurrentIdentifer) do begin
j:=Identifiers[j].Link;
end;
i:=i-1;
until (i<-1) or (j<>0);
if j=0 then begin
Error(106);
end;
Position:=j;
end;
procedure EmitCode(Value:integer);
begin
if CodePosition>MaximalCodeSize then begin
Error(149);
end;
Code[CodePosition]:=Value;
CodePosition:=CodePosition+1;
end;
procedure EmitOpcode(Opcode,a:integer);
begin
case Opcode of
OPDupl,OPEOF,OPEOL,OPLdC,OPLdA,OPLdLA,OPLdL,OPLdG:begin
StackPosition:=StackPosition-4;
end;
OPNeg,OPDiv2,OPRem2,OPSwap,OPLoad,OPHalt,OPWrL,OPRdL,OpAddC,OPMulC,
OPJmp,OPCall,OPExit:begin
end;
OPAdd,OPMul,OPDivD,OPRemD,OPEqlI,OPNEqI,OPLssI,OPLeqI,OPGtrI,OPGEqI,OPAndB,
OPOrB,OPWrC,OPRdI,OPRdC,OPStL,OPStG,OPJZ:begin
StackPosition:=StackPosition+4;
end;
OPStore,OPWrI,OPMove:begin
StackPosition:=StackPosition+8;
end;
OPCopy:begin
StackPosition:=StackPosition-(a-4);
end;
OPAdjS:begin
StackPosition:=StackPosition+a;
end;
end;
if not ((((Opcode=OPAddC) or (Opcode=OPAdjS)) and (a=0)) or ((Opcode=OPMulC) and (a=1))) then begin
if IsLabeled then begin
Code[CodePosition]:=Opcode;
CodePosition:=CodePosition+1;
if Opcode>=OPLdC then begin
Code[CodePosition]:=a;
CodePosition:=CodePosition+1;
end;
IsLabeled:=false;
end else if (LastOpcode=OPLdC) and (Opcode=OPAdd) then begin
Code[CodePosition-2]:=OPAddC;
end else if (LastOpcode=OPLdC) and (Opcode=OPMul) then begin
Code[CodePosition-2]:=OPMulC;
end else if (LastOpcode=OPLdC) and (Opcode=OPNeg) then begin
Code[CodePosition-1]:=-Code[CodePosition-1];
Opcode:=LastOpcode;
end else if (LastOpcode=OPLdC) and (Code[CodePosition-1]=2) and (Opcode=OPDivD) then begin
Code[CodePosition-2]:=OPDiv2;
CodePosition:=CodePosition-1;
end else if (LastOpcode=OPLdC) and (Code[CodePosition-1]=2) and (Opcode=OPRemD) then begin
Code[CodePosition-2]:=OPRem2;
CodePosition:=CodePosition-1;
end else if (LastOpcode=OPLdA) and (Opcode=OPStore) then begin
Code[CodePosition-2]:=OPStG;
end else if (LastOpcode=OPLdA) and (Opcode=OPLoad) then begin
Code[CodePosition-2]:=OPLdG;
end else if (LastOpcode=OPLdLA) and (Opcode=OPStore) then begin
Code[CodePosition-2]:=OPStL;
end else if (LastOpcode=OPLdLA) and (Opcode=OPLoad) then begin
Code[CodePosition-2]:=OPLdL;
end else begin
EmitCode(Opcode);
if Opcode>=OPLdC then begin
EmitCode(a);
end;
end;
LastOpcode:=Opcode;
end;
end;
procedure EmitOpcode2(Opcode:integer);
begin
EmitOpcode(Opcode,0);
end;
function CodeLabel:integer;
begin
CodeLabel:=CodePosition;
IsLabeled:=true;
end;
procedure EmitAddress(Level,Address:integer);
begin
if Level=0 then begin
EmitOpcode(OPLdA,Address);
end else if Level=CurrentLevel then begin
EmitOpcode(OPLdLA,Address-StackPosition);
end else begin
EmitOpcode(OPLdL,-StackPosition);
while Level+1<>CurrentLevel do begin
EmitOpcode2(OPLoad);
Level:=Level+1;
end;
EmitOpcode(OPAddC,Address);
end;
end;
procedure EmitAddressVar(IdentifierIndex:integer);
begin
EmitAddress(Identifiers[IdentifierIndex].VariableLevel,Identifiers[IdentifierIndex].VariableAddress);
if Identifiers[IdentifierIndex].ReferencedParameter then begin
EmitOpcode2(OPLoad);
end;
end;
procedure MustBe(x,y:integer);
begin
if x<>y then begin
if (Types[x].Kind=KindARRAY) and (Types[y].Kind=KindARRAY) and (Types[x].StartIndex=Types[y].StartIndex) and (Types[x].EndIndex=Types[y].EndIndex) then begin
MustBe(Types[x].SubType,Types[y].SubType);
end else begin
Error(107);
end;
end;
end;
procedure Expression(var x:integer); forward;
procedure Selector(var t,IdentifierIndex:integer);
var j,x:integer;
begin
t:=Identifiers[IdentifierIndex].TypeDefinition;
GetSymbol;
if (CurrentSymbol=TokPeriod) or (CurrentSymbol=TokLBracket) then begin
EmitAddressVar(IdentifierIndex);
IdentifierIndex:=0;
while (CurrentSymbol=TokPeriod) or (CurrentSymbol=TokLBracket) do begin
case CurrentSymbol of
TokPeriod:begin
if Types[t].Kind<>KindRECORD then begin
Error(108);
end;
GetSymbol;
Check(TokIdent);
j:=Types[t].Fields;
Identifiers[0].Name:=CurrentIdentifer;
while not StringCompare(Identifiers[j].Name,CurrentIdentifer) do begin
j:=Identifiers[j].Link;