forked from fpc/Lazarus
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodetoolscfgscript.pas
More file actions
2619 lines (2453 loc) · 67 KB
/
Copy pathcodetoolscfgscript.pas
File metadata and controls
2619 lines (2453 loc) · 67 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
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
TCTConfigScriptEngine implements an interpreter for a simple scripting
language, enough for configurations.
Working:
if, then, else, begin..end, ;
()
boolean operators: not, and, or, xor
operators: =, <>, >, <, <=, >=, +
variables
constants: decimal, $hex, &octal, %binary, 'string', #character
functions: string(), integer(), int64(), defined(), undefined()
procedures: undefine()
assignments: :=, +=
Not supported:
- floats
- types
- objects
- loops
- custom functions
}
unit CodeToolsCfgScript;
{$mode objfpc}{$H+}
{$inline on}
{off $Define VerboseCTCfgScript}
{off $DEFINE CheckCTCfgVars}
interface
uses
Classes, SysUtils, TypInfo, AVL_Tree,
// Codetools
BasicCodeTools, KeywordFuncLists, FileProcs, CodeToolsStrConsts;
type
ECodeToolCfgScript = class(Exception);
TCTCSValueType = (
ctcsvNone,
ctcsvString,
ctcsvNumber
);
{ TCTCfgScriptVariable }
TCTCfgScriptVariable = record
Name: PChar;
ValueType: TCTCSValueType;
case Integer of
0: (StrStart: PChar; StrLen: integer);
1: (Number: int64);
end;
PCTCfgScriptVariable = ^TCTCfgScriptVariable;
{ TCTCfgScriptVariables }
TCTCfgScriptVariables = class
private
FItems: TAVLTree; // tree of PCTCfgScriptVariable sorted for name
function GetValues(const Name: string): string;
procedure SetValues(const Name: string; const AValue: string);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function Equals(Vars: TCTCfgScriptVariables): boolean; reintroduce;
procedure Assign(Source: TCTCfgScriptVariables); overload;
procedure Assign(Source: TStrings); overload;
procedure AddOverrides(Source: TCTCfgScriptVariables);
procedure AddOverride(Source: PCTCfgScriptVariable);
function GetVariable(const Name: PChar;
CreateIfNotExists: Boolean = false): PCTCfgScriptVariable;
property Values[const Name: string]: string read GetValues write SetValues; default;
procedure Undefine(Name: PChar);
procedure Define(Name: PChar; const Value: string);
function IsDefined(Name: PChar): boolean;
property Tree: TAVLTree read FItems;
procedure WriteDebugReport(const Title: string; const Prefix: string = '');
end;
PCTCfgScriptVariables = ^TCTCfgScriptVariables;
type
TCTCfgScriptOperator = (
ctcsoNone,
ctcsoNot,
ctcsoAnd,
ctcsoOr,
ctcsoXOr,
ctcsoShL,
ctcsoShR,
ctcsoDiv,
ctcsoMod,
ctcsoPlus,
ctcsoMinus,
ctcsoMultiply,
ctcsoDivide,
ctcsoEqual,
ctcsoNotEqual,
ctcsoLowerThan,
ctcsoLowerOrEqualThan,
ctcsoGreaterThan,
ctcsoGreaterOrEqualThan
);
TCTCfgScriptOperators = set of TCTCfgScriptOperator;
const
CTCfgScriptOperatorLvl: array[TCTCfgScriptOperator] of integer = (
0, //ctcsoNone,
1, //ctcsoNot,
1, //ctcsoAnd,
2, //ctcsoOr,
2, //ctcsoXOr,
1, //ctcsoShL,
1, //ctcsoShR,
1, //ctcsoDiv,
1, //ctcsoMod,
2, //ctcsoPlus,
2, //ctcsoMinus,
1, //ctcsoMultiply,
1, //ctcsoDivide,
4, //ctcsoEqual,
4, //ctcsoNotEqual,
4, //ctcsoLowerThan,
4, //ctcsoLowerOrEqualThan,
4, //ctcsoGreaterThan,
4 //ctcsoGreaterOrEqualThan
);
type
TCTCfgScriptStackItemType = (
ctcssNone,
ctcssStatement,
ctcssBegin,
ctcssIf,
ctcssIfThen,
ctcssIfElse,
ctcssExpression,
ctcssRoundBracketOpen,
ctcssOperand,
ctcssOperator,
ctcssAssignment
);
const
ctcssAllStatementStarts = [ctcssNone,ctcssIfThen,ctcssIfElse,ctcssBegin];
type
TCTCfgScriptStackItem = record
Typ: TCTCfgScriptStackItemType;
StartPos: PChar;
Operand: TCTCfgScriptVariable;
end;
PCTCfgScriptStackItem = ^TCTCfgScriptStackItem;
type
{ TCTCfgScriptStack }
TCTCfgScriptStack = class
public
Items: PCTCfgScriptStackItem;
Top: integer; // current item, -1 = empty
TopTyp: TCTCfgScriptStackItemType;
Capacity: integer;
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Push(Typ: TCTCfgScriptStackItemType; const StartPos: PChar);
procedure Pop(Count: integer = 1);
procedure Delete(Index: integer);
function TopItem: PCTCfgScriptStackItem;
function TopItemOperand: PCTCfgScriptVariable;
{$IFDEF CheckCTCfgVars}
procedure CheckOperands;
{$ENDIF}
end;
{ TCTCfgScriptError }
TCTCfgScriptError = class
public
Msg: string;
Position: integer;
Line: integer;
Column: integer;
constructor Create(const aMsg: string; aPos, aLine, aCol: integer);
constructor Create(const aMsg: string);
end;
{ TCTConfigScriptEngine }
TCTConfigScriptEngine = class
protected
FVariables: TCTCfgScriptVariables;
FStack: TCTCfgScriptStack;
FErrors: TFPList; // list of TCTCfgScriptError
function GetErrors(Index: integer): TCTCfgScriptError;
procedure AddError(const aMsg: string; ErrorPos: PChar); overload;
procedure AddError(const aMsg: string); overload;
procedure PushNumberConstant;
procedure PushBooleanValue(b: boolean);
procedure PushNumberValue(const Number: int64);
function RunDefined(Negate: boolean): boolean;
function RunFunction: boolean;
procedure PushStringConstant;
procedure RunStatement(Skip: boolean);
procedure RunBegin(Skip: boolean);
procedure RunIf(Skip: boolean);
procedure RunUndefine(Skip: boolean);
procedure RunAssignment(Skip: boolean);
function RunExpression: boolean; // if true the stack top has an operand
function ExecuteStack(MaxLevel: integer): boolean;
function GetOperatorLevel(P: PChar): integer;
function IsKeyWord(P: PChar): boolean;
function IsFunction(P: PChar): boolean;
function IsCustomFunction({%H-}FunctionName: PChar): boolean; virtual;
procedure RunCustomSimpleFunction({%H-}FunctionName: PChar; {%H-}Value: PCTCfgScriptVariable); virtual;
public
Src: PChar;
AtomStart: PChar;
SrcStart, SrcEnd: PChar;
MaxErrorCount: integer;
constructor Create;
destructor Destroy; override;
procedure ClearErrors;
property Variables: TCTCfgScriptVariables read FVariables;
function Execute(const Source: string; StopAfterErrors: integer = 1): boolean;// true if no errors
function ErrorCount: integer;
property Errors[Index: integer]: TCTCfgScriptError read GetErrors;
function GetAtom: string;
function GetAtomOrNothing: string;
function GetAtom(P: PChar): string;
function PosToLineCol(p: PChar; out Line, Column: integer): boolean;
function PosToStr(p: PChar): string;
function GetErrorStr(Index: integer): string;
procedure WriteDebugReportStack(Title: string);
end;
procedure RenameCTCSVariable(var Src: string; const OldName, NewName: string);
function CompareCTCSVariables(Var1, Var2: Pointer): integer;
function ComparePCharWithCTCSVariableName(Name, aVar: Pointer): integer;
function AreCTCSVariablesEqual(const V1, V2: PCTCfgScriptVariable): Boolean;
function AreCTCSVariablesExactEqual(const V1, V2: PCTCfgScriptVariable): Boolean;
function CompareCTCSVariables(const Left, Right: PCTCfgScriptVariable;
out Equal, LeftIsLowerThanRight: boolean): boolean;
function NewCTCSVariable: PCTCfgScriptVariable;
function NewCTCSVariable(CloneName: PChar): PCTCfgScriptVariable;
function CloneCTCSVariable(const V: PCTCfgScriptVariable): PCTCfgScriptVariable;
procedure FreeCTCSVariable(var V: PCTCfgScriptVariable);
procedure ClearCTCSVariable(const V: PCTCfgScriptVariable);
procedure SetCTCSVariableAsString(const V: PCTCfgScriptVariable; const s: string);
procedure SetCTCSVariableAsNumber(const V: PCTCfgScriptVariable; const i: int64);
procedure SetCTCSVariableValue(const Src, Dest: PCTCfgScriptVariable);
function GetCTCSVariableAsString(const V: PCTCfgScriptVariable): string;
procedure MakeCTCSVariableString(const V: PCTCfgScriptVariable);
procedure MakeCTCSVariableInt64(const V: PCTCfgScriptVariable);
procedure MakeCTCSVariableInteger(const V: PCTCfgScriptVariable);
procedure AddCTCSVariables(const AddVar, SumVar: PCTCfgScriptVariable);
function CTCSNumberEqualsString(const Number: int64; const P: PChar): boolean; inline;
function CTCSVariableIsTrue(const V: PCTCfgScriptVariable): boolean; inline;
function CTCSVariableIsFalse(const V: PCTCfgScriptVariable): boolean;
function CTCSStringToNumber(P: PChar; out Number: int64): boolean;
function AtomToCTCfgOperator(p: PChar): TCTCfgScriptOperator;
procedure CheckCTCSVariable(const V: PCTCfgScriptVariable);
function dbgs(const t: TCTCfgScriptStackItemType): string; overload;
function dbgs(const t: TCTCSValueType): string; overload;
function dbgs(const t: TCTCfgScriptOperator): string; overload;
function dbgs(const V: PCTCfgScriptVariable): string; overload;
implementation
procedure RenameCTCSVariable(var Src: string; const OldName, NewName: string);
var
p: PChar;
AtomStart: PChar;
SrcPos: PtrUInt;
begin
if (Src='') or not IsValidIdent(OldName) or (NewName='') then exit;
p:=PChar(Src);
//debugln(['RenameCTCSVariable START ',dbgstr(Src)]);
repeat
ReadRawNextPascalAtom(p,AtomStart,nil,false,true);
if (p=AtomStart) then break;
if IsIdentStartChar[AtomStart^]
and (CompareIdentifiers(PChar(OldName),AtomStart)=0)
then begin
SrcPos:=PtrUInt(AtomStart-PChar(Src))+1;
Src:=copy(Src,1,SrcPos-1)+NewName+copy(Src,SrcPos+PtrUInt(length(OldName)),length(Src));
p:=@Src[SrcPos]+length(NewName);
end;
until false;
//debugln(['RenameCTCSVariable END ',dbgstr(Src)]);
end;
function CompareCTCSVariables(Var1, Var2: Pointer): integer;
var
v1: PCTCfgScriptVariable absolute Var1;
v2: PCTCfgScriptVariable absolute Var2;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(v1);
CheckCTCSVariable(v2);
{$ENDIF}
Result:=CompareIdentifiers(v1^.Name,v2^.Name);
end;
function ComparePCharWithCTCSVariableName(Name, aVar: Pointer): integer;
var
n: PChar absolute Name;
v: PCTCfgScriptVariable absolute aVar;
begin
{$IFDEF CheckCTCfgVars}CheckCTCSVariable(v);{$ENDIF}
Result:=CompareIdentifiers(n,v^.Name);
end;
function AreCTCSVariablesEqual(const V1, V2: PCTCfgScriptVariable): Boolean;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(v1);
CheckCTCSVariable(v2);
{$ENDIF}
Result:=false;
case V1^.ValueType of
ctcsvNone:
exit; // invalid is never equal to anything
ctcsvString:
case V2^.ValueType of
ctcsvNone: exit;
ctcsvString:
if (V1^.StrLen<>V2^.StrLen)
or ((V1^.StrStart<>nil)
and (not CompareMem(V1^.StrStart,V2^.StrStart,V1^.StrLen)))
then exit;
ctcsvNumber:
if not CTCSNumberEqualsString(V2^.Number,V1^.StrStart) then exit;
end;
ctcsvNumber:
case V2^.ValueType of
ctcsvNone: exit;
ctcsvString:
if not CTCSNumberEqualsString(V1^.Number,V2^.StrStart) then exit;
ctcsvNumber:
if V1^.Number<>V2^.Number then exit;
end;
end;
Result:=true;
end;
function AreCTCSVariablesExactEqual(const V1, V2: PCTCfgScriptVariable
): Boolean;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(v1);
CheckCTCSVariable(v2);
{$ENDIF}
Result:=false;
if V1^.ValueType<>V2^.ValueType then exit;
case V1^.ValueType of
ctcsvNone: ;
ctcsvString: if (V1^.StrLen<>V2^.StrLen)
or ((V1^.StrStart<>nil)
and (not CompareMem(V1^.StrStart,V2^.StrStart,V1^.StrLen)))
then exit;
ctcsvNumber: if V1^.Number<>V2^.Number then exit;
end;
Result:=true;
end;
function CompareCTCSVariables(const Left, Right: PCTCfgScriptVariable; out
Equal, LeftIsLowerThanRight: boolean): boolean;
{ Rules:
If one of the values is invalid, return false
If both are numbers, compare as numbers
Otherwise compare as string alphabetically case sensitive A<B, A<AA
}
procedure CompareNumberWithString(Number: int64; p: PChar);
var
i: Integer;
Cnt: integer;
s: array[0..30] of char;
begin
if p=nil then begin
Equal:=false;
LeftIsLowerThanRight:=false;
exit;
end;
// convert number to decimal string
if Number=0 then begin
Cnt:=1;
s[0]:='0';
end else begin
Cnt:=0;
if Number<0 then begin
Cnt:=1;
s[0]:='-';
Number:=-Number;
end;
while Number>0 do begin
s[Cnt]:=chr(Number mod 10+ord('0'));
inc(Cnt);
Number:=Number div 10;
end;
end;
for i:=0 to Cnt-1 do begin
if p^<>s[i] then begin
Equal:=false;
LeftIsLowerThanRight:=s[i]<p^;
exit;
end;
inc(p);
end;
if p^=#0 then begin
Equal:=true;
LeftIsLowerThanRight:=false;
end else begin
Equal:=False;
LeftIsLowerThanRight:=true;
end;
end;
var
V1: PChar;
V2: PChar;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Left);
CheckCTCSVariable(Right);
{$ENDIF}
//debugln(['CompareCTCSVariables START Left=',dbgs(Left),' Right=',dbgs(Right)]);
Result:=false;
Equal:=false;
LeftIsLowerThanRight:=false;
case Left^.ValueType of
ctcsvNone:
exit; // invalid is never equal to anything
ctcsvString:
case Right^.ValueType of
ctcsvNone: exit;
ctcsvString:
begin
// compare two strings
V1:=Left^.StrStart;
V2:=Right^.StrStart;
if V1=nil then begin
if V2=nil then begin
Equal:=true;
LeftIsLowerThanRight:=false;
end else begin
Equal:=False;
LeftIsLowerThanRight:=true; // left is shorter than right
end;
end else begin
if V2=nil then begin
Equal:=False;
LeftIsLowerThanRight:=false; // left is longer than right
end else begin
repeat
if V1^=V2^ then begin
if V1^=#0 then begin
Equal:=true;
LeftIsLowerThanRight:=false;
break;
end else begin
inc(V1);
inc(V2);
end;
end else begin
Equal:=false;
LeftIsLowerThanRight:=V1^<V2^;
break;
end;
until false;
end;
end;
end;
ctcsvNumber:
begin
CompareNumberWithString(Right^.Number,Left^.StrStart);
LeftIsLowerThanRight:=not LeftIsLowerThanRight;
end;
end;
ctcsvNumber:
case Right^.ValueType of
ctcsvNone: exit;
ctcsvString:
CompareNumberWithString(Left^.Number,Right^.StrStart);
ctcsvNumber:
begin
Equal:=Left^.Number=Right^.Number;
LeftIsLowerThanRight:=Left^.Number<Right^.Number;
end;
end;
end;
Result:=true;
end;
function NewCTCSVariable: PCTCfgScriptVariable;
begin
New(Result);
FillByte(Result^,SizeOf(TCTCfgScriptVariable),0);
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Result);
{$ENDIF}
end;
function NewCTCSVariable(CloneName: PChar): PCTCfgScriptVariable;
var
l: LongInt;
begin
Result:=NewCTCSVariable();
l:=GetIdentLen(CloneName);
if l>0 then begin
Result^.Name:=GetMem(l+1);
System.Move(CloneName^,Result^.Name^,l);
Result^.Name[l]:=#0;
end;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Result);
{$ENDIF}
end;
function CloneCTCSVariable(const V: PCTCfgScriptVariable): PCTCfgScriptVariable;
var
l: LongInt;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(v);
{$ENDIF}
Result:=NewCTCSVariable(V^.Name);
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Result);
{$ENDIF}
Result^.ValueType:=V^.ValueType;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Result);
{$ENDIF}
case V^.ValueType of
ctcsvNone: ;
ctcsvString:
begin
l:=V^.StrLen;
if l>0 then begin
Result^.StrLen:=l;
Result^.StrStart:=GetMem(l+1);
System.Move(V^.StrStart^,Result^.StrStart^,l);
Result^.StrStart[l]:=#0;
end;
end;
ctcsvNumber:
Result^.Number:=V^.Number;
end;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Result);
{$ENDIF}
end;
procedure SetCTCSVariableValue(const Src, Dest: PCTCfgScriptVariable);
var
l: LongInt;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Src);
CheckCTCSVariable(Dest);
{$ENDIF}
if Src=Dest then exit;
case Src^.ValueType of
ctcsvNone:
ClearCTCSVariable(Dest);
ctcsvString:
begin
if Dest^.ValueType<>ctcsvString then begin
Dest^.ValueType:=ctcsvString;
Dest^.StrStart:=nil;
end;
l:=Src^.StrLen;
Dest^.StrLen:=l;
if l>0 then begin
ReAllocMem(Dest^.StrStart,l+1);
System.Move(Src^.StrStart^,Dest^.StrStart^,l);
Dest^.StrStart[l]:=#0;
end else
ReAllocMem(Dest^.StrStart,0);
end;
ctcsvNumber:
begin
case Dest^.ValueType of
ctcsvNone:
Dest^.ValueType:=ctcsvNumber;
ctcsvString:
begin
Dest^.ValueType:=ctcsvNumber;
if Dest^.StrStart<>nil then
Freemem(Dest^.StrStart);
end;
ctcsvNumber: ;
end;
Dest^.Number:=Src^.Number;
end;
end;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(Src);
CheckCTCSVariable(Dest);
{$ENDIF}
end;
procedure FreeCTCSVariable(var V: PCTCfgScriptVariable);
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(v);
{$ENDIF}
ClearCTCSVariable(V);
ReAllocMem(V^.Name,0);
Dispose(V);
end;
procedure ClearCTCSVariable(const V: PCTCfgScriptVariable);
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(v);
{$ENDIF}
if V^.ValueType=ctcsvString then
ReAllocMem(V^.StrStart,0);
V^.ValueType:=ctcsvNone;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(v);
{$ENDIF}
end;
procedure MakeCTCSVariableString(const V: PCTCfgScriptVariable);
var
s: String;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(V);
{$ENDIF}
case V^.ValueType of
ctcsvNone:
begin
V^.StrLen:=0;
V^.StrStart:=nil;
V^.ValueType:=ctcsvString;
end;
ctcsvString: ;
ctcsvNumber:
begin
s:=IntToStr(V^.Number);
V^.StrLen:=length(s);
V^.StrStart:=GetMem(length(s)+1);
System.Move(s[1],V^.StrStart^,length(s)+1);
V^.ValueType:=ctcsvString;
end;
end;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(V);
{$ENDIF}
end;
procedure MakeCTCSVariableInt64(const V: PCTCfgScriptVariable);
var
i: Int64;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(V);
{$ENDIF}
case V^.ValueType of
ctcsvNone:
begin
V^.Number:=0;
V^.ValueType:=ctcsvNumber;
end;
ctcsvString:
begin
if V^.StrStart<>nil then begin
i:=StrToInt64Def(V^.StrStart,0);
FreeMem(V^.StrStart);
V^.Number:=i;
end else
V^.Number:=0;
V^.ValueType:=ctcsvNumber;
end;
ctcsvNumber: ;
end;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(V);
{$ENDIF}
end;
procedure MakeCTCSVariableInteger(const V: PCTCfgScriptVariable);
var
i: integer;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(V);
{$ENDIF}
case V^.ValueType of
ctcsvNone:
begin
V^.Number:=0;
V^.ValueType:=ctcsvNumber;
end;
ctcsvString:
begin
if V^.StrStart<>nil then begin
i:=StrToIntDef(V^.StrStart,0);
FreeMem(V^.StrStart);
V^.Number:=i;
end else
V^.Number:=0;
V^.ValueType:=ctcsvNumber;
end;
ctcsvNumber: ;
end;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(V);
{$ENDIF}
end;
procedure AddCTCSVariables(const AddVar, SumVar: PCTCfgScriptVariable);
{ If one of them is none, then save in sum the other value
If both are numbers, add them.
Otherwise concatenate as strings.
}
var
OldLen: LongInt;
s: String;
begin
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(AddVar);
CheckCTCSVariable(SumVar);
{$ENDIF}
case SumVar^.ValueType of
ctcsvNone:
SetCTCSVariableValue(AddVar,SumVar);
ctcsvString:
case AddVar^.ValueType of
ctcsvNone:
;
ctcsvString:
if AddVar^.StrLen>0 then begin
// append
OldLen:=SumVar^.StrLen;
SumVar^.StrLen+=AddVar^.StrLen;
ReAllocMem(SumVar^.StrStart,SumVar^.StrLen+1);
System.Move(AddVar^.StrStart^,SumVar^.StrStart[OldLen],AddVar^.StrLen+1);
end;
ctcsvNumber:
begin
// append as string
s:=IntToStr(AddVar^.Number);
OldLen:=SumVar^.StrLen;
SumVar^.StrLen+=length(s);
ReAllocMem(SumVar^.StrStart,SumVar^.StrLen+1);
System.Move(s[1],SumVar^.StrStart[OldLen],length(s)+1);
end;
end;
ctcsvNumber:
case AddVar^.ValueType of
ctcsvNone:
;
ctcsvString:
begin
// convert SumVar from number to string and append
s:=IntToStr(SumVar^.Number);
SumVar^.ValueType:=ctcsvString;
SumVar^.StrLen:=length(s)+AddVar^.StrLen;
SumVar^.StrStart:=GetMem(SumVar^.StrLen+1);
System.Move(s[1],SumVar^.StrStart^,length(s));
if AddVar^.StrStart<>nil then
System.Move(AddVar^.StrStart^,SumVar^.StrStart[length(s)],AddVar^.StrLen+1)
else
SumVar^.StrStart[SumVar^.StrLen]:=#0;
end;
ctcsvNumber:
try
SumVar^.Number+=AddVar^.Number;
except
end;
end;
end;
{$IFDEF CheckCTCfgVars}
CheckCTCSVariable(AddVar);
CheckCTCSVariable(SumVar);
{$ENDIF}
end;
function CTCSNumberEqualsString(const Number: int64; const P: PChar): boolean;
var
n: int64;
begin
Result:=CTCSStringToNumber(P,n) and (n=Number);
end;
function CTCSStringToNumber(P: PChar; out Number: int64): boolean;
var
n: int64;
Negated: Boolean;
begin
Result:=false;
if (P=nil) or (P^=#0) then exit;
try
n:=0;
if p^='-' then begin
Negated:=true;
inc(p);
end else
Negated:=false;
if p^='$' then begin
// hex
repeat
case p^ of
'0'..'9': n:=n*16+Ord(p^)-Ord('0');
'a'..'f': n:=n*16+Ord(p^)-Ord('a')+10;
'A'..'F': n:=n*16+Ord(p^)-Ord('A')+10;
#0: break;
else exit;
end;
inc(p);
until false;
end else if p^='%' then begin
// binary
repeat
case p^ of
'0': n:=n*2;
'1': n:=n*2+1;
#0: break;
else exit;
end;
inc(p);
until false;
end else begin
// decimal
repeat
case p^ of
'0'..'9': n:=n*10+Ord(p^)-Ord('0');
#0: break;
else exit;
end;
inc(p);
until false;
end;
if Negated then n:=-n;
except
exit;
end;
Number:=n;
Result:=true;
end;
function CTCSVariableIsTrue(const V: PCTCfgScriptVariable): boolean;
begin
Result:=not CTCSVariableIsFalse(V);
end;
function CTCSVariableIsFalse(const V: PCTCfgScriptVariable): boolean;
begin
case V^.ValueType of
ctcsvNone:
Result:=false;
ctcsvString:
Result:=(V^.StrLen=1) and (V^.StrStart^='0');
ctcsvNumber:
Result:=V^.Number=0;
end;
end;
function AtomToCTCfgOperator(p: PChar): TCTCfgScriptOperator;
begin
Result:=ctcsoNone;
case UpChars[p^] of
'A':
if CompareIdentifiers('and',p)=0 then Result:=ctcsoAnd;
'D':
if CompareIdentifiers('div',p)=0 then Result:=ctcsoDiv;
'M':
if CompareIdentifiers('mod',p)=0 then Result:=ctcsoMod;
'N':
if CompareIdentifiers('not',p)=0 then Result:=ctcsoNot;
'O':
if CompareIdentifiers('or',p)=0 then Result:=ctcsoOr;
'S':
case UpChars[p[1]] of
'H':
case UpChars[p[2]] of
'L': if CompareIdentifiers('shl',p)=0 then Result:=ctcsoShL;
'R': if CompareIdentifiers('shr',p)=0 then Result:=ctcsoShR;
end;
end;
'X':
if CompareIdentifiers('xor',p)=0 then Result:=ctcsoXOr;
'=':
Result:=ctcsoEqual;
'<':
case p[1] of
'>': Result:=ctcsoNotEqual;
'=': Result:=ctcsoLowerOrEqualThan;
else { < lower than } Result:=ctcsoLowerThan;
end;
'>':
case p[1] of
'=': Result:=ctcsoGreaterOrEqualThan;
else { > greater than } Result:=ctcsoGreaterThan;
end;
'*':
case p[1] of
'*': ;
'=': ;
else { * multiply } Result:=ctcsoMultiply;
end;
'/':
case p[1] of
'/': ;
'=': ;
else { / divide } Result:=ctcsoDivide;
end;
'+':
case p[1] of
'=': ;
else { + plus } Result:=ctcsoPlus;
end;
'-':
case p[1] of
'=': ;
else { - minus } Result:=ctcsoMinus;
end;
':':
case p[1] of
'=': ;
else { : colon } ;
end;
end;
end;
procedure CheckCTCSVariable(const V: PCTCfgScriptVariable);
begin
if V=nil then
RaiseCatchableException('');
if (V^.Name<>nil) and (strlen(V^.Name)>255) then
RaiseCatchableException('');
case V^.ValueType of
ctcsvNone: ;
ctcsvString:
begin
if V^.StrLen=0 then begin
if V^.StrStart<>nil then
RaiseCatchableException('');
end else begin
if V^.StrStart=nil then
RaiseCatchableException('');
if strlen(V^.StrStart)<>V^.StrLen then
RaiseCatchableException('');
end;
end;
ctcsvNumber: ;
end;
end;
function dbgs(const t: TCTCfgScriptStackItemType): string;
begin
Result:=GetEnumName(typeinfo(t),ord(t));
end;
function dbgs(const t: TCTCSValueType): string;
begin
Result:=GetEnumName(typeinfo(t),ord(t));
end;
function dbgs(const t: TCTCfgScriptOperator): string;
begin
Result:=GetEnumName(typeinfo(t),ord(t));
end;
function dbgs(const V: PCTCfgScriptVariable): string;