-
Notifications
You must be signed in to change notification settings - Fork 15
/
rpeval.pas
1514 lines (1436 loc) · 39.2 KB
/
rpeval.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
{*******************************************************}
{ }
{ Rpeval }
{ TRpCustomEvaluator: The Expression evaluator for}
{ Report Manager }
{ }
{ Copyright (c) 1994-2019 Toni Martir }
{ }
{ This file is under the MPL license }
{ If you enhace this file you must provide }
{ source code }
{ }
{ }
{*******************************************************}
unit rpeval;
interface
{$I rpconf.inc}
{$R rpeval.dcr}
uses
SysUtils, Classes,DB,rptypeval,
rpmdconsts,sysconst,rpparser,
{$IFDEF USEEVALHASH}
rpstringhash,
{$ENDIF}
{$IFDEF USEREPORTFUNC}
rpalias,
{$ENDIF}
{$IFDEF USEVARIANTS}
Variants,
{$ENDIF}
rptypes;
type
TRpCustomEvaluator=class(TComponent)
private
// Component to access fields
Rpfunctions:TStringList;
{$IFDEF USEREPORTFUNC}
fRpalias:TRpalias;
{$ENDIF}
// Error information
FError:string;
FPosError:LongInt;
FLineError:Word;
// The parser
Rpparser:TRpparser;
// The expresion to evaluate
FExpression:string;
// Result of the evaluation
FEvalResult:TRpValue;
{$IFDEF USEEVALHASH}
FIdentifiers:TStringHash;
{$ENDIF}
{$IFNDEF USEEVALHASH}
FIdentifiers:TStringList;
{$ENDIF}
FPartial:TRpValue;
// Variable that contains if we are doing syntax checking
FChecking:Boolean;
FLanguage:Integer;
FOnGraphicOp:TRpGraphicOpProc;
FOnOrientationOp:TRpOrientationOpProc;
FOnPageOp:TRpPageOpProc;
FOnImageOp:TRpImageOpProc;
FOnBarcodeOp:TRpBarcodeOpProc;
FOnReOpenOp:TRpReOpenOp;
FOnTextOp:TRpTextOpProc;
FOnTextHeight:TRpTextHeightProc;
FOnNewLanguage:TRpNewLanguage;
FOnGetSQLValue:TRpOnGetSQLValue;
FOnParamInfo:TRpParamInfoProc;
procedure SetExpression(Value:string);
// Recursive functions to evaluate the expresion
procedure variables(var Value:TRpValue);
procedure separator(var Value:TRpValue);
procedure logicalOR(var Value:TRpValue);
procedure logicalAND(var Value:TRpValue);
procedure comparations(var Value:TRpValue);
procedure sum_dif(var Value:TRpValue);
procedure mul_div(var Value:TRpValue);
procedure dosign(var Value:TRpValue);
procedure ExecuteIIF(var Value:TRpValue);
procedure parentesis(var Value:TRpValue);
procedure operand(var Value:TRpValue);
// Aditional priva procedures
function EvaluateExpression:TRpValue;
// Searching indentifiers
function Searchwithoutdot(name1:string):TRpIdentifier;
function GetEvalResultString:String;
procedure AddIdentifiers;
procedure Freerprmfunctions;
procedure InitRpFunctions;
procedure FillFunctions;
protected
procedure Notification(AComponent:TComponent;Operation:TOperation);override;
public
// To avoid infinite recursive
Evaluating:Boolean;
// Creation and destruction
constructor Create(AOwner:TComponent);override;
constructor CreateWithoutiden(AOwner:TComponent;AddIdens:boolean);
destructor Destroy;override;
// Adds identifiers
procedure AddVariable(name1:string;objecte:TRpIdentifier);
procedure AddIden(name1:string;objecte:TRpIdentifier);
function NewVariable(name1:string;ValueIni:TRpValue):TIdenVariable;
// Searching identifiers
function Searchidentifier(name1:WideString):TRpIdentifier;
// The evaluation procedure
procedure Evaluate;
// The evaluation procedure without Expression property
function EvaluateText(text:string):TRpValue;
function GetStreamFromExpression(atext:WideString):TMemoryStream;
// Checking Syntax
procedure CheckSyntax;
property Expression:string Read FExpression write SetExpression;
property EvalResult:TRpValue Read FEvalResult;
// The identifiers including functions
{$IFDEF USEEVALHASH}
property Identifiers:TStringHash read FIdentifiers
{$ENDIF}
{$IFNDEF USEEVALHASH}
property Identifiers:TStringList read FIdentifiers
{$ENDIF}
write FIdentifiers;
property Checking:Boolean read FChecking;
// Error information
property Error:string read FError;
property PosError:LongInt read FPosError;
property LineError:Word read FLineError;
property EvalResultString:string read GetEvalResultString;
// Database access component link
{$IFDEF USEREPORTFUNC}
property Rpalias:TRpalias read FRpalias write FRpalias;
{$ENDIF}
property Language:Integer read FLanguage write FLanguage;
property OnGraphicOp:TRpGraphicOpProc read FOnGraphicOp write FOnGraphicOp;
property OnOrientationOp:TRpOrientationOpProc read FOnOrientationOp write FOnOrientationOp;
property OnPageOp:TRpPageOpProc read FOnPageOp write FOnPageOp;
property OnImageOp:TRpImageOpProc read FOnImageOp write FOnImageOp;
property OnBarcodeOp:TRpbarcodeOpProc read FOnBarcodeOp write FOnBarcodeOp;
property OnTextOp:TRpTextOpProc read FOnTextOp write FOnTextOp;
property OnTextHeight:TRpTextHeightProc read FOnTextHeight write FOnTextHeight;
property OnReOpenOp:TRpReOpenOp read FOnReOpenOp write FOnReOpenOp;
property OnGetSQLValue:TRpOnGetSQLValue read FOnGetSQLValue write FOnGetSQLValue;
property OnNewLanguage:TRpNewLanguage read FOnNewLanguage write FOnNewLanguage;
property OnParamInfo:TRpParamInfoProc read FOnParamInfo write FOnParamInfo;
end;
// The visual component
TRpEvaluator = class(TRpCustomEvaluator)
private
protected
public
{ Public declarations }
published
{ Published declarations }
{$IFDEF USEREPORTFUNC}
property Rpalias;
{$ENDIF}
property EvalResult;
property Expression;
end;
function EvaluateExpression(aexpression:WideString):Variant;
implementation
uses rpevalfunc;
{ Paradox graphic BLOB header }
type
TGraphicHeader = record
Count: Word; { Fixed at 1 }
HType: Word; { Fixed at $0100 }
Size: Longint; { Size not including header }
end;
// TRpCustomEvaluator
constructor TRpCustomEvaluator.CreateWithoutiden(AOwner:TComponent;AddIdens:boolean);
begin
inherited Create(AOwner);
Evaluating:=false;
FExpression:=String(chr(0));
// Creates de parser
Rpparser:=TRpparser.Create;
// The identifiers list
{$IFDEF USEEVALHASH}
FIdentifiers:=TStringHash.Create;
{$ENDIF}
{$IFNDEF USEEVALHASH}
FIdentifiers:=TStringList.Create;
{$ENDIF}
// FIdentifiers.Sorted:=True;
// FIdentifiers.Duplicates:=dupError;
if AddIdens then
AddIdentifiers;
end;
constructor TRpCustomEvaluator.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
InitRpFunctions;
Evaluating:=false;
FExpression:=String(chr(0));
// The parser
Rpparser:=TRpparser.Create;
// The identifiers
{$IFNDEF USEEVALHASH}
FIdentifiers:=TStringList.Create;
{$ENDIF}
{$IFDEF USEEVALHASH}
FIdentifiers:=TStringHash.Create;
{$ENDIF}
// FIdentifiers.Sorted:=True;
// FIdentifiers.Duplicates:=dupError;
// Always add with this constructor
AddIdentifiers;
end;
procedure TRpCustomEvaluator.FillFunctions;
var iden:TRpIdentifier;
begin
if Rpfunctions.count>0 then
exit;
// Boolean constants
iden:=TIdenTrue.Create(nil);
Rpfunctions.AddObject('TRUE',iden);
iden:=TIdenFalse.Create(nil);
Rpfunctions.AddObject('FALSE',iden);
iden:=TIdenTrue.Create(nil);
Rpfunctions.AddObject('CIERTO',iden);
iden:=TIdenFalse.Create(nil);
Rpfunctions.AddObject('FALSO',iden);
// Datetime constants
iden:=TIdenToday.Create(nil);
Rpfunctions.AddObject('TODAY',iden);
iden:=TIdenTime.Create(nil);
Rpfunctions.AddObject('TIME',iden);
iden:=TIdenNow.Create(nil);
Rpfunctions.AddObject('NOW',iden);
// Null constant
iden:=TIdenNULL.Create(nil);
Rpfunctions.AddObject('NULL',iden);
// Functions
iden:=TIdenSinus.Create(nil);
Rpfunctions.AddObject('SIN',iden);
iden:=TIdenMax.Create(nil);
Rpfunctions.AddObject('MAX',iden);
iden:=TIdenFloatToDateTime.Create(nil);
Rpfunctions.AddObject('FLOATTODATETIME',iden);
iden:=TIdenRound.Create(nil);
Rpfunctions.AddObject('ROUND',iden);
iden:=TIdenRoundToInteger.Create(nil);
Rpfunctions.AddObject('ROUNDTOINTEGER',iden);
iden:=TIdenInt.Create(nil);
Rpfunctions.AddObject('INT',iden);
iden:=TIdenAbs.Create(nil);
Rpfunctions.AddObject('ABS',iden);
iden:=TIdenCompareValue.Create(nil);
Rpfunctions.AddObject('COMPAREVALUE',iden);
iden:=TIdenSQRT.Create(nil);
Rpfunctions.AddObject('SQRT',iden);
iden:=TIdenASC2.Create(nil);
Rpfunctions.AddObject('ASC2',iden);
iden:=TIdenFieldExists.Create(nil);
Rpfunctions.AddObject('IDENTEXISTS',iden);
iden:=TIdenIsInteger.Create(nil);
Rpfunctions.AddObject('ISINTEGER',iden);
iden:=TIdenIsNumeric.Create(nil);
Rpfunctions.AddObject('ISNUMERIC',iden);
iden:=TIdenIsValidDateTime.Create(nil);
Rpfunctions.AddObject('ISVALIDDATETIME',iden);
iden:=TIdenCheckExpression.Create(nil);
Rpfunctions.AddObject('CHECKEXPRESSION',iden);
{$IFDEF MSWINDOWS}
{$IFNDEF FPC}
iden:=TIdenChsToCht.Create(nil);
Rpfunctions.AddObject('CHSTOCHT',iden);
iden:=TIdenChtToChs.Create(nil);
Rpfunctions.AddObject('CHTTOCHS',iden);
{$ENDIF}
{$ENDIF}
iden := TIdenGetINIValue.Create(nil);
Rpfunctions.AddObject('GETINIVALUE', iden);
{$IFDEF USEINDY}
iden := TIdenDecode64.Create(nil);
Rpfunctions.AddObject('DECODE64', iden);
{$ENDIF}
iden := TIdenStringToBin.Create(nil);
Rpfunctions.AddObject('STRINGTOBIN', iden);
iden := TIdenLoadFile.Create(nil);
Rpfunctions.AddObject('LOADFILE', iden);
// Functions for compatibility with Gestor reports
iden:=TIdenVariable.Create(nil);
iden.Value:=True;
RpFunctions.AddObject('MODOGRAFICO',iden);
iden:=TIdenSTR.Create(nil);
Rpfunctions.AddObject('STR',iden);
iden:=TIdenVal.Create(nil);
Rpfunctions.AddObject('VAL',iden);
iden:=TIdenLEFT.Create(nil);
Rpfunctions.AddObject('LEFT',iden);
iden:=TIdenLENGTH.Create(nil);
Rpfunctions.AddObject('LENGTH',iden);
iden:=TIdenTrim.Create(nil);
Rpfunctions.AddObject('TRIM',iden);
iden:=TIdenPos.Create(nil);
Rpfunctions.AddObject('POS',iden);
iden:=TIdenModul.Create(nil);
Rpfunctions.AddObject('MOD',iden);
iden:=TIdenMonthname.Create(nil);
Rpfunctions.AddObject('MONTHNAME',iden);
iden:=TIdenMonth.Create(nil);
Rpfunctions.AddObject('MONTH',iden);
iden:=TIdenYear.Create(nil);
Rpfunctions.AddObject('YEAR',iden);
iden:=TIdenDay.Create(nil);
Rpfunctions.AddObject('DAY',iden);
iden:=TIdenRight.Create(nil);
Rpfunctions.AddObject('RIGHT',iden);
iden:=TIdenSubstr.Create(nil);
Rpfunctions.AddObject('SUBSTR',iden);
iden:=TIdenFormatstr.Create(nil);
Rpfunctions.AddObject('FORMATSTR',iden);
iden:=TIdenFormatNum.Create(nil);
Rpfunctions.AddObject('FORMATNUM',iden);
iden:=TIdenFormatMask.Create(nil);
Rpfunctions.AddObject('FORMATMASK',iden);
iden:=TIdenHourMinSec.Create(nil);
Rpfunctions.AddObject('HOURMINSEC',iden);
iden:=TIdenUppercase.Create(nil);
Rpfunctions.AddObject('UPPERCASE',iden);
iden:=TIdenFileExists.Create(nil);
Rpfunctions.AddObject('FILEEXISTS',iden);
iden:=TIdenLowercase.Create(nil);
Rpfunctions.AddObject('LOWERCASE',iden);
iden:=TIdenEvalText.Create(nil);
Rpfunctions.AddObject('EVALTEXT',iden);
iden:=TIdenNumToText.Create(nil);
Rpfunctions.AddObject('NUMTOTEXT',iden);
//added FRB 20030204
iden:=TIdenReplaceStr.Create(nil);
Rpfunctions.AddObject('REPLACESTR',iden);
// Advanced reporting drawing operations
iden:=TIdenOrientationOperation.Create(nil);
Rpfunctions.AddObject('SETPAGEORIENTATION',iden);
iden:=TIdenPageOperation.Create(nil);
Rpfunctions.AddObject('SETPAGESOURCE',iden);
iden:=TIdenGraphicOperation.Create(nil);
Rpfunctions.AddObject('GRAPHICOP',iden);
iden:=TIdenTextOperation.Create(nil);
Rpfunctions.AddObject('TEXTOP',iden);
iden:=TIdenTextHeight.Create(nil);
Rpfunctions.AddObject('TEXTHEIGHT',iden);
iden:=TIdenImageOperation.Create(nil);
Rpfunctions.AddObject('IMAGEOP',iden);
iden:=TIdenBarcodeOperation.Create(nil);
Rpfunctions.AddObject('BARCODEOP',iden);
iden:=TIdenParamInfo.Create(nil);
Rpfunctions.AddObject('PARAMINFO',iden);
iden:=TIdenUTF8ToWideString.Create(nil);
Rpfunctions.AddObject('UTF8TOWIDESTRING',iden);
// Other
// Graphic functions
iden:=TIdenGraphicClear.Create(nil);
Rpfunctions.AddObject('GRAPHICCLEAR',iden);
iden:=TIdenGraphicNew.Create(nil);
Rpfunctions.AddObject('GRAPHICNEW',iden);
iden:=TIdenGraphicNewXY.Create(nil);
Rpfunctions.AddObject('GRAPHICNEWXY',iden);
iden:=TIdenGraphicFunction.Create(nil);
Rpfunctions.AddObject('GRAPHICNEWFUNCTION',iden);
iden:=TIdenGraphicColor.Create(nil);
Rpfunctions.AddObject('GRAPHICCOLOR',iden);
iden:=TIdenGraphicSerieColor.Create(nil);
Rpfunctions.AddObject('GRAPHICSERIECOLOR',iden);
iden:=TIdenGraphicBounds.Create(nil);
Rpfunctions.AddObject('GRAPHICBOUNDS',iden);
// SQl functions
iden:=TIdenGetValueFromSQL.Create(nil);
Rpfunctions.AddObject('GETVALUEFROMSQL',iden);
//
iden:=TIdenReOpenOp.Create(nil);
Rpfunctions.AddObject('REOPEN',iden);
//
iden:=TIdenSetLanguage.Create(nil);
Rpfunctions.AddObject('SETLANGUAGE',iden);
end;
// Adds the identifiers that are on cache
procedure TRpCustomEvaluator.AddIdentifiers;
var
i:integer;
begin
FillFunctions;
for i:=0 to Rpfunctions.Count-1 do
{$IFDEF USEEVALHASH}
Fidentifiers.setValue(RpFunctions.Strings[i],RpFunctions.Objects[i]);
{$ENDIF}
{$IFNDEF USEEVALHASH}
Fidentifiers.AddObject(RpFunctions.Strings[i],RpFunctions.Objects[i]);
{$ENDIF}
// Identifiers.Assign(Rpfunctions);
end;
destructor TRpCustomEvaluator.Destroy;
begin
if Rpparser<>nil then
Rpparser.free;
FIdentifiers.free;
FreeRprmFunctions;
inherited Destroy;
end;
procedure TRpCustomEvaluator.SetExpression(Value:string);
begin
if Evaluating then
Raise Exception.Create(SRpsetexpression);
FExpression:=Value;
end;
// To evaluate a text we must create another evaluator
function TRpCustomEvaluator.EvaluateText(text:string):TRpValue;
var eval:TRpCustomEvaluator;
{$IFDEF USEEVALHASH}
oldiden:TStringHash;
{$ENDIF}
{$IFNDEF USEEVALHASH}
oldiden:TStringList;
{$ENDIF}
begin
oldiden:=nil;
if Evaluating then
begin
eval:=TRpCustomEvaluator.CreateWithoutiden(Self,false);
try
oldiden:=eval.Identifiers;
eval.Identifiers:=Identifiers;
{$IFDEF USEREPORTFUNC}
eval.Rpalias:=FRpalias;
{$ENDIF}
eval.Expression:=text;
eval.Evaluate;
Result:=eval.EvalResult;
finally
eval.Identifiers:=oldiden;
eval.free;
end;
end
else
begin
Expression:=text;
Evaluate;
Result:=EvalResult;
end;
end;
// Checking for syntax
procedure TRpCustomEvaluator.CheckSyntax;
begin
Rpparser.Expression:=FExpression;
if Rpparser.TokenString='' then
begin
FEvalResult:=True;
Exit;
end;
FChecking:=True;
try
EvaluateExpression;
FEvalResult:=True;
except
FEvalResult:=False;
Raise;
end;
end;
procedure TRpCustomEvaluator.Evaluate;
begin
Rpparser.Expression:=FExpression;
FChecking:=False;
if ((Rpparser.TokenString='') AND (Not (Rpparser.Token in [tkString,toWString]))) then
begin
FEvalResult:=True;
Exit;
end;
FEvalResult:=EvaluateExpression;
end;
function TRpCustomEvaluator.EvaluateExpression:TRpValue;
begin
FPartial:=varnull;
try
Evaluating:=True;
try
// Call the recursive tree to evaluate the expresion
separator(FPartial);
finally
Evaluating:=False;
end;
except
// We can assign error information here
on E:EZeroDivide do
begin
FError:=SRpDivisioZero;
FLineError:=Rpparser.SourceLine;
FPosError:=Rpparser.SourcePos;
Raise TRpEvalException.Create(FError,
Rpparser.TokenString,FLineError,FPosError);
end;
On E:TRpNamedException do
begin
FError:=E.ErrorMessage;
FLineError:=Rpparser.SourceLine;
FPosError:=Rpparser.SourcePos;
Raise TRpEvalException.Create(FError+' '''+E.ElementError+'''',
Rpparser.TokenString,FLineError,FPosError)
end;
on EParserError do
begin
FError:=SRpEvalSyntax;
FLineError:=Rpparser.SourceLine;
FPosError:=Rpparser.SourcePos;
Raise TRpEvalException.Create(Ferror,
Rpparser.TokenString,FLineError,FPosError);
end;
on E:TRpEvalException do
begin
FError:=E.ErrorMessage;
FLineError:=E.ErrorLine;
FPosError:=E.ErrorPosition;
Raise;
end;
on E:EVariantError do
begin
if E.Message=SInvalidCast then
begin
FError:=SRpEvalType;
FLineError:=Rpparser.SourceLine;
FPosError:=Rpparser.SourcePos;
Raise TRpEvalException.Create(Ferror,
Rpparser.TokenString,FLineError,FPosError);
end
else
if E.Message=SInvalidVarOp then
begin
FError:=SRpInvalidOperation;
FLineError:=Rpparser.SourceLine;
FPosError:=Rpparser.SourcePos;
Raise TRpEvalException.Create(Ferror,
Rpparser.TokenString,FLineError,FPosError);
end;
end;
else
begin
FError:=SRpEvalSyntax;
FLineError:=Rpparser.SourceLine;
FPosError:=Rpparser.SourcePos;
Raise;
end;
end;
if Rpparser.Token<>toEOF then
begin
FError:=SRpEvalSyntax;
FLineError:=Rpparser.SourceLine;
FPosError:=Rpparser.SourcePos;
Raise TRpEvalException.Create(SRpEvalSyntax+Rpparser.TokenString,
Rpparser.TokenString,Rpparser.SourceLine,Rpparser.SourcePos);
end;
Result:=FPartial;
end;
procedure TRpCustomEvaluator.variables(var Value:TRpValue);
var
iden:TRpIdentifier;
begin
if Rpparser.Token=toSymbol then
begin
// Exists this identifier?
iden:=Searchidentifier(Rpparser.TokenString);
if iden=nil then
begin
Raise TRpEvalException.Create(SRpEvalDescIden+':'+
Rpparser.TokenString,Rpparser.TokenString,
Rpparser.SourceLine,Rpparser.SourcePos);
end
else
begin
iden.evaluator:=self;
// Is a variable?
if iden.RType=RTypeidenvariable then
begin
// Is a := , so we must assign
if Rpparser.NextTokenIs(':=') then
begin
Rpparser.NextToken;
// The :=
Rpparser.NextToken;
// Look for the value
variables(Value);
// If syntax checking not touch the variable
if (Not FChecking) then
(iden As TIdenVariable).Value:=Value;
if ((Rpparser.Token=toOperator) and (Rpparser.TokenString[1]=';')) then
separator(Value);
end
else
// not a := we must continue
logicalOR(Value);
end
else
// look for it
logicalOR(Value);
end
end
else
begin
// look for it
logicalOR(Value);
end;
end;
{**************************************************************************}
procedure TRpCustomEvaluator.logicalOR(var Value:TRpValue);
var operador:string[3];
Auxiliar,Auxiliar2:TRpValue;
begin
// First precedes the AND operator
logicalAND(Value);
if Rpparser.Token=toOperator then
begin
operador:=UpperCase(Rpparser.TokenString);
while (operador='OR') do
begin
Auxiliar2:=Value;
Rpparser.NextToken;
logicalAND(Auxiliar);
// Compatible types?
if (Not FChecking) then
Value:=LogicalORTRpValue(Auxiliar2,Auxiliar);
if Rpparser.Token<>toOperator then
Exit
else
operador:=UpperCase(Rpparser.TokenString);
end;
end;
end;
procedure TRpCustomEvaluator.logicalAND(var Value:TRpValue);
var operador:string[3];
Auxiliar,Auxiliar2:TRpValue;
begin
comparations(Value);
if Rpparser.Token=toOperator then
begin
operador:=UpperCase(Rpparser.TokenString);
while (operador='AND') do
begin
Rpparser.NextToken;
Auxiliar2:=Value;
comparations(Auxiliar);
// Compatible types?
if (Not FChecking) then
Value:=LogicalANDTRpValue(Auxiliar2,Auxiliar);
if Rpparser.Token<>toOperator then
Exit
else
operador:=UpperCase(Rpparser.TokenString);
end;
end;
end;
procedure TRpCustomEvaluator.separator(var Value:TRpValue);
begin
if ((Rpparser.Token=toOperator) and (Rpparser.TokenString[1]=';')) then
begin
while ((Rpparser.Token=toOperator) and (Rpparser.TokenString[1]=';')) do
begin
Rpparser.NextToken;
if (Rpparser.Token<>toEof) then
variables(Value);
end
end
else
begin
variables(Value);
while ((Rpparser.Token=toOperator) and (Rpparser.TokenString[1]=';')) do
begin
Rpparser.NextToken;
if (Rpparser.Token<>toEof) then
variables(Value);
end
end;
end;
procedure TRpCustomEvaluator.comparations(var Value:TRpValue);
var
operation:string[3];
auxiliar,auxiliar2:TRpValue;
begin
sum_dif(Value);
while Rpparser.Token=tooperator do
begin
operation:=Rpparser.TokenString;
if operation='=' then
begin
Rpparser.NextToken;
auxiliar2:=Value;
sum_dif(auxiliar);
if (NOT FChecking) then
Value:=EqualTRpValue(Auxiliar2,auxiliar);
end
else
if ((operation='<>') OR (operation='><')) then
begin
Rpparser.NextToken;
auxiliar2:=Value;
sum_dif(auxiliar);
if (NOT FChecking) then
Value:=DiferentTRpValue(auxiliar2,auxiliar);
end
else
if operation='>=' then
begin
Rpparser.NextToken;
auxiliar2:=Value;
sum_dif(auxiliar);
if (NOT FChecking) then
Value:=MoreThanEqualTRpValue(auxiliar2,auxiliar);
end
else
if operation='<=' then
begin
Rpparser.NextToken;
auxiliar2:=Value;
sum_dif(auxiliar);
if (NOT FChecking) then
Value:=LessThanEqualTRpValue(auxiliar2,auxiliar);
end
else
if operation='>' then
begin
Rpparser.NextToken;
auxiliar2:=Value;
sum_dif(auxiliar);
if (NOT FChecking) then
Value:=MoreThanTRpValue(auxiliar2,auxiliar);
end
else
if operation='<' then
begin
Rpparser.NextToken;
auxiliar2:=Value;
sum_dif(auxiliar);
if (NOT FChecking) then
Value:=LessThanTRpValue(auxiliar2,auxiliar);
end
else
if operation='==' then
begin
Rpparser.NextToken;
auxiliar2:=Value;
sum_dif(auxiliar);
if (NOT FChecking) then
Value:=EqualEqualTRpValue(auxiliar2,auxiliar);
end
else
if operation=':=' then
begin
Raise TRpEvalException.Create(SRpEvalDescIdenLeft+':'+
Rpparser.TokenString,Rpparser.TokenString,
Rpparser.SourceLine,Rpparser.SourcePos);
end
else
Exit;
end;
end;
procedure TRpCustomEvaluator.sum_dif(var Value:TRpValue);
var operador:string[3];
Auxiliar,auxiliar2:TRpValue;
begin
mul_div(Value);
if Rpparser.Token=toOperator then
begin
operador:=UpperCase(Rpparser.TokenString);
while ((operador='+') or (operador='-')) do
begin
Rpparser.NextToken;
auxiliar2:=Value;
mul_div(Auxiliar);
// Compatible types?
if (Not FChecking) then
case operador[1] of
'+':Value:=SumTRpValue(auxiliar2,Auxiliar);
'-':Value:=DifTRpValue(auxiliar2,Auxiliar);
end;
if Rpparser.Token<>toOperator then
Exit
else
operador:=UpperCase(Rpparser.TokenString);
end;
end;
end;
procedure TRpCustomEvaluator.mul_div(var Value:TRpValue);
var operador:string[4];
Auxiliar,auxiliar2:TRpValue;
begin
dosign(Value);
if Rpparser.Token=toOperator then
begin
operador:=Uppercase(Rpparser.TokenString);
while ((operador='*') or (operador='/')) do
begin
Rpparser.NextToken;
auxiliar2:=Value;
dosign(Auxiliar);
if (Not FChecking) then
case operador[1] of
'*':Value:=MultTRpValue(auxiliar2,Auxiliar);
'/':Value:=DivTRpValue(auxiliar2,Auxiliar);
end;
if Rpparser.Token<>toOperator then
Exit
else
operador:=UpperCase(Rpparser.TokenString);
end;
end;
end;
// The sign is same precedence than functions
procedure TRpCustomEvaluator.dosign(var Value:TRpValue);
var operador:string[4];
iden:TRpIdentifier;
i:integer;
begin
iden:=nil;
operador:='';
if Rpparser.Token=toOperator then
begin
operador:=UpperCase(Rpparser.TokenString);
if ((operador='+') or (operador='-')
or (operador='NOT') or (operador='IIF')) then
Rpparser.NextToken;
end
else
// Is a function?
if Rpparser.Token=toSymbol then
begin
iden:=Searchidentifier(Rpparser.TokenString);
if iden=nil then
Raise TRpEvalException.Create(SRpEvalDescIden+
Rpparser.TokenString,Rpparser.TokenString,
Rpparser.SourceLine,Rpparser.SourcePos);
if iden.RType=RTypeidenfunction then
begin
iden.evaluator:=self;
// Ok is a function assign params
if iden.paramcount>0 then
begin
Rpparser.NextToken;
if Rpparser.Token<>toOperator then
Raise TRpEvalException.Create(SRpEvalParent+' ('+
Rpparser.TokenString,' ( '+Rpparser.TokenString,Rpparser.SourceLine,
Rpparser.SourcePos);
if Rpparser.TokenString<>'(' then
Raise TRpEvalException.Create(SRpEvalParent+
Rpparser.TokenString,'('+Rpparser.TokenString,Rpparser.SourceLine,
Rpparser.SourcePos);
end;
for i:=0 to iden.paramcount-1 do
begin
// Next param
Rpparser.NextToken;
// Look for the value
separator(iden.Params[i]);
// Param separator is ','
if iden.paramcount>i+1 then
begin
if Rpparser.Token<>ToOperator then
Raise TRpEvalException.Create(SRpEvalSyntax+
Rpparser.TokenString,Rpparser.TokenString,Rpparser.SourceLine,
Rpparser.SourcePos);
if Rpparser.TokenString[1]<>',' then
Raise TRpEvalException.Create(SRpEvalSyntax+
Rpparser.TokenString,Rpparser.TokenString,Rpparser.SourceLine,
Rpparser.SourcePos);
end;
end;
// Now the close ) expected
if iden.paramcount>0 then
begin
if Rpparser.Token<>toOperator then
Raise TRpEvalException.Create(SRpEvalParent+' )'+
Rpparser.TokenString,' ) '+Rpparser.TokenString,Rpparser.SourceLine,
Rpparser.SourcePos);
if Rpparser.TokenString<>')' then
Raise TRpEvalException.Create(SRpEvalParent+' )'+
Rpparser.TokenString,' ) '+Rpparser.TokenString,Rpparser.SourceLine,
Rpparser.SourcePos);
Rpparser.NextToken;
end;
end
else
// If not a function must be an operator
iden:=nil;
end;
if iden=nil then
begin
if operador='IIF' then
ExecuteIIF(Value)
else
parentesis(Value)
end
else
if iden.paramcount=0 then
Rpparser.NextToken;
// If it's a funcion execute it (if not syntax check)
if iden<>nil then
begin
if Not FChecking then
Value:=iden.Value;
end
else
begin
if operador='-' then
if (NOT FChecking) then
Value:=SignTRpValue(Value)
else
begin
end
else
if operador='NOT' then
if (NOT FChecking) then
Value:=LogicalNOTTRpValue(Value);
end;
end;
procedure TRpCustomEvaluator.Operand(var Value:TRpValue);
VAR
iden:TRpIdentifier;
begin
case Rpparser.Token of
toSymbol:
begin
// Obtaining the value of an identifier
iden:=Searchidentifier(Rpparser.TokenString);
if iden=nil then
begin
Raise TRpEvalException.Create(SRpEvalDescIden+
Rpparser.TokenString,Rpparser.TokenString,
Rpparser.SourceLine,Rpparser.SourcePos);
end;
iden.evaluator:=self;
Value:=iden.Value;
Rpparser.NextToken;
end;
tkString:
begin
Value:=Rpparser.TokenString;
Rpparser.NextToken;
end;
toWString:
begin
Value:=Rpparser.TokenWideString;
Rpparser.NextToken;
end;
toInteger:
begin
Value:=Rpparser.TokenInt;
Rpparser.NextToken;
end;
toFloat: