-
Notifications
You must be signed in to change notification settings - Fork 28
/
lpinternalmethods_algorithm.pas
799 lines (656 loc) · 22.7 KB
/
lpinternalmethods_algorithm.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
{
Author: Niels A.D
Project: Lape (https://github.com/nielsAD/lape)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
}
unit lpinternalmethods_algorithm;
{$I lape.inc}
interface
uses
Classes, SysUtils,
lptypes, lptree, lpvartypes;
type
TLapeTree_InternalMethod_Sort = class(TLapeTree_InternalMethod)
public
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_Sorted = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_Reverse = class(TLapeTree_InternalMethod)
public
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_Reversed = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_Unique = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayMedian = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayMode = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayMin = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayMax = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArraySum = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayMean = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayVariance = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayStdev = class(TLapeTree_InternalMethod)
public
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArrayDifference = class(TLapeTree_InternalMethod)
protected
FMethod: lpString;
public
constructor Create(ACompiler: TLapeCompilerBase; ADocPos: PDocPos = nil); override;
function resType: TLapeType; override;
function Compile(var Offset: Integer): TResVar; override;
end;
TLapeTree_InternalMethod_ArraySymDifference = class(TLapeTree_InternalMethod_ArrayDifference)
public
constructor Create(ACompiler: TLapeCompilerBase; ADocPos: PDocPos = nil); override;
end;
TLapeTree_InternalMethod_ArrayIntersection = class(TLapeTree_InternalMethod_ArrayDifference)
public
constructor Create(ACompiler: TLapeCompilerBase; ADocPos: PDocPos = nil); override;
end;
implementation
uses
lpparser, lpvartypes_array, lpmessages, lpinternalmethods;
// sort(arr)
// sort(arr, comparefunc)
// sort(arr, weights, lowtohigh)
function TLapeTree_InternalMethod_Sort.Compile(var Offset: Integer): TResVar;
var
ArrayVar: TResVar;
ArrayElementType: TLapeType;
procedure DoNormal;
begin
// ensure we can generate such a method
if (not HasMagicMethod(Compiler, '_ArraySort', getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_cmp_GreaterThan], ArrayElementType, DocPos);
with TLapeTree_Invoke.Create('_ArraySort', Self) do
try
addParam(TLapeTree_ResVar.Create(ArrayVar, Self));
Compile(Offset);
finally
Free();
end;
end;
procedure DoCompareFunc;
var
CompareVar: TResVar;
begin
CompareVar := FParams[1].Compile(Offset);
if (not (CompareVar.VarType is TLapeType_Method)) then
LapeException(lpeInvalidCompareMethod, DocPos);
with TLapeType_Method(CompareVar.VarType) do
begin
if not (
(Params.Count = 2) and
(Res.IsOrdinal()) and
(ArrayElementType.Equals(Params[0].VarType)) and
(ArrayElementType.Equals(Params[1].VarType))
) then
LapeException(lpeInvalidCompareMethod, DocPos);
end;
with TLapeTree_Invoke.Create('_ArraySort', Self) do
try
addParam(TLapeTree_ResVar.Create(ArrayVar, Self));
addParam(TLapeTree_ResVar.Create(CompareVar, Self));
Compile(Offset);
finally
Free();
end;
end;
procedure DoWeighted;
var
ArrayPtr: TResVar;
Len, Weights: TLapeTree_Invoke;
begin
if (ArrayVar.VarType.BaseType = ltDynArray) then
begin
ArrayPtr := ArrayVar;
ArrayPtr.VarType := FCompiler.getBaseType(ltPointer);
end else
ArrayPtr := ArrayVar.VarType.Eval(op_Addr, Result, ArrayVar, NullResVar, [], Offset, @_DocPos); // ltStaticArray
Len := TLapeTree_InternalMethod_Length.Create(Self);
Len.addParam(TLapeTree_ResVar.Create(ArrayVar, Self));
Weights := TLapeTree_InternalMethod_Copy.Create(Self);
Weights.addParam(Params[1]);
// _ArraySortWeighted(A: Pointer; ElSize, Len: SizeInt; Weights: array of Int32; SortUp: EvalBool)
with TLapeTree_Invoke.Create('_ArraySortWeighted', Self) do
try
addParam(TLapeTree_ResVar.Create(ArrayPtr, Self));
addParam(TLapeTree_Integer.Create(ArrayElementType.Size, Self));
addParam(Len.FoldConstants() as TLapeTree_ExprBase);
addParam(Weights.FoldConstants() as TLapeTree_ExprBase);
addParam(Self.Params[1]);
Compile(Offset);
finally
Free();
end;
end;
begin
Result := NullResVar;
Dest := NullResVar;
if (not (FParams.Count in [1, 2, 3])) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
ArrayVar := FParams[0].Compile(Offset);
if (not (ArrayVar.VarType is TLapeType_DynArray)) then
LapeException(lpeExpectedArray, DocPos);
ArrayElementType := TLapeType_DynArray(ArrayVar.VarType).PType;
case FParams.Count of
1: DoNormal();
2: DoCompareFunc();
3: DoWeighted();
end;
end;
function TLapeTree_InternalMethod_Sorted.resType: TLapeType;
var
ParamType: TLapeType;
begin
if (FResType = nil) and (FParams.Count > 0) and (not isEmpty(FParams[0])) then
begin
ParamType := FParams[0].resType();
if (ParamType <> nil) then
begin
if (ParamType.BaseType in LapeArrayTypes) then
FResType := ParamType;
if (ParamType is TLapeType_StaticArray) then
if (ParamType.BaseType in LapeStringTypes) then
FResType := FCompiler.getBaseType(ltAnsiString)
else with TLapeType_StaticArray(ParamType) do
FResType := FCompiler.addManagedType(TLapeType_DynArray.Create(PType, FCompiler));
end;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_Sorted.Compile(var Offset: Integer): TResVar;
begin
Result := NullResVar;
Dest := NullResVar;
if (resType() = nil) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
with TLapeTree_InternalMethod_Copy.Create(Self) do
try
addParam(Self.FParams[0]);
Result := Compile(Offset);
Result.isConstant := False;
finally
Free();
end;
with TLapeTree_InternalMethod_Sort.Create(Self) do
try
addParam(TLapeTree_ResVar.Create(Result, Self));
while (Self.FParams.Count > 0) do
addParam(Self.FParams[0]);
Compile(Offset).Spill(1);
finally
Free();
end;
Result.isConstant := True;
Assert((Result.VarPos.MemPos <> mpVar) or (Result.Lock > 0));
end;
function TLapeTree_InternalMethod_Reverse.Compile(var Offset: Integer): TResVar;
var
ArrayVar, ArrayPointer: TResVar;
ArrayHigh: TLapeTree_InternalMethod_High;
ArrayType: TLapeType;
ArrayWasConstant: Boolean;
begin
Result := NullResVar;
Dest := NullResVar;
if (Params.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
if (not FParams[0].CompileToTempVar(Offset, ArrayVar)) then
LapeException(lpeInvalidEvaluation, DocPos);
ArrayWasConstant := ArrayVar.isConstant;
if ArrayWasConstant then
ArrayVar.isConstant := False;
try
case ArrayVar.VarType.BaseType of
ltStaticArray:
begin
with TLapeTree_Operator.Create(op_Addr, Self) do
try
Left := TLapeTree_Operator.Create(op_Index, Self);
TLapeTree_Operator(Left).Left := TLapeTree_ResVar.Create(ArrayVar.IncLock(), Self);
TLapeTree_Operator(Left).Right := TLapeTree_GlobalVar.Create(ArrayVar.VarType.VarLo(), Self);
ArrayPointer := Compile(Offset);
finally
ArrayVar.DecLock(1);
Free();
end;
end;
ltDynArray:
begin
ArrayPointer := ArrayVar;
ArrayPointer.VarType := FCompiler.getBaseType(ltPointer);
end;
else
LapeException(lpeExpectedArray, DocPos);
end;
ArrayType := TLapeType_DynArray(ArrayVar.VarType).PType;
with TLapeTree_Invoke.Create('_ArrayReverse', Self) do
try
addParam(TLapeTree_ResVar.Create(ArrayPointer.IncLock(), Self));
addParam(TLapeTree_Integer.Create(ArrayType.Size, Self));
case ArrayVar.VarType.BaseType of
ltDynArray:
begin
ArrayHigh := TLapeTree_InternalMethod_High.Create(Self);
ArrayHigh.addParam(TLapeTree_ResVar.Create(ArrayVar.IncLock(), Self));
addParam(ArrayHigh);
end;
ltStaticArray:
addParam(TLapeTree_Integer.Create(TLapeType_StaticArray(ArrayVar.VarType).Range.Hi - TLapeType_StaticArray(ArrayVar.VarType).Range.Lo, Self));
end;
Compile(Offset).Spill(1);
finally
ArrayPointer.DecLock(1);
Free();
end;
finally
ArrayVar.isConstant := ArrayWasConstant;
end;
end;
function TLapeTree_InternalMethod_Reversed.resType: TLapeType;
var
ParamType: TLapeType;
begin
if (FResType = nil) and (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
ParamType := FParams[0].resType();
if (ParamType <> nil) then
begin
if (ParamType.BaseType in LapeArrayTypes) then
FResType := ParamType;
if (ParamType is TLapeType_StaticArray) then
if (ParamType.BaseType in LapeStringTypes) then
FResType := FCompiler.getBaseType(ltAnsiString)
else with TLapeType_StaticArray(ParamType) do
FResType := FCompiler.addManagedType(TLapeType_DynArray.Create(PType, FCompiler));
end;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_Reversed.Compile(var Offset: Integer): TResVar;
begin
Result := NullResVar;
Dest := NullResVar;
if (resType() = nil) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
with TLapeTree_InternalMethod_Copy.Create(Self) do
try
addParam(Self.FParams[0]);
Result := Compile(Offset);
finally
Free();
end;
with TLapeTree_InternalMethod_Reverse.Create(Self) do
try
addParam(TLapeTree_ResVar.Create(Result.IncLock(), Self));
Compile(Offset).Spill(1);
finally
Free();
end;
Assert((Result.VarPos.MemPos <> mpVar) or (Result.Lock > 0));
end;
function TLapeTree_InternalMethod_Unique.resType: TLapeType;
var
ParamType: TLapeType;
begin
if (FResType = nil) and (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
ParamType := FParams[0].resType();
if (ParamType <> nil) then
begin
if (ParamType.BaseType in LapeArrayTypes) then
FResType := ParamType;
if (ParamType is TLapeType_StaticArray) then
if (ParamType.BaseType in LapeStringTypes) then
FResType := FCompiler.getBaseType(ltAnsiString)
else with TLapeType_StaticArray(ParamType) do
FResType := FCompiler.addManagedType(TLapeType_DynArray.Create(PType, FCompiler));
end;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_Unique.Compile(var Offset: Integer): TResVar;
var
ArrayElementType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
ArrayElementType := TLapeType_DynArray(resType()).PType;
// ensure we can generate such a method
if (not HasMagicMethod(Compiler, '_ArrayUnique', getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_cmp_Equal], ArrayElementType, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayUnique'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMedian.resType: TLapeType;
var
Typ: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
Typ := FParams[0].resType();
if (Typ is TLapeType_DynArray) then
begin
if (TLapeType_DynArray(Typ).PType.BaseType in LapeIntegerTypes + LapeRealTypes) then
FResType := FCompiler.getBaseType(ltDouble)
else
FResType := TLapeType_DynArray(Typ).PType;
end;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMedian.Compile(var Offset: Integer): TResVar;
var
ArrayType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
ArrayType := resType();
if (ArrayType = nil) then
LapeException(lpeExpectedArray, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayMedian'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMode.resType: TLapeType;
var
Typ: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
Typ := FParams[0].resType();
if (Typ is TLapeType_DynArray) then
FResType := TLapeType_DynArray(Typ).PType;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMode.Compile(var Offset: Integer): TResVar;
var
ArrayVar, SortedArrayVar, LengthVar: TResVar;
ArrayElementType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
if (not (FParams[0].resType() is TLapeType_DynArray)) then
LapeException(lpeExpectedArray, DocPos);
ArrayElementType := resType();
if (ArrayElementType = nil) then
LapeException(lpeExpectedArray, DocPos);
// ensure we can generate such a method
if (not HasMagicMethod(Compiler, '_ArrayMode', getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_cmp_Equal], ArrayElementType, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayMode'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMin.resType: TLapeType;
var
Typ: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
Typ := FParams[0].resType();
if (Typ is TLapeType_DynArray) then
FResType := TLapeType_DynArray(Typ).PType;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMin.Compile(var Offset: Integer): TResVar;
var
ArrayElementType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
ArrayElementType := resType();
if (ArrayElementType = nil) then
LapeException(lpeExpectedArray, DocPos);
// ensure we can generate such a method
if (not HasMagicMethod(Compiler, '_ArrayMin', getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_cmp_LessThan], ArrayElementType, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayMin'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMax.resType: TLapeType;
var
Typ: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
Typ := FParams[0].resType();
if (Typ is TLapeType_DynArray) then
FResType := TLapeType_DynArray(Typ).PType;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMax.Compile(var Offset: Integer): TResVar;
var
ArrayElementType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
ArrayElementType := resType();
if (ArrayElementType = nil) then
LapeException(lpeExpectedArray, DocPos);
// ensure we can generate such a method
if (not HasMagicMethod(Compiler, '_ArrayMax', getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_cmp_GreaterThan], ArrayElementType, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayMax'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArraySum.resType: TLapeType;
var
Typ: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
Typ := FParams[0].resType();
if (Typ is TLapeType_DynArray) then
begin
if (TLapeType_DynArray(Typ).PType.BaseType in LapeRealTypes) then
FResType := FCompiler.getBaseType(ltDouble)
else
if (TLapeType_DynArray(Typ).PType.BaseType in LapeIntegerTypes) then
FResType := FCompiler.getBaseType(ltInt64)
else
FResType := TLapeType_DynArray(Typ).PType;
end;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_ArraySum.Compile(var Offset: Integer): TResVar;
var
ArrayElementType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
if (resType() = nil) then
LapeException(lpeExpectedArray, DocPos);
// ensure we can generate such a method, best to error here
ArrayElementType := TLapeType_DynArray(FParams[0].resType()).PType;
if (not HasMagicMethod(Compiler, '_ArraySum', getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_Plus], ArrayElementType, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArraySum'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMean.resType: TLapeType;
var
Typ: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
Typ := FParams[0].resType();
if (Typ is TLapeType_DynArray) and TLapeType_DynArray(Typ).HasType then
begin
if (TLapeType_DynArray(Typ).PType.BaseType in LapeRealTypes+LapeIntegerTypes) then
FResType := FCompiler.getBaseType(ltDouble)
else
FResType := TLapeType_DynArray(Typ).PType;
end;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayMean.Compile(var Offset: Integer): TResVar;
var
ArrayVar, CounterVar: TResVar;
ArrayElementType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
if (resType() = nil) then
LapeException(lpeExpectedArray, DocPos);
// ensure we can generate such a method, best to error here
ArrayElementType := TLapeType_DynArray(FParams[0].resType()).PType;
if (not HasMagicMethod(Compiler, '_ArrayMean', getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_Plus], ArrayElementType, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayMean'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayVariance.resType: TLapeType;
var
Typ: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 1) and (not isEmpty(FParams[0])) then
begin
Typ := FParams[0].resType();
if (Typ is TLapeType_DynArray) and TLapeType_DynArray(Typ).HasType then
begin
if (TLapeType_DynArray(Typ).PType.BaseType in LapeRealTypes+LapeIntegerTypes) then
FResType := FCompiler.getBaseType(ltDouble)
else
FResType := TLapeType_DynArray(Typ).PType;
end;
end;
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayVariance.Compile(var Offset: Integer): TResVar;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
if (not (FParams[0].resType() is TLapeType_DynArray)) then
LapeException(lpeExpectedArray, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayVariance'], Self));
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayStdev.resType: TLapeType;
begin
if (FResType = nil) then
FResType := FCompiler.getBaseType(ltDouble);
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayStdev.Compile(var Offset: Integer): TResVar;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 1) then
LapeExceptionFmt(lpeWrongNumberParams, [1], DocPos);
if (not (FParams[0].resType() is TLapeType_DynArray)) then
LapeException(lpeExpectedArray, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler['_ArrayStdev'], Self));
Result := inherited;
end;
constructor TLapeTree_InternalMethod_ArrayDifference.Create(ACompiler: TLapeCompilerBase; ADocPos: PDocPos);
begin
inherited Create(ACompiler, ADocPos);
FMethod := '_ArrayDifference';
end;
function TLapeTree_InternalMethod_ArrayDifference.resType: TLapeType;
begin
if (FResType = nil) then
if (FParams.Count = 2) and
(FParams[0].resType() is TLapeType_DynArray) and (FParams[1].resType() is TLapeType_DynArray) and
(FParams[0].resType().Equals(FParams[1].resType())) then
FResType := FParams[0].resType();
Result := inherited;
end;
function TLapeTree_InternalMethod_ArrayDifference.Compile(var Offset: Integer): TResVar;
var
ArrayElementType: TLapeType;
begin
Result := NullResVar;
Dest := NullResVar;
if (FParams.Count <> 2) or isEmpty(FParams[0]) or isEmpty(FParams[1]) then
LapeExceptionFmt(lpeWrongNumberParams, [2], DocPos);
if (resType() = nil) then
LapeException(lpeInvalidEvaluation, DocPos);
// ensure we can generate such a method, best to error here
ArrayElementType := TLapeType_DynArray(FParams[0].resType()).PType;
if (not HasMagicMethod(Compiler, FMethod, getParamTypes(), resType())) then
RequireOperators(FCompiler, [op_cmp_Equal], ArrayElementType, DocPos);
setExpr(TLapeTree_GlobalVar.Create(FCompiler[FMethod], Self));
Result := inherited;
end;
constructor TLapeTree_InternalMethod_ArraySymDifference.Create( ACompiler: TLapeCompilerBase; ADocPos: PDocPos);
begin
inherited Create(ACompiler, ADocPos);
FMethod := '_ArraySymDifference';
end;
constructor TLapeTree_InternalMethod_ArrayIntersection.Create(ACompiler: TLapeCompilerBase; ADocPos: PDocPos);
begin
inherited Create(ACompiler, ADocPos);
FMethod := '_ArrayIntersection';
end;
end.