-
Notifications
You must be signed in to change notification settings - Fork 28
/
lpcompiler.pas
5569 lines (4830 loc) · 200 KB
/
lpcompiler.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
{
Author: Niels A.D
Project: Lape (https://github.com/nielsAD/lape)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
Script compiler, which turns a script into a syntax tree.
}
unit lpcompiler;
{$I lape.inc}
interface
uses
Classes, SysUtils,
lptypes, lpvartypes, lpparser, lptree;
const
ParserToken_ExpressionEnd = [tk_sym_SemiColon];
ParserToken_BlockEnd = [tk_kw_End, tk_kw_Finally, tk_kw_Except, tk_kw_Until];
type
TLapeInternalMethodMap = {$IFDEF FPC}specialize{$ENDIF} TLapeUniqueStringDictionary<TLapeTree_InternalMethodClass>;
TLapeTypeForwards = {$IFDEF FPC}specialize{$ENDIF} TLapeStringMap<TLapeType>;
TLapeFuncForwards = {$IFDEF FPC}specialize{$ENDIF} TLapeList<TLapeGlobalVar>;
TLapeTree_NodeStack = {$IFDEF FPC}specialize{$ENDIF} TLapeStack<TLapeTree_ExprBase>;
TLapeTree_OpStack = {$IFDEF FPC}specialize{$ENDIF} TLapeStack<TLapeTree_Operator>;
TLapeCompiler = class;
TLapeHandleDirective = function(Sender: TLapeCompiler; Directive, Argument: lpString; InPeek, InIgnore: Boolean): Boolean of object;
TLapeHandleExternal = function(Sender: TLapeCompiler; Header: TLapeGlobalVar): Boolean of object;
TLapeFindFile = procedure(Sender: TLapeCompiler; var FileName: lpString) of object;
TLapeCompilerNotification = {$IFDEF FPC}specialize{$ENDIF} TLapeNotifier<TLapeCompiler>;
TLapeTokenizerArray = array of TLapeTokenizerBase;
TLapePreprocessorFunc = function(Sender: TLapeCompiler; Name, Argument: lpString; out Value: lpString): Boolean of object;
TLapePreprocessorFuncs = {$IFDEF FPC}specialize{$ENDIF} TLapeStringMap<TLapePreprocessorFunc>;
TLapeDefine = record Name, Value: lpString; end;
TLapeDefines = {$IFDEF FPC}specialize{$ENDIF} TLapeArrayBuffer<TLapeDefine>;
TLapeConditional = record
Eval: Boolean;
Pos: TDocPos;
end;
TLapeConditionalStack = {$IFDEF FPC}specialize{$ENDIF} TLapeStack<TLapeConditional>;
TLapeVarStates = array of TLapeGlobalVar;
PCompilerState = ^TCompilerState;
TCompilerState = record
Tokenizer: Integer;
Tokenizers: array of TLapeTokenizerBase;
TokStates: array of Pointer;
Options: ECompilerOptionsSet;
Options_PackRecords: UInt8;
Defines: TLapeDefines;
Conditionals: TLapeConditionalStack.TTArray;
end;
PTempTokenizerState = ^TTempTokenizerState;
TTempTokenizerState = record
OldStackInfo: TLapeStackInfo;
OldTokenizer: TLapeTokenizerBase;
OldTokenizerIndex: Integer;
OldState: Pointer;
end;
TLapeCompiler = class(TLapeCompilerBase)
private
__tmp: TDocPos;
function hasTokenizer: Boolean; inline;
function hasMoreTokenizers: Boolean; inline;
function incTokenizerLock(ATokenizer: TLapeTokenizerBase): TLapeTokenizerBase;
procedure decTokenizerLock(var ATokenizer: TLapeTokenizerBase; DoFree: Boolean = True);
procedure setTokenizersPeek(Peek: Boolean);
protected
FTypeID: Integer;
FTokenizers: TLapeTokenizerArray;
FTokenizer: Integer;
FInternalMethodMap: TLapeInternalMethodMap;
FTree: TLapeTree_Base;
FDelayedTree: TLapeTree_DelayedStatementList;
FImporting: Pointer;
FIncludes: TStringList;
FBaseDefines: TLapeDefines;
FDefines: TLapeDefines;
FConditionalStack: TLapeConditionalStack;
FOnHandleDirective: TLapeHandleDirective;
FOnHandleExternal: TLapeHandleExternal;
FOnFindFile: TLapeFindFile;
FAfterParsing: TLapeCompilerNotification;
FPreprocessorFuncs: TLapePreprocessorFuncs;
FPreprocessorMacros: TLapePreprocessorFuncs;
function getPDocPos: PDocPos; inline;
function getDocPos: TDocPos; override;
procedure Reset; override;
function getImporting: Boolean; virtual;
procedure setImporting(Import: Boolean); virtual;
procedure setBaseDefines(Defines: TLapeDefines); virtual;
function getTokenizer: TLapeTokenizerBase; virtual;
procedure setTokenizer(ATokenizer: TLapeTokenizerBase); virtual;
procedure pushTokenizer(ATokenizer: TLapeTokenizerBase); virtual;
function popTokenizer: TLapeTokenizerBase; virtual;
procedure pushConditional(AEval: Boolean; ADocPos: TDocPos); virtual;
function popConditional: TDocPos; virtual;
procedure SetUniqueTypeID(Typ: TLapeType); virtual;
function GetToStringMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetObjectifyMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetDisposeMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetCopyMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArraySort(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayRange(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayMin(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayMax(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayUnique(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayIndexOf(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayIndicesOf(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayMode(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArraySum(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayMean(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayMedian(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayVariance(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayStdev(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayDifference(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArraySymDifference(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayIntersection(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
function GetMethod_ArrayEquals(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar; virtual;
procedure InitBaseDefinitions; virtual;
procedure InitBaseMath; virtual;
procedure InitBaseString; virtual;
procedure InitBaseDateTime; virtual;
procedure InitBaseVariant; virtual;
procedure InitBaseFile; virtual;
function EnsureExpression(Node: TLapeTree_ExprBase): TLapeTree_ExprBase; virtual;
function EnsureTypeExpression(Node: TLapeTree_Base): TLapeTree_Base; virtual;
function EnsureRange(Node: TLapeTree_Base; out VarType: TLapeType): TLapeTree_Range; overload; virtual;
function EnsureRange(Node: TLapeTree_Base): TLapeTree_Range; overload; virtual;
function EnsureRange(VarType: TLapeType): TLapeType; overload; virtual;
function EnsureConstantRange(Node: TLapeTree_Base; out VarType: TLapeType): TLapeRange; overload; virtual;
function EnsureConstantRange(Node: TLapeTree_Base): TLapeRange; overload; virtual;
function FindFile(AFileName: lpString): lpString; virtual;
function HandlePreprocessorFunc(Sender: TLapeCompiler; Name, Argument: lpString; out Value: lpString): Boolean; virtual;
function HandlePreprocessorMacro(Sender: TLapeCompiler; Name, Argument: lpString; out Value: lpString): Boolean; virtual;
function EvalPreprocessorExpr(Expr: String; ADocPos: TDocPos): Boolean; virtual;
function EvalPreprocessorMacro(Argument: String; ADocPos: TDocPos): Boolean; virtual;
function HandleDirective(Sender: TLapeTokenizerBase; Directive, Argument: lpString): Boolean; virtual;
function InIgnore: Boolean; virtual;
function Next: EParserToken; virtual;
function isNext(Tokens: EParserTokenSet; out Token: EParserToken): Boolean; overload; virtual;
function isNext(Tokens: EParserTokenSet): Boolean; overload; virtual;
function Peek: EParserToken; virtual;
function Expect(Token: EParserToken; NextBefore: Boolean = True; NextAfter: Boolean = False): EParserToken; overload; virtual;
function Expect(Tokens: EParserTokenSet; NextBefore: Boolean = True; NextAfter: Boolean = False): EParserToken; overload; virtual;
procedure ParseDeclHint(Decl: TLapeDeclaration; NextBefore: Boolean = False; NextAfter: Boolean = True); virtual;
procedure ParseExpressionEnd(Token: EParserToken; NextBefore: Boolean = False; NextAfter: Boolean = True); overload; virtual;
procedure ParseExpressionEnd(Tokens: EParserTokenSet = ParserToken_ExpressionEnd; NextBefore: Boolean = False; NextAfter: Boolean = True); overload; virtual;
function ParseIdentifierList(FirstNext: Boolean = False): TStringArray; virtual;
function ParseBlockList(StopAfterBeginEnd: Boolean = True): TLapeTree_StatementList; virtual;
function ParseMethodHeader(out Name: lpString; addToScope: Boolean = True): TLapeType_Method; virtual;
function ParseMethod(FuncForwards: TLapeFuncForwards; FuncHeader: TLapeType_Method; FuncName: lpString; isExternal: Boolean): TLapeTree_Method; overload; virtual;
function ParseMethod(FuncForwards: TLapeFuncForwards; FuncHeader: TLapeType_Method; FuncName: lpString): TLapeTree_Method; overload; virtual;
function ParseMethod(FuncForwards: TLapeFuncForwards; isExternal: Boolean = False): TLapeTree_Method; overload; virtual;
function ParseType(TypeForwards: TLapeTypeForwards; addToStackOwner: Boolean = False): TLapeType; virtual;
procedure ParseTypeBlock; virtual;
procedure ParseLabelBlock; virtual;
function ParseVarBlock(OneOnly: Boolean = False; ValidEnd: EParserTokenSet = [tk_sym_SemiColon]): TLapeTree_VarList; virtual;
function ParseExpression(ReturnOn: EParserTokenSet = []; FirstNext: Boolean = True; DoFold: Boolean = True; IsInternalMethod: Boolean = False): TLapeTree_ExprBase; virtual;
function ParseTypeExpression(ReturnOn: EParserTokenSet = []; FirstNext: Boolean = True; DoFold: Boolean = True): TLapeTree_Base; virtual;
function ParseStatement(FirstNext: Boolean = True; ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_Base; virtual;
function ParseStatementList: TLapeTree_StatementList; virtual;
function ParseBeginEnd(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_StatementList; virtual;
function ParseCase(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_Case; virtual;
function ParseFor(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_For; virtual;
function ParseIf(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_If; virtual;
function ParseRepeat(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_Repeat; virtual;
function ParseTry(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_Try; virtual;
function ParseWhile(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_While; virtual;
function ParseWith(ExprEnd: EParserTokenSet = ParserToken_ExpressionEnd): TLapeTree_With; virtual;
public
FreeTokenizer: Boolean;
FreeTree: Boolean;
constructor Create(
ATokenizer: TLapeTokenizerBase; ManageTokenizer: Boolean = True;
AEmitter: TLapeCodeEmitter = nil; ManageEmitter: Boolean = True
); reintroduce; virtual;
destructor Destroy; override;
procedure Clear; override;
function getState: Pointer; virtual;
procedure setState(const State: Pointer; DoFreeState: Boolean = True); virtual;
procedure freeState(const State: Pointer); virtual;
function getTempTokenizerState(const ATokenizer: TLapeTokenizerBase; ResetState: Boolean = True): Pointer; overload; virtual;
function getTempTokenizerState(const AStr: lpString; const AFileName: lpString = ''; ResetState: Boolean = True): Pointer; overload; virtual;
procedure resetTokenizerState(const State: Pointer; DoFreeState: Boolean = True); virtual;
procedure freeTempTokenizerState(const State: Pointer); virtual;
function getVarStates: TLapeVarStates; virtual;
procedure setVarStates(VarStates: TLapeVarStates); virtual;
procedure freeVarStates(VarStates: TLapeVarStates); virtual;
procedure StartImporting; virtual;
procedure EndImporting; virtual;
function addDelayedExpression(Node: TLapeTree_Base; AfterCompilation: Boolean = True; IsGlobal: Boolean = False): TLapeTree_Base; virtual;
function ParseFile: TLapeTree_Base; virtual;
function Compile: Boolean; virtual;
procedure CheckAfterCompile; virtual;
procedure VarToDefault(AVar: TResVar; var Offset: Integer; Pos: PDocPos = nil); override;
procedure FinalizeVar(AVar: TResVar; var Offset: Integer; Pos: PDocPos = nil); override;
function getDeclaration(AName: lpString; AStackInfo: TLapeStackInfo; LocalOnly: Boolean = False; CheckWith: Boolean = True): TLapeDeclaration; override;
function hasDeclaration(AName: lpString; AStackInfo: TLapeStackInfo; LocalOnly: Boolean = False; CheckWith: Boolean = True): Boolean; override;
function hasDeclaration(ADecl: TLapeDeclaration; AStackInfo: TLapeStackInfo; LocalOnly: Boolean = False; CheckWith: Boolean = True): Boolean; override;
function getDeclarationNoWith(AName: lpString; AStackInfo: TLapeStackInfo; LocalOnly: Boolean = False): TLapeDeclaration; overload; virtual;
function getDeclarationNoWith(AName: lpString; LocalOnly: Boolean = False): TLapeDeclaration; overload; virtual;
function getExpression(AName: lpString; AStackInfo: TLapeStackInfo; Pos: PDocPos = nil; LocalOnly: Boolean = False): TLapeTree_ExprBase; overload; virtual;
function getExpression(AName: lpString; Pos: PDocPos = nil; LocalOnly: Boolean = False): TLapeTree_ExprBase; overload; virtual;
function hasInclude(FileName: lpString): Boolean; virtual;
function hasBaseDefine(AName: lpString): Boolean; virtual;
function hasDefine(AName: lpString): Boolean; virtual;
procedure addBaseDefine(AName: lpString; AValue: lpString = ''); virtual;
procedure addDefine(AName: lpString; AValue: lpString = ''); virtual;
procedure addPreprocessorFunc(Name: lpString; Func: TLapePreprocessorFunc); virtual;
procedure addPreprocessorMacro(Name: lpString; Func: TLapePreprocessorFunc); virtual;
function addLocalDecl(Decl: TLapeDeclaration; AStackInfo: TLapeStackInfo): TLapeDeclaration; override;
function addLocalVar(AVar: TLapeType; Name: lpString = ''): TLapeVar; virtual;
function addGlobalVar(AVar: TLapeGlobalVar; AName: lpString = ''): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Typ: TLapeType; Value: Pointer; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Typ: ELapeBaseType; Value: Pointer; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Typ: lpString; Value: lpString; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Typ: lpString; Value: Pointer; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: Int32; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: UInt32; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: Int64; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: UInt64; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: Single; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: Double; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: AnsiString; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: UnicodeString; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: Variant; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalVar(Value: Pointer; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalType(Typ: TLapeType; AName: lpString = ''; ACopy: Boolean = True): TLapeType; overload; virtual;
function addGlobalType(Str: lpString; AName: lpString): TLapeType; overload; virtual;
function addGlobalFunc(AHeader: lpString; Value: Pointer): TLapeGlobalVar; overload; virtual;
function addGlobalFunc(AHeader: TLapeType_Method; AName, Body: lpString; Pos: PDocPos = nil): TLapeTree_Method; overload; virtual;
function addGlobalFunc(AParams: array of TLapeType; AParTypes: array of ELapeParameterType; AParDefaults: array of TLapeGlobalVar; ARes: TLapeType; Value: Pointer; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalFunc(AParams: array of TLapeType; AParTypes: array of ELapeParameterType; AParDefaults: array of TLapeGlobalVar; Value: Pointer; AName: lpString): TLapeGlobalVar; overload; virtual;
function addGlobalMethod(AFunc: TLapeGlobalVar; Value: TMethod; FreeFunc: Boolean = True): TLapeGlobalVar; overload; virtual;
function addGlobalMethod(AHeader: lpString; Value: TMethod): TLapeGlobalVar; overload; virtual;
function addGlobalMethod(AHeader: lpString; AMethod, ASelf: Pointer): TLapeGlobalVar; overload; virtual;
function addDelayedCode(ACode: lpString; AFileName: lpString = ''; AfterCompilation: Boolean = True; IsGlobal: Boolean = True): TLapeTree_Base; virtual;
property InternalMethodMap: TLapeInternalMethodMap read FInternalMethodMap;
property Tree: TLapeTree_Base read FTree;
property DelayedTree: TLapeTree_DelayedStatementList read FDelayedTree;
property Importing: Boolean read getImporting write setImporting;
property Tokenizer: TLapeTokenizerBase read getTokenizer write setTokenizer;
property Defines: TLapeDefines read FDefines write setBaseDefines;
property OnHandleDirective: TLapeHandleDirective read FOnHandleDirective write FOnHandleDirective;
property OnHandleExternal: TLapeHandleExternal read FOnHandleExternal write FOnHandleExternal;
property OnFindFile: TLapeFindFile read FOnFindFile write FOnFindFile;
property AfterParsing: TLapeCompilerNotification read FAfterParsing;
end;
TLapeType_SystemUnit = class(TLapeType)
public
constructor Create(ACompiler: TLapeCompilerBase); reintroduce; virtual;
procedure ClearSubDeclarations; override;
function CanHaveChild: Boolean; override;
function HasChild(AName: lpString): Boolean; override;
function HasChild(ADecl: TLapeDeclaration): Boolean; override;
function HasConstantChild(Left: TLapeGlobalVar; AName: lpString): Boolean; override;
function EvalRes(Op: EOperator; Right: TLapeGlobalVar; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
end;
implementation
uses
Variants,
lpvartypes_ord, lpvartypes_record, lpvartypes_array,
lpinternalmethods, lpinternalmethods_algorithm,
lpmessages, lpeval, lpinterpreter_types;
function TLapeCompiler.getPDocPos: PDocPos;
begin
__tmp := DocPos;
Result := @__tmp;
end;
function TLapeCompiler.hasTokenizer: Boolean;
begin
Result := FTokenizer >= 0;
end;
function TLapeCompiler.hasMoreTokenizers: Boolean;
begin
Result := FTokenizer > 0;
end;
function TLapeCompiler.incTokenizerLock(ATokenizer: TLapeTokenizerBase): TLapeTokenizerBase;
begin
Result := ATokenizer;
if (Result <> nil) then
Inc(Result.Tag);
end;
procedure TLapeCompiler.decTokenizerLock(var ATokenizer: TLapeTokenizerBase; DoFree: Boolean = True);
begin
if (ATokenizer <> nil) then
begin
Dec(ATokenizer.Tag);
if (ATokenizer.Tag < 0) and DoFree then
FreeAndNil(ATokenizer);
end;
end;
type
__LapeTokenizerBase = class(TLapeTokenizerBase);
procedure TLapeCompiler.setTokenizersPeek(Peek: Boolean);
var
i: Integer;
begin
for i := 0 to High(FTokenizers) do
if (FTokenizers[i] <> nil) then
__LapeTokenizerBase(FTokenizers[i]).FInPeek := Peek;
end;
function TLapeCompiler.getDocPos: TDocPos;
begin
if hasTokenizer() then
Result := Tokenizer.DocPos
else
Result := NullDocPos;
end;
procedure TLapeCompiler.Reset;
begin
inherited;
EndImporting();
FOptions := FBaseOptions;
FOptions_PackRecords := FBaseOptions_PackRecords;
FTokenizer := High(FTokenizers);
while hasMoreTokenizers() do
popTokenizer();
if (FTokenizer = 0) and (FTokenizers[0] <> nil) then
FTokenizers[0].Reset();
if FreeTree and (FTree <> nil) then
FTree.Free();
FTree := nil;
if (FDelayedTree <> nil) then
FDelayedTree.Clean();
if (FIncludes <> nil) then
FIncludes.Clear();
if (FConditionalStack <> nil) then
FConditionalStack.Reset();
FDefines := FBaseDefines;
end;
function TLapeCompiler.getImporting: Boolean;
begin
Result := FImporting <> nil;
end;
procedure TLapeCompiler.setImporting(Import: Boolean);
begin
if Import then
StartImporting()
else
EndImporting();
end;
procedure TLapeCompiler.setBaseDefines(Defines: TLapeDefines);
begin
FBaseDefines := Defines;
Reset();
end;
function TLapeCompiler.getTokenizer: TLapeTokenizerBase;
begin
if hasTokenizer() then
Result := FTokenizers[FTokenizer]
else
Result := nil;
end;
procedure TLapeCompiler.setTokenizer(ATokenizer: TLapeTokenizerBase);
begin
if hasTokenizer() and (FreeTokenizer or hasMoreTokenizers()) and
(FTokenizer < Length(FTokenizers)) and (Tokenizer <> ATokenizer)
then
decTokenizerLock(FTokenizers[FTokenizer]);
if (not hasTokenizer()) then
FTokenizer := 0;
if (Length(FTokenizers) <= FTokenizer) then
SetLength(FTokenizers, FTokenizer + 1);
FTokenizers[FTokenizer] := ATokenizer;
if (ATokenizer <> nil) then
begin
ATokenizer.OnHandleDirective := {$IFDEF FPC}@{$ENDIF}HandleDirective;
ATokenizer.Reset();
end;
end;
procedure TLapeCompiler.pushTokenizer(ATokenizer: TLapeTokenizerBase);
var
InPeek: Boolean;
begin
InPeek := (Tokenizer <> nil) and Tokenizer.InPeek;
Inc(FTokenizer);
setTokenizer(ATokenizer);
if (ATokenizer <> nil) then
begin
ATokenizer.RelativeFileNames := (lcoRelativeFileNames in FOptions);
if InPeek then
__LapeTokenizerBase(Tokenizer).FInPeek := True;
end;
end;
function TLapeCompiler.popTokenizer: TLapeTokenizerBase;
var
LastTok: EParserToken;
begin
Result := Tokenizer;
if (Tokenizer <> nil) then
LastTok := Tokenizer.LastTok
else
LastTok := tk_NULL;
setTokenizer(nil);
Dec(FTokenizer);
if (Tokenizer <> nil) then
__LapeTokenizerBase(Tokenizer).FLastTok := LastTok;
end;
procedure TLapeCompiler.pushConditional(AEval: Boolean; ADocPos: TDocPos);
var
Conditional: TLapeConditional;
begin
Assert(FConditionalStack <> nil);
with Conditional do
begin
Eval := AEval;
Pos := ADocPos;
end;
FConditionalStack.Push(Conditional);
end;
function TLapeCompiler.popConditional: TDocPos;
begin
Assert(FConditionalStack <> nil);
if (FConditionalStack.Count > 0) then
Result := FConditionalStack.Pop().Pos
else
LapeException(lpeLostConditional, Tokenizer.DocPos);
end;
procedure TLapeCompiler.SetUniqueTypeID(Typ: TLapeType);
begin
Typ.TypeID := FTypeID;
Inc(FTypeID);
end;
function TLapeCompiler.GetToStringMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar;
var
Body: lpString;
Index: Integer;
Header: TLapeType_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 1) or (AParams[0] = nil) or ((AResult <> nil) and (AResult.BaseType <> ltString)) then
Exit;
AResult := getBaseType(ltString);
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0]], [lptConstRef], [TLapeGlobalVar(nil)], AResult)) as TLapeType_Method;
Result := Header.NewGlobalVar(@_LapeToString_Unknown);
Result.VarType.Name := '_ToString';
Sender.addMethod(Result);
Body := AParams[0].VarToStringBody(Sender);
if (Body = '') and Sender.NeedFullMatch then
try
Sender.NeedFullMatch := False;
Assert(Result.DeclarationList = Sender.ManagedDeclarations);
Result.DeclarationList := nil;
Index := Sender.getMethodIndex(AParams, AResult);
if (Index < 0) then
Index := Sender.getMethodIndex(getTypeArray([getBaseType(AParams[0].BaseType)]), AResult);
if (Index >= 0) then
begin
Body := 'begin Result := System.ToString[' + lpString(IntToStr(Index)) + '](Param0); end;';
Result.DeclarationList := Sender.ManagedDeclarations;
end;
finally
Sender.NeedFullMatch := True;
end;
if (Body <> '') then
Result := addGlobalFunc(Header, 'ToString', 'override;' + LapeDelayedFlags + Body + LineEnding).Method
else
FreeAndNil(Result);
end;
function TLapeCompiler.GetObjectifyMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar;
var
Method: TLapeTree_Method;
Invoke: TLapeTree_Invoke;
Assignment: TLapeTree_Operator;
Callback: TResVar;
i: Int32;
AType: TLapeType_MethodOfObject;
begin
Result := nil;
if (AObjectType = nil) or (AObjectType.ClassType <> TLapeType_MethodOfObject) then
Exit;
AType := TLapeType_MethodOfObject(AObjectType);
IncStackInfo();
try
Result := addManagedDecl(AType.NewGlobalVar(EndJump)) as TLapeGlobalVar;
Result.VarType.Name := '_Objectify';
Method := TLapeTree_Method.Create(Result, FStackInfo, Self);
Method.Statements := TLapeTree_StatementList.Create(Method);
Callback := _ResVar.New(FStackInfo.addSelfVar(lptConstRef, getGlobalType('ConstPointer')));
Callback.VarType := addManagedType(TLapeType_Method.Create(AType));
Invoke := TLapeTree_Invoke.Create(TLapeTree_ResVar.Create(Callback.IncLock(), Self), Self);
for i := 0 to AType.Params.Count - 1 do
Invoke.addParam(TLapeTree_ResVar.Create(_ResVar.New(FStackInfo.addVar(AType.Params[i].ParType, AType.Params[i].VarType)), Self));
if (AType.Res <> nil) then
begin
Assignment := TLapeTree_Operator.Create(op_Assign, Self);
Assignment.Left := TLapeTree_ResVar.Create(_ResVar.New(FStackInfo.addVar(lptOut, AType.Res)), Self);
Assignment.Right := Invoke;
Method.Statements.addStatement(Assignment);
end else
Method.Statements.addStatement(Invoke);
addDelayedExpression(Method);
finally
DecStackInfo(True, False, Method = nil);
end;
end;
function TLapeCompiler.GetDisposeMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar;
var
Method: TLapeTree_Method;
Header: TLapeType_Method;
begin
Result := nil;
Method := nil;
if (Sender = nil) or (Length(AParams) <> 1) or (AParams[0] = nil) or (AResult <> nil) then
Exit;
if (not (lcoFullDisposal in FOptions)) and (not AParams[0].NeedFinalization) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0]], [lptVar], [TLapeGlobalVar(nil)], AResult)) as TLapeType_Method;
IncStackInfo();
try
Result := Header.NewGlobalVar(EndJump);
Result.VarType.Name := '_Dispose';
Sender.addMethod(Result);
Method := TLapeTree_Method.Create(Result, FStackInfo, Self);
Method.Statements := TLapeTree_StatementList.Create(Self);
Method.Statements.addStatement(TLapeTree_FinalizeVar.Create(FStackInfo.addVar(lptVar, AParams[0], '!AVar'), Self));
addDelayedExpression(Method);
finally
DecStackInfo(True, False, Method = nil);
end;
end;
function TLapeCompiler.GetCopyMethod(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray = nil; AResult: TLapeType = nil): TLapeGlobalVar;
var
Method: TLapeTree_Method;
Assignment: TLapeTree_Operator;
Header: TLapeType_Method;
begin
Result := nil;
Method := nil;
if (Sender = nil) or (Length(AParams) <> 2) or (AParams[0] = nil) or (AParams[1] = nil) or (AResult <> nil) or (not AParams[0].CompatibleWith(AParams[1])) then
Exit;
if (not AParams[0].NeedFinalization) and (not AParams[1].NeedInitialization) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0], AParams[1]], [lptConstRef, lptOut], [TLapeGlobalVar(nil), TLapeGlobalVar(nil)], AResult)) as TLapeType_Method;
IncStackInfo();
try
Result := Header.NewGlobalVar(EndJump);
Result.VarType.Name := '_Assign';
Sender.addMethod(Result);
Assignment := TLapeTree_Operator.Create(op_Assign, Self);
Assignment.Right := TLapeTree_ResVar.Create(_ResVar.New(FStackInfo.addVar(lptConstRef, AParams[0], '!Src')), Self);
Assignment.Left := TLapeTree_ResVar.Create(_ResVar.New(FStackInfo.addVar(lptOut, AParams[1], '!Dst')), Self);
Method := TLapeTree_Method.Create(Result, FStackInfo, Self);
Method.Statements := TLapeTree_StatementList.Create(Self);
Method.Statements.addStatement(Assignment);
addDelayedExpression(Method);
finally
DecStackInfo(True, False, Method = nil);
end;
end;
function TLapeCompiler.GetMethod_ArraySort(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (not (Length(AParams) in [1, 2])) then
Exit;
if (Length(AParams) = 1) then
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0]], [lptVar], [TLapeGlobalVar(nil)])) as TLapeType_Method
else
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0], AParams[1]], [lptVar, lptNormal], [TLapeGlobalVar(nil), TLapeGlobalVar(nil)])) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArraySort',
'{$RANGECHECKS OFF} ' + LineEnding +
'type ' + LineEnding +
' TType = PType(Param0); ' + LineEnding +
'const ' + LineEnding +
' Gaps: TIntegerArray = [ ' + LineEnding +
' 835387, 392925, 184011, 85764, 39744, 18298, 8359, ' + LineEnding +
' 3785, 1695, 701, 301, 132, 57, 23, 10, 4, 1 ' + LineEnding +
' ]; ' + LineEnding +
'var ' + LineEnding +
' i, j, Gap, GapLo, Len, Lo, Hi: Int32; ' + LineEnding +
' Temp: TType; ' + LineEnding +
'begin ' + LineEnding +
' Len := Length(Param0); ' + LineEnding +
' Lo := Low(Param0); ' + LineEnding +
' Hi := High(Param0); ' + LineEnding +
' ' + LineEnding +
' for Gap in Gaps do ' + LineEnding +
' begin ' + LineEnding +
' // skip gaps larger than the subsection size ' + LineEnding +
' if (Gap > Len) then ' + LineEnding +
' Continue; ' + LineEnding +
' ' + LineEnding +
' GapLo := Lo + Gap; ' + LineEnding +
' for i := GapLo to Hi do ' + LineEnding +
' begin ' + LineEnding +
' Temp := Param0[i]; ' + LineEnding +
' j := i; ' + LineEnding +
' while (j >= GapLo) and ' + LineEnding +
' {$IFPARAM Param1} ' + LineEnding +
' (Param1(Param0[j - Gap], Temp) > 0) ' + LineEnding +
' {$ELSE} ' + LineEnding +
' (Param0[j - Gap] > Temp) ' + LineEnding +
' {$ENDIF} do ' + LineEnding +
' begin ' + LineEnding +
' Param0[j] := Param0[j - Gap]; ' + LineEnding +
' j := j - Gap; ' + LineEnding +
' end; ' + LineEnding +
' Param0[j] := Temp; ' + LineEnding +
' end; ' + LineEnding +
' end; ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArraySort';
Sender.addMethod(Result);
end;
function TLapeCompiler.GetMethod_ArrayRange(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 3) or (not (AParams[0] is TLapeType_DynArray)) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0], AParams[1], AParams[2]], [lptNormal, lptOut, lptOut], [TLapeGlobalVar(nil), TLapeGlobalVar(nil), TLapeGlobalVar(nil)], getBaseType(ltInt32))) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArrayRange',
'begin ' + LineEnding +
' Result := Length(Param0); ' + LineEnding +
' if (Result > 0) then ' + LineEnding +
' begin ' + LineEnding +
' Param1 := @Param0[Low(Param0)]; ' + LineEnding +
' Param2 := @Param0[High(Param0)]; ' + LineEnding +
' end; ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArrayRange';
Sender.addMethod(Result);
end;
function TLapeCompiler.GetMethod_ArrayMin(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 1) or (not (AParams[0] is TLapeType_DynArray)) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0]], [lptNormal], [TLapeGlobalVar(nil)], TLapeType_DynArray(AParams[0]).PType)) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArrayMin',
'{$RANGECHECKS OFF} ' + LineEnding +
'type ' + LineEnding +
' TType = PType(Param0); ' + LineEnding +
'var ' + LineEnding +
' Ptr, Upper: ^TType; ' + LineEnding +
'begin ' + LineEnding +
' case _ArrayRange(Param0, Ptr, Upper) of ' + LineEnding +
' 0: Result := Default(TType); ' + LineEnding +
' 1: Result := Param0[0]; ' + LineEnding +
' else ' + LineEnding +
' begin ' + LineEnding +
' Result := Param0[0]; ' + LineEnding +
' while (PtrUInt(Ptr) <= PtrUInt(Upper)) do ' + LineEnding +
' begin ' + LineEnding +
' if (Ptr^ < Result) then ' + LineEnding +
' Result := Ptr^; ' + LineEnding +
' Inc(Ptr); ' + LineEnding +
' end; ' + LineEnding +
' end; ' + LineEnding +
' end; ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArrayMin';
Sender.addMethod(Result);
end;
function TLapeCompiler.GetMethod_ArrayMax(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 1) or (not (AParams[0] is TLapeType_DynArray)) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0]], [lptNormal], [TLapeGlobalVar(nil)], TLapeType_DynArray(AParams[0]).PType)) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArrayMax',
'{$RANGECHECKS OFF} ' + LineEnding +
'type ' + LineEnding +
' TType = PType(Param0); ' + LineEnding +
'var ' + LineEnding +
' Ptr, Upper: ^TType; ' + LineEnding +
'begin ' + LineEnding +
' case _ArrayRange(Param0, Ptr, Upper) of ' + LineEnding +
' 0: Result := Default(TType); ' + LineEnding +
' 1: Result := Param0[0]; ' + LineEnding +
' else ' + LineEnding +
' begin ' + LineEnding +
' Result := Param0[0]; ' + LineEnding +
' while (PtrUInt(Ptr) <= PtrUInt(Upper)) do ' + LineEnding +
' begin ' + LineEnding +
' if (Ptr^ > Result) then ' + LineEnding +
' Result := Ptr^; ' + LineEnding +
' Inc(Ptr); ' + LineEnding +
' end; ' + LineEnding +
' end; ' + LineEnding +
' end; ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArrayMax';
Sender.addMethod(Result);
end;
function TLapeCompiler.GetMethod_ArrayUnique(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 1) or (not (AParams[0] is TLapeType_DynArray)) then
Exit;
// Copy() creates dynamic arrays from static arrays
if (AParams[0] is TLapeType_StaticArray) then
AResult := addManagedType(TLapeType_DynArray.Create(TLapeType_DynArray(AParams[0]).PType, Self))
else
AResult := AParams[0];
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0]], [lptNormal], [TLapeGlobalVar(nil)], AResult)) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArrayUnique',
'{$RANGECHECKS OFF} ' + LineEnding +
'type ' + LineEnding +
' TType = PType(Param0); ' + LineEnding +
'var ' + LineEnding +
' Ptr, Upper: ^TType; ' + LineEnding +
' I, NewLen: Int32; ' + LineEnding +
'begin ' + LineEnding +
' Result := Copy(Param0); ' + LineEnding +
' if (_ArrayRange(Result, Ptr, Upper) <= 1) then ' + LineEnding +
' Exit; ' + LineEnding +
' ' + LineEnding +
' while (PtrUInt(Ptr) <= PtrUInt(Upper)) do ' + LineEnding +
' begin ' + LineEnding +
' I := 0; ' + LineEnding +
' while (I < NewLen) and (Ptr^ <> Result[I]) do ' + LineEnding +
' Inc(I); ' + LineEnding +
' ' + LineEnding +
' if (I = NewLen) then ' + LineEnding +
' begin ' + LineEnding +
' Result[NewLen] := Ptr^; ' + LineEnding +
' Inc(NewLen); ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' Inc(Ptr); ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' SetLength(Result, NewLen); ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArrayUnique';
Sender.addMethod(Result);
end;
function TLapeCompiler.GetMethod_ArrayIndexOf(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 2) or (not (AParams[1] is TLapeType_DynArray)) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0], AParams[1]], [lptNormal, lptNormal], [TLapeGlobalVar(nil), TLapeGlobalVar(nil)], getBaseType(ltInt32))) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArrayIndexOf',
'{$RANGECHECKS OFF} ' + LineEnding +
'var ' + LineEnding +
' i: Int32; ' + LineEnding +
'begin ' + LineEnding +
' for i := Low(Param1) to High(Param1) do ' + LineEnding +
' if (Param1[i] = Param0) then ' + LineEnding +
' Exit(i); ' + LineEnding +
' Result := -1; ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArrayIndexOf';
Sender.addMethod(Result);
end;
function TLapeCompiler.GetMethod_ArrayIndicesOf(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 2) or (not (AParams[1] is TLapeType_DynArray)) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0], AParams[1]], [lptNormal, lptNormal], [TLapeGlobalVar(nil), TLapeGlobalVar(nil)], getGlobalType('TIntegerArray'))) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArrayIndicesOf',
'{$RANGECHECKS OFF} ' + LineEnding +
'var ' + LineEnding +
' i, Count, Size: Int32; ' + LineEnding +
'begin ' + LineEnding +
' Result := []; ' + LineEnding +
' Count := Size := 0; ' + LineEnding +
' for i := Low(Param1) to High(Param1) do ' + LineEnding +
' if (Param1[i] = Param0) then ' + LineEnding +
' begin ' + LineEnding +
' if (Count = Size) then ' + LineEnding +
' begin ' + LineEnding +
' Size := 4 + (Size * 2); ' + LineEnding +
' SetLength(Result, Size); ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' Result[Count] := i; ' + LineEnding +
' Inc(Count); ' + LineEnding +
' end; ' + LineEnding +
' SetLength(Result, Count); ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArrayIndicesOf';
Sender.addMethod(Result);
end;
function TLapeCompiler.GetMethod_ArrayMode(Sender: TLapeType_OverloadedMethod; AObjectType: TLapeType; AParams: TLapeTypeArray; AResult: TLapeType): TLapeGlobalVar;
var
Header: TLapeType_Method;
Method: TLapeTree_Method;
begin
Result := nil;
if (Sender = nil) or (Length(AParams) <> 1) or (not (AParams[0] is TLapeType_DynArray)) then
Exit;
Header := addManagedType(TLapeType_Method.Create(Self, [AParams[0]], [lptNormal], [TLapeGlobalVar(nil)], TLapeType_DynArray(AParams[0]).PType)) as TLapeType_Method;
Method := addGlobalFunc(Header, '!ArrayMode',
'{$RANGECHECKS OFF} ' + LineEnding +
'type ' + LineEnding +
' TType = PType(Param0); ' + LineEnding +
'var ' + LineEnding +
' Arr: array of TType; ' + LineEnding +
' Best, Current: record Value: TType; Hits: Int32; end; ' + LineEnding +
' i: Int32; ' + LineEnding +
'begin ' + LineEnding +
' if (Length(Param0) = 0) then ' + LineEnding +
' Exit(Default(Result)); ' + LineEnding +
' ' + LineEnding +
' Arr := Sorted(Param0); ' + LineEnding +
' Best := []; ' + LineEnding +
' Current := [Arr[0], 1]; ' + LineEnding +
' ' + LineEnding +
' for i := 1 to High(Arr) do ' + LineEnding +
' begin ' + LineEnding +
' if (Arr[i] <> Current.Value) then ' + LineEnding +
' begin ' + LineEnding +
' if (Current.Hits > Best.Hits) then ' + LineEnding +
' Best := Current; ' + LineEnding +
' Current := [Arr[i], 1]; ' + LineEnding +
' end else ' + LineEnding +
' Current.Hits := Current.Hits + 1; ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' if (Current.Hits > Best.Hits) then ' + LineEnding +
' Best := Current; ' + LineEnding +
' Result := Best.Value; ' + LineEnding +
'end;'
);
Result := Method.Method;
Result.VarType.Name := '_ArrayMode';
Sender.addMethod(Result);
end;