forked from artzub/tdbf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbf_prsdef.pas
executable file
·1053 lines (859 loc) · 24.8 KB
/
dbf_prsdef.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
unit dbf_prsdef;
interface
{$I dbf_common.inc}
uses
SysUtils,
Classes,
dbf_common,
dbf_prssupp;
const
MaxArg = 6;
ArgAllocSize = 32;
type
TExpressionType = (etInteger, etString, etBoolean, etLargeInt, etFloat, etDateTime,
etLeftBracket, etRightBracket, etComma, etUnknown);
PPChar = ^PChar;
PBoolean = ^Boolean;
PInteger = ^Integer;
PDateTime = ^TDateTime;
EParserException = class(Exception);
PExpressionRec = ^TExpressionRec;
PDynamicType = ^TDynamicType;
TExprWord = class;
TExprFunc = procedure(Expr: PExpressionRec);
//-----
TDynamicType = class(TObject)
private
FMemory: PPChar;
FMemoryPos: PPChar;
FSize: PInteger;
public
constructor Create(DestMem, DestPos: PPChar; ASize: PInteger);
procedure AssureSpace(ASize: Integer);
procedure Resize(NewSize: Integer; Exact: Boolean);
procedure Rewind;
procedure Append(Source: PChar; Length: Integer);
procedure AppendInteger(Source: Integer);
property Memory: PPChar read FMemory;
property MemoryPos: PPChar read FMemoryPos;
property Size: PInteger read FSize;
end;
TExpressionRec = record
//used both as linked tree and linked list for maximum evaluation efficiency
Oper: TExprFunc;
Next: PExpressionRec;
Res: TDynamicType;
ExprWord: TExprWord;
AuxData: pointer;
ResetDest: boolean;
WantsFunction: boolean;
Args: array[0..MaxArg-1] of PChar;
ArgsPos: array[0..MaxArg-1] of PChar;
ArgsSize: array[0..MaxArg-1] of Integer;
ArgsType: array[0..MaxArg-1] of TExpressionType;
ArgList: array[0..MaxArg-1] of PExpressionRec;
end;
TExprCollection = class(TNoOwnerCollection)
public
procedure Check;
procedure EraseExtraBrackets;
end;
TExprWordRec = record
Name: PChar;
ShortName: PChar;
IsOperator: Boolean;
IsVariable: Boolean;
IsFunction: Boolean;
NeedsCopy: Boolean;
FixedLen: Boolean;
CanVary: Boolean;
ResultType: TExpressionType;
MinArg: Integer;
MaxArg: Integer;
TypeSpec: PChar;
Description: PChar;
ExprFunc: TExprFunc;
end;
TExprWord = class(TObject)
private
FName: string;
FExprFunc: TExprFunc;
protected
FRefCount: Cardinal;
function GetIsOperator: Boolean; virtual;
function GetIsVariable: Boolean;
function GetNeedsCopy: Boolean;
function GetFixedLen: Integer; virtual;
function GetCanVary: Boolean; virtual;
function GetResultType: TExpressionType; virtual;
function GetMinFunctionArg: Integer; virtual;
function GetMaxFunctionArg: Integer; virtual;
function GetDescription: string; virtual;
function GetTypeSpec: string; virtual;
function GetShortName: string; virtual;
procedure SetFixedLen(NewLen: integer); virtual;
public
constructor Create(AName: string; AExprFunc: TExprFunc);
function LenAsPointer: PInteger; virtual;
function AsPointer: PChar; virtual;
function IsFunction: Boolean; virtual;
property ExprFunc: TExprFunc read FExprFunc;
property IsOperator: Boolean read GetIsOperator;
property CanVary: Boolean read GetCanVary;
property IsVariable: Boolean read GetIsVariable;
property NeedsCopy: Boolean read GetNeedsCopy;
property FixedLen: Integer read GetFixedLen write SetFixedLen;
property ResultType: TExpressionType read GetResultType;
property MinFunctionArg: Integer read GetMinFunctionArg;
property MaxFunctionArg: Integer read GetMaxFunctionArg;
property Name: string read FName;
property ShortName: string read GetShortName;
property Description: string read GetDescription;
property TypeSpec: string read GetTypeSpec;
end;
TExpressShortList = class(TSortedCollection)
public
function KeyOf(Item: Pointer): Pointer; override;
function Compare(Key1, Key2: Pointer): Integer; override;
procedure FreeItem(Item: Pointer); override;
end;
TExpressList = class(TSortedCollection)
private
FShortList: TExpressShortList;
public
constructor Create;
destructor Destroy; override;
procedure Add(Item: Pointer); override;
function KeyOf(Item: Pointer): Pointer; override;
function Compare(Key1, Key2: Pointer): Integer; override;
function Search(Key: Pointer; var Index: Integer): Boolean; override;
procedure FreeItem(Item: Pointer); override;
end;
TConstant = class(TExprWord)
private
FResultType: TExpressionType;
protected
function GetResultType: TExpressionType; override;
public
constructor Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
end;
TFloatConstant = class(TConstant)
private
FValue: Double;
public
// not overloaded to support older Delphi versions
constructor Create(AName: string; AValue: string);
constructor CreateAsDouble(AName: string; AValue: Double);
function AsPointer: PChar; override;
property Value: Double read FValue write FValue;
end;
TUserConstant = class(TFloatConstant)
private
FDescription: string;
protected
function GetDescription: string; override;
public
constructor CreateAsDouble(AName, Descr: string; AValue: Double);
end;
TStringConstant = class(TConstant)
private
FValue: string;
public
constructor Create(AValue: string);
function AsPointer: PChar; override;
end;
TIntegerConstant = class(TConstant)
private
FValue: Integer;
public
constructor Create(AValue: Integer);
function AsPointer: PChar; override;
end;
TBooleanConstant = class(TConstant)
private
FValue: Boolean;
public
// not overloaded to support older Delphi versions
constructor Create(AName: string; AValue: Boolean);
function AsPointer: PChar; override;
property Value: Boolean read FValue write FValue;
end;
TVariable = class(TExprWord)
private
FResultType: TExpressionType;
protected
function GetCanVary: Boolean; override;
function GetResultType: TExpressionType; override;
public
constructor Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
end;
TFloatVariable = class(TVariable)
private
FValue: PDouble;
public
constructor Create(AName: string; AValue: PDouble);
function AsPointer: PChar; override;
end;
TStringVariable = class(TVariable)
private
FValue: PPChar;
FFixedLen: Integer;
protected
function GetFixedLen: Integer; override;
procedure SetFixedLen(NewLen: integer); override;
public
constructor Create(AName: string; AValue: PPChar);
function LenAsPointer: PInteger; override;
function AsPointer: PChar; override;
property FixedLen: Integer read FFixedLen;
end;
TDateTimeVariable = class(TVariable)
private
FValue: PDateTimeRec;
public
constructor Create(AName: string; AValue: PDateTimeRec);
function AsPointer: PChar; override;
end;
TIntegerVariable = class(TVariable)
private
FValue: PInteger;
public
constructor Create(AName: string; AValue: PInteger);
function AsPointer: PChar; override;
end;
{$ifdef SUPPORT_INT64}
TLargeIntVariable = class(TVariable)
private
FValue: PLargeInt;
public
constructor Create(AName: string; AValue: PLargeInt);
function AsPointer: PChar; override;
end;
{$endif}
TBooleanVariable = class(TVariable)
private
FValue: PBoolean;
public
constructor Create(AName: string; AValue: PBoolean);
function AsPointer: PChar; override;
end;
TLeftBracket = class(TExprWord)
function GetResultType: TExpressionType; override;
end;
TRightBracket = class(TExprWord)
protected
function GetResultType: TExpressionType; override;
end;
TComma = class(TExprWord)
protected
function GetResultType: TExpressionType; override;
end;
TFunction = class(TExprWord)
private
FIsOperator: Boolean;
FOperPrec: Integer;
FMinFunctionArg: Integer;
FMaxFunctionArg: Integer;
FDescription: string;
FTypeSpec: string;
FShortName: string;
FResultType: TExpressionType;
protected
function GetDescription: string; override;
function GetIsOperator: Boolean; override;
function GetMinFunctionArg: Integer; override;
function GetMaxFunctionArg: Integer; override;
function GetResultType: TExpressionType; override;
function GetTypeSpec: string; override;
function GetShortName: string; override;
procedure InternalCreate(AName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
AExprFunc: TExprFunc; AIsOperator: Boolean; AOperPrec: Integer);
public
constructor Create(AName, AShortName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType; AExprFunc: TExprFunc; Descr: string);
constructor CreateOper(AName, ATypeSpec: string; AResultType: TExpressionType; AExprFunc: TExprFunc; AOperPrec: Integer);
function IsFunction: Boolean; override;
property OperPrec: Integer read FOperPrec;
property TypeSpec: string read FTypeSpec;
end;
TVaryingFunction = class(TFunction)
// Functions that can vary for ex. random generators
// should be TVaryingFunction to be sure that they are
// always evaluated
protected
function GetCanVary: Boolean; override;
end;
const
ListChar = ','; {the delimiter used with the 'in' operator: e.g.,
('a' in 'a,b') =True
('c' in 'a,b') =False}
function ExprCharToExprType(ExprChar: Char): TExpressionType;
implementation
function ExprCharToExprType(ExprChar: Char): TExpressionType;
begin
case ExprChar of
'B': Result := etBoolean;
'I': Result := etInteger;
'L': Result := etLargeInt;
'F': Result := etFloat;
'D': Result := etDateTime;
'S': Result := etString;
else
Result := etUnknown;
end;
end;
procedure _FloatVariable(Param: PExpressionRec);
begin
with Param^ do
PDouble(Res.MemoryPos^)^ := PDouble(Args[0])^;
end;
procedure _BooleanVariable(Param: PExpressionRec);
begin
with Param^ do
PBoolean(Res.MemoryPos^)^ := PBoolean(Args[0])^;
end;
procedure _StringConstant(Param: PExpressionRec);
begin
with Param^ do
Res.Append(Args[0], StrLen(Args[0]));
end;
procedure _StringVariable(Param: PExpressionRec);
var
length: integer;
begin
with Param^ do
begin
length := PInteger(Args[1])^;
if length = -1 then
length := StrLen(PPChar(Args[0])^);
Res.Append(PPChar(Args[0])^, length);
end;
end;
procedure _DateTimeVariable(Param: PExpressionRec);
begin
with Param^ do
PDateTimeRec(Res.MemoryPos^)^ := PDateTimeRec(Args[0])^;
end;
procedure _IntegerVariable(Param: PExpressionRec);
begin
with Param^ do
PInteger(Res.MemoryPos^)^ := PInteger(Args[0])^;
end;
{
procedure _SmallIntVariable(Param: PExpressionRec);
begin
with Param^ do
PSmallInt(Res.MemoryPos^)^ := PSmallInt(Args[0])^;
end;
}
{$ifdef SUPPORT_INT64}
procedure _LargeIntVariable(Param: PExpressionRec);
begin
with Param^ do
PLargeInt(Res.MemoryPos^)^ := PLargeInt(Args[0])^;
end;
{$endif}
{ TExpressionWord }
constructor TExprWord.Create(AName: string; AExprFunc: TExprFunc);
begin
FName := AName;
FExprFunc := AExprFunc;
end;
function TExprWord.GetCanVary: Boolean;
begin
Result := False;
end;
function TExprWord.GetDescription: string;
begin
Result := EmptyStr;
end;
function TExprWord.GetShortName: string;
begin
Result := EmptyStr;
end;
function TExprWord.GetIsOperator: Boolean;
begin
Result := False;
end;
function TExprWord.GetIsVariable: Boolean;
begin
// delphi wants to call the function pointed to by the variable, use '@'
// fpc simply returns pointer to function, no '@' needed
Result := (@FExprFunc = @_StringVariable) or
(@FExprFunc = @_StringConstant) or
(@FExprFunc = @_FloatVariable) or
(@FExprFunc = @_IntegerVariable) or
// (FExprFunc = @_SmallIntVariable) or
{$ifdef SUPPORT_INT64}
(@FExprFunc = @_LargeIntVariable) or
{$endif}
(@FExprFunc = @_DateTimeVariable) or
(@FExprFunc = @_BooleanVariable);
end;
function TExprWord.GetNeedsCopy: Boolean;
begin
Result := (@FExprFunc <> @_StringConstant) and
// (@FExprFunc <> @_StringVariable) and
// (@FExprFunc <> @_StringVariableFixedLen) and
// string variable cannot be used as normal parameter
// because it is indirectly referenced and possibly
// not null-terminated (fixed len)
(@FExprFunc <> @_FloatVariable) and
(@FExprFunc <> @_IntegerVariable) and
// (FExprFunc <> @_SmallIntVariable) and
{$ifdef SUPPORT_INT64}
(@FExprFunc <> @_LargeIntVariable) and
{$endif}
(@FExprFunc <> @_DateTimeVariable) and
(@FExprFunc <> @_BooleanVariable);
end;
function TExprWord.GetFixedLen: Integer;
begin
// -1 means variable, non-fixed length
Result := -1;
end;
function TExprWord.GetMinFunctionArg: Integer;
begin
Result := 0;
end;
function TExprWord.GetMaxFunctionArg: Integer;
begin
Result := 0;
end;
function TExprWord.GetResultType: TExpressionType;
begin
Result := etUnknown;
end;
function TExprWord.GetTypeSpec: string;
begin
Result := EmptyStr;
end;
function TExprWord.AsPointer: PChar;
begin
Result := nil;
end;
function TExprWord.LenAsPointer: PInteger;
begin
Result := nil;
end;
function TExprWord.IsFunction: Boolean;
begin
Result := False;
end;
procedure TExprWord.SetFixedLen(NewLen: integer);
begin
end;
{ TConstant }
constructor TConstant.Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
begin
inherited Create(AName, AExprFunc);
FResultType := AVarType;
end;
function TConstant.GetResultType: TExpressionType;
begin
Result := FResultType;
end;
{ TFloatConstant }
constructor TFloatConstant.Create(AName, AValue: string);
begin
inherited Create(AName, etFloat, _FloatVariable);
if Length(AValue) > 0 then
FValue := StrToFloat(AValue)
else
FValue := 0.0;
end;
constructor TFloatConstant.CreateAsDouble(AName: string; AValue: Double);
begin
inherited Create(AName, etFloat, _FloatVariable);
FValue := AValue;
end;
function TFloatConstant.AsPointer: PChar;
begin
Result := PChar(@FValue);
end;
{ TUserConstant }
constructor TUserConstant.CreateAsDouble(AName, Descr: string; AValue: Double);
begin
FDescription := Descr;
inherited CreateAsDouble(AName, AValue);
end;
function TUserConstant.GetDescription: string;
begin
Result := FDescription;
end;
{ TStringConstant }
constructor TStringConstant.Create(AValue: string);
var
firstChar, lastChar: Char;
begin
inherited Create(AValue, etString, _StringConstant);
firstChar := AValue[1];
lastChar := AValue[Length(AValue)];
if (firstChar = lastChar) and ((firstChar = '''') or (firstChar = '"')) then
FValue := Copy(AValue, 2, Length(AValue) - 2)
else
FValue := AValue;
end;
function TStringConstant.AsPointer: PChar;
begin
Result := PChar(FValue);
end;
{ TBooleanConstant }
constructor TBooleanConstant.Create(AName: string; AValue: Boolean);
begin
inherited Create(AName, etBoolean, _BooleanVariable);
FValue := AValue;
end;
function TBooleanConstant.AsPointer: PChar;
begin
Result := PChar(@FValue);
end;
{ TIntegerConstant }
constructor TIntegerConstant.Create(AValue: Integer);
begin
inherited Create(IntToStr(AValue), etInteger, _IntegerVariable);
FValue := AValue;
end;
function TIntegerConstant.AsPointer: PChar;
begin
Result := PChar(@FValue);
end;
{ TVariable }
constructor TVariable.Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
begin
inherited Create(AName, AExprFunc);
FResultType := AVarType;
end;
function TVariable.GetCanVary: Boolean;
begin
Result := True;
end;
function TVariable.GetResultType: TExpressionType;
begin
Result := FResultType;
end;
{ TFloatVariable }
constructor TFloatVariable.Create(AName: string; AValue: PDouble);
begin
inherited Create(AName, etFloat, _FloatVariable);
FValue := AValue;
end;
function TFloatVariable.AsPointer: PChar;
begin
Result := PChar(FValue);
end;
{ TStringVariable }
constructor TStringVariable.Create(AName: string; AValue: PPChar);
begin
// variable or fixed length?
inherited Create(AName, etString, _StringVariable);
// store pointer to string
FValue := AValue;
FFixedLen := -1;
end;
function TStringVariable.AsPointer: PChar;
begin
Result := PChar(FValue);
end;
function TStringVariable.GetFixedLen: Integer;
begin
Result := FFixedLen;
end;
function TStringVariable.LenAsPointer: PInteger;
begin
Result := @FFixedLen;
end;
procedure TStringVariable.SetFixedLen(NewLen: integer);
begin
FFixedLen := NewLen;
end;
{ TDateTimeVariable }
constructor TDateTimeVariable.Create(AName: string; AValue: PDateTimeRec);
begin
inherited Create(AName, etDateTime, _DateTimeVariable);
FValue := AValue;
end;
function TDateTimeVariable.AsPointer: PChar;
begin
Result := PChar(FValue);
end;
{ TIntegerVariable }
constructor TIntegerVariable.Create(AName: string; AValue: PInteger);
begin
inherited Create(AName, etInteger, _IntegerVariable);
FValue := AValue;
end;
function TIntegerVariable.AsPointer: PChar;
begin
Result := PChar(FValue);
end;
{$ifdef SUPPORT_INT64}
{ TLargeIntVariable }
constructor TLargeIntVariable.Create(AName: string; AValue: PLargeInt);
begin
inherited Create(AName, etLargeInt, _LargeIntVariable);
FValue := AValue;
end;
function TLargeIntVariable.AsPointer: PChar;
begin
Result := PChar(FValue);
end;
{$endif}
{ TBooleanVariable }
constructor TBooleanVariable.Create(AName: string; AValue: PBoolean);
begin
inherited Create(AName, etBoolean, _BooleanVariable);
FValue := AValue;
end;
function TBooleanVariable.AsPointer: PChar;
begin
Result := PChar(FValue);
end;
{ TLeftBracket }
function TLeftBracket.GetResultType: TExpressionType;
begin
Result := etLeftBracket;
end;
{ TRightBracket }
function TRightBracket.GetResultType: TExpressionType;
begin
Result := etRightBracket;
end;
{ TComma }
function TComma.GetResultType: TExpressionType;
begin
Result := etComma;
end;
{ TExpressList }
constructor TExpressList.Create;
begin
inherited;
FShortList := TExpressShortList.Create;
end;
destructor TExpressList.Destroy;
begin
inherited;
FShortList.Free;
end;
procedure TExpressList.Add(Item: Pointer);
var
I: Integer;
begin
inherited;
{ remember we reference the object }
Inc(TExprWord(Item).FRefCount);
{ also add ShortName as reference }
if Length(TExprWord(Item).ShortName) > 0 then
begin
FShortList.Search(FShortList.KeyOf(Item), I);
FShortList.Insert(I, Item);
end;
end;
function TExpressList.Compare(Key1, Key2: Pointer): Integer;
begin
Result := StrIComp(PChar(Key1), PChar(Key2));
end;
function TExpressList.KeyOf(Item: Pointer): Pointer;
begin
Result := PChar(TExprWord(Item).Name);
end;
procedure TExpressList.FreeItem(Item: Pointer);
begin
Dec(TExprWord(Item).FRefCount);
FShortList.Remove(Item);
if TExprWord(Item).FRefCount = 0 then
inherited;
end;
function TExpressList.Search(Key: Pointer; var Index: Integer): Boolean;
var
SecIndex: Integer;
begin
Result := inherited Search(Key, Index);
if not Result then
begin
Result := FShortList.Search(Key, SecIndex);
if Result then
Index := IndexOf(FShortList.Items[SecIndex]);
end;
end;
function TExpressShortList.Compare(Key1, Key2: Pointer): Integer;
begin
Result := StrIComp(PChar(Key1), PChar(Key2));
end;
function TExpressShortList.KeyOf(Item: Pointer): Pointer;
begin
Result := PChar(TExprWord(Item).ShortName);
end;
procedure TExpressShortList.FreeItem(Item: Pointer);
begin
end;
{ TExprCollection }
procedure TExprCollection.Check;
var
brCount, I: Integer;
begin
brCount := 0;
for I := 0 to Count - 1 do
begin
case TExprWord(Items[I]).ResultType of
etLeftBracket: Inc(brCount);
etRightBracket: Dec(brCount);
end;
end;
if brCount <> 0 then
raise EParserException.Create('Unequal brackets');
end;
procedure TExprCollection.EraseExtraBrackets;
var
I: Integer;
brCount: Integer;
begin
if (TExprWord(Items[0]).ResultType = etLeftBracket) then
begin
brCount := 1;
I := 1;
while (I < Count) and (brCount > 0) do
begin
case TExprWord(Items[I]).ResultType of
etLeftBracket: Inc(brCount);
etRightBracket: Dec(brCount);
end;
Inc(I);
end;
if (brCount = 0) and (I = Count) and (TExprWord(Items[I - 1]).ResultType =
etRightBracket) then
begin
for I := 0 to Count - 3 do
Items[I] := Items[I + 1];
Count := Count - 2;
EraseExtraBrackets; //Check if there are still too many brackets
end;
end;
end;
{ TFunction }
constructor TFunction.Create(AName, AShortName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
AExprFunc: TExprFunc; Descr: string);
begin
//to increase compatibility don't use default parameters
FDescription := Descr;
FShortName := AShortName;
InternalCreate(AName, ATypeSpec, AMinFuncArg, AResultType, AExprFunc, false, 0);
end;
constructor TFunction.CreateOper(AName, ATypeSpec: string; AResultType: TExpressionType;
AExprFunc: TExprFunc; AOperPrec: Integer);
begin
InternalCreate(AName, ATypeSpec, -1, AResultType, AExprFunc, true, AOperPrec);
end;
procedure TFunction.InternalCreate(AName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
AExprFunc: TExprFunc; AIsOperator: Boolean; AOperPrec: Integer);
begin
inherited Create(AName, AExprFunc);
FMaxFunctionArg := Length(ATypeSpec);
FMinFunctionArg := AMinFuncArg;
if AMinFuncArg = -1 then
FMinFunctionArg := FMaxFunctionArg;
FIsOperator := AIsOperator;
FOperPrec := AOperPrec;
FTypeSpec := ATypeSpec;
FResultType := AResultType;
// check correctness
if FMaxFunctionArg > MaxArg then
raise EParserException.Create('Too many arguments');
end;
function TFunction.GetDescription: string;
begin
Result := FDescription;
end;
function TFunction.GetIsOperator: Boolean;
begin
Result := FIsOperator;
end;
function TFunction.GetMinFunctionArg: Integer;
begin
Result := FMinFunctionArg;
end;
function TFunction.GetMaxFunctionArg: Integer;
begin
Result := FMaxFunctionArg;
end;
function TFunction.GetResultType: TExpressionType;
begin
Result := FResultType;
end;
function TFunction.GetShortName: string;
begin
Result := FShortName;
end;
function TFunction.GetTypeSpec: string;
begin
Result := FTypeSpec;
end;
function TFunction.IsFunction: Boolean;
begin
Result := True;
end;
{ TVaryingFunction }
function TVaryingFunction.GetCanVary: Boolean;
begin
Result := True;
end;
{ TDynamicType }
constructor TDynamicType.Create(DestMem, DestPos: PPChar; ASize: PInteger);
begin
inherited Create;
FMemory := DestMem;
FMemoryPos := DestPos;
FSize := ASize;
end;
procedure TDynamicType.Rewind;
begin
FMemoryPos^ := FMemory^;
end;
procedure TDynamicType.AssureSpace(ASize: Integer);