dbf_prsdef.pas 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. unit dbf_prsdef;
  2. interface
  3. {$I Dbf_Common.inc}
  4. uses
  5. SysUtils,
  6. Classes,
  7. Dbf_Common,
  8. Dbf_PrsSupp;
  9. const
  10. MaxArg = 6;
  11. ArgAllocSize = 32;
  12. type
  13. TExpressionType = (etInteger, etString, etBoolean, etLargeInt, etFloat, etDateTime,
  14. etLeftBracket, etRightBracket, etComma, etUnknown);
  15. PPChar = ^PChar;
  16. PBoolean = ^Boolean;
  17. PInteger = ^Integer;
  18. PDateTime = ^TDateTime;
  19. EParserException = class(Exception);
  20. PExpressionRec = ^TExpressionRec;
  21. PDynamicType = ^TDynamicType;
  22. TExprWord = class;
  23. TExprFunc = procedure(Expr: PExpressionRec);
  24. //-----
  25. TDynamicType = class(TObject)
  26. private
  27. FMemory: PPChar;
  28. FMemoryPos: PPChar;
  29. FSize: PInteger;
  30. public
  31. constructor Create(DestMem, DestPos: PPChar; Size: PInteger);
  32. procedure AssureSpace(ASize: Integer);
  33. procedure Resize(NewSize: Integer; Exact: Boolean);
  34. procedure Rewind;
  35. procedure Append(Source: PChar; Length: Integer);
  36. procedure AppendInteger(Source: Integer);
  37. property Memory: PPChar read FMemory;
  38. property MemoryPos: PPChar read FMemoryPos;
  39. property Size: PInteger read FSize;
  40. end;
  41. TExpressionRec = record
  42. //used both as linked tree and linked list for maximum evaluation efficiency
  43. Oper: TExprFunc;
  44. Next: PExpressionRec;
  45. Res: TDynamicType;
  46. ExprWord: TExprWord;
  47. AuxData: pointer;
  48. ResetDest: Boolean;
  49. Args: array[0..MaxArg-1] of PChar;
  50. ArgsPos: array[0..MaxArg-1] of PChar;
  51. ArgsSize: array[0..MaxArg-1] of Integer;
  52. ArgsType: array[0..MaxArg-1] of TExpressionType;
  53. ArgList: array[0..MaxArg-1] of PExpressionRec;
  54. end;
  55. TExprCollection = class(TNoOwnerCollection)
  56. public
  57. procedure Check;
  58. procedure EraseExtraBrackets;
  59. end;
  60. TExprWordRec = record
  61. Name: PChar;
  62. ShortName: PChar;
  63. IsOperator: Boolean;
  64. IsVariable: Boolean;
  65. IsFunction: Boolean;
  66. NeedsCopy: Boolean;
  67. FixedLen: Boolean;
  68. CanVary: Boolean;
  69. ResultType: TExpressionType;
  70. MinArg: Integer;
  71. MaxArg: Integer;
  72. TypeSpec: PChar;
  73. Description: PChar;
  74. ExprFunc: TExprFunc;
  75. end;
  76. TExprWord = class(TObject)
  77. private
  78. FName: string;
  79. FExprFunc: TExprFunc;
  80. protected
  81. FRefCount: Cardinal;
  82. function GetIsOperator: Boolean; virtual;
  83. function GetIsVariable: Boolean;
  84. function GetNeedsCopy: Boolean;
  85. function GetFixedLen: Integer; virtual;
  86. function GetCanVary: Boolean; virtual;
  87. function GetResultType: TExpressionType; virtual;
  88. function GetMinFunctionArg: Integer; virtual;
  89. function GetMaxFunctionArg: Integer; virtual;
  90. function GetDescription: string; virtual;
  91. function GetTypeSpec: string; virtual;
  92. function GetShortName: string; virtual;
  93. public
  94. constructor Create(AName: string; AExprFunc: TExprFunc);
  95. function LenAsPointer: PInteger; virtual;
  96. function AsPointer: PChar; virtual;
  97. function IsFunction: Boolean; virtual;
  98. property ExprFunc: TExprFunc read FExprFunc;
  99. property IsOperator: Boolean read GetIsOperator;
  100. property CanVary: Boolean read GetCanVary;
  101. property IsVariable: Boolean read GetIsVariable;
  102. property NeedsCopy: Boolean read GetNeedsCopy;
  103. property FixedLen: Integer read GetFixedLen;
  104. property ResultType: TExpressionType read GetResultType;
  105. property MinFunctionArg: Integer read GetMinFunctionArg;
  106. property MaxFunctionArg: Integer read GetMaxFunctionArg;
  107. property Name: string read FName;
  108. property ShortName: string read GetShortName;
  109. property Description: string read GetDescription;
  110. property TypeSpec: string read GetTypeSpec;
  111. end;
  112. TExpressShortList = class(TSortedCollection)
  113. public
  114. function KeyOf(Item: Pointer): Pointer; override;
  115. function Compare(Key1, Key2: Pointer): Integer; override;
  116. procedure FreeItem(Item: Pointer); override;
  117. end;
  118. TExpressList = class(TSortedCollection)
  119. private
  120. FShortList: TExpressShortList;
  121. public
  122. constructor Create;
  123. destructor Destroy; override;
  124. procedure Add(Item: Pointer); override;
  125. function KeyOf(Item: Pointer): Pointer; override;
  126. function Compare(Key1, Key2: Pointer): Integer; override;
  127. function Search(Key: Pointer; var Index: Integer): Boolean; override;
  128. procedure FreeItem(Item: Pointer); override;
  129. end;
  130. TConstant = class(TExprWord)
  131. private
  132. FResultType: TExpressionType;
  133. protected
  134. function GetResultType: TExpressionType; override;
  135. public
  136. constructor Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
  137. end;
  138. TFloatConstant = class(TConstant)
  139. private
  140. FValue: Double;
  141. public
  142. // not overloaded to support older Delphi versions
  143. constructor Create(AName: string; AValue: string);
  144. constructor CreateAsDouble(AName: string; AValue: Double);
  145. function AsPointer: PChar; override;
  146. property Value: Double read FValue write FValue;
  147. end;
  148. TUserConstant = class(TFloatConstant)
  149. private
  150. FDescription: string;
  151. protected
  152. function GetDescription: string; override;
  153. public
  154. constructor CreateAsDouble(AName, Descr: string; AValue: Double);
  155. end;
  156. TStringConstant = class(TConstant)
  157. private
  158. FValue: string;
  159. public
  160. constructor Create(AValue: string);
  161. function AsPointer: PChar; override;
  162. end;
  163. TIntegerConstant = class(TConstant)
  164. private
  165. FValue: Integer;
  166. public
  167. constructor Create(AValue: Integer);
  168. function AsPointer: PChar; override;
  169. end;
  170. TBooleanConstant = class(TConstant)
  171. private
  172. FValue: Boolean;
  173. public
  174. // not overloaded to support older Delphi versions
  175. constructor Create(AName: string; AValue: Boolean);
  176. function AsPointer: PChar; override;
  177. property Value: Boolean read FValue write FValue;
  178. end;
  179. TVariable = class(TExprWord)
  180. private
  181. FResultType: TExpressionType;
  182. protected
  183. function GetCanVary: Boolean; override;
  184. function GetResultType: TExpressionType; override;
  185. public
  186. constructor Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
  187. end;
  188. TFloatVariable = class(TVariable)
  189. private
  190. FValue: PDouble;
  191. public
  192. constructor Create(AName: string; AValue: PDouble);
  193. function AsPointer: PChar; override;
  194. end;
  195. TStringVariable = class(TVariable)
  196. private
  197. FValue: PPChar;
  198. FFixedLen: Integer;
  199. protected
  200. function GetFixedLen: Integer; override;
  201. public
  202. constructor Create(AName: string; AValue: PPChar; AFixedLen: Integer);
  203. function LenAsPointer: PInteger; override;
  204. function AsPointer: PChar; override;
  205. property FixedLen: Integer read FFixedLen;
  206. end;
  207. TDateTimeVariable = class(TVariable)
  208. private
  209. FValue: PDateTimeRec;
  210. public
  211. constructor Create(AName: string; AValue: PDateTimeRec);
  212. function AsPointer: PChar; override;
  213. end;
  214. TIntegerVariable = class(TVariable)
  215. private
  216. FValue: PInteger;
  217. public
  218. constructor Create(AName: string; AValue: PInteger);
  219. function AsPointer: PChar; override;
  220. end;
  221. {$ifdef SUPPORT_INT64}
  222. TLargeIntVariable = class(TVariable)
  223. private
  224. FValue: PLargeInt;
  225. public
  226. constructor Create(AName: string; AValue: PLargeInt);
  227. function AsPointer: PChar; override;
  228. end;
  229. {$endif}
  230. TBooleanVariable = class(TVariable)
  231. private
  232. FValue: PBoolean;
  233. public
  234. constructor Create(AName: string; AValue: PBoolean);
  235. function AsPointer: PChar; override;
  236. end;
  237. TLeftBracket = class(TExprWord)
  238. function GetResultType: TExpressionType; override;
  239. end;
  240. TRightBracket = class(TExprWord)
  241. protected
  242. function GetResultType: TExpressionType; override;
  243. end;
  244. TComma = class(TExprWord)
  245. protected
  246. function GetResultType: TExpressionType; override;
  247. end;
  248. TFunction = class(TExprWord)
  249. private
  250. FIsOperator: Boolean;
  251. FOperPrec: Integer;
  252. FMinFunctionArg: Integer;
  253. FMaxFunctionArg: Integer;
  254. FDescription: string;
  255. FTypeSpec: string;
  256. FShortName: string;
  257. FResultType: TExpressionType;
  258. protected
  259. function GetDescription: string; override;
  260. function GetIsOperator: Boolean; override;
  261. function GetMinFunctionArg: Integer; override;
  262. function GetMaxFunctionArg: Integer; override;
  263. function GetResultType: TExpressionType; override;
  264. function GetTypeSpec: string; override;
  265. function GetShortName: string; override;
  266. procedure InternalCreate(AName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
  267. AExprFunc: TExprFunc; AIsOperator: Boolean; AOperPrec: Integer);
  268. public
  269. constructor Create(AName, AShortName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType; AExprFunc: TExprFunc; Descr: string);
  270. constructor CreateOper(AName, ATypeSpec: string; AResultType: TExpressionType; AExprFunc: TExprFunc; AOperPrec: Integer);
  271. function IsFunction: Boolean; override;
  272. property OperPrec: Integer read FOperPrec;
  273. property TypeSpec: string read FTypeSpec;
  274. end;
  275. TVaryingFunction = class(TFunction)
  276. // Functions that can vary for ex. random generators
  277. // should be TVaryingFunction to be sure that they are
  278. // always evaluated
  279. protected
  280. function GetCanVary: Boolean; override;
  281. end;
  282. const
  283. ListChar = ','; {the delimiter used with the 'in' operator: e.g.,
  284. ('a' in 'a,b') =True
  285. ('c' in 'a,b') =False}
  286. function ExprCharToExprType(ExprChar: Char): TExpressionType;
  287. implementation
  288. function ExprCharToExprType(ExprChar: Char): TExpressionType;
  289. begin
  290. case ExprChar of
  291. 'B': Result := etBoolean;
  292. 'I': Result := etInteger;
  293. 'L': Result := etLargeInt;
  294. 'F': Result := etFloat;
  295. 'D': Result := etDateTime;
  296. 'S': Result := etString;
  297. else
  298. Result := etUnknown;
  299. end;
  300. end;
  301. procedure _FloatVariable(Param: PExpressionRec);
  302. begin
  303. with Param^ do
  304. PDouble(Res.MemoryPos^)^ := PDouble(Args[0])^;
  305. end;
  306. procedure _BooleanVariable(Param: PExpressionRec);
  307. begin
  308. with Param^ do
  309. PBoolean(Res.MemoryPos^)^ := PBoolean(Args[0])^;
  310. end;
  311. procedure _StringConstant(Param: PExpressionRec);
  312. begin
  313. with Param^ do
  314. Res.Append(Args[0], StrLen(Args[0]));
  315. end;
  316. procedure _StringVariable(Param: PExpressionRec);
  317. begin
  318. with Param^ do
  319. Res.Append(PPChar(Args[0])^, StrLen(PPChar(Args[0])^));
  320. end;
  321. procedure _StringVariableFixedLen(Param: PExpressionRec);
  322. begin
  323. with Param^ do
  324. Res.Append(PPChar(Args[0])^, PInteger(Args[1])^);
  325. end;
  326. procedure _DateTimeVariable(Param: PExpressionRec);
  327. begin
  328. with Param^ do
  329. PDateTimeRec(Res.MemoryPos^)^ := PDateTimeRec(Args[0])^;
  330. end;
  331. procedure _IntegerVariable(Param: PExpressionRec);
  332. begin
  333. with Param^ do
  334. PInteger(Res.MemoryPos^)^ := PInteger(Args[0])^;
  335. end;
  336. {
  337. procedure _SmallIntVariable(Param: PExpressionRec);
  338. begin
  339. with Param^ do
  340. PSmallInt(Res.MemoryPos^)^ := PSmallInt(Args[0])^;
  341. end;
  342. }
  343. {$ifdef SUPPORT_INT64}
  344. procedure _LargeIntVariable(Param: PExpressionRec);
  345. begin
  346. with Param^ do
  347. PLargeInt(Res.MemoryPos^)^ := PLargeInt(Args[0])^;
  348. end;
  349. {$endif}
  350. { TExpressionWord }
  351. constructor TExprWord.Create(AName: string; AExprFunc: TExprFunc);
  352. begin
  353. FName := AName;
  354. FExprFunc := AExprFunc;
  355. end;
  356. function TExprWord.GetCanVary: Boolean;
  357. begin
  358. Result := False;
  359. end;
  360. function TExprWord.GetDescription: string;
  361. begin
  362. Result := EmptyStr;
  363. end;
  364. function TExprWord.GetShortName: string;
  365. begin
  366. Result := EmptyStr;
  367. end;
  368. function TExprWord.GetIsOperator: Boolean;
  369. begin
  370. Result := False;
  371. end;
  372. function TExprWord.GetIsVariable: Boolean;
  373. begin
  374. Result := (@FExprFunc = @_StringVariable) or
  375. (@FExprFunc = @_StringConstant) or
  376. (@FExprFunc = @_StringVariableFixedLen) or
  377. (@FExprFunc = @_FloatVariable) or
  378. (@FExprFunc = @_IntegerVariable) or
  379. // (@FExprFunc = @_SmallIntVariable) or
  380. {$ifdef SUPPORT_INT64}
  381. (@FExprFunc = @_LargeIntVariable) or
  382. {$endif}
  383. (@FExprFunc = @_DateTimeVariable) or
  384. (@FExprFunc = @_BooleanVariable);
  385. end;
  386. function TExprWord.GetNeedsCopy: Boolean;
  387. begin
  388. Result := (@FExprFunc <> @_StringConstant) and
  389. // (@FExprFunc <> @_StringVariable) and
  390. // (@FExprFunc <> @_StringVariableFixedLen) and
  391. // string variable cannot be used as normal parameter
  392. // because it is indirectly referenced and possibly
  393. // not null-terminated (fixed len)
  394. (@FExprFunc <> @_FloatVariable) and
  395. (@FExprFunc <> @_IntegerVariable) and
  396. // (@FExprFunc <> @_SmallIntVariable) and
  397. {$ifdef SUPPORT_INT64}
  398. (@FExprFunc <> @_LargeIntVariable) and
  399. {$endif}
  400. (@FExprFunc <> @_DateTimeVariable) and
  401. (@FExprFunc <> @_BooleanVariable);
  402. end;
  403. function TExprWord.GetFixedLen: Integer;
  404. begin
  405. // -1 means variable, non-fixed length
  406. Result := -1;
  407. end;
  408. function TExprWord.GetMinFunctionArg: Integer;
  409. begin
  410. Result := 0;
  411. end;
  412. function TExprWord.GetMaxFunctionArg: Integer;
  413. begin
  414. Result := 0;
  415. end;
  416. function TExprWord.GetResultType: TExpressionType;
  417. begin
  418. Result := etUnknown;
  419. end;
  420. function TExprWord.GetTypeSpec: string;
  421. begin
  422. Result := EmptyStr;
  423. end;
  424. function TExprWord.AsPointer: PChar;
  425. begin
  426. Result := nil;
  427. end;
  428. function TExprWord.LenAsPointer: PInteger;
  429. begin
  430. Result := nil;
  431. end;
  432. function TExprWord.IsFunction: Boolean;
  433. begin
  434. Result := False;
  435. end;
  436. { TConstant }
  437. constructor TConstant.Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
  438. begin
  439. inherited Create(AName, AExprFunc);
  440. FResultType := AVarType;
  441. end;
  442. function TConstant.GetResultType: TExpressionType;
  443. begin
  444. Result := FResultType;
  445. end;
  446. { TFloatConstant }
  447. constructor TFloatConstant.Create(AName, AValue: string);
  448. begin
  449. inherited Create(AName, etFloat, _FloatVariable);
  450. if Length(AValue) > 0 then
  451. FValue := StrToFloat(AValue)
  452. else
  453. FValue := 0.0;
  454. end;
  455. constructor TFloatConstant.CreateAsDouble(AName: string; AValue: Double);
  456. begin
  457. inherited Create(AName, etFloat, _FloatVariable);
  458. FValue := AValue;
  459. end;
  460. function TFloatConstant.AsPointer: PChar;
  461. begin
  462. Result := PChar(@FValue);
  463. end;
  464. { TUserConstant }
  465. constructor TUserConstant.CreateAsDouble(AName, Descr: string; AValue: Double);
  466. begin
  467. FDescription := Descr;
  468. inherited CreateAsDouble(AName, AValue);
  469. end;
  470. function TUserConstant.GetDescription: string;
  471. begin
  472. Result := FDescription;
  473. end;
  474. { TStringConstant }
  475. constructor TStringConstant.Create(AValue: string);
  476. var
  477. firstChar, lastChar: Char;
  478. begin
  479. inherited Create(AValue, etString, _StringConstant);
  480. firstChar := AValue[1];
  481. lastChar := AValue[Length(AValue)];
  482. if (firstChar = lastChar) and ((firstChar = '''') or (firstChar = '"')) then
  483. FValue := Copy(AValue, 2, Length(AValue) - 2)
  484. else
  485. FValue := AValue;
  486. end;
  487. function TStringConstant.AsPointer: PChar;
  488. begin
  489. Result := PChar(FValue);
  490. end;
  491. { TBooleanConstant }
  492. constructor TBooleanConstant.Create(AName: string; AValue: Boolean);
  493. begin
  494. inherited Create(AName, etBoolean, _BooleanVariable);
  495. FValue := AValue;
  496. end;
  497. function TBooleanConstant.AsPointer: PChar;
  498. begin
  499. Result := PChar(@FValue);
  500. end;
  501. { TIntegerConstant }
  502. constructor TIntegerConstant.Create(AValue: Integer);
  503. begin
  504. inherited Create(IntToStr(AValue), etInteger, _IntegerVariable);
  505. FValue := AValue;
  506. end;
  507. function TIntegerConstant.AsPointer: PChar;
  508. begin
  509. Result := PChar(@FValue);
  510. end;
  511. { TVariable }
  512. constructor TVariable.Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
  513. begin
  514. inherited Create(AName, AExprFunc);
  515. FResultType := AVarType;
  516. end;
  517. function TVariable.GetCanVary: Boolean;
  518. begin
  519. Result := True;
  520. end;
  521. function TVariable.GetResultType: TExpressionType;
  522. begin
  523. Result := FResultType;
  524. end;
  525. { TFloatVariable }
  526. constructor TFloatVariable.Create(AName: string; AValue: PDouble);
  527. begin
  528. inherited Create(AName, etFloat, _FloatVariable);
  529. FValue := AValue;
  530. end;
  531. function TFloatVariable.AsPointer: PChar;
  532. begin
  533. Result := PChar(FValue);
  534. end;
  535. { TStringVariable }
  536. constructor TStringVariable.Create(AName: string; AValue: PPChar; AFixedLen: Integer);
  537. begin
  538. // variable or fixed length?
  539. if (AFixedLen < 0) then
  540. inherited Create(AName, etString, _StringVariable)
  541. else
  542. inherited Create(AName, etString, _StringVariableFixedLen);
  543. // store pointer to string
  544. FValue := AValue;
  545. FFixedLen := AFixedLen;
  546. end;
  547. function TStringVariable.AsPointer: PChar;
  548. begin
  549. Result := PChar(FValue);
  550. end;
  551. function TStringVariable.GetFixedLen: Integer;
  552. begin
  553. Result := FFixedLen;
  554. end;
  555. function TStringVariable.LenAsPointer: PInteger;
  556. begin
  557. Result := @FFixedLen;
  558. end;
  559. { TDateTimeVariable }
  560. constructor TDateTimeVariable.Create(AName: string; AValue: PDateTimeRec);
  561. begin
  562. inherited Create(AName, etDateTime, _DateTimeVariable);
  563. FValue := AValue;
  564. end;
  565. function TDateTimeVariable.AsPointer: PChar;
  566. begin
  567. Result := PChar(FValue);
  568. end;
  569. { TIntegerVariable }
  570. constructor TIntegerVariable.Create(AName: string; AValue: PInteger);
  571. begin
  572. inherited Create(AName, etInteger, _IntegerVariable);
  573. FValue := AValue;
  574. end;
  575. function TIntegerVariable.AsPointer: PChar;
  576. begin
  577. Result := PChar(FValue);
  578. end;
  579. {$ifdef SUPPORT_INT64}
  580. { TLargeIntVariable }
  581. constructor TLargeIntVariable.Create(AName: string; AValue: PLargeInt);
  582. begin
  583. inherited Create(AName, etLargeInt, _LargeIntVariable);
  584. FValue := AValue;
  585. end;
  586. function TLargeIntVariable.AsPointer: PChar;
  587. begin
  588. Result := PChar(FValue);
  589. end;
  590. {$endif}
  591. { TBooleanVariable }
  592. constructor TBooleanVariable.Create(AName: string; AValue: PBoolean);
  593. begin
  594. inherited Create(AName, etBoolean, _BooleanVariable);
  595. FValue := AValue;
  596. end;
  597. function TBooleanVariable.AsPointer: PChar;
  598. begin
  599. Result := PChar(FValue);
  600. end;
  601. { TLeftBracket }
  602. function TLeftBracket.GetResultType: TExpressionType;
  603. begin
  604. Result := etLeftBracket;
  605. end;
  606. { TRightBracket }
  607. function TRightBracket.GetResultType: TExpressionType;
  608. begin
  609. Result := etRightBracket;
  610. end;
  611. { TComma }
  612. function TComma.GetResultType: TExpressionType;
  613. begin
  614. Result := etComma;
  615. end;
  616. { TExpressList }
  617. constructor TExpressList.Create;
  618. begin
  619. inherited;
  620. FShortList := TExpressShortList.Create;
  621. end;
  622. destructor TExpressList.Destroy;
  623. begin
  624. inherited;
  625. FShortList.Free;
  626. end;
  627. procedure TExpressList.Add(Item: Pointer);
  628. var
  629. I: Integer;
  630. begin
  631. inherited;
  632. { remember we reference the object }
  633. Inc(TExprWord(Item).FRefCount);
  634. { also add ShortName as reference }
  635. if Length(TExprWord(Item).ShortName) > 0 then
  636. begin
  637. FShortList.Search(FShortList.KeyOf(Item), I);
  638. FShortList.Insert(I, Item);
  639. end;
  640. end;
  641. function TExpressList.Compare(Key1, Key2: Pointer): Integer;
  642. begin
  643. Result := StrIComp(PChar(Key1), PChar(Key2));
  644. end;
  645. function TExpressList.KeyOf(Item: Pointer): Pointer;
  646. begin
  647. Result := PChar(TExprWord(Item).Name);
  648. end;
  649. procedure TExpressList.FreeItem(Item: Pointer);
  650. begin
  651. Dec(TExprWord(Item).FRefCount);
  652. FShortList.Remove(Item);
  653. if TExprWord(Item).FRefCount = 0 then
  654. inherited;
  655. end;
  656. function TExpressList.Search(Key: Pointer; var Index: Integer): Boolean;
  657. var
  658. SecIndex: Integer;
  659. begin
  660. Result := inherited Search(Key, Index);
  661. if not Result then
  662. begin
  663. Result := FShortList.Search(Key, SecIndex);
  664. if Result then
  665. Index := IndexOf(FShortList.Items[SecIndex]);
  666. end;
  667. end;
  668. function TExpressShortList.Compare(Key1, Key2: Pointer): Integer;
  669. begin
  670. Result := StrIComp(PChar(Key1), PChar(Key2));
  671. end;
  672. function TExpressShortList.KeyOf(Item: Pointer): Pointer;
  673. begin
  674. Result := PChar(TExprWord(Item).ShortName);
  675. end;
  676. procedure TExpressShortList.FreeItem(Item: Pointer);
  677. begin
  678. end;
  679. { TExprCollection }
  680. procedure TExprCollection.Check;
  681. var
  682. brCount, I: Integer;
  683. begin
  684. brCount := 0;
  685. for I := 0 to Count - 1 do
  686. begin
  687. case TExprWord(Items[I]).ResultType of
  688. etLeftBracket: Inc(brCount);
  689. etRightBracket: Dec(brCount);
  690. end;
  691. end;
  692. if brCount <> 0 then
  693. raise EParserException.Create('Unequal brackets');
  694. end;
  695. procedure TExprCollection.EraseExtraBrackets;
  696. var
  697. I: Integer;
  698. brCount: Integer;
  699. begin
  700. if (TExprWord(Items[0]).ResultType = etLeftBracket) then
  701. begin
  702. brCount := 1;
  703. I := 1;
  704. while (I < Count) and (brCount > 0) do
  705. begin
  706. case TExprWord(Items[I]).ResultType of
  707. etLeftBracket: Inc(brCount);
  708. etRightBracket: Dec(brCount);
  709. end;
  710. Inc(I);
  711. end;
  712. if (brCount = 0) and (I = Count) and (TExprWord(Items[I - 1]).ResultType =
  713. etRightBracket) then
  714. begin
  715. for I := 0 to Count - 3 do
  716. Items[I] := Items[I + 1];
  717. Count := Count - 2;
  718. EraseExtraBrackets; //Check if there are still too many brackets
  719. end;
  720. end;
  721. end;
  722. { TFunction }
  723. constructor TFunction.Create(AName, AShortName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
  724. AExprFunc: TExprFunc; Descr: string);
  725. begin
  726. //to increase compatibility don't use default parameters
  727. FDescription := Descr;
  728. FShortName := AShortName;
  729. InternalCreate(AName, ATypeSpec, AMinFuncArg, AResultType, AExprFunc, false, 0);
  730. end;
  731. constructor TFunction.CreateOper(AName, ATypeSpec: string; AResultType: TExpressionType;
  732. AExprFunc: TExprFunc; AOperPrec: Integer);
  733. begin
  734. InternalCreate(AName, ATypeSpec, -1, AResultType, AExprFunc, true, AOperPrec);
  735. end;
  736. procedure TFunction.InternalCreate(AName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
  737. AExprFunc: TExprFunc; AIsOperator: Boolean; AOperPrec: Integer);
  738. begin
  739. inherited Create(AName, AExprFunc);
  740. FMaxFunctionArg := Length(ATypeSpec);
  741. FMinFunctionArg := AMinFuncArg;
  742. if AMinFuncArg = -1 then
  743. FMinFunctionArg := FMaxFunctionArg;
  744. FIsOperator := AIsOperator;
  745. FOperPrec := AOperPrec;
  746. FTypeSpec := ATypeSpec;
  747. FResultType := AResultType;
  748. // check correctness
  749. if FMaxFunctionArg > MaxArg then
  750. raise EParserException.Create('Too many arguments');
  751. end;
  752. function TFunction.GetDescription: string;
  753. begin
  754. Result := FDescription;
  755. end;
  756. function TFunction.GetIsOperator: Boolean;
  757. begin
  758. Result := FIsOperator;
  759. end;
  760. function TFunction.GetMinFunctionArg: Integer;
  761. begin
  762. Result := FMinFunctionArg;
  763. end;
  764. function TFunction.GetMaxFunctionArg: Integer;
  765. begin
  766. Result := FMaxFunctionArg;
  767. end;
  768. function TFunction.GetResultType: TExpressionType;
  769. begin
  770. Result := FResultType;
  771. end;
  772. function TFunction.GetShortName: string;
  773. begin
  774. Result := FShortName;
  775. end;
  776. function TFunction.GetTypeSpec: string;
  777. begin
  778. Result := FTypeSpec;
  779. end;
  780. function TFunction.IsFunction: Boolean;
  781. begin
  782. Result := True;
  783. end;
  784. { TVaryingFunction }
  785. function TVaryingFunction.GetCanVary: Boolean;
  786. begin
  787. Result := True;
  788. end;
  789. { TDynamicType }
  790. constructor TDynamicType.Create(DestMem, DestPos: PPChar; Size: PInteger);
  791. begin
  792. inherited Create;
  793. FMemory := DestMem;
  794. FMemoryPos := DestPos;
  795. FSize := Size;
  796. end;
  797. procedure TDynamicType.Rewind;
  798. begin
  799. FMemoryPos^ := FMemory^;
  800. end;
  801. procedure TDynamicType.AssureSpace(ASize: Integer);
  802. begin
  803. // need more memory?
  804. if ((FMemoryPos^) - (FMemory^) + ASize) > (FSize^) then
  805. Resize((FMemoryPos^) - (FMemory^) + ASize, False);
  806. end;
  807. procedure TDynamicType.Resize(NewSize: Integer; Exact: Boolean);
  808. var
  809. tempBuf: PChar;
  810. bytesCopy, pos: Integer;
  811. begin
  812. // if not exact requested make newlength a multiple of ArgAllocSize
  813. if not Exact then
  814. NewSize := NewSize div ArgAllocSize * ArgAllocSize + ArgAllocSize;
  815. // create new buffer
  816. GetMem(tempBuf, NewSize);
  817. // copy memory
  818. bytesCopy := FSize^;
  819. if bytesCopy > NewSize then
  820. bytesCopy := NewSize;
  821. Move(FMemory^^, tempBuf^, bytesCopy);
  822. // save position in string
  823. pos := FMemoryPos^ - FMemory^;
  824. // delete old mem
  825. FreeMem(FMemory^);
  826. // assign new
  827. FMemory^ := tempBuf;
  828. FSize^ := NewSize;
  829. // assign position
  830. FMemoryPos^ := FMemory^ + pos;
  831. end;
  832. procedure TDynamicType.Append(Source: PChar; Length: Integer);
  833. begin
  834. // make room for string plus null-terminator
  835. AssureSpace(Length+4);
  836. // copy
  837. Move(Source^, FMemoryPos^^, Length);
  838. Inc(FMemoryPos^, Length);
  839. // null-terminate
  840. FMemoryPos^^ := #0;
  841. end;
  842. procedure TDynamicType.AppendInteger(Source: Integer);
  843. begin
  844. // make room for number
  845. AssureSpace(12);
  846. Inc(FMemoryPos^, GetStrFromInt(Source, FMemoryPos^));
  847. FMemoryPos^^ := #0;
  848. end;
  849. end.