-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuListObject.pas
More file actions
1028 lines (855 loc) · 26.6 KB
/
Copy pathuListObject.pas
File metadata and controls
1028 lines (855 loc) · 26.6 KB
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
// Developed using Delphi for Windows and Mac platforms.
// This source is distributed under Apache 2.0
// Copyright (C) 2019-2024 Herbert M Sauro
// Author Contact Information:
// email: [email protected]
unit uListObject;
interface
Uses System.SysUtils, uUtils, System.generics.Collections,
uDataObjectMethods,
uMemoryManager,
uRhodusTypes,
uStringObject,
uArrayObject,
uMatrixObject,
uValueObject,
uDataObject;
type
TListItemType = TElementType;
TListItem = class;
TListMethods = class (TMethodsBase)
procedure getLength (vm : TObject);
procedure append (vm : TObject);
procedure remove (vm : TObject);
procedure getSum (vm : TObject);
procedure insert (vm : TObject);
procedure removeLastElement (vm : TObject);
procedure getMax (vm : TObject);
procedure getMin (vm : TObject);
constructor Create;
destructor Destroy; override;
end;
TListContainer = class(TList<TListItem>)
destructor Destroy; override;
end;
TListObject = class(TDataObject)
private
public
list: TListContainer; // Contains the data
class var listMethods : TListMethods;
class function addLists(list1, list2: TListObject): TListObject;
class function multiply(value: integer; aList: TListObject): TListObject;
class function listEquals(list1, list2: TListObject): boolean;
//procedure setItem (item : TListItem);
function getItem (index : integer) : TListItem;
procedure setItemToDouble (index : integer; value : double); // space must exist before you call this
procedure append(iValue: integer); overload;
procedure append(bValue: boolean); overload;
procedure append(dValue: double); overload;
procedure append(data : TDataObject); overload;
procedure appendModule(mValue: TObject);
procedure remove(index: integer);
procedure insert(index, iValue: integer); overload;
procedure insert(index: integer; dValue: double); overload;
procedure insert(index: integer; bValue: boolean); overload;
procedure insert(index: integer; obj: TDataObject); overload;
procedure insertModule(index: integer; mValue: TObject);
function findMin : double;
function findMax : double;
function slice (lower, upper : integer) : TListObject;
class function sliceList2 (alist : TListObject; lower, upper : integer) : TListObject;
function clone: TDataObject; override;
function ToString: string; override;
function getSize: integer; override;
property Item[index : Integer]: TListItem read getItem; default; // write setItem; default;
constructor Create; overload;
constructor Create(count: integer); overload;
destructor Destroy; override;
end;
TListItem = class(TObject)
itemType: TListItemType;
iValue: integer;
bValue: boolean;
dValue: double;
dataObject : TDataObject;
moduleValue: TObject; // Module
function elementToString: string;
class function listEquals(list1: TListItem; list2: TListItem): boolean;
function getsize(): integer;
function getScalar : double;
procedure setInteger (value : integer);
procedure setDouble (value : double);
function clone : TListItem;
constructor Create(iValue: integer); overload;
constructor Create(bValue: boolean); overload;
constructor Create(dValue: double); overload;
constructor Create(obj: TDataObject); overload;
constructor CreateModule(mValue: TObject);
constructor Create(item: TListItem); overload;
destructor Destroy; override;
end;
procedure createAndAttachMethods;
implementation
Uses Math,
Rtti,
uVMExceptions,
uSymbolTable,
uMachineStack,
uVM;
type
TIntArray = array of integer;
TDoubleArray = array of double;
procedure createAndAttachMethods;
begin
TListObject.listMethods := TListMethods.Create;
end;
constructor TListMethods.Create;
begin
methodList := TMethodList.Create (self);
methodList.Add(TMethodDetails.Create ('len', 'ListObject', 0, getLength));
methodList.Add(TMethodDetails.Create ('append', 'ListObject', 1, append));
methodList.Add(TMethodDetails.Create ('remove', 'ListObject', 1, remove));
methodList.Add(TMethodDetails.Create ('insert', 'ListObject', 2, insert));
methodList.Add(TMethodDetails.Create ('sum', 'ListObject', 0, getSum));
methodList.Add(TMethodDetails.Create ('pop', 'ListObject', 0, removeLastElement));
methodList.Add(TMethodDetails.Create ('max', 'ListObject', 0, getMax));
methodList.Add(TMethodDetails.Create ('min', 'ListObject', 0, getMin));
methodList.Add(TMethodDetails.Create ('help', -1, 'Returns the help string associated with the variable. m.help ()', getHelp));
methodList.Add(TMethodDetails.Create ('dir', 0, 'dir of matrix object methods', dir));
end;
destructor TListMethods.Destroy;
begin
inherited;
end;
// Object method for the list object type
// In all these methods the order of the stack from the top is:
// Arguments
// Object Method
// Object
procedure TListMethods.getLength (vm : TObject);
var s : TListObject;
md : TMethodDetails;
begin
// No arguments for this method
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
TVM (vm).push (s.list.Count);
end;
procedure TListMethods.append (vm : TObject);
var s : TListObject;
value : PMachineStackRecord;
md : TMethodDetails;
data : TDataObject;
begin
value := TVM (vm).pop;
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
case value.stackType of
symInteger : s.append (value.iValue);
symDouble : s.append (value.dValue);
symBoolean : s.append (value.bValue);
symString,
symList,
symArray,
symMatrix,
symUserFunc: begin
data := value.dataObject.clone;
data.blockType := btOwned;
s.append (data);
end;
symModule : raise ERuntimeException.Create('Adding modules to a list is not permitted');
else
raise ERuntimeException.Create('Internal error: unrecognized data type during list insert');
end;
TVM (vm).pushNone;
end;
// Remove an element from a list with a given index, retun nothing
procedure TListMethods.remove (vm : TObject);
var s : TListObject;
index : integer;
md : TMethodDetails;
begin
index := TVM (vm).popInteger;
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
s.remove (index);
TVM (vm).pushNone;
end;
procedure TListMethods.getSum (vm : TObject);
var s : TListObject;
sum : double;
md : TMethodDetails;
begin
// No argument to pop off
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
sum := 0;
for var i := 0 to s.list.Count - 1 do
case s.list[i].itemType of
symInteger : sum := sum + s.list[i].iValue;
symDouble : sum := sum + s.list[i].dValue;
else
raise ERuntimeException.Create('You cannot sum non-numerical values in a list');
end;
TVM (vm).push(sum);
end;
// Remove an element for the list and push it onto the stack for the caller to retrieve
procedure TListMethods.removeLastElement (vm : TObject);
var s : TListObject;
r : TListItem;
md : TMethodDetails;
begin
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
if s.list.count = 0 then
begin
TVM (vm).push (s);
exit;
end;
r := s.list [s.list.Count - 1];
case r.itemType of
symInteger : TVM (vm).push (r.iValue);
symDouble : TVM (vm).push (r.dValue);
symBoolean : TVM (vm).push (r.bValue);
symString : TVM (vm).push (r.dataObject.clone);
symList : TVM (vm).push (r.dataObject.clone);
symArray : TVM (vm).push (r.dataObject.clone);
symMatrix : TVM (vm).push (r.dataObject.clone);
symUserFunc : TVM (vm).push (r.dataObject);
symModule : TVM (vm).push (TModule (r.moduleValue));
end;
s.remove (s.list.Count - 1);
// We don't push None as with the other methods because
// the above pushes a value on to the return stack
end;
procedure TListMethods.insert (vm : TObject);
var s : TListObject;
index : integer;
value : PMachineStackRecord;
md : TMethodDetails;
begin
value := TVM (vm).pop;
index := TVM (vm).popInteger;
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
if (index < 0) or (index > s.list.Count) then
raise ERuntimeException.Create('List index out of range');
case value.stackType of
symInteger : s.insert (index, value.iValue);
symDouble : s.insert (index, value.dValue);
symBoolean : s.insert (index, value.bValue);
symString : s.insert (index, value.dataObject);
symList : s.insert (index, value.dataObject);
symArray : s.insert (index, value.dataObject);
symMatrix : s.insert (index, value.dataObject);
symUserFunc: s.insert (index, value.dataObject);
symModule : s.insertModule (index, value.module)
else
raise ERuntimeException.Create('Internal error: unrecognized data type during list insert');
end;
TVM (vm).pushNone;
end;
procedure TListMethods.getMax (vm : TObject);
var s : TListObject;
md : TMethodDetails;
begin
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
TVM (vm).push (s.findMax());
end;
procedure TListMethods.getMin (vm : TObject);
var s : TListObject;
md : TMethodDetails;
begin
md := TVM (vm).popMethodDetails;
s := TListObject (md.self);
TVM (vm).push (s.findMin());
end;
procedure countItems (s : TListObject; dims : TIntArray; level : integer);
var i : integer;
begin
for i := 0 to s.list.Count - 1 do
begin
if s.list[i].itemType = symInteger then
inc (dims[level])
else
countItems (TListObject (s.list[i].dataObject), dims, level+1);
writeln (TRttiEnumerationType.GetName(s.list[i].itemType));
end;
end;
procedure collectData (s : TListObject; data : TDoubleArray; var count : integer);
var i : integer;
begin
for i := 0 to s.list.Count - 1 do
begin
if s.list[i].itemType = symInteger then
begin
data[count] := s.list[i].iValue;
inc (count);
end;
if s.list[i].itemType = symList then
collectData (TListObject (s.list[i].dataObject), data, count);
end;
end;
// -------------------------------------------------------------------------------------------------
destructor TListContainer.Destroy;
begin
inherited;
end;
// --------------------------------------------------------------------------------------------------
// Create an empty list
constructor TListObject.Create;
begin
inherited Create; // Adds object to the memory pool
objectType := symList;
list := TListContainer.Create;
methods := TListObject.listMethods;
end;
constructor TListObject.Create(count: integer);
var
i: integer;
begin
Create;
for i := 0 to count - 1 do
list.add(TListItem.Create(0));
end;
destructor TListObject.Destroy;
var
i: integer;
begin
for i := list.count - 1 downto 0 do
list[i].free;
list.free;
inherited;
end;
function TListObject.clone: TDataObject;
var
i: integer;
li : TListObject;
begin
result := TListObject.Create(self.list.count);
for i := 0 to self.list.count - 1 do
begin
li := result as TListObject;
case self.list[i].itemType of
symInteger:
begin
li.list[i].itemType := self.list[i].itemType;
li.list[i].iValue := self.list[i].iValue;
end;
symBoolean:
begin
li.list[i].itemType := self.list[i].itemType;
li.list[i].bValue := self.list[i].bValue;
end;
symDouble:
begin
li.list[i].itemType := self.list[i].itemType;
li.list[i].dValue := self.list[i].dValue;
end;
symString,
symList,
symArray,
symMatrix,
symUserFunc :
begin
li.list[i].itemType := self.list[i].itemType;
li.list[i].dataObject := self.list[i].dataObject.clone;
li.list[i].dataObject.blockType := btOwned; // owned by the container list
end;
symModule:
begin
raise ERuntimeException.Create('Copying modules is not permitted');
// We don't clone modules
end;
end;
end;
end;
function TListObject.toString: string;
var
i: integer;
begin
result := '[';
if self.list.count > 0 then
result := result + self.list[0].elementToString;
for i := 1 to self.list.count - 1 do
begin
result := result + ',' + self.list[i].elementToString;
end;
result := result + ']';
end;
function TListObject.getItem (index : integer) : TListItem;
begin
result := self.list[index];
end;
procedure TListObject.setItemToDouble (index : integer; value : double);
begin
list[index].dValue := value;
list[index].itemType := symDouble;
end;
function TListObject.getsize: integer;
var
i: integer;
begin
result := self.InstanceSize;
for i := 0 to self.list.count - 1 do
result := result + list[i].getsize();
end;
procedure TListObject.remove(index: integer);
begin
if (index < 0) or (index > list.count - 1) then
raise ERuntimeException.Create ('out of range while indexing element in list');
list[index].Free;
list.Delete(index);
end;
procedure TListObject.insert(index, iValue: integer);
begin
list.insert(index, TListItem.Create(iValue));
end;
procedure TListObject.insert(index: integer; dValue: double);
begin
list.insert(index, TListItem.Create(dValue));
end;
procedure TListObject.insert(index: integer; bValue: boolean);
begin
list.insert(index, TListItem.Create(bValue));
end;
procedure TListObject.insert(index : integer; obj: TDataObject);
begin
list.insert(index, TListItem.Create(obj));
end;
procedure TListObject.insertModule(index: integer; mValue: TObject);
begin
list.insert(index, TListItem.CreateModule(mValue));
end;
function TListObject.findMin : double;
var i : integer;
begin
if list.Count = 0 then
result := 0 // error?
else
begin
case list[0].itemType of
symInteger : result := list[0].iValue;
symDouble : result := list[0].dValue;
else
raise ERuntimeException.Create('Can only find the min value for numeric data');
end;
for i := 0 to list.Count - 1 do
case list[i].itemType of
symInteger:
begin
if list[i].iValue < result then
result := list[i].iValue;
end;
symDouble :
begin
if list[i].dValue < result then
result := list[i].dValue;
end
else
raise ERuntimeException.Create('Can only find the min value for numeric data');
end;
end;
end;
function TListObject.findMax : double;
var i : integer;
begin
if list.Count = 0 then
result := 0 // error?
else
begin
case list[0].itemType of
symInteger : result := list[0].iValue;
symDouble : result := list[0].dValue;
else
raise ERuntimeException.Create('Can only find the max value for numeric data');
end;
for i := 0 to list.Count - 1 do
case list[i].itemType of
symInteger:
begin
if list[i].iValue > result then
result := list[i].iValue;
end;
symDouble :
begin
if list[i].dValue > result then
result := list[i].dValue;
end
else
raise ERuntimeException.Create('Can only find the min value for numeric data');
end;
end;
end;
function TListObject.slice (lower, upper : integer) : TListObject;
var i : integer;
obj : TListObject;
begin
// -1 means slice all
if lower = -1 then
lower := 0;
if upper = -1 then
upper := list.Count;
if upper < lower then
obj := TListObject.Create(0)
else
begin
if upper >= list.Count then
upper := list.Count - 1;
obj := TListObject.Create (0);
for i := lower to upper do
begin
case list[i].itemType of
symInteger : obj.append(list[i].iValue);
symBoolean : obj.append(list[i].bValue);
symDouble : obj.append(list[i].dValue);
symString : obj.append(list[i].dataObject.clone);
symList : obj.append(list[i].dataObject.clone);
symArray : obj.append(list[i].dataObject.clone);
symMatrix : obj.append(list[i].dataObject.clone);
symUserFunc: obj.append(list[i].dataObject.clone);
symModule : obj.appendModule(TModule (list[i].moduleValue));
end;
end;
end;
result := obj;
end;
// ---------------------------------------------------------------
// Class methods
class function TListObject.sliceList2 (alist : TListObject; lower, upper : integer) : TListObject;
var i : integer;
obj : TListObject;
begin
// -1 means slice all
if lower = -1 then
lower := 0;
if upper = -1 then
upper := alist.list.Count;
if upper < lower then
obj := TListObject.Create(0)
else
begin
if upper >= alist.list.Count then
upper := alist.list.Count - 1;
obj := TListObject.Create (0);
for i := lower to upper do
begin
case alist.list[i].itemType of
symInteger : obj.append(alist.list[i].iValue);
symBoolean : obj.append(alist.list[i].bValue);
symDouble : obj.append(alist.list[i].dValue);
symString : obj.append(alist.list[i].dataObject.clone);
symList : obj.append(alist.list[i].dataObject.clone);
symArray : obj.append(alist.list[i].dataObject.clone);
symMatrix : obj.append(alist.list[i].dataObject.clone);
symUserFunc: obj.append(TUserFunction (alist.list[i].dataObject).clone);
symModule : obj.appendModule(TModule (alist.list[i].moduleValue));
end;
end;
end;
result := obj;
end;
class function TListObject.addLists(list1, list2: TListObject): TListObject;
var
i: integer;
begin
if list1.blockType = btGarbage then
result := list1
else
result := list1.clone as TListObject;
for i := 0 to list2.list.count - 1 do
result.list.add(TListItem.Create(list2.list[i]));
end;
// Make value copies of aList and combine them into one list
// eg 3*{1} = {1,1,1}
class function TListObject.multiply(value: integer; aList: TListObject) : TListObject;
var
i, j: integer;
nContents: integer;
workingCopy: TListObject;
item: TListItem;
begin
if aList.isBound then
workingCopy := aList.clone as TListObject
else
workingCopy := aList;
nContents := aList.list.count;
result := TListObject.Create(0);
for i := 0 to value - 1 do
if nContents = 0 then
result.list.add(TListItem.Create(0))
else
for j := 0 to nContents - 1 do
begin
item := TListItem.Create(workingCopy.list[j]);
result.list.add(item);
end;
end;
class function TListObject.listEquals(list1, list2: TListObject): boolean;
var
i: integer;
begin
if list1.list.count <> list2.list.count then
exit(False);
result := True;
for i := 0 to list1.list.count - 1 do
result := result and TListItem.listEquals(list1.list[i], list2.list[i]);
end;
procedure TListObject.append(iValue: integer);
begin
list.add(TListItem.Create(iValue));
end;
procedure TListObject.append(bValue: boolean);
begin
list.add(TListItem.Create(bValue));
end;
procedure TListObject.append(dValue: double);
begin
list.add(TListItem.Create(dValue));
end;
procedure TListObject.append(data : TDataObject);
begin
if data.blockType = btGarbage then
data.blockType := btOwned;
list.add(TListItem.Create(data));
end;
procedure TListObject.appendModule(mValue: TObject);
begin
list.add(TListItem.CreateModule(mValue));
end;
// ----------------------------------------------------------------------
constructor TListItem.Create(item: TListItem);
begin
inherited Create;
itemType := item.itemType;
case itemType of
symInteger:
self.iValue := item.iValue;
symDouble:
self.dValue := item.dValue;
symBoolean:
self.bValue := item.bValue;
symString:
begin
self.dataObject := item.dataObject.clone;
self.dataObject.blockType := btOwned;
end;
symList:
begin
self.dataObject := item.dataObject.clone;
self.dataObject.blockType := btOwned;
end;
symArray:
begin
self.dataObject := item.dataObject.clone;
self.dataObject.blockType := btOwned;
end;
symMatrix:
begin
self.dataObject := item.dataObject.clone;
self.dataObject.blockType := btOwned;
end;
symUserFunc:
begin
self.dataObject := item.dataObject
end;
symModule:
begin
self.moduleValue := item.moduleValue;
end;
else
ERuntimeException.Create
('Internal Error: unknown itemType in ListItem.Create');
end;
end;
constructor TListItem.Create(iValue: integer);
begin
itemType := symInteger;
self.iValue := iValue;
end;
constructor TListItem.Create(bValue: boolean);
begin
itemType := symBoolean;
self.bValue := bValue;
end;
constructor TListItem.Create(dValue: double);
begin
itemType := symDouble;
self.dValue := dValue;
end;
constructor TListItem.Create(obj: TDataObject);
begin
itemType := obj.objectType;;
self.dataObject := obj;
end;
constructor TListItem.CreateModule(mValue: TObject);
begin
itemType := symModule;
self.moduleValue := mValue;
end;
// This needs looking at.
destructor TListItem.Destroy;
begin
case itemType of
symList,
symString,
symMatrix,
symArray : begin
if dataObject.isOwned then
dataObject.blockType := btGarbage
else
dataObject.free;
end;
symUserFunc : begin
if (dataObject as TUserFunction).blockType = btOwned then
(dataObject as TUserFunction).blockType := btGarbage
else
dataObject.free;
end;
symInteger, symDouble, symBoolean, symValueObject : begin end
else
begin raise ERuntimeException.Create ('Failed to free object in TListItem.Destroy:' + stToStr(itemType)); end;
end;
inherited;
end;
function TListItem.clone : TListItem;
begin
case itemType of
symInteger: result := TListItem.Create(iValue);
symBoolean: result := TListItem.Create(bValue);
symDouble: result := TListItem.Create(dValue);
symString: result := TListItem.Create(dataObject.clone);
symList: begin result := TListItem.Create(dataObject.clone); result.dataObject.blockType := btOwned; end;
symArray: result := TListItem.Create(dataObject.clone);
symMatrix: result := TListItem.Create(dataObject.clone);
symUserFunc: result := TListItem.Create(dataObject.clone);
symModule: raise ERuntimeException.Create('can''t clone module');
else
raise ERuntimeException.Create('Internal Error in ListItem Clone');
end;
end;
function TListItem.getsize(): integer;
begin
result := sizeof(TObject);
result := result + sizeof(self.itemType);
case itemType of
symInteger:
result := result + sizeof(integer);
symBoolean:
result := result + sizeof(boolean);
symDouble:
result := result + sizeof(double);
symString:
result := result + dataObject.getsize();
symList:
result := result + dataObject.getsize();
symObject:
result := result + dataObject.getSize();
symArray:
result := result + dataObject.getSize();
else
raise ERuntimeException.Create('Error in size not implement for this type');
end;
end;
function TListItem.getScalar : double;
begin
if itemType = symInteger then
exit (iValue);
if itemType = symDouble then
exit (dValue);
exit (0);
end;
procedure TListItem.setInteger (value : integer);
begin
iValue := value;
itemType := symInteger;
end;
procedure TListItem.setDouble (value : double);
begin
dValue := value;
itemType := symDouble;
end;
function boolToString(value: boolean): string;
begin
if value then
result := 'True'
else
result := 'False';
end;
function TListItem.elementToString: string;
begin
result := '';
case self.itemType of
symInteger:
result := result + inttostr(self.iValue);
symBoolean:
result := result + boolToString(self.bValue);
symDouble:
result := result + floattostr(self.dValue);
symString:
result := result + '"' + self.dataObject.ToString + '"';
symList:
result := result + self.dataObject.ToString;
symUserFunc:
result := result + self.dataObject.ToString;
symModule:
result := result + TModule(self.moduleValue).moduleName;
symArray:
result := result + self.dataObject.ToString;
symMatrix:
result := result + self.dataObject.ToString;
symObject:
result := result + self.dataObject.ToString;
symValueObject:
result := result + self.dataObject.ToString;
else
raise ERuntimeException.Create('Unknown type in elementToString: ' + stToStr(self.itemType));
end;
end;
class function TListItem.listEquals(list1: TListItem; list2: TListItem) : boolean;
begin
result := False;
if (list1.itemType = symInteger) and (list2.itemType = symInteger) then
if list1.iValue = list2.iValue then
exit(True)
else
exit(False);
if (list1.itemType = symBoolean) and (list2.itemType = symBoolean) then
if list1.bValue = list2.bValue then
exit(True)
else
exit(False);
if (list1.itemType = symDouble) and (list2.itemType = symDouble) then
if list1.dValue = list2.dValue then
exit(True)
else
exit(False);
if (list1.itemType = symString) and (list2.itemType = symString) then
if TStringObject(list1.dataObject).isEqualTo(TStringObject(list2.dataObject)) then
exit(True)
else
exit(False);
if (list1.itemType = symList) and (list2.itemType = symList) then