dbf_prsdef.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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; ASize: 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. // delphi wants to call the function pointed to by the variable, use '@'
  375. // fpc simply returns pointer to function, no '@' needed
  376. Result := (@FExprFunc = @_StringVariable) or
  377. (@FExprFunc = @_StringConstant) or
  378. (@FExprFunc = @_StringVariableFixedLen) or
  379. (@FExprFunc = @_FloatVariable) or
  380. (@FExprFunc = @_IntegerVariable) or
  381. // (FExprFunc = @_SmallIntVariable) or
  382. {$ifdef SUPPORT_INT64}
  383. (@FExprFunc = @_LargeIntVariable) or
  384. {$endif}
  385. (@FExprFunc = @_DateTimeVariable) or
  386. (@FExprFunc = @_BooleanVariable);
  387. end;
  388. function TExprWord.GetNeedsCopy: Boolean;
  389. begin
  390. Result := (@FExprFunc <> @_StringConstant) and
  391. // (@FExprFunc <> @_StringVariable) and
  392. // (@FExprFunc <> @_StringVariableFixedLen) and
  393. // string variable cannot be used as normal parameter
  394. // because it is indirectly referenced and possibly
  395. // not null-terminated (fixed len)
  396. (@FExprFunc <> @_FloatVariable) and
  397. (@FExprFunc <> @_IntegerVariable) and
  398. // (FExprFunc <> @_SmallIntVariable) and
  399. {$ifdef SUPPORT_INT64}
  400. (@FExprFunc <> @_LargeIntVariable) and
  401. {$endif}
  402. (@FExprFunc <> @_DateTimeVariable) and
  403. (@FExprFunc <> @_BooleanVariable);
  404. end;
  405. function TExprWord.GetFixedLen: Integer;
  406. begin
  407. // -1 means variable, non-fixed length
  408. Result := -1;
  409. end;
  410. function TExprWord.GetMinFunctionArg: Integer;
  411. begin
  412. Result := 0;
  413. end;
  414. function TExprWord.GetMaxFunctionArg: Integer;
  415. begin
  416. Result := 0;
  417. end;
  418. function TExprWord.GetResultType: TExpressionType;
  419. begin
  420. Result := etUnknown;
  421. end;
  422. function TExprWord.GetTypeSpec: string;
  423. begin
  424. Result := EmptyStr;
  425. end;
  426. function TExprWord.AsPointer: PChar;
  427. begin
  428. Result := nil;
  429. end;
  430. function TExprWord.LenAsPointer: PInteger;
  431. begin
  432. Result := nil;
  433. end;
  434. function TExprWord.IsFunction: Boolean;
  435. begin
  436. Result := False;
  437. end;
  438. { TConstant }
  439. constructor TConstant.Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
  440. begin
  441. inherited Create(AName, AExprFunc);
  442. FResultType := AVarType;
  443. end;
  444. function TConstant.GetResultType: TExpressionType;
  445. begin
  446. Result := FResultType;
  447. end;
  448. { TFloatConstant }
  449. constructor TFloatConstant.Create(AName, AValue: string);
  450. begin
  451. inherited Create(AName, etFloat, _FloatVariable);
  452. if Length(AValue) > 0 then
  453. FValue := StrToFloat(AValue)
  454. else
  455. FValue := 0.0;
  456. end;
  457. constructor TFloatConstant.CreateAsDouble(AName: string; AValue: Double);
  458. begin
  459. inherited Create(AName, etFloat, _FloatVariable);
  460. FValue := AValue;
  461. end;
  462. function TFloatConstant.AsPointer: PChar;
  463. begin
  464. Result := PChar(@FValue);
  465. end;
  466. { TUserConstant }
  467. constructor TUserConstant.CreateAsDouble(AName, Descr: string; AValue: Double);
  468. begin
  469. FDescription := Descr;
  470. inherited CreateAsDouble(AName, AValue);
  471. end;
  472. function TUserConstant.GetDescription: string;
  473. begin
  474. Result := FDescription;
  475. end;
  476. { TStringConstant }
  477. constructor TStringConstant.Create(AValue: string);
  478. var
  479. firstChar, lastChar: Char;
  480. begin
  481. inherited Create(AValue, etString, _StringConstant);
  482. firstChar := AValue[1];
  483. lastChar := AValue[Length(AValue)];
  484. if (firstChar = lastChar) and ((firstChar = '''') or (firstChar = '"')) then
  485. FValue := Copy(AValue, 2, Length(AValue) - 2)
  486. else
  487. FValue := AValue;
  488. end;
  489. function TStringConstant.AsPointer: PChar;
  490. begin
  491. Result := PChar(FValue);
  492. end;
  493. { TBooleanConstant }
  494. constructor TBooleanConstant.Create(AName: string; AValue: Boolean);
  495. begin
  496. inherited Create(AName, etBoolean, _BooleanVariable);
  497. FValue := AValue;
  498. end;
  499. function TBooleanConstant.AsPointer: PChar;
  500. begin
  501. Result := PChar(@FValue);
  502. end;
  503. { TIntegerConstant }
  504. constructor TIntegerConstant.Create(AValue: Integer);
  505. begin
  506. inherited Create(IntToStr(AValue), etInteger, _IntegerVariable);
  507. FValue := AValue;
  508. end;
  509. function TIntegerConstant.AsPointer: PChar;
  510. begin
  511. Result := PChar(@FValue);
  512. end;
  513. { TVariable }
  514. constructor TVariable.Create(AName: string; AVarType: TExpressionType; AExprFunc: TExprFunc);
  515. begin
  516. inherited Create(AName, AExprFunc);
  517. FResultType := AVarType;
  518. end;
  519. function TVariable.GetCanVary: Boolean;
  520. begin
  521. Result := True;
  522. end;
  523. function TVariable.GetResultType: TExpressionType;
  524. begin
  525. Result := FResultType;
  526. end;
  527. { TFloatVariable }
  528. constructor TFloatVariable.Create(AName: string; AValue: PDouble);
  529. begin
  530. inherited Create(AName, etFloat, _FloatVariable);
  531. FValue := AValue;
  532. end;
  533. function TFloatVariable.AsPointer: PChar;
  534. begin
  535. Result := PChar(FValue);
  536. end;
  537. { TStringVariable }
  538. constructor TStringVariable.Create(AName: string; AValue: PPChar; AFixedLen: Integer);
  539. begin
  540. // variable or fixed length?
  541. if (AFixedLen < 0) then
  542. inherited Create(AName, etString, _StringVariable)
  543. else
  544. inherited Create(AName, etString, _StringVariableFixedLen);
  545. // store pointer to string
  546. FValue := AValue;
  547. FFixedLen := AFixedLen;
  548. end;
  549. function TStringVariable.AsPointer: PChar;
  550. begin
  551. Result := PChar(FValue);
  552. end;
  553. function TStringVariable.GetFixedLen: Integer;
  554. begin
  555. Result := FFixedLen;
  556. end;
  557. function TStringVariable.LenAsPointer: PInteger;
  558. begin
  559. Result := @FFixedLen;
  560. end;
  561. { TDateTimeVariable }
  562. constructor TDateTimeVariable.Create(AName: string; AValue: PDateTimeRec);
  563. begin
  564. inherited Create(AName, etDateTime, _DateTimeVariable);
  565. FValue := AValue;
  566. end;
  567. function TDateTimeVariable.AsPointer: PChar;
  568. begin
  569. Result := PChar(FValue);
  570. end;
  571. { TIntegerVariable }
  572. constructor TIntegerVariable.Create(AName: string; AValue: PInteger);
  573. begin
  574. inherited Create(AName, etInteger, _IntegerVariable);
  575. FValue := AValue;
  576. end;
  577. function TIntegerVariable.AsPointer: PChar;
  578. begin
  579. Result := PChar(FValue);
  580. end;
  581. {$ifdef SUPPORT_INT64}
  582. { TLargeIntVariable }
  583. constructor TLargeIntVariable.Create(AName: string; AValue: PLargeInt);
  584. begin
  585. inherited Create(AName, etLargeInt, _LargeIntVariable);
  586. FValue := AValue;
  587. end;
  588. function TLargeIntVariable.AsPointer: PChar;
  589. begin
  590. Result := PChar(FValue);
  591. end;
  592. {$endif}
  593. { TBooleanVariable }
  594. constructor TBooleanVariable.Create(AName: string; AValue: PBoolean);
  595. begin
  596. inherited Create(AName, etBoolean, _BooleanVariable);
  597. FValue := AValue;
  598. end;
  599. function TBooleanVariable.AsPointer: PChar;
  600. begin
  601. Result := PChar(FValue);
  602. end;
  603. { TLeftBracket }
  604. function TLeftBracket.GetResultType: TExpressionType;
  605. begin
  606. Result := etLeftBracket;
  607. end;
  608. { TRightBracket }
  609. function TRightBracket.GetResultType: TExpressionType;
  610. begin
  611. Result := etRightBracket;
  612. end;
  613. { TComma }
  614. function TComma.GetResultType: TExpressionType;
  615. begin
  616. Result := etComma;
  617. end;
  618. { TExpressList }
  619. constructor TExpressList.Create;
  620. begin
  621. inherited;
  622. FShortList := TExpressShortList.Create;
  623. end;
  624. destructor TExpressList.Destroy;
  625. begin
  626. inherited;
  627. FShortList.Free;
  628. end;
  629. procedure TExpressList.Add(Item: Pointer);
  630. var
  631. I: Integer;
  632. begin
  633. inherited;
  634. { remember we reference the object }
  635. Inc(TExprWord(Item).FRefCount);
  636. { also add ShortName as reference }
  637. if Length(TExprWord(Item).ShortName) > 0 then
  638. begin
  639. FShortList.Search(FShortList.KeyOf(Item), I);
  640. FShortList.Insert(I, Item);
  641. end;
  642. end;
  643. function TExpressList.Compare(Key1, Key2: Pointer): Integer;
  644. begin
  645. Result := StrIComp(PChar(Key1), PChar(Key2));
  646. end;
  647. function TExpressList.KeyOf(Item: Pointer): Pointer;
  648. begin
  649. Result := PChar(TExprWord(Item).Name);
  650. end;
  651. procedure TExpressList.FreeItem(Item: Pointer);
  652. begin
  653. Dec(TExprWord(Item).FRefCount);
  654. FShortList.Remove(Item);
  655. if TExprWord(Item).FRefCount = 0 then
  656. inherited;
  657. end;
  658. function TExpressList.Search(Key: Pointer; var Index: Integer): Boolean;
  659. var
  660. SecIndex: Integer;
  661. begin
  662. Result := inherited Search(Key, Index);
  663. if not Result then
  664. begin
  665. Result := FShortList.Search(Key, SecIndex);
  666. if Result then
  667. Index := IndexOf(FShortList.Items[SecIndex]);
  668. end;
  669. end;
  670. function TExpressShortList.Compare(Key1, Key2: Pointer): Integer;
  671. begin
  672. Result := StrIComp(PChar(Key1), PChar(Key2));
  673. end;
  674. function TExpressShortList.KeyOf(Item: Pointer): Pointer;
  675. begin
  676. Result := PChar(TExprWord(Item).ShortName);
  677. end;
  678. procedure TExpressShortList.FreeItem(Item: Pointer);
  679. begin
  680. end;
  681. { TExprCollection }
  682. procedure TExprCollection.Check;
  683. var
  684. brCount, I: Integer;
  685. begin
  686. brCount := 0;
  687. for I := 0 to Count - 1 do
  688. begin
  689. case TExprWord(Items[I]).ResultType of
  690. etLeftBracket: Inc(brCount);
  691. etRightBracket: Dec(brCount);
  692. end;
  693. end;
  694. if brCount <> 0 then
  695. raise EParserException.Create('Unequal brackets');
  696. end;
  697. procedure TExprCollection.EraseExtraBrackets;
  698. var
  699. I: Integer;
  700. brCount: Integer;
  701. begin
  702. if (TExprWord(Items[0]).ResultType = etLeftBracket) then
  703. begin
  704. brCount := 1;
  705. I := 1;
  706. while (I < Count) and (brCount > 0) do
  707. begin
  708. case TExprWord(Items[I]).ResultType of
  709. etLeftBracket: Inc(brCount);
  710. etRightBracket: Dec(brCount);
  711. end;
  712. Inc(I);
  713. end;
  714. if (brCount = 0) and (I = Count) and (TExprWord(Items[I - 1]).ResultType =
  715. etRightBracket) then
  716. begin
  717. for I := 0 to Count - 3 do
  718. Items[I] := Items[I + 1];
  719. Count := Count - 2;
  720. EraseExtraBrackets; //Check if there are still too many brackets
  721. end;
  722. end;
  723. end;
  724. { TFunction }
  725. constructor TFunction.Create(AName, AShortName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
  726. AExprFunc: TExprFunc; Descr: string);
  727. begin
  728. //to increase compatibility don't use default parameters
  729. FDescription := Descr;
  730. FShortName := AShortName;
  731. InternalCreate(AName, ATypeSpec, AMinFuncArg, AResultType, AExprFunc, false, 0);
  732. end;
  733. constructor TFunction.CreateOper(AName, ATypeSpec: string; AResultType: TExpressionType;
  734. AExprFunc: TExprFunc; AOperPrec: Integer);
  735. begin
  736. InternalCreate(AName, ATypeSpec, -1, AResultType, AExprFunc, true, AOperPrec);
  737. end;
  738. procedure TFunction.InternalCreate(AName, ATypeSpec: string; AMinFuncArg: Integer; AResultType: TExpressionType;
  739. AExprFunc: TExprFunc; AIsOperator: Boolean; AOperPrec: Integer);
  740. begin
  741. inherited Create(AName, AExprFunc);
  742. FMaxFunctionArg := Length(ATypeSpec);
  743. FMinFunctionArg := AMinFuncArg;
  744. if AMinFuncArg = -1 then
  745. FMinFunctionArg := FMaxFunctionArg;
  746. FIsOperator := AIsOperator;
  747. FOperPrec := AOperPrec;
  748. FTypeSpec := ATypeSpec;
  749. FResultType := AResultType;
  750. // check correctness
  751. if FMaxFunctionArg > MaxArg then
  752. raise EParserException.Create('Too many arguments');
  753. end;
  754. function TFunction.GetDescription: string;
  755. begin
  756. Result := FDescription;
  757. end;
  758. function TFunction.GetIsOperator: Boolean;
  759. begin
  760. Result := FIsOperator;
  761. end;
  762. function TFunction.GetMinFunctionArg: Integer;
  763. begin
  764. Result := FMinFunctionArg;
  765. end;
  766. function TFunction.GetMaxFunctionArg: Integer;
  767. begin
  768. Result := FMaxFunctionArg;
  769. end;
  770. function TFunction.GetResultType: TExpressionType;
  771. begin
  772. Result := FResultType;
  773. end;
  774. function TFunction.GetShortName: string;
  775. begin
  776. Result := FShortName;
  777. end;
  778. function TFunction.GetTypeSpec: string;
  779. begin
  780. Result := FTypeSpec;
  781. end;
  782. function TFunction.IsFunction: Boolean;
  783. begin
  784. Result := True;
  785. end;
  786. { TVaryingFunction }
  787. function TVaryingFunction.GetCanVary: Boolean;
  788. begin
  789. Result := True;
  790. end;
  791. { TDynamicType }
  792. constructor TDynamicType.Create(DestMem, DestPos: PPChar; ASize: PInteger);
  793. begin
  794. inherited Create;
  795. FMemory := DestMem;
  796. FMemoryPos := DestPos;
  797. FSize := ASize;
  798. end;
  799. procedure TDynamicType.Rewind;
  800. begin
  801. FMemoryPos^ := FMemory^;
  802. end;
  803. procedure TDynamicType.AssureSpace(ASize: Integer);
  804. begin
  805. // need more memory?
  806. if ((FMemoryPos^) - (FMemory^) + ASize) > (FSize^) then
  807. Resize((FMemoryPos^) - (FMemory^) + ASize, False);
  808. end;
  809. procedure TDynamicType.Resize(NewSize: Integer; Exact: Boolean);
  810. var
  811. tempBuf: PChar;
  812. bytesCopy, pos: Integer;
  813. begin
  814. // if not exact requested make newlength a multiple of ArgAllocSize
  815. if not Exact then
  816. NewSize := NewSize div ArgAllocSize * ArgAllocSize + ArgAllocSize;
  817. // create new buffer
  818. GetMem(tempBuf, NewSize);
  819. // copy memory
  820. bytesCopy := FSize^;
  821. if bytesCopy > NewSize then
  822. bytesCopy := NewSize;
  823. Move(FMemory^^, tempBuf^, bytesCopy);
  824. // save position in string
  825. pos := FMemoryPos^ - FMemory^;
  826. // delete old mem
  827. FreeMem(FMemory^);
  828. // assign new
  829. FMemory^ := tempBuf;
  830. FSize^ := NewSize;
  831. // assign position
  832. FMemoryPos^ := FMemory^ + pos;
  833. end;
  834. procedure TDynamicType.Append(Source: PChar; Length: Integer);
  835. begin
  836. // make room for string plus null-terminator
  837. AssureSpace(Length+4);
  838. // copy
  839. Move(Source^, FMemoryPos^^, Length);
  840. Inc(FMemoryPos^, Length);
  841. // null-terminate
  842. FMemoryPos^^ := #0;
  843. end;
  844. procedure TDynamicType.AppendInteger(Source: Integer);
  845. begin
  846. // make room for number
  847. AssureSpace(12);
  848. Inc(FMemoryPos^, GetStrFromInt(Source, FMemoryPos^));
  849. FMemoryPos^^ := #0;
  850. end;
  851. end.