tcexprparser.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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 TestBinarySubIdent;
  94. Procedure TestArrayElement;
  95. Procedure TestArrayElementRecord;
  96. Procedure TestArrayElement2Dims;
  97. Procedure TestFunctionCall;
  98. Procedure TestFunctionCall2args;
  99. Procedure TestFunctionCallNoArgs;
  100. Procedure TestSubIdentStrWithFormat;
  101. Procedure TestAPlusCallB;
  102. Procedure TestAPlusBBracketFuncParams;
  103. Procedure TestAPlusBBracketArrayParams;
  104. Procedure TestAPlusBBracketDotC;
  105. Procedure TestADotBDotC;
  106. Procedure TestADotBBracketC;
  107. Procedure TestSelfDotBBracketC;
  108. Procedure TestAasBDotCBracketFuncParams;
  109. Procedure TestRange;
  110. Procedure TestBracketsTotal;
  111. Procedure TestBracketsLeft;
  112. Procedure TestBracketsRight;
  113. Procedure TestPrecedenceLeftToRight;
  114. Procedure TestPrecedenceLeftToRightMinus;
  115. Procedure TestPrecedenceLeftToRightMultiply;
  116. Procedure TestPrecedenceLeftToRightDivision;
  117. Procedure TestPrecedenceLeftToRightPlusMinus;
  118. Procedure TestPrecedenceLeftToRightMinusPlus;
  119. Procedure TestPrecedenceLeftToRightMultiplyDivision;
  120. Procedure TestPrecedenceLeftToRightDivisionMultiply;
  121. Procedure TestPrecedencePlusMultiply;
  122. Procedure TestPrecedencePlusDivide;
  123. Procedure TestPrecedenceMinusMultiply;
  124. Procedure TestPrecedenceMinusDivide;
  125. Procedure TestPrecedencePlusOr;
  126. Procedure TestPrecedenceAndOr;
  127. Procedure TestPrecedenceAndNot;
  128. Procedure TestPrecedencePlusAnd;
  129. Procedure TestPrecedenceMinusOr;
  130. Procedure TestPrecedenceMinusAnd;
  131. Procedure TestPrecedenceMultiplyOr;
  132. Procedure TestPrecedenceMultiplyAnd;
  133. Procedure TestPrecedencePlusDiv;
  134. Procedure TestPrecedencePlusMod;
  135. Procedure TestPrecedenceMultiplyDiv;
  136. Procedure TestPrecedenceDivMultiply;
  137. Procedure TestTypeCast;
  138. procedure TestTypeCast2;
  139. Procedure TestCreate;
  140. procedure TestChainedPointers;
  141. procedure TestChainedPointers2;
  142. procedure TestChainedPointers3;
  143. Procedure TestNilCaret;
  144. Procedure TestExpCaret;
  145. Procedure TestArrayAccess;
  146. Procedure TestHelperOnLiteral;
  147. procedure TestParseAdhocExpression;
  148. end;
  149. implementation
  150. procedure TTestExpressions.DeclareVar(const AVarType: String;
  151. const AVarName: String = 'a');
  152. begin
  153. FVariables.Add(AVarName+' : '+AVarType+';');
  154. end;
  155. procedure TTestExpressions.TestPrimitiveInteger;
  156. begin
  157. ParseExpression('1');
  158. AssertExpression('Simple integer',theExpr,pekNumber,'1');
  159. end;
  160. procedure TTestExpressions.TestPrimitiveIntegerHex;
  161. begin
  162. ParseExpression('$FF');
  163. AssertExpression('Simple integer',theExpr,pekNumber,'$FF');
  164. end;
  165. procedure TTestExpressions.TestPrimitiveIntegerOctal;
  166. begin
  167. ParseExpression('&777');
  168. AssertExpression('Simple integer',theExpr,pekNumber,'&777');
  169. end;
  170. procedure TTestExpressions.TestPrimitiveIntegerBinary;
  171. begin
  172. ParseExpression('%10101010');
  173. AssertExpression('Simple integer',theExpr,pekNumber,'%10101010');
  174. end;
  175. procedure TTestExpressions.TestPrimitiveDouble;
  176. begin
  177. ParseExpression('1.2');
  178. AssertExpression('Simple double',theExpr,pekNumber,'1.2');
  179. end;
  180. procedure TTestExpressions.TestPrimitiveDouble2;
  181. begin
  182. ParseExpression('1.200');
  183. AssertExpression('Simple double',theExpr,pekNumber,'1.200');
  184. end;
  185. procedure TTestExpressions.TestPrimitiveDouble3;
  186. begin
  187. ParseExpression('01.2');
  188. AssertExpression('Simple double',theExpr,pekNumber,'01.2');
  189. end;
  190. procedure TTestExpressions.TestPrimitiveDouble4;
  191. begin
  192. ParseExpression('1.2e10');
  193. AssertExpression('Simple double',theExpr,pekNumber,'1.2e10');
  194. end;
  195. procedure TTestExpressions.TestPrimitiveDouble5;
  196. begin
  197. ParseExpression('1.2e-10');
  198. AssertExpression('Simple double',theExpr,pekNumber,'1.2e-10');
  199. end;
  200. procedure TTestExpressions.TestPrimitiveDouble6;
  201. begin
  202. ParseExpression('12e10');
  203. AssertExpression('Simple double',theExpr,pekNumber,'12e10');
  204. end;
  205. procedure TTestExpressions.TestPrimitiveDouble7;
  206. begin
  207. ParseExpression('12e-10');
  208. AssertExpression('Simple double',theExpr,pekNumber,'12e-10');
  209. end;
  210. procedure TTestExpressions.TestPrimitiveDouble8;
  211. begin
  212. ParseExpression('8.5');
  213. AssertExpression('Simple double',theExpr,pekNumber,'8.5');
  214. end;
  215. procedure TTestExpressions.TestPrimitiveDouble9;
  216. begin
  217. ParseExpression('8.E5');
  218. AssertExpression('Simple double',theExpr,pekNumber,'8.E5');
  219. end;
  220. procedure TTestExpressions.TestPrimitiveDouble10;
  221. begin
  222. ParseExpression('8.E-5');
  223. AssertExpression('Simple double',theExpr,pekNumber,'8.E-5');
  224. end;
  225. procedure TTestExpressions.TestPrimitiveDouble11;
  226. begin
  227. ParseExpression('8E+5');
  228. AssertExpression('Simple double',theExpr,pekNumber,'8E+5');
  229. end;
  230. procedure TTestExpressions.TestPrimitiveString;
  231. begin
  232. DeclareVar('string');
  233. ParseExpression('''123''');
  234. AssertExpression('Simple string',theExpr,pekString,'''123''');
  235. end;
  236. procedure TTestExpressions.TestPrimitiveIdent;
  237. begin
  238. DeclareVar('integer','a');
  239. DeclareVar('integer','b');
  240. ParseExpression('b');
  241. AssertExpression('Simple identifier',theExpr,pekIdent,'b');
  242. end;
  243. procedure TTestExpressions.TestBinarySubIdent;
  244. begin
  245. DeclareVar('integer','a');
  246. DeclareVar('record x,y : integer; end','b');
  247. ParseExpression('b.x');
  248. AssertBinaryExpr('sub identifier',eopSubIdent,Fleft,FRight);
  249. AssertExpression('Simple identifier',Theleft,pekIdent,'b');
  250. AssertExpression('Simple identifier',Theright,pekIdent,'x');
  251. end;
  252. procedure TTestExpressions.TestArrayElement;
  253. Var
  254. P : TParamsExpr;
  255. begin
  256. DeclareVar('integer','a');
  257. DeclareVar('array[1..2] of integer','b');
  258. ParseExpression('b[1]');
  259. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekArrayParams,TParamsExpr));
  260. AssertExpression('Name of array',P.Value,pekIdent,'b');
  261. AssertEquals('One dimension',1,Length(p.params));
  262. AssertExpression('Simple identifier',p.params[0],pekNumber,'1');
  263. end;
  264. procedure TTestExpressions.TestArrayElementRecord;
  265. Var
  266. P : TParamsExpr;
  267. B : TBinaryExpr;
  268. begin
  269. DeclareVar('record a : array[1..2] of integer; end ','b');
  270. ParseExpression('b.a[1]');
  271. P:=TParamsExpr(AssertExpression('Array Param',TheExpr,pekArrayParams,TParamsExpr));
  272. TAssert.AssertSame('P.value.parent=P',P,P.Value.Parent);
  273. AssertEquals('One dimension',1,Length(P.params));
  274. AssertExpression('Simple identifier',P.params[0],pekNumber,'1');
  275. B:=TBinaryExpr(AssertExpression('Binary of record',P.Value,pekBinary,TBinaryExpr));
  276. AssertEquals('Name is Subident',eopSubIdent,B.Opcode);
  277. AssertExpression('Name of array',B.Left,pekIdent,'b');
  278. AssertExpression('Name of array',B.right,pekIdent,'a');
  279. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  280. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  281. end;
  282. procedure TTestExpressions.TestArrayElement2Dims;
  283. Var
  284. P : TParamsExpr;
  285. begin
  286. DeclareVar('integer','a');
  287. DeclareVar('array[1..2,1..2] of integer','b');
  288. ParseExpression('b[1,2]');
  289. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekArrayParams,TParamsExpr));
  290. AssertExpression('Name of array',P.Value,pekIdent,'b');
  291. AssertEquals('Two dimensions',2,Length(p.params));
  292. AssertExpression('Simple identifier',p.params[0],pekNumber,'1');
  293. AssertExpression('Simple identifier',p.params[1],pekNumber,'2');
  294. end;
  295. procedure TTestExpressions.TestFunctionCall;
  296. Var
  297. P : TParamsExpr;
  298. begin
  299. DeclareVar('integer','a');
  300. ParseExpression('Random(10)');
  301. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekFuncParams,TParamsExpr));
  302. AssertExpression('Name of function',P.Value,pekIdent,'Random');
  303. AssertEquals('1 argument',1,Length(p.params));
  304. AssertExpression('Simple identifier',p.params[0],pekNumber,'10');
  305. end;
  306. procedure TTestExpressions.TestFunctionCall2args;
  307. Var
  308. P : TParamsExpr;
  309. begin
  310. DeclareVar('integer','a');
  311. ParseExpression('Random(10,12)');
  312. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekFuncParams,TParamsExpr));
  313. AssertExpression('Name of function',P.Value,pekIdent,'Random');
  314. AssertEquals('2 argument',2,Length(p.params));
  315. AssertExpression('Simple identifier 1',p.params[0],pekNumber,'10');
  316. AssertExpression('Simple identifier 2',p.params[1],pekNumber,'12');
  317. end;
  318. procedure TTestExpressions.TestFunctionCallNoArgs;
  319. Var
  320. P : TParamsExpr;
  321. begin
  322. DeclareVar('integer','a');
  323. ParseExpression('Random()');
  324. P:=TParamsExpr(AssertExpression('Simple identifier',theExpr,pekFuncParams,TParamsExpr));
  325. AssertExpression('Name of function',P.Value,pekIdent,'Random');
  326. AssertEquals('0 arguments',0,Length(p.params));
  327. end;
  328. procedure TTestExpressions.TestRange;
  329. Var
  330. P : TParamsExpr;
  331. B : TBinaryExpr;
  332. begin
  333. DeclareVar('boolean','a');
  334. DeclareVar('byte','b');
  335. ParseExpression('b in [0..10]');
  336. AssertBinaryExpr('Simple binary In',eopIn,FLeft,FRight);
  337. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  338. P:=TParamsExpr(AssertExpression('Right is set',TheRight,pekSet,TParamsExpr));
  339. AssertEquals('Number of items',1,Length(P.Params));
  340. B:=TBinaryExpr(AssertExpression('First element is range',P.Params[0],pekRange,TBinaryExpr));
  341. AssertExpression('Left is 0',B.Left,pekNumber,'0');
  342. AssertExpression('Right is 10',B.Right,pekNumber,'10');
  343. B:=TBinaryExpr(TheExpr);
  344. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  345. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  346. end;
  347. procedure TTestExpressions.TestBracketsTotal;
  348. begin
  349. DeclareVar('integer','a');
  350. ParseExpression('(3+4)');
  351. AssertBinaryExpr('simple binary add',eopAdd,FLeft,FRight);
  352. AssertExpression('Inner Left is 3',TheLeft,pekNumber,'3');
  353. AssertExpression('Inner Right is 4',TheRight,pekNumber,'4');
  354. end;
  355. procedure TTestExpressions.TestBracketsLeft;
  356. begin
  357. DeclareVar('integer','a');
  358. ParseExpression('2*(3+4)');
  359. AssertRightPrecedence(2,eopMultiply,3,eopAdd,4);
  360. end;
  361. procedure TTestExpressions.TestBracketsRight;
  362. begin
  363. DeclareVar('integer','a');
  364. ParseExpression('(2*3)+4');
  365. AssertLeftPrecedence(2,eopMultiply,3,eopAdd,4);
  366. end;
  367. procedure TTestExpressions.TestPrecedenceLeftToRight;
  368. begin
  369. ParseExpression('1+2+3');
  370. AssertLeftPrecedence(1,eopAdd,2,eopAdd,3);
  371. end;
  372. procedure TTestExpressions.TestPrecedenceLeftToRightMinus;
  373. begin
  374. ParseExpression('1-2-3');
  375. AssertLeftPrecedence(1,eopSubtract,2,eopSubtract,3);
  376. end;
  377. procedure TTestExpressions.TestPrecedenceLeftToRightMultiply;
  378. begin
  379. ParseExpression('1*2*3');
  380. AssertLeftPrecedence(1,eopMultiply,2,eopMultiply,3);
  381. end;
  382. procedure TTestExpressions.TestPrecedenceLeftToRightDivision;
  383. begin
  384. ParseExpression('1/2/3');
  385. AssertLeftPrecedence(1,eopDivide,2,eopDivide,3);
  386. end;
  387. procedure TTestExpressions.TestPrecedenceLeftToRightPlusMinus;
  388. begin
  389. ParseExpression('1+2-3');
  390. AssertLeftPrecedence(1,eopAdd,2,eopSubtract,3);
  391. end;
  392. procedure TTestExpressions.TestPrecedenceLeftToRightMinusPlus;
  393. begin
  394. ParseExpression('1-2+3');
  395. AssertLeftPrecedence(1,eopSubtract,2,eopAdd,3);
  396. end;
  397. procedure TTestExpressions.TestPrecedenceLeftToRightMultiplyDivision;
  398. begin
  399. ParseExpression('1*2/3');
  400. AssertLeftPrecedence(1,eopMultiply,2,eopDivide,3);
  401. end;
  402. procedure TTestExpressions.TestPrecedenceLeftToRightDivisionMultiply;
  403. begin
  404. ParseExpression('1/2*3');
  405. AssertLeftPrecedence(1,eopDivide,2,eopMultiply,3);
  406. end;
  407. procedure TTestExpressions.TestPrecedencePlusMultiply;
  408. begin
  409. ParseExpression('1+2*3');
  410. AssertRightPrecedence(1,eopAdd,2,eopMultiply,3);
  411. end;
  412. procedure TTestExpressions.TestPrecedencePlusDivide;
  413. begin
  414. ParseExpression('1+2/3');
  415. AssertRightPrecedence(1,eopAdd,2,eopDivide,3);
  416. end;
  417. procedure TTestExpressions.TestPrecedenceMinusMultiply;
  418. begin
  419. ParseExpression('1-2*3');
  420. AssertRightPrecedence(1,eopsubtract,2,eopMultiply,3);
  421. end;
  422. procedure TTestExpressions.TestPrecedenceMinusDivide;
  423. begin
  424. ParseExpression('1-2/3');
  425. AssertRightPrecedence(1,eopsubtract,2,eopDivide,3);
  426. end;
  427. procedure TTestExpressions.TestPrecedencePlusOr;
  428. begin
  429. ParseExpression('1 or 2 + 3');
  430. AssertLeftPrecedence(1,eopor,2,eopAdd,3);
  431. end;
  432. procedure TTestExpressions.TestPrecedenceAndOr;
  433. begin
  434. ParseExpression('1 or 2 and 3');
  435. AssertRightPrecedence(1,eopor,2,eopAnd,3);
  436. end;
  437. procedure TTestExpressions.TestPrecedenceAndNot;
  438. begin
  439. ParseExpression('Not 1 and 3');
  440. AssertBinaryExpr('Simple binary and',eopAnd,FLeft,FRight);
  441. AssertExpression('Outer right is 3',TheRight,pekNumber,'3');
  442. AssertUnaryExpr('Left is Unary not ',TheLeft,eopNot,FRight);
  443. AssertExpression('Inner Right is 1',TheRight,pekNumber,'1');
  444. end;
  445. procedure TTestExpressions.TestPrecedencePlusAnd;
  446. begin
  447. ParseExpression('1 + 2 and 3');
  448. AssertRightPrecedence(1,eopAdd,2,eopAnd,3);
  449. end;
  450. procedure TTestExpressions.TestPrecedenceMinusOr;
  451. begin
  452. ParseExpression('1 or 2 - 3');
  453. AssertLeftPrecedence(1,eopOr,2,eopSubtract,3);
  454. end;
  455. procedure TTestExpressions.TestPrecedenceMinusAnd;
  456. begin
  457. ParseExpression('1 - 2 and 3');
  458. AssertRightPrecedence(1,eopSubtract,2,eopand,3);
  459. end;
  460. procedure TTestExpressions.TestPrecedenceMultiplyOr;
  461. begin
  462. ParseExpression('1 or 2 * 3');
  463. AssertRightPrecedence(1,eopOr,2,eopMultiply,3);
  464. end;
  465. procedure TTestExpressions.TestPrecedenceMultiplyAnd;
  466. begin
  467. ParseExpression('1 * 2 and 3');
  468. AssertLeftPrecedence(1,eopMultiply,2,eopAnd,3);
  469. end;
  470. procedure TTestExpressions.TestPrecedencePlusDiv;
  471. begin
  472. ParseExpression('1+2 div 3');
  473. AssertRightPrecedence(1,eopAdd,2,eopDiv,3);
  474. end;
  475. procedure TTestExpressions.TestPrecedencePlusMod;
  476. begin
  477. ParseExpression('1+2 mod 3');
  478. AssertRightPrecedence(1,eopAdd,2,eopMod,3);
  479. end;
  480. procedure TTestExpressions.AssertLeftPrecedence(AInnerLeft : Integer; AInnerOp : TExprOpCode; AInnerRight : Integer; AOuterOp : TexprOpCode; AOuterRight: Integer);
  481. begin
  482. AssertBinaryExpr('Outer expression',AOuterOp,FLeft,FRight);
  483. AssertExpression('Outer right constant',TheRight,pekNumber,intToStr(AOuterRight));
  484. AssertBinaryExpr('Inner (left) expression',TheLeft,AInnerOp,FLeft,FRight);
  485. AssertExpression('Inner Left constant',TheLeft,pekNumber,IntToStr(AInnerLeft));
  486. AssertExpression('Inner Right constant',TheRight,pekNumber,IntToStr(AInnerRight));
  487. end;
  488. procedure TTestExpressions.AssertRightPrecedence(AOuterLeft : Integer; AOuterOp : TExprOpCode; AInnerLeft : Integer; AInnerOp : TexprOpCode; AInnerRight: Integer);
  489. begin
  490. AssertBinaryExpr('Outer expression',AOuterOp,FLeft,FRight);
  491. AssertExpression('Outer left constant',TheLeft,pekNumber,intToStr(AOuterLeft));
  492. AssertBinaryExpr('Inner (right) expression',TheRight,AInnerOp,FLeft,FRight);
  493. AssertExpression('Inner Left constant',TheLeft,pekNumber,IntToStr(AInnerLeft));
  494. AssertExpression('Inner Right constant',TheRight,pekNumber,IntToStr(AInnerRight));
  495. end;
  496. procedure TTestExpressions.TestPrecedenceMultiplyDiv;
  497. begin
  498. ParseExpression('1 * 2 div 3');
  499. AssertLeftPrecedence(1,eopMultiply,2,eopDiv,3);
  500. end;
  501. procedure TTestExpressions.TestPrecedenceDivMultiply;
  502. begin
  503. ParseExpression('1 div 2 * 3');
  504. AssertLeftPrecedence(1,eopDiv,2,eopMultiply,3);
  505. end;
  506. procedure TTestExpressions.TestTypeCast;
  507. begin
  508. DeclareVar('TSDOBaseDataObjectClass');
  509. ParseExpression('TSDOBaseDataObjectClass(Self.ClassType).Create');
  510. end;
  511. procedure TTestExpressions.TestTypeCast2;
  512. begin
  513. DeclareVar('TSDOBaseDataObjectClass');
  514. ParseExpression('TSDOBaseDataObjectClass(Self.ClassType).Create.D');
  515. end;
  516. procedure TTestExpressions.TestCreate;
  517. begin
  518. DeclareVar('ESDOSerializationException');
  519. ParseExpression('ESDOSerializationException.CreateFmt(SERR_InvalidDataTypeInContext,[IntToStr(Ord(AOwner^.DataType))])');
  520. end;
  521. procedure TTestExpressions.TestChainedPointers;
  522. begin
  523. // From bug report 31719
  524. Source.Add('type');
  525. Source.Add(' PTResourceManager=^TResourceManager;');
  526. Source.Add(' TResourceManager=object');
  527. Source.Add(' function LoadResourceFromFile(filename:string):PTResourceManager;');
  528. Source.Add(' end;');
  529. Source.Add(' function TResourceManager.LoadResourceFromFile(filename:string):PTResourceManager;');
  530. Source.Add(' begin');
  531. Source.Add(' result:=@self;');
  532. Source.Add(' end;');
  533. Source.Add('');
  534. Source.Add(' var');
  535. Source.Add(' ResourceManager:TResourceManager;');
  536. Source.Add('');
  537. Source.Add(' begin');
  538. Source.Add(' ResourceManager.LoadResourceFromFile(''file1'')');
  539. Source.Add(' ^.LoadResourceFromFile(''file2'');');
  540. Source.Add(' end.');
  541. ParseModule;
  542. end;
  543. procedure TTestExpressions.TestChainedPointers2;
  544. begin
  545. Source.Add('program afile;');
  546. Source.Add('procedure test;');
  547. Source.Add('begin');
  548. Source.Add('ResourcePool.Shared^.Register(TypeOf(tTexture), @LoadTexture)^.Tag(GLResourceTag)');
  549. Source.Add(' ^.Register(TypeOf(tShader), @LoadShader)^.Tag(GLResourceTag)//space - works');
  550. Source.Add('^.Register(TypeOf(ShaderProgram), @LoadShaderProgram)^.Tag(GLResourceTag);//without space - does not work');
  551. Source.Add('end;');
  552. Source.Add('begin');
  553. Source.Add('end.');
  554. ParseModule;
  555. end;
  556. procedure TTestExpressions.TestChainedPointers3;
  557. begin
  558. Source.Add('program afile;');
  559. Source.Add('procedure test;');
  560. Source.Add('begin');
  561. Source.Add('ResourcePool.Shared^.Register(TypeOf(tTexture), @LoadTexture)^.Tag(GLResourceTag)');
  562. Source.Add(' ^.Register(TypeOf(tShader), @LoadShader)^.Tag(GLResourceTag)//space - works');
  563. Source.Add(#9'^.Register(TypeOf(ShaderProgram), @LoadShaderProgram)^.Tag(GLResourceTag);// tab - does not work');
  564. Source.Add('end;');
  565. Source.Add('begin');
  566. Source.Add('end.');
  567. ParseModule;
  568. end;
  569. procedure TTestExpressions.TestNilCaret;
  570. begin
  571. Source.Add('{$mode objfpc}');
  572. Source.Add('begin');
  573. Source.Add('FillChar(nil^,10,10);');
  574. Source.Add('end.');
  575. ParseModule;
  576. end;
  577. procedure TTestExpressions.TestExpCaret;
  578. begin
  579. Source.Add('{$mode objfpc}');
  580. Source.Add('begin');
  581. Source.Add('A:=B^;');
  582. Source.Add('end.');
  583. ParseModule;
  584. end;
  585. procedure TTestExpressions.TestArrayAccess;
  586. begin
  587. Source.Add('begin');
  588. Source.Add('DoSomething((pb + 10)[4]);');
  589. Source.Add('end.');
  590. ParseModule;
  591. end;
  592. procedure TTestExpressions.TestHelperOnLiteral;
  593. begin
  594. Source.Add('begin');
  595. Source.Add('writeln(''10''.toint);');
  596. Source.Add('end.');
  597. ParseModule;
  598. end;
  599. procedure TTestExpressions.TestUnaryMinus;
  600. begin
  601. DeclareVar('integer','a');
  602. DeclareVar('integer','b');
  603. ParseExpression('-b');
  604. AssertUnaryExpr('Simple minus unary',eopSubtract,FLeft);
  605. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  606. end;
  607. procedure TTestExpressions.TestUnaryMinusWhiteSpace;
  608. begin
  609. DeclareVar('integer','a');
  610. DeclareVar('integer','b');
  611. ParseExpression('- b');
  612. AssertUnaryExpr('Simple minus unary',eopSubtract,FLeft);
  613. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  614. end;
  615. procedure TTestExpressions.TestUnaryAddress;
  616. begin
  617. DeclareVar('integer','a');
  618. DeclareVar('integer','b');
  619. ParseExpression('@b');
  620. AssertUnaryExpr('Simple address unary',eopAddress,FLeft);
  621. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  622. end;
  623. procedure TTestExpressions.TestUnaryNot;
  624. begin
  625. DeclareVar('boolean','a');
  626. DeclareVar('boolean','b');
  627. ParseExpression('not b');
  628. AssertUnaryExpr('Simple address unary',eopNot,FLeft);
  629. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  630. end;
  631. procedure TTestExpressions.TestUnaryDeref;
  632. begin
  633. DeclareVar('integer','a');
  634. DeclareVar('pinteger','b');
  635. ParseExpression('b^');
  636. AssertUnaryExpr('Simple deref unary',eopDeref,FLeft);
  637. AssertExpression('Simple identifier',theLeft,pekIdent,'b');
  638. end;
  639. procedure TTestExpressions.TestUnaryDoubleDeref;
  640. begin
  641. DeclareVar('integer','a');
  642. DeclareVar('ppinteger','b');
  643. ParseExpression('(b)^^');
  644. AssertExpression('Deref expression 1',TheExpr,pekUnary,TUnaryExpr);
  645. AssertExpression('Deref expression 2',TUnaryExpr(TheExpr).Operand,pekUnary,TUnaryExpr);
  646. AssertExpression('Deref expression 3',TUnaryExpr(TUnaryExpr(TheExpr).Operand).Operand,pekIdent,'b');
  647. end;
  648. procedure TTestExpressions.TestUnaryDoubleDeref2;
  649. begin
  650. DeclareVar('integer','a');
  651. DeclareVar('ppinteger','b');
  652. ParseExpression('b^^');
  653. AssertExpression('Deref expression 1',TheExpr,pekUnary,TUnaryExpr);
  654. AssertExpression('Deref expression 2',TUnaryExpr(TheExpr).Operand,pekUnary,TUnaryExpr);
  655. AssertExpression('Deref expression 3',TUnaryExpr(TUnaryExpr(TheExpr).Operand).Operand,pekIdent,'b');
  656. end;
  657. procedure TTestExpressions.TestBinaryAdd;
  658. begin
  659. ParseExpression('1+2');
  660. AssertBinaryExpr('Simple binary add',eopAdd,FLeft,FRight);
  661. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  662. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  663. end;
  664. procedure TTestExpressions.TestBinarySubtract;
  665. begin
  666. ParseExpression('1-2');
  667. AssertBinaryExpr('Simple binary subtract',eopSubtract,FLeft,FRight);
  668. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  669. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  670. end;
  671. procedure TTestExpressions.TestBinaryMultiply;
  672. begin
  673. ParseExpression('1*2');
  674. AssertBinaryExpr('Simple binary multiply',eopMultiply,FLeft,FRight);
  675. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  676. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  677. end;
  678. procedure TTestExpressions.TestBinaryDivision;
  679. begin
  680. DeclareVar('double');
  681. ParseExpression('1/2');
  682. AssertBinaryExpr('Simple binary division',eopDivide,FLeft,FRight);
  683. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  684. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  685. end;
  686. procedure TTestExpressions.TestBinaryPower;
  687. begin
  688. DeclareVar('double');
  689. ParseExpression('1**2');
  690. AssertBinaryExpr('Simple binary power',eopPower,FLeft,FRight);
  691. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  692. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  693. end;
  694. procedure TTestExpressions.TestBinaryMod;
  695. begin
  696. ParseExpression('1 mod 2');
  697. AssertBinaryExpr('Simple binary mod',eopMod,FLeft,FRight);
  698. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  699. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  700. end;
  701. procedure TTestExpressions.TestBinaryDiv;
  702. begin
  703. ParseExpression('1 div 2');
  704. AssertBinaryExpr('Simple binary div',eopDiv,FLeft,FRight);
  705. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  706. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  707. end;
  708. procedure TTestExpressions.TestBinaryShl;
  709. begin
  710. ParseExpression('1 shl 2');
  711. AssertBinaryExpr('Simple binary shl',eopShl,FLeft,FRight);
  712. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  713. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  714. end;
  715. procedure TTestExpressions.TestBinaryShr;
  716. begin
  717. ParseExpression('1 shr 2');
  718. AssertBinaryExpr('Simple binary shr',eopShr,FLeft,FRight);
  719. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  720. AssertExpression('Right is 2',TheRight,pekNumber,'2');
  721. end;
  722. procedure TTestExpressions.TestBinarySymmetricalDifference;
  723. begin
  724. DeclareVar('Set of Byte','a');
  725. DeclareVar('Set of Byte','b');
  726. DeclareVar('Set of Byte','c');
  727. ParseExpression('b >< c');
  728. AssertBinaryExpr('Simple binary smmetrical difference',eopSymmetricalDifference,FLeft,FRight);
  729. AssertExpression('Left is b',TheLeft,pekident,'b');
  730. AssertExpression('Right is c',TheRight,pekIdent,'c');
  731. end;
  732. procedure TTestExpressions.TestBinaryAnd;
  733. begin
  734. DeclareVar('boolean','a');
  735. DeclareVar('boolean','b');
  736. DeclareVar('boolean','b');
  737. ParseExpression('b and c');
  738. AssertBinaryExpr('Simple binary and',eopAnd,FLeft,FRight);
  739. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  740. AssertExpression('Right is c',TheRight,pekIdent,'c');
  741. end;
  742. procedure TTestExpressions.TestBinaryOr;
  743. begin
  744. DeclareVar('boolean','a');
  745. DeclareVar('boolean','b');
  746. DeclareVar('boolean','b');
  747. ParseExpression('b or c');
  748. AssertBinaryExpr('Simple binary or',eopOr,FLeft,FRight);
  749. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  750. AssertExpression('Right is c',TheRight,pekIdent,'c');
  751. end;
  752. procedure TTestExpressions.TestBinaryXOr;
  753. begin
  754. DeclareVar('boolean','a');
  755. DeclareVar('boolean','b');
  756. DeclareVar('boolean','b');
  757. ParseExpression('b xor c');
  758. AssertBinaryExpr('Simple binary xor',eopxOr,FLeft,FRight);
  759. AssertExpression('Left is b',TheLeft,pekIdent,'b');
  760. AssertExpression('Right is c',TheRight,pekIdent,'c');
  761. end;
  762. procedure TTestExpressions.TestBinaryIn;
  763. begin
  764. DeclareVar('boolean','a');
  765. ParseExpression('1 in [1,2,3]');
  766. AssertBinaryExpr('Simple binary In',eopIn,FLeft,FRight);
  767. AssertExpression('Left is 1',TheLeft,pekNumber,'1');
  768. AssertExpression('Right is array set',TheRight,pekSet,TParamsExpr);
  769. end;
  770. procedure TTestExpressions.TestBinaryIs;
  771. begin
  772. DeclareVar('boolean','a');
  773. DeclareVar('TObject','b');
  774. ParseExpression('b is TObject');
  775. AssertBinaryExpr('Simple binary Is',eopIs,FLeft,FRight);
  776. AssertExpression('Left is 1',TheLeft,pekident,'b');
  777. AssertExpression('Right is TObject',TheRight,pekIdent,'TObject');
  778. end;
  779. procedure TTestExpressions.TestBinaryAs;
  780. begin
  781. DeclareVar('TObject','a');
  782. DeclareVar('TObject','b');
  783. ParseExpression('b as TObject');
  784. AssertBinaryExpr('Simple binary As',eopAs,FLeft,FRight);
  785. AssertExpression('Left is 1',TheLeft,pekident,'b');
  786. AssertExpression('Right is TObject',TheRight,pekIdent,'TObject');
  787. end;
  788. procedure TTestExpressions.TestBinaryEquals;
  789. begin
  790. DeclareVar('boolean','a');
  791. DeclareVar('integer','b');
  792. DeclareVar('integer','c');
  793. ParseExpression('b=c');
  794. AssertBinaryExpr('Simple binary equals',eopEqual,FLeft,FRight);
  795. AssertExpression('Left is b',TheLeft,pekident,'b');
  796. AssertExpression('Right is c',TheRight,pekIdent,'c');
  797. end;
  798. procedure TTestExpressions.TestBinaryDiffers;
  799. begin
  800. DeclareVar('boolean','a');
  801. DeclareVar('integer','b');
  802. DeclareVar('integer','c');
  803. ParseExpression('b<>c');
  804. AssertBinaryExpr('Simple binary differs',eopNotEqual,FLeft,FRight);
  805. AssertExpression('Left is b',TheLeft,pekident,'b');
  806. AssertExpression('Right is c',TheRight,pekIdent,'c');
  807. end;
  808. procedure TTestExpressions.TestBinaryLessThan;
  809. begin
  810. DeclareVar('boolean','a');
  811. DeclareVar('integer','b');
  812. DeclareVar('integer','c');
  813. ParseExpression('b<c');
  814. AssertBinaryExpr('Simple binary less than',eopLessThan,FLeft,FRight);
  815. AssertExpression('Left is b',TheLeft,pekident,'b');
  816. AssertExpression('Right is c',TheRight,pekIdent,'c');
  817. end;
  818. procedure TTestExpressions.TestBinaryLessThanEqual;
  819. begin
  820. DeclareVar('boolean','a');
  821. DeclareVar('integer','b');
  822. DeclareVar('integer','c');
  823. ParseExpression('b<=c');
  824. AssertBinaryExpr('Simple binary less than or equal',eopLessThanEqual,FLeft,FRight);
  825. AssertExpression('Left is b',TheLeft,pekident,'b');
  826. AssertExpression('Right is c',TheRight,pekIdent,'c');
  827. end;
  828. procedure TTestExpressions.TestBinaryLargerThan;
  829. begin
  830. DeclareVar('boolean','a');
  831. DeclareVar('integer','b');
  832. DeclareVar('integer','c');
  833. ParseExpression('b>c');
  834. AssertBinaryExpr('Simple binary larger than ',eopGreaterThan,FLeft,FRight);
  835. AssertExpression('Left is b',TheLeft,pekident,'b');
  836. AssertExpression('Right is c',TheRight,pekIdent,'c');
  837. end;
  838. procedure TTestExpressions.TestBinaryLargerThanEqual;
  839. begin
  840. DeclareVar('boolean','a');
  841. DeclareVar('integer','b');
  842. DeclareVar('integer','c');
  843. ParseExpression('b>=c');
  844. AssertBinaryExpr('Simple binary larger than or equal',eopGreaterThanEqual,FLeft,FRight);
  845. AssertExpression('Left is b',TheLeft,pekident,'b');
  846. AssertExpression('Right is c',TheRight,pekIdent,'c');
  847. end;
  848. procedure TTestExpressions.TestPrimitiveBooleanFalse;
  849. begin
  850. DeclareVar('boolean','a');
  851. ParseExpression('False');
  852. AssertExpression('Simple boolean',theExpr,pekBoolConst,TBoolConstExpr);
  853. AssertEquals('Boolean false',False,TBoolConstExpr(TheExpr).Value);
  854. end;
  855. procedure TTestExpressions.TestPrimitiveBooleanTrue;
  856. begin
  857. DeclareVar('boolean','a');
  858. ParseExpression('True');
  859. AssertExpression('Simple boolean',theExpr,pekBoolConst,TBoolConstExpr);
  860. AssertEquals('Boolean true',True,TBoolConstExpr(TheExpr).Value);
  861. end;
  862. procedure TTestExpressions.TestPrimitiveNil;
  863. begin
  864. DeclareVar('pointer','a');
  865. ParseExpression('Nil');
  866. AssertExpression('Nil expr',theExpr,pekNil,TNilExpr);
  867. end;
  868. procedure TTestExpressions.TestPrimitiveSet;
  869. Var
  870. P : TParamsExpr;
  871. begin
  872. DeclareVar('set of byte','a');
  873. ParseExpression('[1,2,3]');
  874. P:=TParamsExpr(AssertExpression('Set expr',theExpr,pekSet,TParamsExpr));
  875. AssertEquals('Element count',3,Length(P.Params));
  876. AssertExpression('Element 1 in set',P.Params[0],pekNumber,'1');
  877. AssertExpression('Element 2 in set',P.Params[1],pekNumber,'2');
  878. AssertExpression('Element 3 in set',P.Params[2],pekNumber,'3');
  879. end;
  880. procedure TTestExpressions.TestPrimitiveChar;
  881. begin
  882. DeclareVar('char');
  883. ParseExpression('#32');
  884. AssertExpression('Simple string',theExpr,pekString,'#32');
  885. end;
  886. procedure TTestExpressions.TestPrimitiveControlChar;
  887. begin
  888. DeclareVar('char');
  889. ParseExpression('^M');
  890. AssertExpression('Simple string',theExpr,pekString,'^M');
  891. end;
  892. procedure TTestExpressions.TestPrimitiveSetEmpty;
  893. Var
  894. P : TParamsExpr;
  895. begin
  896. DeclareVar('set of byte','a');
  897. ParseExpression('[]');
  898. P:=TParamsExpr(AssertExpression('Set expr',theExpr,pekSet,TParamsExpr));
  899. AssertEquals('Element count',0,Length(P.Params));
  900. end;
  901. procedure TTestExpressions.TestPrimitiveSelf;
  902. begin
  903. DeclareVar('pointer','a');
  904. ParseExpression('Self');
  905. AssertExpression('Inherited expr',theExpr,pekSelf,TSelfExpr);
  906. end;
  907. procedure TTestExpressions.TestInherited;
  908. begin
  909. DeclareVar('pointer','a');
  910. ParseExpression('inherited');
  911. AssertExpression('Inherited expr',theExpr,pekInherited,TInheritedExpr);
  912. end;
  913. procedure TTestExpressions.TestInheritedFunction;
  914. begin
  915. DeclareVar('pointer','a');
  916. ParseExpression('inherited myfunction');
  917. AssertBinaryExpr('Inherited expr',eopNone,Fleft,FRight);
  918. AssertExpression('Inherited expr',theleft,pekInherited,TInheritedExpr);
  919. AssertExpression('Inherited expr',theright,pekIdent,'myfunction');
  920. end;
  921. procedure TTestExpressions.SetUp;
  922. begin
  923. Inherited;
  924. FVariables:=TStringList.Create;
  925. end;
  926. procedure TTestExpressions.TearDown;
  927. begin
  928. FreeAndNil(FVariables);
  929. Inherited;
  930. end;
  931. procedure TTestExpressions.SetExpression(const AExpression: String);
  932. Var
  933. I : Integer;
  934. begin
  935. StartProgram(ExtractFileUnitName(MainFilename));
  936. if FVariables.Count=0 then
  937. DeclareVar('integer');
  938. Add('Var');
  939. For I:=0 to FVariables.Count-1 do
  940. Add(' '+Fvariables[I]);
  941. Add('begin');
  942. Add(' a:='+AExpression+';');
  943. end;
  944. procedure TTestExpressions.ParseExpression;
  945. begin
  946. ParseModule;
  947. AssertEquals('Have program',TPasProgram,Module.ClassType);
  948. AssertNotNull('Have program section',PasProgram.ProgramSection);
  949. AssertNotNull('Have initialization section',PasProgram.InitializationSection);
  950. AssertEquals('Have initialization statement',1,PasProgram.InitializationSection.Elements.Count);
  951. AssertNotNull('Have initialization statement',PasProgram.InitializationSection.Elements[0]);
  952. AssertEquals('Assignment statement',TPasImplAssign,TObject(PasProgram.InitializationSection.Elements[0]).ClassType);
  953. FTheExpr:=TPasImplAssign(PasProgram.InitializationSection.Elements[0]).right;
  954. AssertNotNull('Have assignment expression',FTheExpr);
  955. end;
  956. procedure TTestExpressions.ParseExpression(const AExpression: String);
  957. begin
  958. SetExpression(AExpression);
  959. ParseExpression;
  960. end;
  961. function TTestExpressions.AssertBinaryExpr(const Msg: String; Op: TExprOpCode;
  962. out ALeft, ARight: TPasExpr): TBinaryExpr;
  963. begin
  964. Result:=AssertBinaryExpr(Msg,TheExpr,Op,ALeft,ARight);
  965. end;
  966. function TTestExpressions.AssertBinaryExpr(const Msg: String; AExpr: TPasExpr;
  967. Op: TExprOpCode; out ALeft, ARight: TPasExpr): TBinaryExpr;
  968. begin
  969. AssertExpression(Msg+' is binary',AExpr,pekBinary,TBinaryExpr);
  970. Result:=AExpr as TBinaryExpr;
  971. AssertEquals(Msg+' opcode OK',Op,Result.OpCode);
  972. ALeft:=Result.Left;
  973. ARight:=Result.Right;
  974. AssertNotNull('Have left',ALeft);
  975. AssertNotNull('Have right',ARight);
  976. TAssert.AssertSame('Result.left.parent=B',Result,Result.left.Parent);
  977. TAssert.AssertSame('Result.right.parent=B',Result,Result.right.Parent);
  978. end;
  979. function TTestExpressions.AssertUnaryExpr(const Msg: String; Op: TExprOpCode;
  980. out AOperand : TPasExpr): TUnaryExpr;
  981. begin
  982. Result:=AssertUnaryExpr(Msg,TheExpr,OP,AOperand);
  983. end;
  984. function TTestExpressions.AssertUnaryExpr(const Msg: String; AExpr: TPasExpr;
  985. Op: TExprOpCode; out AOperand: TPasExpr): TUnaryExpr;
  986. begin
  987. AssertExpression(Msg+' is unary',AExpr,pekUnary,TUnaryExpr);
  988. Result:=AExpr as TUnaryExpr;
  989. AssertEquals(Msg+' opcode OK',Op,Result.OpCode);
  990. AOperand:=Result.Operand;
  991. AssertNotNull('Have left',AOperand);
  992. end;
  993. procedure TTestExpressions.TestSubIdentStrWithFormat;
  994. Var
  995. P : TParamsExpr;
  996. B : TBinaryExpr;
  997. begin
  998. DeclareVar('string','a');
  999. DeclareVar('integer','i');
  1000. ParseExpression('system.str(i:0:3,a)');
  1001. P:=TParamsExpr(AssertExpression('Params',TheExpr,pekFuncParams,TParamsExpr));
  1002. TAssert.AssertSame('P.value.parent=P',P,P.Value.Parent);
  1003. AssertEquals('2 argument',2,Length(p.params));
  1004. AssertExpression('Simple identifier',p.params[0],pekIdent,'i');
  1005. AssertExpression('Simple identifier',p.params[1],pekIdent,'a');
  1006. TAssert.AssertSame('P.params[0].parent=P',P,P.params[0].Parent);
  1007. TAssert.AssertSame('P.params[1].parent=P',P,P.params[1].Parent);
  1008. B:=TBinaryExpr(AssertExpression('Binary identifier',P.Value,pekBinary,TBinaryExpr));
  1009. AssertExpression('Name of unit',B.left,pekIdent,'system');
  1010. AssertExpression('Name of function',B.right,pekIdent,'str');
  1011. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1012. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1013. end;
  1014. procedure TTestExpressions.TestAPlusCallB;
  1015. var
  1016. B: TBinaryExpr;
  1017. P: TParamsExpr;
  1018. begin
  1019. DeclareVar('string','a');
  1020. DeclareVar('integer','b');
  1021. ParseExpression('a+b(1)');
  1022. B:=TBinaryExpr(AssertExpression('Binary identifier',TheExpr,pekBinary,TBinaryExpr));
  1023. AssertExpression('left a',B.left,pekIdent,'a');
  1024. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1025. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1026. P:=TParamsExpr(AssertExpression('Params',B.right,pekFuncParams,TParamsExpr));
  1027. TAssert.AssertSame('P.value.parent=P',P,P.Value.Parent);
  1028. AssertEquals('1 argument',1,Length(p.params));
  1029. AssertExpression('param 1',p.params[0],pekNumber,'1');
  1030. end;
  1031. procedure TTestExpressions.TestAPlusBBracketFuncParams;
  1032. var
  1033. P: TParamsExpr;
  1034. B: TBinaryExpr;
  1035. begin
  1036. DeclareVar('string','a');
  1037. DeclareVar('integer','b');
  1038. ParseExpression('(a+b)(1)');
  1039. P:=TParamsExpr(AssertExpression('Params',TheExpr,pekFuncParams,TParamsExpr));
  1040. TAssert.AssertSame('P.value.parent=P',P,P.Value.Parent);
  1041. AssertEquals('1 argument',1,Length(p.params));
  1042. AssertExpression('param 1',p.params[0],pekNumber,'1');
  1043. B:=TBinaryExpr(AssertExpression('Binary identifier',P.Value,pekBinary,TBinaryExpr));
  1044. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1045. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1046. AssertExpression('left a',B.left,pekIdent,'a');
  1047. AssertExpression('right b',B.right,pekIdent,'b');
  1048. end;
  1049. procedure TTestExpressions.TestAPlusBBracketArrayParams;
  1050. var
  1051. B: TBinaryExpr;
  1052. P: TParamsExpr;
  1053. begin
  1054. DeclareVar('string','a');
  1055. DeclareVar('integer','b');
  1056. ParseExpression('(a+b)[1]');
  1057. P:=TParamsExpr(AssertExpression('Params',TheExpr,pekArrayParams,TParamsExpr));
  1058. TAssert.AssertSame('P.value.parent=P',P,P.Value.Parent);
  1059. AssertEquals('1 argument',1,Length(p.params));
  1060. AssertExpression('param 1',p.params[0],pekNumber,'1');
  1061. B:=TBinaryExpr(AssertExpression('Binary identifier',P.Value,pekBinary,TBinaryExpr));
  1062. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1063. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1064. AssertExpression('left a',B.left,pekIdent,'a');
  1065. AssertExpression('right b',B.right,pekIdent,'b');
  1066. end;
  1067. procedure TTestExpressions.TestAPlusBBracketDotC;
  1068. var
  1069. B, PlusB: TBinaryExpr;
  1070. begin
  1071. DeclareVar('string','a');
  1072. DeclareVar('integer','b');
  1073. ParseExpression('(a+b).c');
  1074. B:=TBinaryExpr(AssertExpression('Binary identifier',TheExpr,pekBinary,TBinaryExpr));
  1075. AssertEquals('().',eopSubIdent,B.OpCode);
  1076. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1077. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1078. AssertExpression('right c',B.right,pekIdent,'c');
  1079. PlusB:=TBinaryExpr(AssertExpression('Binary identifier',B.left,pekBinary,TBinaryExpr));
  1080. TAssert.AssertSame('PlusB.left.parent=PlusB',PlusB,PlusB.left.Parent);
  1081. TAssert.AssertSame('PlusB.right.parent=PlusB',PlusB,PlusB.right.Parent);
  1082. AssertExpression('left a',PlusB.left,pekIdent,'a');
  1083. AssertExpression('right b',PlusB.right,pekIdent,'b');
  1084. end;
  1085. procedure TTestExpressions.TestADotBDotC;
  1086. var
  1087. B, SubB: TBinaryExpr;
  1088. begin
  1089. ParseExpression('a.b.c');
  1090. B:=TBinaryExpr(AssertExpression('Binary identifier',TheExpr,pekBinary,TBinaryExpr));
  1091. AssertEquals('dot expr',eopSubIdent,B.OpCode);
  1092. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1093. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1094. AssertExpression('right c',B.right,pekIdent,'c');
  1095. SubB:=TBinaryExpr(AssertExpression('Binary identifier',B.left,pekBinary,TBinaryExpr));
  1096. TAssert.AssertSame('PlusB.left.parent=PlusB',SubB,SubB.left.Parent);
  1097. TAssert.AssertSame('PlusB.right.parent=PlusB',SubB,SubB.right.Parent);
  1098. AssertExpression('left a',SubB.left,pekIdent,'a');
  1099. AssertExpression('right b',SubB.right,pekIdent,'b');
  1100. end;
  1101. procedure TTestExpressions.TestADotBBracketC;
  1102. var
  1103. P: TParamsExpr;
  1104. B: TBinaryExpr;
  1105. begin
  1106. ParseExpression('a.b[c]');
  1107. P:=TParamsExpr(AssertExpression('ArrayParams',TheExpr,pekArrayParams,TParamsExpr));
  1108. B:=TBinaryExpr(AssertExpression('Binary identifier',P.Value,pekBinary,TBinaryExpr));
  1109. AssertEquals('dot expr',eopSubIdent,B.OpCode);
  1110. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1111. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1112. AssertExpression('left a',B.left,pekIdent,'a');
  1113. AssertExpression('right b',B.right,pekIdent,'b');
  1114. AssertEquals('length(p.Params)',length(p.Params),1);
  1115. AssertExpression('first param c',p.Params[0],pekIdent,'c');
  1116. end;
  1117. procedure TTestExpressions.TestSelfDotBBracketC;
  1118. var
  1119. P: TParamsExpr;
  1120. B: TBinaryExpr;
  1121. begin
  1122. ParseExpression('self.b[c]');
  1123. P:=TParamsExpr(AssertExpression('ArrayParams',TheExpr,pekArrayParams,TParamsExpr));
  1124. B:=TBinaryExpr(AssertExpression('Binary identifier',P.Value,pekBinary,TBinaryExpr));
  1125. AssertEquals('dot expr',eopSubIdent,B.OpCode);
  1126. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1127. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1128. AssertEquals('left self',TSelfExpr,B.left.classtype);
  1129. AssertExpression('right b',B.right,pekIdent,'b');
  1130. AssertEquals('length(p.Params)',length(p.Params),1);
  1131. AssertExpression('first param c',p.Params[0],pekIdent,'c');
  1132. end;
  1133. procedure TTestExpressions.TestAasBDotCBracketFuncParams;
  1134. var
  1135. P: TParamsExpr;
  1136. B, AsExpr: TBinaryExpr;
  1137. begin
  1138. ParseExpression('(a as b).c(d)');
  1139. P:=TParamsExpr(AssertExpression('FuncParams',TheExpr,pekFuncParams,TParamsExpr));
  1140. AssertEquals('length(p.Params)',length(p.Params),1);
  1141. AssertExpression('first param d',p.Params[0],pekIdent,'d');
  1142. B:=TBinaryExpr(AssertExpression('Upper Binary identifier',P.Value,pekBinary,TBinaryExpr));
  1143. AssertEquals('dot c expr',eopSubIdent,B.OpCode);
  1144. TAssert.AssertSame('B.left.parent=B',B,B.left.Parent);
  1145. TAssert.AssertSame('B.right.parent=B',B,B.right.Parent);
  1146. AssertExpression('dot c',b.right,pekIdent,'c');
  1147. AsExpr:=TBinaryExpr(AssertExpression('lower binary identifier',B.left,pekBinary,TBinaryExpr));
  1148. AssertEquals('AS expr',eopAs,AsExpr.OpCode);
  1149. TAssert.AssertSame('AsExpr.left.parent=AsExpr',AsExpr,AsExpr.left.Parent);
  1150. TAssert.AssertSame('AsExpr.right.parent=AsExpr',AsExpr,AsExpr.right.Parent);
  1151. AssertExpression('left AS a',AsExpr.left,pekIdent,'a');
  1152. AssertExpression('right AS b',AsExpr.right,pekIdent,'b');
  1153. end;
  1154. procedure TTestExpressions.TestParseAdhocExpression;
  1155. var
  1156. ExprElement: TPasExpr;
  1157. BinaryExpression: TBinaryExpr;
  1158. begin
  1159. // Unlike the other tests, this is not about the parser, but about the
  1160. // ability to parse an expression on it's own. Without any further context.
  1161. Add('True=False');
  1162. StartParsing;
  1163. Parser.NextToken;
  1164. Parser.ParseAdhocExpression(ExprElement);
  1165. BinaryExpression := AssertExpression('Some expression, parsed separately',ExprElement,pekBinary,TBinaryExpr) as TBinaryExpr;
  1166. AssertExpression('Some expression, parsed separately, left part', BinaryExpression.left, pekBoolConst, TBoolConstExpr);
  1167. AssertExpression('Some expression, parsed separately, right part',BinaryExpression.right, pekBoolConst, TBoolConstExpr);
  1168. end;
  1169. initialization
  1170. RegisterTest(TTestExpressions);
  1171. end.