tcexprparser.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. unit tcexprparser;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, tcbaseparser, pastree;
  6. type
  7. { TTestExpressions }
  8. TTestExpressions= class(TTestParser)
  9. private
  10. FLeft: TPAsExpr;
  11. FRight: TPAsExpr;
  12. FTheExpr: TPasExpr;
  13. FVariables : TStringList;
  14. procedure AssertLeftPrecedence(AInnerLeft: Integer; AInnerOp: TExprOpCode;
  15. AInnerRight: Integer; AOuterOp: TexprOpCode; AOuterRight: Integer);
  16. procedure AssertRightPrecedence(AOuterLeft: Integer; AOuterOp: TExprOpCode;
  17. AInnerLeft: Integer; AInnerOp: TexprOpCode; AInnerRight: Integer);
  18. procedure DeclareVar(const AVarType: String; const AVarName: String = 'a');
  19. protected
  20. procedure SetUp; override;
  21. procedure TearDown; override;
  22. Procedure SetExpression(Const AExpression : String);
  23. Procedure ParseExpression;
  24. Procedure ParseExpression(Const AExpression : String);
  25. Function AssertBinaryExpr(Const Msg : String; Op : TExprOpCode; Out ALeft,ARight : TPasExpr) : TBinaryExpr;
  26. Function AssertBinaryExpr(Const Msg : String; AExpr : TPasExpr; Op : TExprOpCode; Out ALeft,ARight : TPasExpr) : TBinaryExpr;
  27. Function AssertUnaryExpr(Const Msg : String; Op : TExprOpCode; Out AOperand : TPasExpr) : TUnaryExpr;
  28. Function AssertUnaryExpr(Const Msg : String; AExpr: TPasExpr; Op : TExprOpCode; Out AOperand : TPasExpr) : TUnaryExpr;
  29. Property TheExpr : TPasExpr read FTheExpr;
  30. Property Theleft : TPAsExpr Read FLeft;
  31. Property TheRight : TPAsExpr Read FRight;
  32. published
  33. {
  34. TPasExprKind = (pekRange,
  35. pekListOfExp, );
  36. }
  37. procedure TestPrimitiveInteger;
  38. procedure TestPrimitiveIntegerHex;
  39. procedure TestPrimitiveIntegerOctal;
  40. procedure TestPrimitiveIntegerBinary;
  41. procedure TestPrimitiveDouble;
  42. procedure TestPrimitiveDouble2;
  43. procedure TestPrimitiveDouble3;
  44. procedure TestPrimitiveDouble4;
  45. procedure TestPrimitiveDouble5;
  46. procedure TestPrimitiveDouble6;
  47. procedure TestPrimitiveDouble7;
  48. procedure TestPrimitiveDouble8;
  49. procedure TestPrimitiveDouble9;
  50. procedure TestPrimitiveDouble10;
  51. procedure TestPrimitiveDouble11;
  52. procedure TestPrimitiveString;
  53. procedure TestPrimitiveIdent;
  54. procedure TestPrimitiveBooleanFalse;
  55. procedure TestPrimitiveBooleanTrue;
  56. procedure TestPrimitiveNil;
  57. procedure TestPrimitiveSet;
  58. procedure TestPrimitiveChar;
  59. procedure TestPrimitiveControlChar;
  60. procedure TestPrimitiveSetEmpty;
  61. procedure TestPrimitiveSelf;
  62. Procedure TestInherited;
  63. Procedure TestInheritedFunction;
  64. Procedure TestUnaryMinus;
  65. Procedure TestUnaryMinusWhiteSpace;
  66. Procedure TestUnaryAddress;
  67. Procedure TestUnaryNot;
  68. Procedure TestUnaryDeref;
  69. Procedure TestUnaryDoubleDeref;
  70. Procedure TestUnaryDoubleDeref2;
  71. Procedure TestBinaryAdd;
  72. Procedure TestBinarySubtract;
  73. Procedure TestBinaryMultiply;
  74. Procedure TestBinaryDivision;
  75. Procedure TestBinaryPower;
  76. Procedure TestBinaryMod;
  77. Procedure TestBinaryDiv;
  78. procedure TestBinaryShl;
  79. procedure TestBinaryShr;
  80. Procedure TestBinarySymmetricalDifference;
  81. Procedure TestBinaryAnd;
  82. Procedure TestBinaryOr;
  83. Procedure TestBinaryXOr;
  84. Procedure TestBinaryIn;
  85. Procedure TestBinaryIs;
  86. Procedure TestBinaryAs;
  87. Procedure TestBinaryEquals;
  88. Procedure TestBinaryDiffers;
  89. Procedure TestBinaryLessThan;
  90. Procedure TestBinaryLessThanEqual;
  91. Procedure TestBinaryLargerThan;
  92. Procedure TestBinaryLargerThanEqual;
  93. procedure TestBinaryFullIdent;
  94. Procedure TestArrayElement;
  95. Procedure TestArrayElementrecord;
  96. Procedure TestArrayElement2Dims;
  97. Procedure TestFunctionCall;
  98. Procedure TestFunctionCall2args;
  99. Procedure TestFunctionCallNoArgs;
  100. Procedure ParseStrWithFormatFullyQualified;
  101. Procedure TestRange;
  102. Procedure TestBracketsTotal;
  103. Procedure TestBracketsLeft;
  104. Procedure TestBracketsRight;
  105. Procedure TestPrecedenceLeftToRight;
  106. Procedure TestPrecedenceLeftToRightMinus;
  107. Procedure TestPrecedenceLeftToRightMultiply;
  108. Procedure TestPrecedenceLeftToRightDivision;
  109. Procedure TestPrecedenceLeftToRightPlusMinus;
  110. Procedure TestPrecedenceLeftToRightMinusPlus;
  111. Procedure TestPrecedenceLeftToRightMultiplyDivision;
  112. Procedure TestPrecedenceLeftToRightDivisionMultiply;
  113. Procedure TestPrecedencePlusMultiply;
  114. Procedure TestPrecedencePlusDivide;
  115. Procedure TestPrecedenceMinusMultiply;
  116. Procedure TestPrecedenceMinusDivide;
  117. Procedure TestPrecedencePlusOr;
  118. Procedure TestPrecedenceAndOr;
  119. Procedure TestPrecedenceAndNot;
  120. Procedure TestPrecedencePlusAnd;
  121. Procedure TestPrecedenceMinusOr;
  122. Procedure TestPrecedenceMinusAnd;
  123. Procedure TestPrecedenceMultiplyOr;
  124. Procedure TestPrecedenceMultiplyAnd;
  125. Procedure TestPrecedencePlusDiv;
  126. Procedure TestPrecedencePlusMod;
  127. Procedure TestPrecedenceMultiplyDiv;
  128. Procedure TestPrecedenceDivMultiply;
  129. Procedure TestTypeCast;
  130. procedure TestTypeCast2;
  131. Procedure TestCreate;
  132. procedure TestChainedPointers;
  133. Procedure TestNilCaret;
  134. Procedure TestExpCaret;
  135. end;
  136. implementation
  137. procedure TTestExpressions.DeclareVar(const AVarType: String;
  138. const AVarName: String = 'a');
  139. begin
  140. FVariables.Add(AVarName+' : '+AVarType+';');
  141. end;
  142. procedure TTestExpressions.TestPrimitiveInteger;
  143. begin
  144. ParseExpression('1');
  145. AssertExpression('Simple integer',theExpr,pekNumber,'1');
  146. end;
  147. procedure TTestExpressions.TestPrimitiveIntegerHex;
  148. begin
  149. ParseExpression('$FF');
  150. AssertExpression('Simple integer',theExpr,pekNumber,'$FF');
  151. end;
  152. procedure TTestExpressions.TestPrimitiveIntegerOctal;
  153. begin
  154. ParseExpression('&777');
  155. AssertExpression('Simple integer',theExpr,pekNumber,'&777');
  156. end;
  157. procedure TTestExpressions.TestPrimitiveIntegerBinary;
  158. begin
  159. ParseExpression('%10101010');
  160. AssertExpression('Simple integer',theExpr,pekNumber,'%10101010');
  161. end;
  162. procedure TTestExpressions.TestPrimitiveDouble;
  163. begin
  164. ParseExpression('1.2');
  165. AssertExpression('Simple double',theExpr,pekNumber,'1.2');
  166. end;
  167. procedure TTestExpressions.TestPrimitiveDouble2;
  168. begin
  169. ParseExpression('1.200');
  170. AssertExpression('Simple double',theExpr,pekNumber,'1.200');
  171. end;
  172. procedure TTestExpressions.TestPrimitiveDouble3;
  173. begin
  174. ParseExpression('01.2');
  175. AssertExpression('Simple double',theExpr,pekNumber,'01.2');
  176. end;
  177. procedure TTestExpressions.TestPrimitiveDouble4;
  178. begin
  179. ParseExpression('1.2e10');
  180. AssertExpression('Simple double',theExpr,pekNumber,'1.2e10');
  181. end;
  182. procedure TTestExpressions.TestPrimitiveDouble5;
  183. begin
  184. ParseExpression('1.2e-10');
  185. AssertExpression('Simple double',theExpr,pekNumber,'1.2e-10');
  186. end;
  187. procedure TTestExpressions.TestPrimitiveDouble6;
  188. begin
  189. ParseExpression('12e10');
  190. AssertExpression('Simple double',theExpr,pekNumber,'12e10');
  191. end;
  192. procedure TTestExpressions.TestPrimitiveDouble7;
  193. begin
  194. ParseExpression('12e-10');
  195. AssertExpression('Simple double',theExpr,pekNumber,'12e-10');
  196. end;
  197. procedure TTestExpressions.TestPrimitiveDouble8;
  198. begin
  199. ParseExpression('8.5');
  200. AssertExpression('Simple double',theExpr,pekNumber,'8.5');
  201. end;
  202. procedure TTestExpressions.TestPrimitiveDouble9;
  203. begin
  204. ParseExpression('8.E5');
  205. AssertExpression('Simple double',theExpr,pekNumber,'8.E5');
  206. end;
  207. procedure TTestExpressions.TestPrimitiveDouble10;
  208. begin
  209. ParseExpression('8.E-5');
  210. AssertExpression('Simple double',theExpr,pekNumber,'8.E-5');
  211. end;
  212. procedure TTestExpressions.TestPrimitiveDouble11;
  213. begin
  214. ParseExpression('8E+5');
  215. AssertExpression('Simple double',theExpr,pekNumber,'8E+5');
  216. end;
  217. procedure TTestExpressions.TestPrimitiveString;
  218. begin
  219. DeclareVar('string');
  220. ParseExpression('''123''');
  221. AssertExpression('Simple string',theExpr,pekString,'''123''');
  222. end;
  223. procedure TTestExpressions.TestPrimitiveIdent;
  224. begin
  225. DeclareVar('integer','a');
  226. DeclareVar('integer','b');
  227. ParseExpression('b');
  228. AssertExpression('Simple identifier',theExpr,pekIdent,'b');
  229. end;
  230. procedure TTestExpressions.TestBinaryFullIdent;
  231. begin
  232. DeclareVar('integer','a');
  233. DeclareVar('record x,y : integer; end','b');
  234. ParseExpression('b.x');
  235. AssertBinaryExpr('sub identifier',eopSubIdent,Fleft,FRight);
  236. AssertExpression('Simple identifier',Theleft,pekIdent,'b');
  237. AssertExpression('Simple identifier',Theright,pekIdent,'x');
  238. end;
  239. procedure TTestExpressions.TestArrayElement;
  240. Var
  241. P : TParamsExpr;
  242. begin
  243. DeclareVar('integer','a');
  244. DeclareVar('array[1..2] of integer','b');
  245. ParseExpression('b[1]');
  246. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekArrayParams,TParamsExpr));
  247. AssertExpression('Name of array',P.Value,pekIdent,'b');
  248. AssertEquals('One dimension',1,Length(p.params));
  249. AssertExpression('Simple identifier',p.params[0],pekNumber,'1');
  250. end;
  251. procedure TTestExpressions.TestArrayElementrecord;
  252. Var
  253. P : TParamsExpr;
  254. B : TBinaryExpr;
  255. begin
  256. DeclareVar('record a : array[1..2] of integer; end ','b');
  257. ParseExpression('b.a[1]');
  258. B:=AssertExpression('Binary of record',TheExpr,pekBinary,TBinaryExpr) as TBinaryExpr;
  259. AssertEquals('Name is Subident',eopSubIdent,B.Opcode);
  260. AssertExpression('Name of array',B.Left,pekIdent,'b');
  261. P:=TParamsExpr(AssertExpression('Simple identifier',B.right,pekArrayParams,TParamsExpr));
  262. AssertExpression('Name of array',P.Value,pekIdent,'a');
  263. TAssert.AssertSame('P.value.parent=P',P,P.Value.Parent);
  264. AssertEquals('One dimension',1,Length(P.params));
  265. AssertExpression('Simple identifier',P.params[0],pekNumber,'1');
  266. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  267. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  268. end;
  269. procedure TTestExpressions.TestArrayElement2Dims;
  270. Var
  271. P : TParamsExpr;
  272. begin
  273. DeclareVar('integer','a');
  274. DeclareVar('array[1..2,1..2] of integer','b');
  275. ParseExpression('b[1,2]');
  276. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekArrayParams,TParamsExpr));
  277. AssertExpression('Name of array',P.Value,pekIdent,'b');
  278. AssertEquals('Two dimensions',2,Length(p.params));
  279. AssertExpression('Simple identifier',p.params[0],pekNumber,'1');
  280. AssertExpression('Simple identifier',p.params[1],pekNumber,'2');
  281. end;
  282. procedure TTestExpressions.TestFunctionCall;
  283. Var
  284. P : TParamsExpr;
  285. begin
  286. DeclareVar('integer','a');
  287. ParseExpression('Random(10)');
  288. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekFuncParams,TParamsExpr));
  289. AssertExpression('Name of function',P.Value,pekIdent,'Random');
  290. AssertEquals('1 argument',1,Length(p.params));
  291. AssertExpression('Simple identifier',p.params[0],pekNumber,'10');
  292. end;
  293. procedure TTestExpressions.TestFunctionCall2args;
  294. Var
  295. P : TParamsExpr;
  296. begin
  297. DeclareVar('integer','a');
  298. ParseExpression('Random(10,12)');
  299. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekFuncParams,TParamsExpr));
  300. AssertExpression('Name of function',P.Value,pekIdent,'Random');
  301. AssertEquals('2 argument',2,Length(p.params));
  302. AssertExpression('Simple identifier 1',p.params[0],pekNumber,'10');
  303. AssertExpression('Simple identifier 2',p.params[1],pekNumber,'12');
  304. end;
  305. procedure TTestExpressions.TestFunctionCallNoArgs;
  306. Var
  307. P : TParamsExpr;
  308. begin
  309. DeclareVar('integer','a');
  310. ParseExpression('Random()');
  311. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekFuncParams,TParamsExpr));
  312. AssertExpression('Name of function',P.Value,pekIdent,'Random');
  313. AssertEquals('0 arguments',0,Length(p.params));
  314. end;
  315. procedure TTestExpressions.TestRange;
  316. Var
  317. P : TParamsExpr;
  318. B : TBinaryExpr;
  319. begin
  320. DeclareVar('boolean','a');
  321. DeclareVar('byte','b');
  322. ParseExpression('b in [0..10]');
  323. AssertBinaryExpr('Simple binary In',eopIn,FLeft,FRight);
  324. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  325. P:=TParamsExpr(AssertExpression('Right is set',TheRight,pekSet,TParamsExpr));
  326. AssertEquals('Number of items',1,Length(P.Params));
  327. B:=TBinaryExpr(AssertExpression('First element is range',P.Params[0],pekRange,TBinaryExpr));
  328. AssertExpression('Left is 0',B.Left,pekNumber,'0');
  329. AssertExpression('Right is 10',B.Right,pekNumber,'10');
  330. B:=TBinaryExpr(TheExpr);
  331. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  332. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  333. end;
  334. procedure TTestExpressions.TestBracketsTotal;
  335. begin
  336. DeclareVar('integer','a');
  337. ParseExpression('(3+4)');
  338. AssertBinaryExpr('simple binary add',eopAdd,FLeft,FRight);
  339. AssertExpression('Inner Left is 3',TheLeft,pekNumber,'3');
  340. AssertExpression('Inner Right is 4',TheRight,pekNumber,'4');
  341. end;
  342. procedure TTestExpressions.TestBracketsLeft;
  343. begin
  344. DeclareVar('integer','a');
  345. ParseExpression('2*(3+4)');
  346. AssertRightPrecedence(2,eopMultiply,3,eopAdd,4);
  347. end;
  348. procedure TTestExpressions.TestBracketsRight;
  349. begin
  350. DeclareVar('integer','a');
  351. ParseExpression('(2*3)+4');
  352. AssertLeftPrecedence(2,eopMultiply,3,eopAdd,4);
  353. end;
  354. procedure TTestExpressions.TestPrecedenceLeftToRight;
  355. begin
  356. ParseExpression('1+2+3');
  357. AssertLeftPrecedence(1,eopAdd,2,eopAdd,3);
  358. end;
  359. procedure TTestExpressions.TestPrecedenceLeftToRightMinus;
  360. begin
  361. ParseExpression('1-2-3');
  362. AssertLeftPrecedence(1,eopSubtract,2,eopSubtract,3);
  363. end;
  364. procedure TTestExpressions.TestPrecedenceLeftToRightMultiply;
  365. begin
  366. ParseExpression('1*2*3');
  367. AssertLeftPrecedence(1,eopMultiply,2,eopMultiply,3);
  368. end;
  369. procedure TTestExpressions.TestPrecedenceLeftToRightDivision;
  370. begin
  371. ParseExpression('1/2/3');
  372. AssertLeftPrecedence(1,eopDivide,2,eopDivide,3);
  373. end;
  374. procedure TTestExpressions.TestPrecedenceLeftToRightPlusMinus;
  375. begin
  376. ParseExpression('1+2-3');
  377. AssertLeftPrecedence(1,eopAdd,2,eopSubtract,3);
  378. end;
  379. procedure TTestExpressions.TestPrecedenceLeftToRightMinusPlus;
  380. begin
  381. ParseExpression('1-2+3');
  382. AssertLeftPrecedence(1,eopSubtract,2,eopAdd,3);
  383. end;
  384. procedure TTestExpressions.TestPrecedenceLeftToRightMultiplyDivision;
  385. begin
  386. ParseExpression('1*2/3');
  387. AssertLeftPrecedence(1,eopMultiply,2,eopDivide,3);
  388. end;
  389. procedure TTestExpressions.TestPrecedenceLeftToRightDivisionMultiply;
  390. begin
  391. ParseExpression('1/2*3');
  392. AssertLeftPrecedence(1,eopDivide,2,eopMultiply,3);
  393. end;
  394. procedure TTestExpressions.TestPrecedencePlusMultiply;
  395. begin
  396. ParseExpression('1+2*3');
  397. AssertRightPrecedence(1,eopAdd,2,eopMultiply,3);
  398. end;
  399. procedure TTestExpressions.TestPrecedencePlusDivide;
  400. begin
  401. ParseExpression('1+2/3');
  402. AssertRightPrecedence(1,eopAdd,2,eopDivide,3);
  403. end;
  404. procedure TTestExpressions.TestPrecedenceMinusMultiply;
  405. begin
  406. ParseExpression('1-2*3');
  407. AssertRightPrecedence(1,eopsubtract,2,eopMultiply,3);
  408. end;
  409. procedure TTestExpressions.TestPrecedenceMinusDivide;
  410. begin
  411. ParseExpression('1-2/3');
  412. AssertRightPrecedence(1,eopsubtract,2,eopDivide,3);
  413. end;
  414. procedure TTestExpressions.TestPrecedencePlusOr;
  415. begin
  416. ParseExpression('1 or 2 + 3');
  417. AssertLeftPrecedence(1,eopor,2,eopAdd,3);
  418. end;
  419. procedure TTestExpressions.TestPrecedenceAndOr;
  420. begin
  421. ParseExpression('1 or 2 and 3');
  422. AssertRightPrecedence(1,eopor,2,eopAnd,3);
  423. end;
  424. procedure TTestExpressions.TestPrecedenceAndNot;
  425. begin
  426. ParseExpression('Not 1 and 3');
  427. AssertBinaryExpr('Simple binary and',eopAnd,FLeft,FRight);
  428. AssertExpression('Outer right is 3',TheRight,pekNumber,'3');
  429. AssertUnaryExpr('Left is Unary not ',TheLeft,eopNot,FRight);
  430. AssertExpression('Inner Right is 1',TheRight,pekNumber,'1');
  431. end;
  432. procedure TTestExpressions.TestPrecedencePlusAnd;
  433. begin
  434. ParseExpression('1 + 2 and 3');
  435. AssertRightPrecedence(1,eopAdd,2,eopAnd,3);
  436. end;
  437. procedure TTestExpressions.TestPrecedenceMinusOr;
  438. begin
  439. ParseExpression('1 or 2 - 3');
  440. AssertLeftPrecedence(1,eopOr,2,eopSubtract,3);
  441. end;
  442. procedure TTestExpressions.TestPrecedenceMinusAnd;
  443. begin
  444. ParseExpression('1 - 2 and 3');
  445. AssertRightPrecedence(1,eopSubtract,2,eopand,3);
  446. end;
  447. procedure TTestExpressions.TestPrecedenceMultiplyOr;
  448. begin
  449. ParseExpression('1 or 2 * 3');
  450. AssertRightPrecedence(1,eopOr,2,eopMultiply,3);
  451. end;
  452. procedure TTestExpressions.TestPrecedenceMultiplyAnd;
  453. begin
  454. ParseExpression('1 * 2 and 3');
  455. AssertLeftPrecedence(1,eopMultiply,2,eopAnd,3);
  456. end;
  457. procedure TTestExpressions.TestPrecedencePlusDiv;
  458. begin
  459. ParseExpression('1+2 div 3');
  460. AssertRightPrecedence(1,eopAdd,2,eopDiv,3);
  461. end;
  462. procedure TTestExpressions.TestPrecedencePlusMod;
  463. begin
  464. ParseExpression('1+2 mod 3');
  465. AssertRightPrecedence(1,eopAdd,2,eopMod,3);
  466. end;
  467. procedure TTestExpressions.AssertLeftPrecedence(AInnerLeft : Integer; AInnerOp : TExprOpCode; AInnerRight : Integer; AOuterOp : TexprOpCode; AOuterRight: Integer);
  468. begin
  469. AssertBinaryExpr('Outer expression',AOuterOp,FLeft,FRight);
  470. AssertExpression('Outer right constant',TheRight,pekNumber,intToStr(AOuterRight));
  471. AssertBinaryExpr('Inner (left) expression',TheLeft,AInnerOp,FLeft,FRight);
  472. AssertExpression('Inner Left constant',TheLeft,pekNumber,IntToStr(AInnerLeft));
  473. AssertExpression('Inner Right constant',TheRight,pekNumber,IntToStr(AInnerRight));
  474. end;
  475. procedure TTestExpressions.AssertRightPrecedence(AOuterLeft : Integer; AOuterOp : TExprOpCode; AInnerLeft : Integer; AInnerOp : TexprOpCode; AInnerRight: Integer);
  476. begin
  477. AssertBinaryExpr('Outer expression',AOuterOp,FLeft,FRight);
  478. AssertExpression('Outer left constant',TheLeft,pekNumber,intToStr(AOuterLeft));
  479. AssertBinaryExpr('Inner (right) expression',TheRight,AInnerOp,FLeft,FRight);
  480. AssertExpression('Inner Left constant',TheLeft,pekNumber,IntToStr(AInnerLeft));
  481. AssertExpression('Inner Right constant',TheRight,pekNumber,IntToStr(AInnerRight));
  482. end;
  483. procedure TTestExpressions.TestPrecedenceMultiplyDiv;
  484. begin
  485. ParseExpression('1 * 2 div 3');
  486. AssertLeftPrecedence(1,eopMultiply,2,eopDiv,3);
  487. end;
  488. procedure TTestExpressions.TestPrecedenceDivMultiply;
  489. begin
  490. ParseExpression('1 div 2 * 3');
  491. AssertLeftPrecedence(1,eopDiv,2,eopMultiply,3);
  492. end;
  493. procedure TTestExpressions.TestTypeCast;
  494. begin
  495. DeclareVar('TSDOBaseDataObjectClass');
  496. ParseExpression('TSDOBaseDataObjectClass(Self.ClassType).Create');
  497. end;
  498. procedure TTestExpressions.TestTypeCast2;
  499. begin
  500. DeclareVar('TSDOBaseDataObjectClass');
  501. ParseExpression('TSDOBaseDataObjectClass(Self.ClassType).Create.D');
  502. end;
  503. procedure TTestExpressions.TestCreate;
  504. begin
  505. DeclareVar('ESDOSerializationException');
  506. ParseExpression('ESDOSerializationException.CreateFmt(SERR_InvalidDataTypeInContext,[IntToStr(Ord(AOwner^.DataType))])');
  507. end;
  508. procedure TTestExpressions.TestChainedPointers;
  509. begin
  510. // From bug report 31719
  511. Source.Add('type');
  512. Source.Add(' PTResourceManager=^TResourceManager;');
  513. Source.Add(' TResourceManager=object');
  514. Source.Add(' function LoadResourceFromFile(filename:string):PTResourceManager;');
  515. Source.Add(' end;');
  516. Source.Add(' function TResourceManager.LoadResourceFromFile(filename:string):PTResourceManager;');
  517. Source.Add(' begin');
  518. Source.Add(' result:=@self;');
  519. Source.Add(' end;');
  520. Source.Add('');
  521. Source.Add(' var');
  522. Source.Add(' ResourceManager:TResourceManager;');
  523. Source.Add('');
  524. Source.Add(' begin');
  525. Source.Add(' ResourceManager.LoadResourceFromFile(''file1'')');
  526. Source.Add(' ^.LoadResourceFromFile(''file2'');');
  527. Source.Add(' end.');
  528. ParseModule;
  529. end;
  530. procedure TTestExpressions.TestNilCaret;
  531. begin
  532. Source.Add('{$mode objfpc}');
  533. Source.Add('begin');
  534. Source.Add('FillChar(nil^,10,10);');
  535. Source.Add('end.');
  536. ParseModule;
  537. end;
  538. procedure TTestExpressions.TestExpCaret;
  539. begin
  540. Source.Add('{$mode objfpc}');
  541. Source.Add('begin');
  542. Source.Add('A:=B^;');
  543. Source.Add('end.');
  544. ParseModule;
  545. end;
  546. procedure TTestExpressions.TestUnaryMinus;
  547. begin
  548. DeclareVar('integer','a');
  549. DeclareVar('integer','b');
  550. ParseExpression('-b');
  551. AssertUnaryExpr('Simple minus unary',eopSubtract,FLeft);
  552. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  553. end;
  554. procedure TTestExpressions.TestUnaryMinusWhiteSpace;
  555. begin
  556. DeclareVar('integer','a');
  557. DeclareVar('integer','b');
  558. ParseExpression('- b');
  559. AssertUnaryExpr('Simple minus unary',eopSubtract,FLeft);
  560. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  561. end;
  562. procedure TTestExpressions.TestUnaryAddress;
  563. begin
  564. DeclareVar('integer','a');
  565. DeclareVar('integer','b');
  566. ParseExpression('@b');
  567. AssertUnaryExpr('Simple address unary',eopAddress,FLeft);
  568. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  569. end;
  570. procedure TTestExpressions.TestUnaryNot;
  571. begin
  572. DeclareVar('boolean','a');
  573. DeclareVar('boolean','b');
  574. ParseExpression('not b');
  575. AssertUnaryExpr('Simple address unary',eopNot,FLeft);
  576. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  577. end;
  578. procedure TTestExpressions.TestUnaryDeref;
  579. begin
  580. DeclareVar('integer','a');
  581. DeclareVar('pinteger','b');
  582. ParseExpression('b^');
  583. AssertUnaryExpr('Simple deref unary',eopDeref,FLeft);
  584. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  585. end;
  586. procedure TTestExpressions.TestUnaryDoubleDeref;
  587. begin
  588. DeclareVar('integer','a');
  589. DeclareVar('ppinteger','b');
  590. ParseExpression('(b)^^');
  591. AssertExpression('Deref expression 1',TheExpr,pekUnary,TUnaryExpr);
  592. AssertExpression('Deref expression 2',TUnaryExpr(TheExpr).Operand,pekUnary,TUnaryExpr);
  593. AssertExpression('Deref expression 3',TUnaryExpr(TUnaryExpr(TheExpr).Operand).Operand,pekIdent,'b');
  594. end;
  595. procedure TTestExpressions.TestUnaryDoubleDeref2;
  596. begin
  597. DeclareVar('integer','a');
  598. DeclareVar('ppinteger','b');
  599. ParseExpression('b^^');
  600. AssertExpression('Deref expression 1',TheExpr,pekUnary,TUnaryExpr);
  601. AssertExpression('Deref expression 2',TUnaryExpr(TheExpr).Operand,pekUnary,TUnaryExpr);
  602. AssertExpression('Deref expression 3',TUnaryExpr(TUnaryExpr(TheExpr).Operand).Operand,pekIdent,'b');
  603. end;
  604. procedure TTestExpressions.TestBinaryAdd;
  605. begin
  606. ParseExpression('1+2');
  607. AssertBinaryExpr('Simple binary add',eopAdd,FLeft,FRight);
  608. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  609. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  610. end;
  611. procedure TTestExpressions.TestBinarySubtract;
  612. begin
  613. ParseExpression('1-2');
  614. AssertBinaryExpr('Simple binary subtract',eopSubtract,FLeft,FRight);
  615. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  616. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  617. end;
  618. procedure TTestExpressions.TestBinaryMultiply;
  619. begin
  620. ParseExpression('1*2');
  621. AssertBinaryExpr('Simple binary multiply',eopMultiply,FLeft,FRight);
  622. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  623. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  624. end;
  625. procedure TTestExpressions.TestBinaryDivision;
  626. begin
  627. DeclareVar('double');
  628. ParseExpression('1/2');
  629. AssertBinaryExpr('Simple binary division',eopDivide,FLeft,FRight);
  630. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  631. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  632. end;
  633. procedure TTestExpressions.TestBinaryPower;
  634. begin
  635. DeclareVar('double');
  636. ParseExpression('1**2');
  637. AssertBinaryExpr('Simple binary power',eopPower,FLeft,FRight);
  638. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  639. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  640. end;
  641. procedure TTestExpressions.TestBinaryMod;
  642. begin
  643. ParseExpression('1 mod 2');
  644. AssertBinaryExpr('Simple binary mod',eopMod,FLeft,FRight);
  645. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  646. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  647. end;
  648. procedure TTestExpressions.TestBinaryDiv;
  649. begin
  650. ParseExpression('1 div 2');
  651. AssertBinaryExpr('Simple binary div',eopDiv,FLeft,FRight);
  652. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  653. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  654. end;
  655. procedure TTestExpressions.TestBinaryShl;
  656. begin
  657. ParseExpression('1 shl 2');
  658. AssertBinaryExpr('Simple binary shl',eopShl,FLeft,FRight);
  659. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  660. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  661. end;
  662. procedure TTestExpressions.TestBinaryShr;
  663. begin
  664. ParseExpression('1 shr 2');
  665. AssertBinaryExpr('Simple binary shr',eopShr,FLeft,FRight);
  666. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  667. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  668. end;
  669. procedure TTestExpressions.TestBinarySymmetricalDifference;
  670. begin
  671. DeclareVar('Set of Byte','a');
  672. DeclareVar('Set of Byte','b');
  673. DeclareVar('Set of Byte','c');
  674. ParseExpression('b >< c');
  675. AssertBinaryExpr('Simple binary smmetrical difference',eopSymmetricalDifference,FLeft,FRight);
  676. AssertExpression('Left is b',TheLeft,pekident,'b');
  677. AssertExpression('Right is c',TheRight,pekIdent,'c');
  678. end;
  679. procedure TTestExpressions.TestBinaryAnd;
  680. begin
  681. DeclareVar('boolean','a');
  682. DeclareVar('boolean','b');
  683. DeclareVar('boolean','b');
  684. ParseExpression('b and c');
  685. AssertBinaryExpr('Simple binary and',eopAnd,FLeft,FRight);
  686. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  687. AssertExpression('Right is c',TheRight,pekIdent,'c');
  688. end;
  689. procedure TTestExpressions.TestBinaryOr;
  690. begin
  691. DeclareVar('boolean','a');
  692. DeclareVar('boolean','b');
  693. DeclareVar('boolean','b');
  694. ParseExpression('b or c');
  695. AssertBinaryExpr('Simple binary or',eopOr,FLeft,FRight);
  696. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  697. AssertExpression('Right is c',TheRight,pekIdent,'c');
  698. end;
  699. procedure TTestExpressions.TestBinaryXOr;
  700. begin
  701. DeclareVar('boolean','a');
  702. DeclareVar('boolean','b');
  703. DeclareVar('boolean','b');
  704. ParseExpression('b xor c');
  705. AssertBinaryExpr('Simple binary xor',eopxOr,FLeft,FRight);
  706. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  707. AssertExpression('Right is c',TheRight,pekIdent,'c');
  708. end;
  709. procedure TTestExpressions.TestBinaryIn;
  710. begin
  711. DeclareVar('boolean','a');
  712. ParseExpression('1 in [1,2,3]');
  713. AssertBinaryExpr('Simple binary In',eopIn,FLeft,FRight);
  714. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  715. AssertExpression('Right is array set',TheRight,pekSet,TParamsExpr);
  716. end;
  717. procedure TTestExpressions.TestBinaryIs;
  718. begin
  719. DeclareVar('boolean','a');
  720. DeclareVar('TObject','b');
  721. ParseExpression('b is TObject');
  722. AssertBinaryExpr('Simple binary Is',eopIs,FLeft,FRight);
  723. AssertExpression('Left is 1',TheLeft,pekident,'b');
  724. AssertExpression('Right is TObject',TheRight,pekIdent,'TObject');
  725. end;
  726. procedure TTestExpressions.TestBinaryAs;
  727. begin
  728. DeclareVar('TObject','a');
  729. DeclareVar('TObject','b');
  730. ParseExpression('b as TObject');
  731. AssertBinaryExpr('Simple binary As',eopAs,FLeft,FRight);
  732. AssertExpression('Left is 1',TheLeft,pekident,'b');
  733. AssertExpression('Right is TObject',TheRight,pekIdent,'TObject');
  734. end;
  735. procedure TTestExpressions.TestBinaryEquals;
  736. begin
  737. DeclareVar('boolean','a');
  738. DeclareVar('integer','b');
  739. DeclareVar('integer','c');
  740. ParseExpression('b=c');
  741. AssertBinaryExpr('Simple binary equals',eopEqual,FLeft,FRight);
  742. AssertExpression('Left is b',TheLeft,pekident,'b');
  743. AssertExpression('Right is c',TheRight,pekIdent,'c');
  744. end;
  745. procedure TTestExpressions.TestBinaryDiffers;
  746. begin
  747. DeclareVar('boolean','a');
  748. DeclareVar('integer','b');
  749. DeclareVar('integer','c');
  750. ParseExpression('b<>c');
  751. AssertBinaryExpr('Simple binary differs',eopNotEqual,FLeft,FRight);
  752. AssertExpression('Left is b',TheLeft,pekident,'b');
  753. AssertExpression('Right is c',TheRight,pekIdent,'c');
  754. end;
  755. procedure TTestExpressions.TestBinaryLessThan;
  756. begin
  757. DeclareVar('boolean','a');
  758. DeclareVar('integer','b');
  759. DeclareVar('integer','c');
  760. ParseExpression('b<c');
  761. AssertBinaryExpr('Simple binary less than',eopLessThan,FLeft,FRight);
  762. AssertExpression('Left is b',TheLeft,pekident,'b');
  763. AssertExpression('Right is c',TheRight,pekIdent,'c');
  764. end;
  765. procedure TTestExpressions.TestBinaryLessThanEqual;
  766. begin
  767. DeclareVar('boolean','a');
  768. DeclareVar('integer','b');
  769. DeclareVar('integer','c');
  770. ParseExpression('b<=c');
  771. AssertBinaryExpr('Simple binary less than or equal',eopLessThanEqual,FLeft,FRight);
  772. AssertExpression('Left is b',TheLeft,pekident,'b');
  773. AssertExpression('Right is c',TheRight,pekIdent,'c');
  774. end;
  775. procedure TTestExpressions.TestBinaryLargerThan;
  776. begin
  777. DeclareVar('boolean','a');
  778. DeclareVar('integer','b');
  779. DeclareVar('integer','c');
  780. ParseExpression('b>c');
  781. AssertBinaryExpr('Simple binary larger than ',eopGreaterThan,FLeft,FRight);
  782. AssertExpression('Left is b',TheLeft,pekident,'b');
  783. AssertExpression('Right is c',TheRight,pekIdent,'c');
  784. end;
  785. procedure TTestExpressions.TestBinaryLargerThanEqual;
  786. begin
  787. DeclareVar('boolean','a');
  788. DeclareVar('integer','b');
  789. DeclareVar('integer','c');
  790. ParseExpression('b>=c');
  791. AssertBinaryExpr('Simple binary larger than or equal',eopGreaterThanEqual,FLeft,FRight);
  792. AssertExpression('Left is b',TheLeft,pekident,'b');
  793. AssertExpression('Right is c',TheRight,pekIdent,'c');
  794. end;
  795. procedure TTestExpressions.TestPrimitiveBooleanFalse;
  796. begin
  797. DeclareVar('boolean','a');
  798. ParseExpression('False');
  799. AssertExpression('Simple boolean',theExpr,pekBoolConst,TBoolConstExpr);
  800. AssertEquals('Boolean false',False,TBoolConstExpr(TheExpr).Value);
  801. end;
  802. procedure TTestExpressions.TestPrimitiveBooleanTrue;
  803. begin
  804. DeclareVar('boolean','a');
  805. ParseExpression('True');
  806. AssertExpression('Simple boolean',theExpr,pekBoolConst,TBoolConstExpr);
  807. AssertEquals('Boolean true',True,TBoolConstExpr(TheExpr).Value);
  808. end;
  809. procedure TTestExpressions.TestPrimitiveNil;
  810. begin
  811. DeclareVar('pointer','a');
  812. ParseExpression('Nil');
  813. AssertExpression('Nil expr',theExpr,pekNil,TNilExpr);
  814. end;
  815. procedure TTestExpressions.TestPrimitiveSet;
  816. Var
  817. P : TParamsExpr;
  818. begin
  819. DeclareVar('set of byte','a');
  820. ParseExpression('[1,2,3]');
  821. P:=TParamsExpr(AssertExpression('Set expr',theExpr,pekSet,TParamsExpr));
  822. AssertEquals('Element count',3,Length(P.Params));
  823. AssertExpression('Element 1 in set',P.Params[0],pekNumber,'1');
  824. AssertExpression('Element 2 in set',P.Params[1],pekNumber,'2');
  825. AssertExpression('Element 3 in set',P.Params[2],pekNumber,'3');
  826. end;
  827. procedure TTestExpressions.TestPrimitiveChar;
  828. begin
  829. DeclareVar('char');
  830. ParseExpression('#32');
  831. AssertExpression('Simple string',theExpr,pekString,'#32');
  832. end;
  833. procedure TTestExpressions.TestPrimitiveControlChar;
  834. begin
  835. DeclareVar('char');
  836. ParseExpression('^M');
  837. AssertExpression('Simple string',theExpr,pekString,'^M');
  838. end;
  839. procedure TTestExpressions.TestPrimitiveSetEmpty;
  840. Var
  841. P : TParamsExpr;
  842. begin
  843. DeclareVar('set of byte','a');
  844. ParseExpression('[]');
  845. P:=TParamsExpr(AssertExpression('Set expr',theExpr,pekSet,TParamsExpr));
  846. AssertEquals('Element count',0,Length(P.Params));
  847. end;
  848. procedure TTestExpressions.TestPrimitiveSelf;
  849. begin
  850. DeclareVar('pointer','a');
  851. ParseExpression('Self');
  852. AssertExpression('Inherited expr',theExpr,pekSelf,TSelfExpr);
  853. end;
  854. procedure TTestExpressions.TestInherited;
  855. begin
  856. DeclareVar('pointer','a');
  857. ParseExpression('inherited');
  858. AssertExpression('Inherited expr',theExpr,pekInherited,TInheritedExpr);
  859. end;
  860. procedure TTestExpressions.TestInheritedFunction;
  861. begin
  862. DeclareVar('pointer','a');
  863. ParseExpression('inherited myfunction');
  864. AssertBinaryExpr('Inherited expr',eopNone,Fleft,FRight);
  865. AssertExpression('Inherited expr',theleft,pekInherited,TInheritedExpr);
  866. AssertExpression('Inherited expr',theright,pekIdent,'myfunction');
  867. end;
  868. procedure TTestExpressions.SetUp;
  869. begin
  870. Inherited;
  871. FVariables:=TStringList.Create;
  872. end;
  873. procedure TTestExpressions.TearDown;
  874. begin
  875. FreeAndNil(FVariables);
  876. Inherited;
  877. end;
  878. procedure TTestExpressions.SetExpression(const AExpression: String);
  879. Var
  880. I : Integer;
  881. begin
  882. StartProgram(ExtractFileUnitName(MainFilename));
  883. if FVariables.Count=0 then
  884. DeclareVar('integer');
  885. Add('Var');
  886. For I:=0 to FVariables.Count-1 do
  887. Add(' '+Fvariables[I]);
  888. Add('begin');
  889. Add(' a:='+AExpression+';');
  890. end;
  891. procedure TTestExpressions.ParseExpression;
  892. begin
  893. ParseModule;
  894. AssertEquals('Have program',TPasProgram,Module.ClassType);
  895. AssertNotNull('Have program section',PasProgram.ProgramSection);
  896. AssertNotNull('Have initialization section',PasProgram.InitializationSection);
  897. AssertEquals('Have initialization statement',1,PasProgram.InitializationSection.Elements.Count);
  898. AssertNotNull('Have initialization statement',PasProgram.InitializationSection.Elements[0]);
  899. AssertEquals('Assignment statement',TPasImplAssign,TObject(PasProgram.InitializationSection.Elements[0]).ClassType);
  900. FTheExpr:=TPasImplAssign(PasProgram.InitializationSection.Elements[0]).right;
  901. AssertNotNull('Have assignment expression',FTheExpr);
  902. end;
  903. procedure TTestExpressions.ParseExpression(const AExpression: String);
  904. begin
  905. SetExpression(AExpression);
  906. ParseExpression;
  907. end;
  908. function TTestExpressions.AssertBinaryExpr(const Msg: String; Op: TExprOpCode;
  909. out ALeft, ARight: TPasExpr): TBinaryExpr;
  910. begin
  911. Result:=AssertBinaryExpr(Msg,TheExpr,Op,ALeft,ARight);
  912. end;
  913. function TTestExpressions.AssertBinaryExpr(const Msg: String; AExpr: TPasExpr;
  914. Op: TExprOpCode; out ALeft, ARight: TPasExpr): TBinaryExpr;
  915. begin
  916. AssertExpression(Msg+' is binary',AExpr,pekBinary,TBinaryExpr);
  917. Result:=AExpr as TBinaryExpr;
  918. AssertEquals(Msg+' opcode OK',Op,Result.OpCode);
  919. ALeft:=Result.Left;
  920. ARight:=Result.Right;
  921. AssertNotNull('Have left',ALeft);
  922. AssertNotNull('Have right',ARight);
  923. TAssert.AssertSame('Result.left.parent=B',Result,Result.left.Parent);
  924. TAssert.AssertSame('Result.right.parent=B',Result,Result.right.Parent);
  925. end;
  926. function TTestExpressions.AssertUnaryExpr(const Msg: String; Op: TExprOpCode;
  927. out AOperand : TPasExpr): TUnaryExpr;
  928. begin
  929. Result:=AssertUnaryExpr(Msg,TheExpr,OP,AOperand);
  930. end;
  931. function TTestExpressions.AssertUnaryExpr(const Msg: String; AExpr: TPasExpr;
  932. Op: TExprOpCode; out AOperand: TPasExpr): TUnaryExpr;
  933. begin
  934. AssertExpression(Msg+' is unary',AExpr,pekUnary,TUnaryExpr);
  935. Result:=AExpr as TUnaryExpr;
  936. AssertEquals(Msg+' opcode OK',Op,Result.OpCode);
  937. AOperand:=Result.Operand;
  938. AssertNotNull('Have left',AOperand);
  939. end;
  940. Procedure TTestExpressions.ParseStrWithFormatFullyQualified;
  941. Var
  942. P : TParamsExpr;
  943. B : TBinaryExpr;
  944. begin
  945. DeclareVar('string','a');
  946. DeclareVar('integer','i');
  947. ParseExpression('system.str(i:0:3,a)');
  948. B:=TBinaryExpr(AssertExpression('Binary identifier',theExpr,pekBinary,TBinaryExpr));
  949. P:=TParamsExpr(AssertExpression('Simple identifier',B.Right,pekFuncParams,TParamsExpr));
  950. AssertExpression('Name of function',P.Value,pekIdent,'str');
  951. AssertEquals('2 argument',2,Length(p.params));
  952. AssertExpression('Simple identifier',p.params[0],pekIdent,'i');
  953. AssertExpression('Simple identifier',p.params[1],pekIdent,'a');
  954. end;
  955. initialization
  956. RegisterTest(TTestExpressions);
  957. end.