-
Notifications
You must be signed in to change notification settings - Fork 4
/
lpvartypes_ord.pas
997 lines (857 loc) · 35.2 KB
/
lpvartypes_ord.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
{
Author: Niels A.D
Project: Lape (http://code.google.com/p/la-pe/)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
All (script)type and (script)variable classes, including corresponding evaluation functions (runtime/compile time).
}
unit lpvartypes_ord;
{$I lape.inc}
interface
uses
Classes, SysUtils,
lptypes, lpvartypes;
type
{$IFDEF FPC}generic{$ENDIF} TLapeType_Integer<_Type> = class(TLapeType)
protected type
PType = ^_Type;
var public
function NewGlobalVar(Val: _Type; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
end;
{$IFDEF FPC}generic{$ENDIF} TLapeType_Float<_Type> = class(TLapeType)
protected type
PType = ^_Type;
var public
function NewGlobalVar(Val: _Type; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
end;
{$IFDEF FPC}generic{$ENDIF} TLapeType_Char<_Type> = class(TLapeType)
protected type
PType = ^_Type;
var public
function NewGlobalVar(Val: _Type; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
end;
TLapeType_UInt8 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<UInt8>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Int8 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<Int8>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_UInt16 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<UInt16>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Int16 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<Int16>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_UInt32 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<UInt32>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Int32 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<Int32>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_UInt64 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<UInt64>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Int64 = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Integer<Int64>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Single = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Float<Single>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Double = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Float<Double>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Currency = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Float<Currency>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Extended = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Float<Extended>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_AnsiChar = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Char<AnsiChar>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_WideChar = class({$IFDEF FPC}specialize{$ENDIF} TLapeType_Char<WideChar>)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_Variant = class(TLapeType)
public
constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function NewGlobalVar(Val: Variant; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
end;
TLapeType_SubRange = class(TLapeType)
protected
FRange: TLapeRange;
FVarType: TLapeType;
function getAsString: lpString; override;
public
constructor Create(ARange: TLapeRange; ACompiler: TLapeCompilerBase; AVarType: TLapeType; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; override;
function VarToString(AVar: Pointer): lpString; override;
function VarLo(AVar: Pointer = nil): TLapeGlobalVar; override;
function VarHi(AVar: Pointer = nil): TLapeGlobalVar; override;
function NewGlobalVar(Value: Int64 = 0; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
property Range: TLapeRange read FRange;
property VarType: TLapeType read FVarType;
end;
TEnumMap = TStringList;
TLapeType_Enum = class(TLapeType_SubRange)
protected
FMemberMap: TEnumMap;
FSmall: Boolean;
function getAsString: lpString; override;
public
FreeMemberMap: Boolean;
constructor Create(ACompiler: TLapeCompilerBase; AMemberMap: TEnumMap; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
destructor Destroy; override;
function hasMember(AName: lpString): Boolean; virtual;
function addMember(Value: Int64; AName: lpString): Int64; overload; virtual;
function addMember(AName: lpString): Int64; overload; virtual;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; override;
function VarToString(AVar: Pointer): lpString; override;
function NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
function EvalAsSubType(Op: EOperator; Right: TLapeType): Boolean; virtual;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
property MemberMap: TEnumMap read FMemberMap;
property Small: Boolean read FSmall;
end;
TLapeType_Boolean = class(TLapeType_Enum)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function NewGlobalVar(Value: Int64 = 0; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
function EvalAsSubType(Op: EOperator; Right: TLapeType): Boolean; override;
end;
TLapeType_Bool = class(TLapeType_SubRange)
public
constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
destructor Destroy; override;
function NewGlobalVar(Value: Int64 = 0; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; override;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
function EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar; override;
function Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar; override;
end;
TLapeType_ByteBool = class(TLapeType_Bool)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_WordBool = class(TLapeType_Bool)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
TLapeType_LongBool = class(TLapeType_Bool)
public constructor Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual; end;
{$IFDEF Lape_SmallCode}
TLapeType_EvalBool = TLapeType_Boolean;
{$ELSE}
TLapeType_EvalBool = TLapeType_LongBool;
{$ENDIF}
TLapeType_Set = class(TLapeType)
protected
FRange: TLapeType_SubRange;
FSmall: Boolean;
function getAsString: lpString; override;
public
constructor Create(ARange: TLapeType_SubRange; ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil); reintroduce; virtual;
function CreateCopy(DeepCopy: Boolean = False): TLapeType; override;
function VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString; override;
function VarToString(AVar: Pointer): lpString; override;
function NewGlobalVar(Values: array of UInt8; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar; virtual;
function EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType; override;
property Range: TLapeType_SubRange read FRange;
property Small: Boolean read FSmall;
end;
implementation
uses
Variants,
{$IFDEF Lape_NeedAnsiStringsUnit}AnsiStrings,{$ENDIF}
lpparser, lpeval, lpexceptions;
function TLapeType_Integer{$IFNDEF FPC}<_Type>{$ENDIF}.NewGlobalVar(Val: _Type; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
Result := NewGlobalVarP(nil, AName, ADocPos);
PType(Result.Ptr)^ := Val;
end;
function TLapeType_Integer{$IFNDEF FPC}<_Type>{$ENDIF}.NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
{$IFNDEF FPC}
var
i64: Int64;
ui64: UInt64;
t: PType;
{$ENDIF}
begin
if {(Length(Str) > 1) and (Str[1] <> '-')} (BaseType in LapeUnsignedIntegerTypes) then
begin
{$IFDEF FPC}
Result := NewGlobalVar(StrToUInt64(string(Str)), AName, ADocPos);
{$ELSE}
ui64 := StrToUInt64(string(Str)); t := @ui64;
Result := NewGlobalVar(t^ , AName, ADocPos);
{$ENDIF}
end
else
begin
{$IFDEF FPC}
Result := NewGlobalVar(StrToInt64(string(Str)), AName, ADocPos);
{$ELSE}
i64 := StrToInt64(string(Str)); t := @i64;
Result := NewGlobalVar(t^ , AName, ADocPos);
{$ENDIF}
end;
end;
function TLapeType_Float{$IFNDEF FPC}<_Type>{$ENDIF}.NewGlobalVar(Val: _Type; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
Result := NewGlobalVarP(nil, AName, ADocPos);
PType(Result.Ptr)^ := Val;
end;
function TLapeType_Float{$IFNDEF FPC}<_Type>{$ENDIF}.NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
{$IFNDEF FPC}var a: Extended; b: PType;{$ENDIF}
begin
{$IFDEF FPC}
Result := NewGlobalVar(StrToFloatDot(string(Str)), AName, ADocPos);
{$ELSE}
a := StrToFloatDot(string(Str)); b := @a;
Result := NewGlobalVar(b^ , AName, ADocPos);
{$ENDIF}
end;
function TLapeType_Char{$IFNDEF FPC}<_Type>{$ENDIF}.NewGlobalVar(Val: _Type; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
Result := NewGlobalVarP(nil, AName, ADocPos);
PType(Result.Ptr)^ := Val;
end;
function TLapeType_Char{$IFNDEF FPC}<_Type>{$ENDIF}.NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
{$IFNDEF FPC}var a: WideChar; b: PType;{$ENDIF}
var
c: Integer;
begin
if (Length(Str) <> 1) then
try
if (Str[1] = '#') then
Delete(Str, 1, 1);
c := StrToInt(string(Str));
Result := NewGlobalVarP(nil, AName, ADocPos);
case Size of
SizeOf(UInt8): PUInt8(Result.Ptr)^ := c;
SizeOf(UInt16): PUInt16(Result.Ptr)^ := c;
else LapeException(lpeImpossible);
end;
Exit;
except
LapeExceptionFmt(lpeInvalidValueForType, [AsString]);
end;
{$IFDEF FPC}
Result := NewGlobalVar(_Type(Str[1]), AName, ADocPos);
{$ELSE}
a := Str[1]; b := @a;
Result := NewGlobalVar(b^, AName, ADocPos);
{$ENDIF}
end;
constructor TLapeType_UInt8.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltUInt8, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Int8.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltInt8, ACompiler, AName, ADocPos);
end;
constructor TLapeType_UInt16.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltUInt16, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Int16.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltInt16, ACompiler, AName, ADocPos);
end;
constructor TLapeType_UInt32.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltUInt32, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Int32.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltInt32, ACompiler, AName, ADocPos);
end;
constructor TLapeType_UInt64.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltUInt64, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Int64.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltInt64, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Single.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltSingle, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Double.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltDouble, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Currency.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltCurrency, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Extended.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltExtended, ACompiler, AName, ADocPos);
end;
constructor TLapeType_AnsiChar.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltAnsiChar, ACompiler, AName, ADocPos);
end;
constructor TLapeType_WideChar.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltWideChar, ACompiler, AName, ADocPos);
end;
constructor TLapeType_Variant.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltVariant, ACompiler, AName, ADocPos);
end;
function TLapeType_Variant.NewGlobalVar(Val: Variant; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
Result := NewGlobalVarP(nil, AName, ADocPos);
PVariant(Result.Ptr)^ := Val;
end;
function TLapeType_Variant.NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
Result := NewGlobalVar(Str, AName, ADocPos);
end;
function TLapeType_SubRange.getAsString: lpString;
begin
if (FAsString = '') then
if (FVarType <> nil) then
FAsString := FVarType.VarToString(@FRange.Lo) + '..' + FVarType.VarToString(@FRange.Hi)
else
FAsString := lpString(IntToStr(FRange.Lo)) + '..' + lpString(IntToStr(FRange.Hi));
Result := inherited;
end;
constructor TLapeType_SubRange.Create(ARange: TLapeRange; ACompiler: TLapeCompilerBase; AVarType: TLapeType; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ltUnknown, ACompiler, AName, ADocPos);
if (AVarType = nil) and (ACompiler <> nil) then
AVarType := ACompiler.getBaseType(DetermineIntType(ARange.Lo, ARange.Hi));
if (AVarType <> nil) then
FBaseType := AVarType.BaseType;
FRange := ARange;
FVarType := AVarType;
end;
function TLapeType_SubRange.VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString;
var
Index: Integer;
begin
Result := '';
if (ToStr = nil) or (FCompiler = nil) or (FVarType = nil) then
Exit;
Index := ToStr.getMethodIndex(getTypeArray([FVarType]), FCompiler.getBaseType(ltString));
if (Index < 0) then
Exit;
Result := 'begin Result := System.ToString['+lpString(IntToStr(Index))+'](Param0); end;';
end;
function TLapeType_SubRange.VarToString(AVar: Pointer): lpString;
begin
if (FVarType <> nil) then
Result := FVarType.VarToString(AVar)
else
Result := lpString(IntToStr(VarToInt(AVar)));
end;
function TLapeType_SubRange.VarLo(AVar: Pointer = nil): TLapeGlobalVar;
begin
if (FLo = nil) and (FCompiler <> nil) and IsOrdinal() then
FLo := FCompiler.addManagedVar(NewGlobalVarStr(lpString(IntToStr(Range.Lo)))) as TLapeGlobalVar;
Result := FLo;
end;
function TLapeType_SubRange.VarHi(AVar: Pointer = nil): TLapeGlobalVar;
begin
if (FHi = nil) and (FCompiler <> nil) and IsOrdinal() then
FHi := FCompiler.addManagedVar(NewGlobalVarStr(lpString(IntToStr(Range.Hi)))) as TLapeGlobalVar;
Result := FHi;
end;
function TLapeType_SubRange.CreateCopy(DeepCopy: Boolean = False): TLapeType;
type TLapeClassType = class of TLapeType_SubRange;
begin
Result := TLapeClassType(Self.ClassType).Create(FRange, FCompiler, FVarType, Name, @_DocPos);
with TLapeType_SubRange(Result) do
begin
inheritManagedDecls(Self, not DeepCopy);
TypeID := Self.TypeID;
FBaseType := Self.BaseType;
end;
end;
function TLapeType_SubRange.NewGlobalVar(Value: Int64 = 0; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
if (Value < FRange.Lo) or (Value > FRange.Hi) then
LapeExceptionFmt(lpeOutOfTypeRange1, [Value, FRange.Lo, FRange.Hi]);
Result := NewGlobalVarP(nil, AName);
case BaseIntType of
ltInt8: PInt8(Result.Ptr)^ := Value;
ltUInt8: PUInt8(Result.Ptr)^ := Value;
ltInt16: PInt16(Result.Ptr)^ := Value;
ltUInt16: PUInt16(Result.Ptr)^ := Value;
ltInt32: PInt32(Result.Ptr)^ := Value;
ltUInt32: PUInt32(Result.Ptr)^ := Value;
ltInt64: PInt64(Result.Ptr)^ := Value;
ltUInt64: PUInt64(Result.Ptr)^ := Value;
end;
end;
function TLapeType_SubRange.NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
with FVarType.NewGlobalVarStr(Str) do
try
Result := NewGlobalVar(AsInteger, AName, ADocPos);
finally
Free();
end;
end;
function TLapeType_Enum.getAsString: lpString;
var
i: Integer;
begin
if (FAsString = '') then
begin
FAsString := '(';
for i := 0 to FMemberMap.Count - 1 do
begin
if (FMemberMap[i] = '') then
Continue;
if (FAsString <> '(') then
FAsString := FAsString + ', ';
FAsString := FAsString + lpString(FMemberMap[i]) + '=' + lpString(IntToStr(i+FRange.Lo));
end;
FAsString := FAsString + ')';
end;
Result := inherited;
end;
constructor TLapeType_Enum.Create(ACompiler: TLapeCompilerBase; AMemberMap: TEnumMap; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(NullRange, ACompiler, nil, AName, ADocPos);
FBaseType := ltLargeEnum;
FVarType := nil;
FreeMemberMap := (AMemberMap = nil);
if (AMemberMap = nil) then
begin
AMemberMap := TEnumMap.Create();
AMemberMap.CaseSensitive := LapeCaseSensitive;
end;
FMemberMap := AMemberMap;
FRange.Hi := Int64(FMemberMap.Count) - 1;
FSmall := (FRange.Hi <= Ord(High(ELapeSmallEnum)));
if FSmall then
FBaseType := ltSmallEnum;
end;
destructor TLapeType_Enum.Destroy;
begin
if FreeMemberMap then
FMemberMap.Free();
inherited;
end;
function TLapeType_Enum.hasMember(AName: lpString): Boolean;
begin
Result := FMemberMap.IndexOf(string(AName)) > -1;
end;
function TLapeType_Enum.addMember(Value: Int64; AName: lpString): Int64;
var
i: Integer;
begin
if (Value < FMemberMap.Count) then
LapeException(lpeInvalidRange)
else if (AName = '') or hasMember(AName) then
LapeException(lpeDuplicateDeclaration);
Result:= Value;
FRange.Hi := Value;
if (FMemberMap.Count = 0) then
FRange.Lo := Value
else for i := FMemberMap.Count to Value - FRange.Lo - 1 do
FMemberMap.Add('');
FMemberMap.Add(string(AName));
FSmall := (FRange.Hi <= Ord(High(ELapeSmallEnum)));
if (not FSmall) then
FBaseType := ltLargeEnum;
ClearCache();
end;
function TLapeType_Enum.addMember(AName: lpString): Int64;
begin
Result := addMember(FRange.Hi + 1, AName);
end;
function TLapeType_Enum.VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString;
var
i: Integer;
begin
Result := '';
for i := 0 to FMemberMap.Count - 1 do
begin
if (Result <> '') then
Result := Result + ', ';
Result := Result + #39 + lpString(FMemberMap[i]) + #39;
end;
Result := Format(lpString(
'type TEnumToString = private function(constref Arr; Index, Lo, Hi: System.SizeInt): System.string;' + LineEnding +
'begin Result := TEnumToString(System._EnumToString)([%s], System.Ord(Param0), %d, %d); end;'),
[Result, FRange.Lo, FRange.Hi]
);
end;
function TLapeType_Enum.VarToString(AVar: Pointer): lpString;
var
i: Int64;
begin
try
Result := '';
i := VarToInt(AVar);
if (FBaseType in LapeBoolTypes) then
if (i = 0) then
Result := 'False'
else if (Abs(i) > 1) then
Result := 'True(' + lpString(IntToStr(i)) + ')'
else
Result := 'True'
else
begin
if (i > -1) and (i < FMemberMap.Count) then
Result := lpString(FMemberMap[i]);
if (Result = '') then
if (Name <> '') then
Result := Name + '(' + lpString(IntToStr(i)) + ')'
else
Result := 'InvalidEnum';
end;
except
Result := 'EnumException';
end;
end;
function TLapeType_Enum.CreateCopy(DeepCopy: Boolean = False): TLapeType;
type
TLapeClassType = class of TLapeType_Enum;
begin
if DeepCopy then
begin
Result := TLapeClassType(Self.ClassType).Create(FCompiler, nil, Name, @_DocPos);
TLapeType_Enum(Result).MemberMap.Assign(FMemberMap);
end
else
Result := TLapeClassType(Self.ClassType).Create(FCompiler, FMemberMap, Name, @_DocPos);
with TLapeType_Enum(Result) do
begin
inheritManagedDecls(Self, not DeepCopy);
TypeID := Self.TypeID;
FBaseType := Self.BaseType;
FRange := Self.Range;
FSmall := Self.Small;
end;
end;
function TLapeType_Enum.NewGlobalVarStr(Str: UnicodeString; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
if (Str <> '') and (AnsiChar(Str[1]) in ['-', '0'..'9']) then
Result := NewGlobalVar(StrToInt64(string(Str)), AName, ADocPos)
else
Result := NewGlobalVar(FMemberMap.IndexOf(Str), AName, ADocPos);
end;
function TLapeType_Enum.EvalAsSubType(Op: EOperator; Right: TLapeType): Boolean;
begin
if (Right = nil) then
Result := False
else if (op in CompareOperators + [op_Assign]) then
Result := Equals(Right)
else if (op in [op_Plus, op_Minus]) then
Result := Right.BaseType in LapeIntegerTypes
else
Result := False;
end;
function TLapeType_Enum.EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType;
begin
Assert(FCompiler <> nil);
if EvalAsSubType(Op, Right) then
begin
Result := FCompiler.getBaseType(BaseIntType).EvalRes(Op, FCompiler.getBaseType(Right.BaseIntType), Flags);
if (Result <> nil) and (not (op in CompareOperators)) then
Result := Self;
end
else
Result := inherited;
end;
function TLapeType_Enum.EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar;
var
tmpType: TLapeType;
tmpRes: TLapeGlobalVar;
begin
Assert(FCompiler <> nil);
Assert((Left = nil) or (Left.VarType = Self));
if (Right <> nil) and Right.HasType() and EvalAsSubType(Op, Right.VarType) then
try
tmpType := Right.VarType;
if (not IsOrdinal()) or (not Right.HasType()) or (not Right.VarType.IsOrdinal()) then
LapeException(lpeInvalidEvaluation);
Left.VarType := FCompiler.getBaseType(BaseIntType);
Right.VarType := FCompiler.getBaseType(Right.VarType.BaseIntType);
Result := Left.VarType.EvalConst(Op, Left, Right, Flags);
if Result.HasType() and (not (op in CompareOperators)) then
begin
if (Result.VarType.BaseIntType <> BaseIntType) then
try
tmpRes := Result;
Result := Left.VarType.EvalConst(op_Assign, Left.VarType.NewGlobalVarP(), tmpRes, []);
finally
FreeAndNil(tmpRes);
end;
Result.VarType := Self
end;
finally
Left.VarType := Self;
Right.VarType := tmpType;
end
else
Result := inherited;
end;
function TLapeType_Enum.Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar;
var
tmpRes: TResVar;
begin
Assert(FCompiler <> nil);
Assert(Left.VarType = Self);
if Right.HasType() and EvalAsSubType(Op, Right.VarType) then
begin
if (not IsOrdinal()) or (not Right.HasType()) or (not Right.VarType.IsOrdinal()) then
LapeException(lpeInvalidEvaluation);
Left.VarType := FCompiler.getBaseType(BaseIntType);
Right.VarType := FCompiler.getBaseType(Right.VarType.BaseIntType);
if Dest.HasType() and Equals(Dest.VarType) then
Dest.VarType := Left.VarType;
Result := Left.VarType.Eval(Op, Dest, Left, Right, Flags, Offset, Pos);
if Result.HasType() and (not (op in CompareOperators)) then
begin
if (Result.VarType.BaseIntType <> BaseIntType) then
begin
tmpRes := Result;
Result := NullResVar;
Result.VarType := Left.VarType;
FCompiler.getDestVar(Dest, Result, Op);
Result := Left.VarType.Eval(op_Assign, Dest, Result, tmpRes, [], Offset, Pos);
tmpRes.Spill(1);
end;
Result.VarType := Self;
end;
end
else
Result := inherited;
end;
constructor TLapeType_Boolean.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(ACompiler, nil, AName, ADocPos);
addMember('False');
addMember('True');
FBaseType := ltBoolean;
end;
function TLapeType_Boolean.NewGlobalVar(Value: Int64 = 0; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
if (Value <> 0) then
Value := FRange.Hi;
Result := inherited;
end;
function TLapeType_Boolean.EvalAsSubType(Op: EOperator; Right: TLapeType): Boolean;
begin
if (op in [op_Plus, op_Minus]) then
Result := inherited
else
Result := False;
end;
constructor TLapeType_Bool.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
inherited Create(NullRange, ACompiler, ACompiler.getBaseType(ltBoolean).CreateCopy(), AName, ADocPos);
end;
destructor TLapeType_Bool.Destroy;
begin
FVarType.Free();
inherited;
end;
function TLapeType_Bool.CreateCopy(DeepCopy: Boolean = False): TLapeType;
type
TLapeClassType = class of TLapeType_Bool;
begin
Result := TLapeClassType(Self.ClassType).Create(FCompiler, Name, @_DocPos);
with TLapeType_Bool(Result) do
begin
inheritManagedDecls(Self, not DeepCopy);
TypeID := Self.TypeID;
FBaseType := Self.BaseType;
FRange := Self.Range;
TLapeType_Boolean(FVarType).FSize := Self.VarType.Size;
end;
end;
function TLapeType_Bool.NewGlobalVar(Value: Int64 = 0; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
begin
if (Value <> 0) then
Value := FRange.Hi;
Result := inherited;
end;
function TLapeType_Bool.EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType;
begin
Result := inherited;
if (Result = nil) then
begin
if Equals(Right, False) then
Right := FVarType;
Result := FVarType.EvalRes(Op, Right, Flags);
if (Result = FVarType) then
Result := Self;
end;
end;
function TLapeType_Bool.EvalConst(Op: EOperator; Left, Right: TLapeGlobalVar; Flags: ELapeEvalFlags): TLapeGlobalVar;
var
Res, tmpType: TLapeType;
begin
Assert(Left.VarType = Self);
if (Right = nil) or (not Right.HasType()) then
Res := inherited EvalRes(Op, TLapeType(nil), Flags)
else
Res := inherited EvalRes(Op, Right.VarType, Flags);
if (Res <> nil) then
Result := inherited
else
begin
Left.VarType := FVarType;
if (Right <> nil) and Right.HasType() and Equals(Right.VarType) then
begin
tmpType := Right.VarType;
Right.VarType := FVarType;
end
else
tmpType := nil;
try
Result := FVarType.EvalConst(Op, Left, Right, Flags);
if (Result.VarType = FVarType) then
Result.VarType := Self;
finally
Left.VarType := Self;
if (tmpType <> nil) then
Right.VarType := tmpType;
end;
end;
end;
function TLapeType_Bool.Eval(Op: EOperator; var Dest: TResVar; Left, Right: TResVar; Flags: ELapeEvalFlags; var Offset: Integer; Pos: PDocPos = nil): TResVar;
var
Res: TLapeType;
begin
Assert(Left.VarType = Self);
Res := inherited EvalRes(Op, Right.VarType, Flags);
if (Res <> nil) then
Result := inherited
else
begin
Left.VarType := FVarType;
if Right.HasType() and Equals(Right.VarType) then
Right.VarType := FVarType;
Result := FVarType.Eval(Op, Dest, Left, Right, Flags, Offset, Pos);
if (Result.VarType = FVarType) then
Result.VarType := Self;
end;
end;
constructor TLapeType_ByteBool.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
const
BoolRange: TLapeRange = (Lo: Ord(Low(UInt8{ByteBool})); Hi: Ord(High(UInt8{ByteBool})));
begin
inherited Create(ACompiler, AName, ADocPos);
FRange := BoolRange;
FBaseType := ltByteBool;
TLapeType_Boolean(FVarType).FSize := LapeTypeSize[ltByteBool];
end;
constructor TLapeType_WordBool.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
const
BoolRange: TLapeRange = (Lo: Ord(Low(UInt16{WordBool})); Hi: Ord(High(UInt16{WordBool})));
begin
inherited Create(ACompiler, AName, ADocPos);
FRange := BoolRange;
FBaseType := ltWordBool;
TLapeType_Boolean(FVarType).FSize := LapeTypeSize[ltWordBool];
end;
constructor TLapeType_LongBool.Create(ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
const
BoolRange: TLapeRange = (Lo: Ord(Low(UInt32{LongBool})); Hi: Ord(High(UInt32{LongBool})));
begin
inherited Create(ACompiler, AName, ADocPos);
FRange := BoolRange;
FBaseType := ltLongBool;
TLapeType_Boolean(FVarType).FSize := LapeTypeSize[ltLongBool];
end;
function TLapeType_Set.getAsString: lpString;
begin
if (FAsString = '') then
FAsString := 'set of ' + FRange.AsString;
Result := inherited;
end;
constructor TLapeType_Set.Create(ARange: TLapeType_SubRange; ACompiler: TLapeCompilerBase; AName: lpString = ''; ADocPos: PDocPos = nil);
begin
Assert(ARange <> nil);
inherited Create(ltLargeSet, ACompiler, AName, ADocPos);
FRange := ARange;
FSmall := (FRange.Range.Hi <= Ord(High(ELapeSmallEnum)));
if FSmall then
FBaseType := ltSmallSet;
if (FRange.Range.Lo < Ord(Low(ELapeSmallEnum))) or (FRange.Range.Hi > Ord(High(ELapeLargeEnum))) then
if (FRange.Range.Lo < Ord(Low(ELapeSmallEnum))) then
LapeExceptionFmt(lpeOutOfTypeRange1, [FRange.Range.Lo, Ord(Low(ELapeSmallEnum)), Ord(High(ELapeLargeEnum))])
else
LapeExceptionFmt(lpeOutOfTypeRange1, [FRange.Range.Hi, Ord(Low(ELapeSmallEnum)), Ord(High(ELapeLargeEnum))])
end;
function TLapeType_Set.VarToStringBody(ToStr: TLapeType_OverloadedMethod = nil): lpString;
var
Index: Integer;
begin
Result := '';
if (ToStr = nil) or (FCompiler = nil) or (FRange = nil) or (FRange.BaseIntType = ltUnknown) then
Exit;
Index := ToStr.getMethodIndex(getTypeArray([FRange]), FCompiler.getBaseType(ltString));
if (Index < 0) then
Exit;
if FSmall then
Result := '_SmallSetToString'
else
Result := '_LargeSetToString';
Result := Format(lpString(
' function _ElementToString(p: System.ConstPointer): System.string; static;' + LineEnding +
' type' + LineEnding +
' TEnum = (se0, se1 = %d);' + LineEnding +
' TRangeToString = function(constref r: System.%s): string;' + LineEnding +
' begin' + LineEnding +
' Result := TRangeToString(System.ToString[%d])(Ord(TEnum(p^)));' + LineEnding +
' end;' + LineEnding +
'type' + LineEnding +
' TSetToString = private function(constref ASet; AToString: System.ConstPointer; Lo, Hi: System.SizeInt): System.string;' + LineEnding +
'begin' + LineEnding +
' Result := TSetToString(System.%s)(Param0, _ElementToString, %d, %d);' + LineEnding +
'end;'
), [FRange.Range.Hi, LapeTypeToString(FRange.BaseIntType), Index, Result, FRange.Range.Lo, FRange.Range.Hi]);
end;
function TLapeType_Set.VarToString(AVar: Pointer): lpString;
var
i: Integer;
begin
Result := '[';
for i := FRange.Range.Lo to FRange.Range.Hi do
if (FSmall and (ELapeSmallEnum(i) in PLapeSmallSet(AVar)^)) or ((not FSmall) and (ELapeLargeEnum(i) in PLapeLargeSet(AVar)^)) then
begin
if (Result <> '[') then
Result := Result + ', ';
Result := Result + FRange.VarToString(@i);
end;
Result := Result + ']';
end;
function TLapeType_Set.CreateCopy(DeepCopy: Boolean = False): TLapeType;
type
TLapeClassType = class of TLapeType_Set;
begin
Result := TLapeClassType(Self.ClassType).Create(FRange, FCompiler, Name, @_DocPos);
Result.inheritManagedDecls(Self, not DeepCopy);
Result.TypeID := TypeID;
end;
function TLapeType_Set.NewGlobalVar(Values: array of UInt8; AName: lpString = ''; ADocPos: PDocPos = nil): TLapeGlobalVar;
var
i: Integer;
begin
Result := NewGlobalVarP(nil, AName);
for i := 0 to High(Values) do
if FSmall then
PLapeSmallSet(Result.Ptr)^ := PLapeSmallSet(Result.Ptr)^ + [ELapeSmallEnum(Values[i])]
else
PLapeLargeSet(Result.Ptr)^ := PLapeLargeSet(Result.Ptr)^ + [ELapeLargeEnum(Values[i])]
end;
function TLapeType_Set.EvalRes(Op: EOperator; Right: TLapeType = nil; Flags: ELapeEvalFlags = []): TLapeType;
begin
Result := nil;
if (Right = nil) then
Result := inherited
else
case getEvalRes(Op, FBaseType, Right.BaseType) of
ltSmallEnum, ltLargeEnum:
if (not (Right.BaseType in LapeEnumTypes)) or Right.Equals(FRange) then
Result := FRange.VarType;
ltSmallSet, ltLargeSet:
if (not (Right.BaseType in LapeEnumTypes + LapeSetTypes)) or Right.Equals(FRange) or Equals(Right) then
Result := Self;
else
Result := inherited;
end;
end;
end.