forked from artzub/tdbf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbf_prscore.pas
executable file
·1122 lines (1034 loc) · 32.3 KB
/
dbf_prscore.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_prscore;
{--------------------------------------------------------------
| TCustomExpressionParser
|
| - contains core expression parser
|---------------------------------------------------------------}
interface
{$I dbf_common.inc}
uses
SysUtils,
Classes,
dbf_common,
dbf_prssupp,
dbf_prsdef;
{$define ENG_NUMBERS}
// ENG_NUMBERS will force the use of english style numbers 8.1 instead of 8,1
// (if the comma is your decimal separator)
// the advantage is that arguments can be separated with a comma which is
// fairly common, otherwise there is ambuigity: what does 'var1,8,4,4,5' mean?
// if you don't define ENG_NUMBERS and DecimalSeparator is a comma then
// the argument separator will be a semicolon ';'
type
TCustomExpressionParser = class(TObject)
private
FHexChar: Char;
FArgSeparator: Char;
FDecimalSeparator: Char;
FOptimize: Boolean;
FConstantsList: TOCollection;
FLastRec: PExpressionRec;
FCurrentRec: PExpressionRec;
FExpResult: PChar;
FExpResultPos: PChar;
FExpResultSize: Integer;
procedure ParseString(AnExpression: string; DestCollection: TExprCollection);
function MakeTree(Expr: TExprCollection; FirstItem, LastItem: Integer): PExpressionRec;
procedure MakeLinkedList(var ExprRec: PExpressionRec; Memory: PPChar;
MemoryPos: PPChar; MemSize: PInteger);
procedure Check(AnExprList: TExprCollection);
procedure CheckArguments(ExprRec: PExpressionRec);
procedure RemoveConstants(var ExprRec: PExpressionRec);
function ResultCanVary(ExprRec: PExpressionRec): Boolean;
protected
FWordsList: TSortedCollection;
function MakeRec: PExpressionRec; virtual;
procedure FillExpressList; virtual; abstract;
procedure HandleUnknownVariable(VarName: string); virtual; abstract;
procedure CompileExpression(AnExpression: string);
procedure EvaluateCurrent;
procedure DisposeList(ARec: PExpressionRec);
procedure DisposeTree(ExprRec: PExpressionRec);
function CurrentExpression: string; virtual; abstract;
function GetResultType: TExpressionType; virtual;
property CurrentRec: PExpressionRec read FCurrentRec write FCurrentRec;
property LastRec: PExpressionRec read FLastRec write FLastRec;
property ExpResult: PChar read FExpResult;
property ExpResultPos: PChar read FExpResultPos write FExpResultPos;
public
constructor Create;
destructor Destroy; override;
function DefineFloatVariable(AVarName: string; AValue: PDouble): TExprWord;
function DefineIntegerVariable(AVarName: string; AValue: PInteger): TExprWord;
// procedure DefineSmallIntVariable(AVarName: string; AValue: PSmallInt);
{$ifdef SUPPORT_INT64}
function DefineLargeIntVariable(AVarName: string; AValue: PLargeInt): TExprWord;
{$endif}
function DefineDateTimeVariable(AVarName: string; AValue: PDateTimeRec): TExprWord;
function DefineBooleanVariable(AVarName: string; AValue: PBoolean): TExprWord;
function DefineStringVariable(AVarName: string; AValue: PPChar): TExprWord;
function DefineFunction(AFunctName, AShortName, ADescription, ATypeSpec: string;
AMinFunctionArg: Integer; AResultType: TExpressionType; AFuncAddress: TExprFunc): TExprWord;
procedure Evaluate(AnExpression: string);
function AddExpression(AnExpression: string): Integer;
procedure ClearExpressions; virtual;
// procedure GetGeneratedVars(AList: TList);
procedure GetFunctionNames(AList: TStrings);
function GetFunctionDescription(AFunction: string): string;
property HexChar: Char read FHexChar write FHexChar;
property ArgSeparator: Char read FArgSeparator write FArgSeparator;
property Optimize: Boolean read FOptimize write FOptimize;
property ResultType: TExpressionType read GetResultType;
//if optimize is selected, constant expressions are tried to remove
//such as: 4*4*x is evaluated as 16*x and exp(1)-4*x is repaced by 2.17 -4*x
end;
implementation
procedure LinkVariable(ExprRec: PExpressionRec);
begin
with ExprRec^ do
begin
if ExprWord.IsVariable then
begin
// copy pointer to variable
Args[0] := ExprWord.AsPointer;
// is this a fixed length string variable?
if ExprWord.FixedLen >= 0 then
begin
// store length as second parameter
Args[1] := PChar(ExprWord.LenAsPointer);
end;
end;
end;
end;
procedure LinkVariables(ExprRec: PExpressionRec);
var
I: integer;
begin
with ExprRec^ do
begin
I := 0;
while (I < MaxArg) and (ArgList[I] <> nil) do
begin
LinkVariables(ArgList[I]);
Inc(I);
end;
end;
LinkVariable(ExprRec);
end;
{ TCustomExpressionParser }
constructor TCustomExpressionParser.Create;
begin
inherited;
FHexChar := '$';
{$IFDEF ENG_NUMBERS}
FDecimalSeparator := '.';
FArgSeparator := ',';
{$ELSE}
FDecimalSeparator := DecimalSeparator;
if DecimalSeparator = ',' then
FArgSeparator := ';'
else
FArgSeparator := ',';
{$ENDIF}
FConstantsList := TOCollection.Create;
FWordsList := TExpressList.Create;
GetMem(FExpResult, ArgAllocSize);
FExpResultPos := FExpResult;
FExpResultSize := ArgAllocSize;
FOptimize := true;
FillExpressList;
end;
destructor TCustomExpressionParser.Destroy;
begin
ClearExpressions;
FreeMem(FExpResult);
FConstantsList.Free;
FWordsList.Free;
inherited;
end;
procedure TCustomExpressionParser.CompileExpression(AnExpression: string);
var
ExpColl: TExprCollection;
ExprTree: PExpressionRec;
begin
if Length(AnExpression) > 0 then
begin
ExprTree := nil;
ExpColl := TExprCollection.Create;
try
// FCurrentExpression := anExpression;
ParseString(AnExpression, ExpColl);
Check(ExpColl);
ExprTree := MakeTree(ExpColl, 0, ExpColl.Count - 1);
FCurrentRec := nil;
CheckArguments(ExprTree);
LinkVariables(ExprTree);
if Optimize then
RemoveConstants(ExprTree);
// all constant expressions are evaluated and replaced by variables
FCurrentRec := nil;
FExpResultPos := FExpResult;
MakeLinkedList(ExprTree, @FExpResult, @FExpResultPos, @FExpResultSize);
except
on E: Exception do
begin
DisposeTree(ExprTree);
ExpColl.Free;
raise;
end;
end;
ExpColl.Free;
end;
end;
procedure TCustomExpressionParser.CheckArguments(ExprRec: PExpressionRec);
var
TempExprWord: TExprWord;
I, error, firstFuncIndex, funcIndex: Integer;
foundAltFunc: Boolean;
procedure FindAlternate;
begin
// see if we can find another function
if funcIndex < 0 then
begin
firstFuncIndex := FWordsList.IndexOf(ExprRec^.ExprWord);
funcIndex := firstFuncIndex;
end;
// check if not last function
if (0 <= funcIndex) and (funcIndex < FWordsList.Count - 1) then
begin
inc(funcIndex);
TempExprWord := TExprWord(FWordsList.Items[funcIndex]);
if FWordsList.Compare(FWordsList.KeyOf(ExprRec^.ExprWord), FWordsList.KeyOf(TempExprWord)) = 0 then
begin
ExprRec^.ExprWord := TempExprWord;
ExprRec^.Oper := ExprRec^.ExprWord.ExprFunc;
foundAltFunc := true;
end;
end;
end;
procedure InternalCheckArguments;
begin
I := 0;
error := 0;
foundAltFunc := false;
with ExprRec^ do
begin
if WantsFunction <> (ExprWord.IsFunction and not ExprWord.IsOperator) then
begin
error := 4;
exit;
end;
while (I < ExprWord.MaxFunctionArg) and (ArgList[I] <> nil) and (error = 0) do
begin
// test subarguments first
CheckArguments(ArgList[I]);
// test if correct type
if (ArgList[I]^.ExprWord.ResultType <> ExprCharToExprType(ExprWord.TypeSpec[I+1])) then
error := 2;
// goto next argument
Inc(I);
end;
// test if enough parameters passed; I = num args user passed
if (error = 0) and (I < ExprWord.MinFunctionArg) then
error := 1;
// test if too many parameters passed
if (error = 0) and (I > ExprWord.MaxFunctionArg) then
error := 3;
end;
end;
begin
funcIndex := -1;
repeat
InternalCheckArguments;
// error occurred?
if error <> 0 then
FindAlternate;
until (error = 0) or not foundAltFunc;
// maybe it's an undefined variable
if (error <> 0) and not ExprRec^.WantsFunction and (firstFuncIndex >= 0) then
begin
HandleUnknownVariable(ExprRec^.ExprWord.Name);
{ must not add variable as first function in this set of duplicates,
otherwise following searches will not find it }
FWordsList.Exchange(firstFuncIndex, firstFuncIndex+1);
ExprRec^.ExprWord := TExprWord(FWordsList.Items[firstFuncIndex+1]);
ExprRec^.Oper := ExprRec^.ExprWord.ExprFunc;
InternalCheckArguments;
end;
// fatal error?
case error of
1: raise EParserException.Create('Function or operand has too few arguments');
2: raise EParserException.Create('Argument type mismatch');
3: raise EParserException.Create('Function or operand has too many arguments');
4: raise EParserException.Create('No function with this name, remove brackets for variable');
end;
end;
function TCustomExpressionParser.ResultCanVary(ExprRec: PExpressionRec):
Boolean;
var
I: Integer;
begin
with ExprRec^ do
begin
Result := ExprWord.CanVary;
if not Result then
for I := 0 to ExprWord.MaxFunctionArg - 1 do
if (ArgList[I] <> nil) and ResultCanVary(ArgList[I]) then
begin
Result := true;
Exit;
end
end;
end;
procedure TCustomExpressionParser.RemoveConstants(var ExprRec: PExpressionRec);
var
I: Integer;
begin
if not ResultCanVary(ExprRec) then
begin
if not ExprRec^.ExprWord.IsVariable then
begin
// reset current record so that make list generates new
FCurrentRec := nil;
FExpResultPos := FExpResult;
MakeLinkedList(ExprRec, @FExpResult, @FExpResultPos, @FExpResultSize);
try
// compute result
EvaluateCurrent;
// make new record to store constant in
ExprRec := MakeRec;
// check result type
with ExprRec^ do
begin
case ResultType of
etBoolean: ExprWord := TBooleanConstant.Create(EmptyStr, PBoolean(FExpResult)^);
etFloat: ExprWord := TFloatConstant.CreateAsDouble(EmptyStr, PDouble(FExpResult)^);
etString: ExprWord := TStringConstant.Create(FExpResult);
end;
// fill in structure
Oper := ExprWord.ExprFunc;
Args[0] := ExprWord.AsPointer;
FConstantsList.Add(ExprWord);
end;
finally
DisposeList(FCurrentRec);
FCurrentRec := nil;
end;
end;
end else
with ExprRec^ do
begin
for I := 0 to ExprWord.MaxFunctionArg - 1 do
if ArgList[I] <> nil then
RemoveConstants(ArgList[I]);
end;
end;
procedure TCustomExpressionParser.DisposeTree(ExprRec: PExpressionRec);
var
I: Integer;
begin
if ExprRec <> nil then
begin
with ExprRec^ do
begin
if ExprWord <> nil then
for I := 0 to ExprWord.MaxFunctionArg - 1 do
DisposeTree(ArgList[I]);
if Res <> nil then
Res.Free;
end;
Dispose(ExprRec);
end;
end;
procedure TCustomExpressionParser.DisposeList(ARec: PExpressionRec);
var
TheNext: PExpressionRec;
I: Integer;
begin
if ARec <> nil then
repeat
TheNext := ARec^.Next;
if ARec^.Res <> nil then
ARec^.Res.Free;
I := 0;
while ARec^.ArgList[I] <> nil do
begin
FreeMem(ARec^.Args[I]);
Inc(I);
end;
Dispose(ARec);
ARec := TheNext;
until ARec = nil;
end;
procedure TCustomExpressionParser.MakeLinkedList(var ExprRec: PExpressionRec;
Memory: PPChar; MemoryPos: PPChar; MemSize: PInteger);
var
I: Integer;
begin
// test function type
if @ExprRec^.ExprWord.ExprFunc = nil then
begin
// special 'no function' function
// indicates no function is present -> we can concatenate all instances
// we don't create new arguments...these 'fall' through
// use destination as we got it
I := 0;
while ExprRec^.ArgList[I] <> nil do
begin
// convert arguments to list
MakeLinkedList(ExprRec^.ArgList[I], Memory, MemoryPos, MemSize);
// goto next argument
Inc(I);
end;
// don't need this record anymore
Dispose(ExprRec);
ExprRec := nil;
end else begin
// inc memory pointer so we know if we are first
ExprRec^.ResetDest := MemoryPos^ = Memory^;
Inc(MemoryPos^);
// convert arguments to list
I := 0;
while ExprRec^.ArgList[I] <> nil do
begin
// save variable type for easy access
ExprRec^.ArgsType[I] := ExprRec^.ArgList[I]^.ExprWord.ResultType;
// check if we need to copy argument, variables in general do not
// need copying, except for fixed len strings which are not
// null-terminated
// if ExprRec^.ArgList[I].ExprWord.NeedsCopy then
// begin
// get memory for argument
GetMem(ExprRec^.Args[I], ArgAllocSize);
ExprRec^.ArgsPos[I] := ExprRec^.Args[I];
ExprRec^.ArgsSize[I] := ArgAllocSize;
MakeLinkedList(ExprRec^.ArgList[I], @ExprRec^.Args[I], @ExprRec^.ArgsPos[I],
@ExprRec^.ArgsSize[I]);
// end else begin
// copy reference
// ExprRec^.Args[I] := ExprRec^.ArgList[I].Args[0];
// ExprRec^.ArgsPos[I] := ExprRec^.Args[I];
// ExprRec^.ArgsSize[I] := 0;
// FreeMem(ExprRec^.ArgList[I]);
// ExprRec^.ArgList[I] := nil;
// end;
// goto next argument
Inc(I);
end;
// link result to target argument
ExprRec^.Res := TDynamicType.Create(Memory, MemoryPos, MemSize);
// link to next operation
if FCurrentRec = nil then
begin
FCurrentRec := ExprRec;
FLastRec := ExprRec;
end else begin
FLastRec^.Next := ExprRec;
FLastRec := ExprRec;
end;
end;
end;
function TCustomExpressionParser.MakeTree(Expr: TExprCollection;
FirstItem, LastItem: Integer): PExpressionRec;
{
- This is the most complex routine, it breaks down the expression and makes
a linked tree which is used for fast function evaluations
- it is implemented recursively
}
var
I, IArg, IStart, IEnd, lPrec, brCount: Integer;
ExprWord: TExprWord;
begin
// remove redundant brackets
brCount := 0;
while (FirstItem+brCount < LastItem) and (TExprWord(
Expr.Items[FirstItem+brCount]).ResultType = etLeftBracket) do
Inc(brCount);
I := LastItem;
while (I > FirstItem) and (TExprWord(
Expr.Items[I]).ResultType = etRightBracket) do
Dec(I);
// test max of start and ending brackets
if brCount > (LastItem-I) then
brCount := LastItem-I;
// count number of bracket pairs completely open from start to end
// IArg is min.brCount
I := FirstItem + brCount;
IArg := brCount;
while (I <= LastItem - brCount) and (brCount > 0) do
begin
case TExprWord(Expr.Items[I]).ResultType of
etLeftBracket: Inc(brCount);
etRightBracket:
begin
Dec(brCount);
if brCount < IArg then
IArg := brCount;
end;
end;
Inc(I);
end;
// useful pair bracket count, is in minimum, is IArg
brCount := IArg;
// check if subexpression closed within (bracket level will be zero)
if brCount > 0 then
begin
Inc(FirstItem, brCount);
Dec(LastItem, brCount);
end;
// check for empty range
if LastItem < FirstItem then
begin
Result := nil;
exit;
end;
// get new record
Result := MakeRec;
// simple constant, variable or function?
if LastItem = FirstItem then
begin
Result^.ExprWord := TExprWord(Expr.Items[FirstItem]);
Result^.Oper := Result^.ExprWord.ExprFunc;
exit;
end;
// no...more complex, find operator with lowest precedence
brCount := 0;
IArg := 0;
IEnd := FirstItem-1;
lPrec := -1;
for I := FirstItem to LastItem do
begin
ExprWord := TExprWord(Expr.Items[I]);
if (brCount = 0) and ExprWord.IsOperator and (TFunction(ExprWord).OperPrec > lPrec) then
begin
IEnd := I;
lPrec := TFunction(ExprWord).OperPrec;
end;
case ExprWord.ResultType of
etLeftBracket: Inc(brCount);
etRightBracket: Dec(brCount);
end;
end;
// operator found ?
if IEnd >= FirstItem then
begin
// save operator
Result^.ExprWord := TExprWord(Expr.Items[IEnd]);
Result^.Oper := Result^.ExprWord.ExprFunc;
// recurse into left part if present
if IEnd > FirstItem then
begin
Result^.ArgList[IArg] := MakeTree(Expr, FirstItem, IEnd-1);
Inc(IArg);
end;
// recurse into right part if present
if IEnd < LastItem then
Result^.ArgList[IArg] := MakeTree(Expr, IEnd+1, LastItem);
end else
if TExprWord(Expr.Items[FirstItem]).IsFunction then
begin
// save function
Result^.ExprWord := TExprWord(Expr.Items[FirstItem]);
Result^.Oper := Result^.ExprWord.ExprFunc;
Result^.WantsFunction := true;
// parse function arguments
IEnd := FirstItem + 1;
IStart := IEnd;
brCount := 0;
if TExprWord(Expr.Items[IEnd]).ResultType = etLeftBracket then
begin
// opening bracket found, first argument expression starts at next index
Inc(brCount);
Inc(IStart);
while (IEnd < LastItem) and (brCount <> 0) do
begin
Inc(IEnd);
case TExprWord(Expr.Items[IEnd]).ResultType of
etLeftBracket: Inc(brCount);
etComma:
if brCount = 1 then
begin
// argument separation found, build tree of argument expression
Result^.ArgList[IArg] := MakeTree(Expr, IStart, IEnd-1);
Inc(IArg);
IStart := IEnd + 1;
end;
etRightBracket: Dec(brCount);
end;
end;
// parse last argument
Result^.ArgList[IArg] := MakeTree(Expr, IStart, IEnd-1);
end;
end else
raise EParserException.Create('Operator/function missing');
end;
procedure TCustomExpressionParser.ParseString(AnExpression: string; DestCollection: TExprCollection);
var
isConstant: Boolean;
I, I1, I2, Len, DecSep: Integer;
W, S: string;
TempWord: TExprWord;
procedure ReadConstant(AnExpr: String; isHex: Boolean);
begin
isConstant := true;
while (I2 <= Len) and ({$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['0'..'9']) or
(isHex and {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['a'..'f', 'A'..'F']))) do
Inc(I2);
if I2 <= Len then
begin
if AnExpr[I2] = FDecimalSeparator then
begin
Inc(I2);
while (I2 <= Len) and ({$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['0'..'9'])) do
Inc(I2);
end;
if (I2 <= Len) and (AnExpr[I2] = 'e') then
begin
Inc(I2);
if (I2 <= Len) and {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['+', '-']) then
Inc(I2);
while (I2 <= Len) and ({$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['0'..'9'])) do
Inc(I2);
end;
end;
end;
procedure ReadWord(AnExpr: String);
var
OldI2: Integer;
constChar: Char;
begin
isConstant := false;
I1 := I2;
while (I1 < Len) and (AnExpr[I1] = ' ') do
Inc(I1);
I2 := I1;
if I1 <= Len then
begin
if AnExpr[I2] = HexChar then
begin
Inc(I2);
OldI2 := I2;
ReadConstant(AnExpr, true);
if I2 = OldI2 then
begin
isConstant := false;
while (I2 <= Len) and {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['a'..'z', 'A'..'Z', '_', '0'..'9']) do
Inc(I2);
end;
end
else if AnExpr[I2] = FDecimalSeparator then
ReadConstant(AnExpr, false)
else
case AnExpr[I2] of
'''', '"':
begin
isConstant := true;
constChar := AnExpr[I2];
Inc(I2);
while (I2 <= Len) and (AnExpr[I2] <> constChar) do
Inc(I2);
if I2 <= Len then
Inc(I2);
end;
'a'..'z', 'A'..'Z', '_':
begin
while (I2 <= Len) and {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['a'..'z', 'A'..'Z', '_', '0'..'9']) do
Inc(I2);
end;
'>', '<':
begin
if (I2 <= Len) then
Inc(I2);
if {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['=', '<', '>']) then
Inc(I2);
end;
'=':
begin
if (I2 <= Len) then
Inc(I2);
if {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['=', '<', '>']) then
Inc(I2);
end;
'&':
begin
if (I2 <= Len) then
Inc(I2);
if {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['&']) then
Inc(I2);
end;
'|':
begin
if (I2 <= Len) then
Inc(I2);
if {$IFDEF DELPHI_2009}CharInSet{$ENDIF}(AnExpr[I2] {$IFDEF DELPHI_2009},{$ELSE} in {$ENDIF}['|']) then
Inc(I2);
end;
':':
begin
if (I2 <= Len) then
Inc(I2);
if AnExpr[I2] = '=' then
Inc(I2);
end;
'!':
begin
if (I2 <= Len) then
Inc(I2);
if AnExpr[I2] = '=' then //support for !=
Inc(I2);
end;
'+':
begin
Inc(I2);
if (AnExpr[I2] = '+') and FWordsList.Search(PChar('++'), I) then
Inc(I2);
end;
'-':
begin
Inc(I2);
if (AnExpr[I2] = '-') and FWordsList.Search(PChar('--'), I) then
Inc(I2);
end;
'^', '/', '\', '*', '(', ')', '%', '~', '$':
Inc(I2);
'0'..'9':
ReadConstant(AnExpr, false);
else
begin
Inc(I2);
end;
end;
end;
end;
begin
I2 := 1;
S := Trim(AnExpression);
Len := Length(S);
repeat
ReadWord(S);
W := Trim(Copy(S, I1, I2 - I1));
if isConstant then
begin
if W[1] = HexChar then
begin
// convert hexadecimal to decimal
W[1] := '$';
W := IntToStr(StrToInt(W));
end;
if (W[1] = '''') or (W[1] = '"') then
TempWord := TStringConstant.Create(W)
else begin
DecSep := Pos(FDecimalSeparator, W);
if (DecSep > 0) then
begin
{$IFDEF ENG_NUMBERS}
// we'll have to convert FDecimalSeparator into DecimalSeparator
// otherwise the OS will not understand what we mean
{$IFDEF DELPHI_XE3}
W[DecSep] := FormatSettings.DecimalSeparator;
{$ELSE}
W[DecSep] := DecimalSeparator;
{$ENDIF}
{$ENDIF}
TempWord := TFloatConstant.Create(W, W)
end else begin
TempWord := TIntegerConstant.Create(StrToInt(W));
end;
end;
DestCollection.Add(TempWord);
FConstantsList.Add(TempWord);
end
else if Length(W) > 0 then
if FWordsList.Search(PChar(W), I) then
begin
DestCollection.Add(FWordsList.Items[I])
end else begin
// unknown variable -> fire event
HandleUnknownVariable(W);
// try to search again
if FWordsList.Search(PChar(W), I) then
begin
DestCollection.Add(FWordsList.Items[I])
end else begin
raise EParserException.Create('Unknown variable '''+W+''' found.');
end;
end;
until I2 > Len;
end;
procedure TCustomExpressionParser.Check(AnExprList: TExprCollection);
var
I, J, K, L: Integer;
begin
AnExprList.Check;
with AnExprList do
begin
I := 0;
while I < Count do
begin
{----CHECK ON DOUBLE MINUS OR DOUBLE PLUS----}
if ((TExprWord(Items[I]).Name = '-') or
(TExprWord(Items[I]).Name = '+'))
and ((I = 0) or
(TExprWord(Items[I - 1]).ResultType = etComma) or
(TExprWord(Items[I - 1]).ResultType = etLeftBracket) or
(TExprWord(Items[I - 1]).IsOperator and (TExprWord(Items[I - 1]).MaxFunctionArg
= 2))) then
begin
{replace e.g. ----1 with +1}
if TExprWord(Items[I]).Name = '-' then
K := -1
else
K := 1;
L := 1;
while (I + L < Count) and ((TExprWord(Items[I + L]).Name = '-')
or (TExprWord(Items[I + L]).Name = '+')) and ((I + L = 0) or
(TExprWord(Items[I + L - 1]).ResultType = etComma) or
(TExprWord(Items[I + L - 1]).ResultType = etLeftBracket) or
(TExprWord(Items[I + L - 1]).IsOperator and (TExprWord(Items[I + L -
1]).MaxFunctionArg = 2))) do
begin
if TExprWord(Items[I + L]).Name = '-' then
K := -1 * K;
Inc(L);
end;
if L > 0 then
begin
Dec(L);
for J := I + 1 to Count - 1 - L do
Items[J] := Items[J + L];
Count := Count - L;
end;
if K = -1 then
begin
if FWordsList.Search(pchar('-@'), J) then
Items[I] := FWordsList.Items[J];
end
else if FWordsList.Search(pchar('+@'), J) then
Items[I] := FWordsList.Items[J];
end;
{----CHECK ON DOUBLE NOT----}
if (TExprWord(Items[I]).Name = 'not')
and ((I = 0) or
(TExprWord(Items[I - 1]).ResultType = etLeftBracket) or
TExprWord(Items[I - 1]).IsOperator) then
begin
{replace e.g. not not 1 with 1}
K := -1;
L := 1;
while (I + L < Count) and (TExprWord(Items[I + L]).Name = 'not') and ((I
+ L = 0) or
(TExprWord(Items[I + L - 1]).ResultType = etLeftBracket) or
TExprWord(Items[I + L - 1]).IsOperator) do
begin
K := -K;
Inc(L);
end;
if L > 0 then
begin
if K = 1 then
begin //remove all
for J := I to Count - 1 - L do
Items[J] := Items[J + L];
Count := Count - L;
end
else
begin //keep one
Dec(L);
for J := I + 1 to Count - 1 - L do
Items[J] := Items[J + L];
Count := Count - L;
end
end;
end;
{-----MISC CHECKS-----}
if (TExprWord(Items[I]).IsVariable) and ((I < Count - 1) and
(TExprWord(Items[I + 1]).IsVariable)) then
raise EParserException.Create('Missing operator between '''+TExprWord(Items[I]).Name+''' and '''+TExprWord(Items[I]).Name+'''');
if (TExprWord(Items[I]).ResultType = etLeftBracket) and (I >= Count - 1) then
raise EParserException.Create('Missing closing bracket');
if (TExprWord(Items[I]).ResultType = etRightBracket) and ((I < Count - 1) and
(TExprWord(Items[I + 1]).ResultType = etLeftBracket)) then
raise EParserException.Create('Missing operator between )(');
if (TExprWord(Items[I]).ResultType = etRightBracket) and ((I < Count - 1) and
(TExprWord(Items[I + 1]).IsVariable)) then
raise EParserException.Create('Missing operator between ) and constant/variable');
if (TExprWord(Items[I]).ResultType = etLeftBracket) and ((I > 0) and
(TExprWord(Items[I - 1]).IsVariable)) then
raise EParserException.Create('Missing operator between constant/variable and (');
{-----CHECK ON INTPOWER------}
if (TExprWord(Items[I]).Name = '^') and ((I < Count - 1) and
(TExprWord(Items[I + 1]).ClassType = TIntegerConstant)) then
if FWordsList.Search(PChar('^@'), J) then
Items[I] := FWordsList.Items[J]; //use the faster intPower if possible
Inc(I);
end;
end;
end;
procedure TCustomExpressionParser.EvaluateCurrent;
var
TempRec: PExpressionRec;
begin
if FCurrentRec <> nil then
begin
// get current record
TempRec := FCurrentRec;
// execute list
repeat
with TempRec^ do
begin
// do we need to reset pointer?
if ResetDest then
Res.MemoryPos^ := Res.Memory^;
Oper(TempRec);
// goto next
TempRec := Next;
end;
until TempRec = nil;
end;
end;
function TCustomExpressionParser.DefineFunction(AFunctName, AShortName, ADescription, ATypeSpec: string;
AMinFunctionArg: Integer; AResultType: TExpressionType; AFuncAddress: TExprFunc): TExprWord;
begin
Result := TFunction.Create(AFunctName, AShortName, ATypeSpec, AMinFunctionArg, AResultType, AFuncAddress, ADescription);
FWordsList.Add(Result);
end;
function TCustomExpressionParser.DefineIntegerVariable(AVarName: string; AValue: PInteger): TExprWord;
begin
Result := TIntegerVariable.Create(AVarName, AValue);
FWordsList.Add(Result);
end;
{$ifdef SUPPORT_INT64}
function TCustomExpressionParser.DefineLargeIntVariable(AVarName: string; AValue: PLargeInt): TExprWord;
begin
Result := TLargeIntVariable.Create(AVarName, AValue);
FWordsList.Add(Result);
end;
{$endif}
function TCustomExpressionParser.DefineDateTimeVariable(AVarName: string; AValue: PDateTimeRec): TExprWord;
begin
Result := TDateTimeVariable.Create(AVarName, AValue);
FWordsList.Add(Result);
end;
function TCustomExpressionParser.DefineBooleanVariable(AVarName: string; AValue: PBoolean): TExprWord;
begin
Result := TBooleanVariable.Create(AVarName, AValue);
FWordsList.Add(Result);
end;
function TCustomExpressionParser.DefineFloatVariable(AVarName: string; AValue: PDouble): TExprWord;
begin
Result := TFloatVariable.Create(AVarName, AValue);
FWordsList.Add(Result);
end;
function TCustomExpressionParser.DefineStringVariable(AVarName: string; AValue: PPChar): TExprWord;