test_unit_eberon.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. "use strict";
  2. var Class = require("rtl.js").Class;
  3. //var EberonCodeGenerator = require("js/EberonCodeGenerator.js");
  4. var language = require("eberon/eberon_grammar.js").language;
  5. var TestUnitCommon = require("test_unit_common.js");
  6. var TypePromotion = require("eberon/eberon_type_promotion.js");
  7. var assert = TestUnitCommon.assert;
  8. var pass = TestUnitCommon.pass;
  9. var fail = TestUnitCommon.fail;
  10. var context = TestUnitCommon.context;
  11. var grammar = language.grammar;
  12. function testWithContext(context, pass, fail){
  13. return TestUnitCommon.testWithContext(context, grammar.declarationSequence, language, pass, fail);
  14. }
  15. function testWithModule(src, pass, fail){
  16. return TestUnitCommon.testWithModule(src, language, pass, fail);
  17. }
  18. function testWithGrammar(parser, pass, fail){
  19. return TestUnitCommon.testWithGrammar(parser, language, pass, fail);
  20. }
  21. var temporaryValues = {
  22. context: context(
  23. grammar.declarationSequence,
  24. "TYPE Base = RECORD END;"
  25. + "Derived = RECORD (Base) flag: BOOLEAN END;"
  26. + "Derived2 = RECORD (Derived) flag2: BOOLEAN END;"
  27. + "PBase = POINTER TO Base;"
  28. + "PDerived = POINTER TO Derived;"
  29. + "PDerived2 = POINTER TO Derived2;"
  30. + "VAR pBase: POINTER TO Base; bVar: BOOLEAN;"
  31. + "PROCEDURE proc(b: BOOLEAN): BOOLEAN; RETURN b END proc;"),
  32. __expression: function(e){
  33. return "PROCEDURE p(); BEGIN b <- pBase; b2 <- pBase; ASSERT(" + e + "); END p;";
  34. },
  35. __statement: function(e){
  36. return "PROCEDURE p(); BEGIN b <- pBase; b2 <- pBase; " + e + " END p;";
  37. },
  38. passExpressions: function(){
  39. return this.__pass(this.__expression.bind(this), arguments);
  40. },
  41. passStatements: function(){
  42. return this.__pass(this.__statement.bind(this), arguments);
  43. },
  44. failExpressions: function(){
  45. return this.__fail(this.__expression.bind(this), arguments);
  46. },
  47. failStatements: function(){
  48. return this.__fail(this.__statement.bind(this), arguments);
  49. },
  50. __pass: function(make, cases){
  51. var result = [];
  52. for(var i = 0; i < cases.length; ++i)
  53. result.push(make(cases[i]));
  54. return pass.apply(this, result);
  55. },
  56. __fail: function(make, cases){
  57. var result = [];
  58. for(var i = 0; i < cases.length; ++i)
  59. result.push([make(cases[i]), "type 'Base' has no 'flag' field"]);
  60. return fail.apply(this, result);
  61. }
  62. };
  63. var TestVar = Class.extend({
  64. init: function(){
  65. this.__type = "type";
  66. },
  67. type: function(){return this.__type;},
  68. setType: function(type){this.__type = type;}
  69. });
  70. /*
  71. function makeCodeSuite(){
  72. return {
  73. "insertion": pass(
  74. function(){
  75. var g = EberonCodeGenerator.makeGenerator();
  76. g.write("a");
  77. var i = g.makeInsertion();
  78. g.write("b");
  79. g.insert(i, "c");
  80. assert(g.result() == "acb");
  81. },
  82. function(){
  83. var g = EberonCodeGenerator.makeGenerator();
  84. g.write("ab");
  85. var i1 = g.makeInsertion();
  86. var i2 = g.makeInsertion();
  87. g.write("cd");
  88. g.insert(i1, "123");
  89. g.insert(i2, "345");
  90. assert(g.result() == "ab123345cd");
  91. },
  92. function(){
  93. var g = EberonCodeGenerator.makeGenerator();
  94. g.write("ab");
  95. var i = g.makeInsertion();
  96. g.write("cd");
  97. assert(g.result() == "abcd");
  98. }
  99. )
  100. };
  101. }
  102. */
  103. exports.suite = {
  104. //"code": makeCodeSuite(),
  105. "arithmetic operators": testWithContext(
  106. context(grammar.statement, "VAR b1: BOOLEAN;"),
  107. pass(),
  108. fail(["b1 := b1 + b1", "operator '+' type mismatch: numeric type or SET or STRING expected, got 'BOOLEAN'"])
  109. ),
  110. "key words": testWithGrammar(
  111. grammar.variableDeclaration,
  112. pass(),
  113. fail(["SELF: INTEGER", "not parsed"],
  114. ["SUPER: INTEGER", "not parsed"],
  115. ["STRING: INTEGER", "'STRING' already declared"]
  116. )
  117. ),
  118. "abstract method declaration": testWithContext(
  119. context(grammar.declarationSequence,
  120. "TYPE T = RECORD PROCEDURE p() END;"
  121. + "D = RECORD(T) END;"
  122. + "T2 = RECORD PROCEDURE p1(); PROCEDURE p2(i: INTEGER): BOOLEAN END;"
  123. ),
  124. pass(),
  125. fail(["VAR r: T;",
  126. "cannot instantiate 'T' because it has abstract method(s): p"],
  127. ["VAR r: T2;",
  128. "cannot instantiate 'T2' because it has abstract method(s): p1, p2"],
  129. ["PROCEDURE p(); VAR p: POINTER TO T; BEGIN NEW(p); END p;",
  130. "cannot instantiate 'T' because it has abstract method(s): p"],
  131. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR r: LocalT; END p;",
  132. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  133. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR p: POINTER TO LocalT; BEGIN NEW(p) END p;",
  134. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  135. ["VAR r: D;",
  136. "cannot instantiate 'D' because it has abstract method(s): p"],
  137. ["PROCEDURE p(); VAR p: POINTER TO D; BEGIN NEW(p); END p;",
  138. "cannot instantiate 'D' because it has abstract method(s): p"],
  139. ["PROCEDURE p(); TYPE HasAbstractField = RECORD f: T; END; END;",
  140. "cannot instantiate 'T' because it has abstract method(s): p"]
  141. )
  142. ),
  143. "new method declaration": testWithContext(
  144. context(grammar.declarationSequence,
  145. "TYPE T = RECORD PROCEDURE p(); intField: INTEGER END; A = ARRAY 1 OF INTEGER;"),
  146. pass("PROCEDURE T.p(); END T.p;"
  147. ),
  148. fail(["PROCEDURE TUnk.p(), NEW; END TUnk.p;", "undeclared identifier: 'TUnk'"],
  149. ["PROCEDURE A.p(), NEW; END A.p;",
  150. "RECORD type expected in method declaration, got 'ARRAY 1 OF INTEGER'"],
  151. ["PROCEDURE T.p(), NEW; END;", "not parsed"],
  152. ["PROCEDURE T.p(); END p;",
  153. "mismatched method names: expected 'T.p' at the end (or nothing), got 'p'"],
  154. ["PROCEDURE T.p(); END T2.p;",
  155. "mismatched method names: expected 'T.p' at the end (or nothing), got 'T2.p'"],
  156. ["PROCEDURE T.p(); END T.p2;",
  157. "mismatched method names: expected 'T.p' at the end (or nothing), got 'T.p2'"],
  158. ["PROCEDURE T.intField(); END T.intField;",
  159. "'T' has no declaration for method 'intField'"],
  160. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(); END T.p;",
  161. "method 'T.p' already defined"],
  162. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); PROCEDURE m() END; END p;",
  163. "cannot declare a new method 'm': method already was declared"],
  164. ["PROCEDURE p(); TYPE T = RECORD m: INTEGER; PROCEDURE m() END; END p;",
  165. "cannot declare method, record already has field 'm'"],
  166. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); m: INTEGER END; END p;",
  167. "cannot declare field, record already has method 'm'"]
  168. )
  169. ),
  170. "overridden method declaration": testWithContext(
  171. context(grammar.declarationSequence,
  172. "TYPE Base = RECORD PROCEDURE p() END; T = RECORD (Base) END;"
  173. + "PROCEDURE Base.p(); END Base.p;"),
  174. pass("PROCEDURE T.p(); END T.p;"),
  175. fail(["PROCEDURE T.pUnk(); END T.pUnk;",
  176. "'T' has no declaration for method 'pUnk'"],
  177. ["PROCEDURE proc(); TYPE T = RECORD (Base) PROCEDURE p() END; END proc;",
  178. "cannot declare a new method 'p': method already was declared"],
  179. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(); END T.p;",
  180. "method 'T.p' already defined"],
  181. ["PROCEDURE T.p(a: INTEGER); END T.p;",
  182. "overridden method 'p' signature mismatch: should be 'PROCEDURE', got 'PROCEDURE(INTEGER)'"],
  183. ["PROCEDURE p(); PROCEDURE T.p(); END T.p; END p;",
  184. "method should be defined in the same scope as its bound type 'T'"]
  185. )
  186. ),
  187. "SELF": testWithContext(
  188. context(grammar.declarationSequence,
  189. "TYPE T = RECORD PROCEDURE p(); i: INTEGER END;"
  190. + "PROCEDURE proc(i: INTEGER); END proc;"),
  191. pass("PROCEDURE T.p(); BEGIN SELF.i := 0; END T.p;",
  192. "PROCEDURE T.p(); BEGIN proc(SELF.i); END T.p;"
  193. ),
  194. fail(["PROCEDURE p(); BEGIN SELF.i := 0; END p;",
  195. "SELF can be used only in methods"])
  196. ),
  197. "SELF as pointer": testWithContext(
  198. context(grammar.declarationSequence,
  199. "TYPE T = RECORD PROCEDURE method() END;"
  200. + "PT = POINTER TO T;"
  201. + "VAR pVar: PT;"
  202. + "PROCEDURE refProc(VAR p: PT); END refProc;"
  203. ),
  204. pass("PROCEDURE T.method(); BEGIN pVar := SELF(POINTER) END T.method;",
  205. "PROCEDURE p();"
  206. + "TYPE Derived = RECORD(T) END; VAR pd: POINTER TO Derived;"
  207. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN NEW(pd); pVar := SELF(POINTER); END Derived.method;"
  208. + "END p;"),
  209. fail(["PROCEDURE T.method(); BEGIN refProc(SELF(POINTER)) END T.method;",
  210. "read-only variable cannot be used as VAR parameter"],
  211. ["PROCEDURE T.method(); BEGIN SELF(POINTER) := pVar; END T.method;",
  212. "cannot assign to read-only variable"],
  213. ["PROCEDURE p();"
  214. + "TYPE Derived = RECORD(T) END; VAR d: Derived;"
  215. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN pVar := SELF(POINTER); END Derived.method;"
  216. + "END p;",
  217. "cannot declare a variable of type 'Derived' (and derived types) because SELF(POINTER) was used in its method(s)"],
  218. ["PROCEDURE p();"
  219. + "TYPE Derived = RECORD(T) END; Derived2 = RECORD(Derived) END;"
  220. + "VAR d: Derived2;"
  221. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN pVar := SELF(POINTER); END Derived.method;"
  222. + "END p;",
  223. "cannot declare a variable of type 'Derived' (and derived types) because SELF(POINTER) was used in its method(s)"]
  224. )
  225. ),
  226. "method call": testWithContext(
  227. context(grammar.expression,
  228. "TYPE T = RECORD PROCEDURE p(); PROCEDURE f(): INTEGER END;"
  229. + "VAR o: T;"
  230. + "PROCEDURE T.p(); END T.p;"
  231. + "PROCEDURE T.f(): INTEGER; RETURN 0 END T.f;"
  232. ),
  233. pass("o.f()"),
  234. fail(["o.p()", "procedure returning no result cannot be used in an expression"])
  235. ),
  236. "method call as statement": testWithContext(
  237. context(grammar.statement,
  238. "TYPE T = RECORD PROCEDURE p(); PROCEDURE f(): INTEGER END;"
  239. + "VAR o: T;"
  240. + "PROCEDURE T.p(); END T.p;"
  241. + "PROCEDURE T.f(): INTEGER; RETURN 0 END T.f;"
  242. ),
  243. pass("o.p"),
  244. fail(["o.f", "procedure returning a result cannot be used as a statement"])
  245. ),
  246. "cannot assign to method": testWithContext(
  247. context(grammar.statement,
  248. "TYPE T = RECORD PROCEDURE p() END;"
  249. + "VAR o: T;"
  250. + "PROCEDURE T.p(); END T.p;"
  251. ),
  252. pass(),
  253. fail(["o.p := o.p", "method 'p' cannot be referenced"],
  254. ["o.p := NIL", "cannot assign to method"])
  255. ),
  256. "method cannot be referenced": testWithContext(
  257. context(grammar.statement,
  258. "TYPE T = RECORD PROCEDURE p() END;"
  259. + "Proc = PROCEDURE();"
  260. + "VAR o: T;"
  261. + "PROCEDURE T.p(); END T.p;"
  262. + "PROCEDURE proc(p: Proc); END proc;"
  263. ),
  264. pass(),
  265. fail(["proc(o.p)", "method 'p' cannot be referenced"],
  266. ["v <- o.p", "method 'p' cannot be referenced"])
  267. ),
  268. "method super call": testWithContext(
  269. context(grammar.declarationSequence,
  270. "TYPE T = RECORD PROCEDURE p(); PROCEDURE pAbstract(); PROCEDURE pAbstract2() END;"
  271. + "D = RECORD(T) PROCEDURE pNoSuper() END;"
  272. + "PROCEDURE T.p(); END T.p;"
  273. ),
  274. pass("PROCEDURE D.p(); BEGIN SUPER() END D.p;"),
  275. fail(["PROCEDURE D.pNoSuper(); BEGIN SUPER() END D.pNoSuper;",
  276. "there is no method 'pNoSuper' in base type(s)"],
  277. ["PROCEDURE p(); BEGIN SUPER() END p;",
  278. "SUPER can be used only in methods"],
  279. ["PROCEDURE T.pNoBase(); BEGIN SUPER() END T.pNoBase;",
  280. "'T' has no base type - SUPER cannot be used"],
  281. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract;",
  282. "cannot use abstract method(s) in SUPER calls: pAbstract"],
  283. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract; PROCEDURE D.pAbstract2(); BEGIN SUPER() END D.pAbstract2;",
  284. "cannot use abstract method(s) in SUPER calls: pAbstract, pAbstract2"]
  285. )
  286. ),
  287. "export method": testWithContext(
  288. context(grammar.declarationSequence,
  289. "TYPE T = RECORD PROCEDURE p() END;"
  290. ),
  291. pass(),
  292. fail(["PROCEDURE T.p*(); END T.p;",
  293. "method implementation cannot be exported: p"])
  294. ),
  295. "import method": testWithModule(
  296. "MODULE test;"
  297. + "TYPE T* = RECORD PROCEDURE m*(); PROCEDURE mNotExported() END;"
  298. + "PROCEDURE T.m(); END T.m; PROCEDURE T.mNotExported(); END T.mNotExported;"
  299. + "END test.",
  300. pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.m(); END m.",
  301. "MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.m(); END T.m; END m."
  302. ),
  303. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.mNotExported(); END m.",
  304. "type 'T' has no 'mNotExported' field"],
  305. ["MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.mNotExported(); END T.mNotExported; END m.",
  306. "'T' has no declaration for method 'mNotExported'"])
  307. ),
  308. "import abstract record": testWithModule(
  309. "MODULE test;"
  310. + "TYPE T* = RECORD PROCEDURE m*(); END;"
  311. + "END test.",
  312. pass("MODULE m; IMPORT test; TYPE T = RECORD f: POINTER TO test.T; END; END m."
  313. ),
  314. fail(["MODULE m; IMPORT test; TYPE T = RECORD f: test.T; END; END m",
  315. "cannot instantiate 'T' because it has abstract method(s): m"])
  316. ),
  317. "non-scalar variables can be exported": testWithContext(
  318. context(grammar.declarationSequence,
  319. "TYPE T = RECORD END; A = ARRAY 3 OF INTEGER;"
  320. ),
  321. pass("VAR r*: T;",
  322. "VAR a*: A;"),
  323. fail()
  324. ),
  325. "export as read-only": testWithContext(
  326. context(grammar.declarationSequence, ""),
  327. pass("TYPE T* = RECORD i-: INTEGER END;"),
  328. fail(["TYPE T- = RECORD END;",
  329. "type cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  330. ["PROCEDURE p-(); END p;",
  331. "procedure cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  332. ["CONST c- = 123;",
  333. "constant cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  334. ["VAR i-: INTEGER;",
  335. "variable cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  336. ["TYPE T* = RECORD PROCEDURE p-() END;",
  337. "method cannot be exported as read-only using '-' mark (did you mean '*'?)"]
  338. )
  339. ),
  340. "field exported as read-only is writable in current module": testWithContext(
  341. context(grammar.statement,
  342. "TYPE T* = RECORD i-: INTEGER END;"
  343. + "VAR r: T;"
  344. ),
  345. pass("r.i := 123"),
  346. fail()
  347. ),
  348. "import as read-only": testWithModule(
  349. "MODULE test; TYPE T* = RECORD f-: INTEGER END; END test.",
  350. pass(),
  351. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 123; END m.",
  352. "cannot assign to read-only variable"],
  353. ["MODULE m; IMPORT test; TYPE D = RECORD(test.T) END; VAR r: D; BEGIN r.f := 123; END m.",
  354. "cannot assign to read-only variable"]
  355. )),
  356. "STRING variable": testWithGrammar(
  357. grammar.variableDeclaration,
  358. pass("s: STRING")
  359. ),
  360. "STRING expression": testWithContext(
  361. context(grammar.expression,
  362. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  363. pass("s1 + s2",
  364. "s1 + \"abc\"",
  365. "\"abc\" + s1",
  366. "s1 = s2",
  367. "s1 = \"abc\"",
  368. "s1 = 22X",
  369. "\"abc\" = s1",
  370. "22X = s1",
  371. "s1 # s2",
  372. "s1 # \"abc\"",
  373. "s1 # 22X",
  374. "\"abc\" # s1",
  375. "22X # s1",
  376. "s1 < s2",
  377. "s1 < \"abc\"",
  378. "s1 < 22X",
  379. "\"abc\" < s1",
  380. "22X < s1",
  381. "s1 > s2",
  382. "s1 > \"abc\"",
  383. "s1 > 22X",
  384. "\"abc\" > s1",
  385. "22X > s1",
  386. "s1 <= s2",
  387. "s1 <= \"abc\"",
  388. "\"abc\" <=s1",
  389. "22X <= s1",
  390. "s1 >= \"abc\"",
  391. "s1 >= 22X",
  392. "s1 >= s2",
  393. "\"abc\" >= s1",
  394. "22X >= s1"
  395. ),
  396. fail(["s1 = NIL", "type mismatch: expected 'STRING', got 'NIL'"],
  397. ["s1 = a", "type mismatch: expected 'STRING', got 'ARRAY 10 OF CHAR'"],
  398. ["a = s1", "type mismatch: expected 'ARRAY 10 OF CHAR', got 'STRING'"]
  399. )
  400. ),
  401. "STRING literal expression": testWithContext(
  402. context(grammar.expression,
  403. "CONST cs = \"abc\";"
  404. + "PROCEDURE pString(s: STRING): STRING; RETURN s END pString;"
  405. + "PROCEDURE pStringByRef(VAR s: STRING): STRING; RETURN s END pStringByRef;"
  406. ),
  407. pass("\"abc\" + \"cde\"",
  408. "cs + cs",
  409. "cs + \"abc\"",
  410. "cs = \"abc\"",
  411. "cs # \"abc\"",
  412. "cs < \"abc\"",
  413. "cs > \"abc\"",
  414. "cs <= \"abc\"",
  415. "cs >= \"abc\"",
  416. "pString(cs)",
  417. "pString(\"abc\")"
  418. ),
  419. fail(["pStringByRef(cs)", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"],
  420. ["pStringByRef(\"abc\")", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"]
  421. )
  422. ),
  423. "STRING assignment": testWithContext(
  424. context(grammar.statement,
  425. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  426. pass("s1 := s2",
  427. "s1 := \"abc\"",
  428. "s1 := 22X"
  429. ),
  430. fail(["a := s1", "type mismatch: 'a' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'STRING' expression"],
  431. ["s1 := a", "type mismatch: 's1' is 'STRING' and cannot be assigned to 'ARRAY 10 OF CHAR' expression"]
  432. )
  433. ),
  434. "STRING and ARRAY OF CHAR": testWithContext(
  435. context(grammar.expression,
  436. "VAR s: STRING; a: ARRAY 10 OF CHAR;"
  437. + "PROCEDURE pArray(a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pArray;"
  438. + "PROCEDURE pString(s: STRING): BOOLEAN; RETURN FALSE END pString;"
  439. + "PROCEDURE pVar(VAR a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pVar;"
  440. + "PROCEDURE pIntArray(a: ARRAY OF INTEGER): BOOLEAN; RETURN FALSE END pIntArray;"
  441. ),
  442. pass("pArray(s)"),
  443. fail(["pVar(s)", "type mismatch for argument 1: cannot pass 'STRING' as VAR parameter of type 'ARRAY OF CHAR'"],
  444. ["pString(a)", "type mismatch for argument 1: 'ARRAY 10 OF CHAR' cannot be converted to 'STRING'"],
  445. ["pIntArray(s)", "type mismatch for argument 1: 'STRING' cannot be converted to 'ARRAY OF INTEGER'"]
  446. )
  447. ),
  448. "STRING LEN": testWithContext(
  449. context(grammar.expression,
  450. "VAR s: STRING;"),
  451. pass("LEN(s)"),
  452. fail()
  453. ),
  454. "STRING indexing": testWithContext(
  455. context(grammar.expression,
  456. "VAR s: STRING;"
  457. + "PROCEDURE pCharByVar(VAR c: CHAR): CHAR; RETURN c END pCharByVar;"),
  458. pass("s[0]"),
  459. fail(["s[-1]", "index is negative: -1"],
  460. ["pCharByVar(s[0])", "string element cannot be used as VAR parameter"]
  461. )
  462. ),
  463. "designate call result in expression": testWithContext(
  464. context(grammar.expression,
  465. "TYPE PT = POINTER TO RECORD field: INTEGER END;"
  466. + "VAR p: PT;"
  467. + "PROCEDURE proc(): PT; RETURN p END proc;"
  468. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  469. + "PROCEDURE intVar(VAR i: INTEGER): INTEGER; RETURN i END intVar;"),
  470. pass("proc().field",
  471. "intVar(proc().field)"),
  472. fail(["intVar(int())", "expression cannot be used as VAR parameter"])
  473. ),
  474. "designate call result in statement": testWithContext(
  475. context(grammar.statement,
  476. "TYPE PT = POINTER TO RECORD field: INTEGER; proc: PROCEDURE END;"
  477. + "ProcType = PROCEDURE;"
  478. + "VAR p: PT;"
  479. + "PROCEDURE procVoid(); END procVoid;"
  480. + "PROCEDURE proc(): PT; RETURN p END proc;"
  481. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  482. + "PROCEDURE intVar(VAR i: INTEGER); END intVar;"
  483. + "PROCEDURE returnProc(): ProcType; RETURN procVoid END returnProc;"
  484. ),
  485. pass("proc().field := 0",
  486. "proc().proc()",
  487. "proc().proc"
  488. ),
  489. fail(["int() := 0", "cannot assign to procedure call result"],
  490. ["intVar(int())", "expression cannot be used as VAR parameter"],
  491. ["procVoid()()", "PROCEDURE expected, got 'procedure call statement'"],
  492. ["int()()", "PROCEDURE expected, got 'INTEGER'"],
  493. ["returnProc()", "procedure returning a result cannot be used as a statement"] // call is not applied implicitly to result
  494. )
  495. ),
  496. "type promotion": {
  497. "or" : pass(
  498. function(){
  499. var or = new TypePromotion.OrPromotions();
  500. var a = new TestVar();
  501. var p = or.next();
  502. assert(a.type() == "type");
  503. p.invert();
  504. p.promote(a, "type1");
  505. assert(a.type() == "type");
  506. or.next();
  507. assert(a.type() == "type1");
  508. or.reset();
  509. assert(a.type() == "type");
  510. },
  511. function(){
  512. var or = new TypePromotion.OrPromotions();
  513. var a = new TestVar();
  514. var p = or.next(p);
  515. p.promote(a, "type1");
  516. or.next();
  517. assert(a.type() == "type");
  518. },
  519. function(){
  520. var or = new TypePromotion.OrPromotions();
  521. var a = new TestVar();
  522. var p1 = or.next();
  523. p1.promote(a, "type1");
  524. var p2 = or.next();
  525. p2.invert();
  526. p2.promote(a, "type2");
  527. assert(a.type() == "type");
  528. assert(a.type() == "type");
  529. or.next();
  530. assert(a.type() == "type2");
  531. or.reset();
  532. assert(a.type() == "type");
  533. }
  534. ),
  535. "and": pass(
  536. function(){
  537. var and = new TypePromotion.AndPromotions();
  538. var a = new TestVar();
  539. var p = and.next();
  540. p.promote(a, "type1");
  541. and.next();
  542. assert(a.type() == "type1");
  543. and.reset();
  544. assert(a.type() == "type");
  545. },
  546. function(){ // (a IS type1) & (v OR (a IS type2)) & v
  547. var and = new TypePromotion.AndPromotions();
  548. var a = new TestVar();
  549. var p = and.next();
  550. p.promote(a, "type1");
  551. var subOr = and.next().makeOr();
  552. subOr.next();
  553. subOr.next().promote(a, "type2");
  554. and.next();
  555. assert(a.type() == "type1");
  556. and.reset();
  557. assert(a.type() == "type");
  558. },
  559. function(){ // (a IS type1) & ~(v OR ~(a IS type2)) & v
  560. var and = new TypePromotion.AndPromotions();
  561. var a = new TestVar();
  562. and.next().promote(a, "type1");
  563. var subOr = and.next();
  564. subOr.invert();
  565. subOr = subOr.makeOr();
  566. subOr.next();
  567. var p = subOr.next();
  568. p.invert();
  569. p.promote(a, "type2");
  570. and.next();
  571. assert(a.type() == "type2");
  572. and.reset();
  573. assert(a.type() == "type");
  574. },
  575. function(){ // (a IS type1) & (v & (a IS type2))
  576. var and = new TypePromotion.AndPromotions();
  577. var a = new TestVar();
  578. and.next().promote(a, "type1");
  579. var sub = and.next().makeAnd();
  580. sub.next();
  581. assert(a.type() == "type1");
  582. sub.next().promote(a, "type2");
  583. assert(a.type() == "type1");
  584. and.and();
  585. assert(a.type() == "type2");
  586. and.or();
  587. assert(a.type() == "type");
  588. },
  589. function(){ // (~(~(a IS type1)) & v) OR v
  590. var a = new TestVar();
  591. var or = new TypePromotion.OrPromotions();
  592. var and = or.next().makeAnd();
  593. var p1 = and.next();
  594. p1.invert();
  595. var p2 = p1.makeOr().next().makeAnd().next();
  596. p2.invert();
  597. p2.promote(a, "type1");
  598. and.next();
  599. assert(a.type() == "type1");
  600. or.next();
  601. assert(a.type() == "type");
  602. },
  603. function(){ // (v OR (a IS type1)) & v)
  604. var a = new TestVar();
  605. var and = new TypePromotion.AndPromotions();
  606. var or = and.next().makeOr();
  607. or.next();
  608. or.next().makeAnd().next().promote(a, "type1");
  609. and.next();
  610. assert(a.type() == "type");
  611. }
  612. )
  613. },
  614. "in place variables": {
  615. "initialization": testWithContext(
  616. context(grammar.statement,
  617. "VAR i: INTEGER;"
  618. + "PROCEDURE p(): BOOLEAN; RETURN FALSE END p;"
  619. + "PROCEDURE void(); END void;"
  620. ),
  621. pass("v <- 0",
  622. "v <- 1.23",
  623. "v <- \"abc\"",
  624. "v <- TRUE",
  625. "v <- i",
  626. "v <- i + i",
  627. "v <- p()",
  628. "v <- void" // procedure type
  629. ),
  630. fail(["v <-", "initialization expression expected"],
  631. ["v <- void()", "procedure returning no result cannot be used in an expression"],
  632. ["v <- NIL", "cannot use NIL to initialize variable"])
  633. ),
  634. "read-only if initialized with string literal": testWithContext(
  635. context(grammar.declarationSequence, ""),
  636. pass(),
  637. fail(["PROCEDURE p(); BEGIN s <- \"abc\"; s := \"def\"; END p;", "cannot assign to string literal"],
  638. ["PROCEDURE p(); BEGIN s <- \"abc\"; s[0] := \"d\"; END p;", "cannot assign to read-only variable"])
  639. ),
  640. "scope": testWithContext(
  641. temporaryValues.context,
  642. temporaryValues.passStatements(
  643. "v1 <- 0; v2 <-0;",
  644. "i <- 0; ASSERT(i = 0);",
  645. "WHILE FALSE DO v <- 0; ASSERT(v = 0); END; WHILE FALSE DO v <- 0; END;",
  646. "WHILE FALSE DO i1 <- 0; WHILE FALSE DO i2 <- 0; ASSERT(i1 = 0); ASSERT(i2 = 0); END; END;",
  647. "WHILE bVar DO v <- 0; ELSIF ~bVar DO v <- 0 END;",
  648. "IF FALSE THEN v <- 0; ASSERT(v = 0); END; IF FALSE THEN v <- 0; END;",
  649. "IF FALSE THEN v <- 0; END; IF FALSE THEN v <- 0; END;",
  650. "IF FALSE THEN v <- 0; ELSIF FALSE THEN v <- 0; ELSE v <- 0; END;",
  651. "i <- 0; CASE i OF 0: v <- 0 | 1: v <- 1; ; ASSERT(v = 1); END;",
  652. "REPEAT v <- 0; UNTIL FALSE; REPEAT v <- 0; UNTIL FALSE;",
  653. "REPEAT v <- 0; ASSERT(v = 0); UNTIL v # 0;",
  654. "i <- 0; FOR i := 0 TO 10 DO v <- 0; END; FOR i := 0 TO 10 DO v <- 0; END;"
  655. ),
  656. fail(["PROCEDURE p(); BEGIN v <- 0; v <-0; END p;", "'v' already declared"],
  657. ["PROCEDURE p(); VAR v: INTEGER; BEGIN v <- 0; END p;", "'v' already declared"],
  658. ["PROCEDURE p(); BEGIN v <- 0; WHILE FALSE DO v <- 0; END; END p;",
  659. "'v' already declared in procedure scope"],
  660. ["PROCEDURE p(); BEGIN i <- 0; IF FALSE THEN i <- 0; END; END p;",
  661. "'i' already declared in procedure scope"],
  662. ["PROCEDURE p(); BEGIN i <- 0; IF TRUE THEN IF TRUE THEN i <- 0; END; END; END p;",
  663. "'i' already declared in procedure scope"],
  664. ["PROCEDURE p(); BEGIN WHILE FALSE DO i <- 0; WHILE FALSE DO i <- 0; END; END; END p;",
  665. "'i' already declared in operator scope"]
  666. )
  667. ),
  668. "type promotion in expression": testWithContext(
  669. temporaryValues.context,
  670. temporaryValues.passExpressions(
  671. "(b IS PDerived) & b.flag",
  672. "(b IS PDerived) & bVar & b.flag",
  673. "(b IS PDerived) & (bVar OR b.flag)",
  674. "(b IS PDerived) & (b2 IS PDerived) & b.flag & b2.flag",
  675. "(b IS PDerived) & proc(TRUE) & b.flag",
  676. "(b IS PDerived) & ~proc(TRUE) & b.flag",
  677. "~(~(b IS PDerived)) & b.flag",
  678. "~~(b IS PDerived) & b.flag",
  679. "(b IS PDerived) & ((b IS PDerived2) OR bVar) & b.flag",
  680. "(b IS PDerived) & (bVar OR (b IS PDerived2)) & b.flag",
  681. "(b IS PDerived) & ~(bVar OR ~(b IS PDerived2)) & b.flag2",
  682. "~(bVar & (b IS PDerived)) OR b.flag"
  683. //TODO: "((b IS PDerived) = TRUE) & b.flag); END p;",
  684. ),
  685. temporaryValues.failExpressions(
  686. "(b IS PDerived) OR b.flag",
  687. "(bVar OR (b IS PDerived)) & b.flag",
  688. "(b IS PDerived) OR bVar & b.flag",
  689. "~(b IS PDerived) & b.flag",
  690. "((b IS PDerived) & (b2 IS PDerived) OR bVar) & b.flag",
  691. "proc(b IS PDerived) & proc(b.flag)",
  692. "ORD(b IS PDerived) * ORD(b.flag) = 0",
  693. "((b IS PDerived) = FALSE) & b.flag",
  694. "((b IS PDerived) & bVar) = b.flag",
  695. "b IS PDerived); ASSERT(b.flag",
  696. "((b IS PDerived) OR (b IS PDerived)) & b.flag",
  697. "(b IS PDerived) OR (b IS PDerived) OR b.flag",
  698. "(bVar OR (b IS PDerived)) & b.flag",
  699. "~(bVar & ~(b IS PDerived)) & b.flag"
  700. )
  701. ),
  702. "invert type promotion in expression": testWithContext(
  703. temporaryValues.context,
  704. temporaryValues.passExpressions(
  705. "~(b IS PDerived) OR b.flag",
  706. "~(b IS PDerived) OR b.flag OR bVar",
  707. "~(b IS PDerived) OR b.flag & bVar",
  708. "~(b IS PDerived) OR bVar & b.flag",
  709. "~(b IS PDerived) OR (bVar & b.flag)",
  710. "~(b IS PDerived) OR bVar OR b.flag",
  711. "~(b IS PDerived) OR (bVar = b.flag)",
  712. "~(~(b IS PDerived) OR bVar) & b.flag",
  713. "~(~(b IS PDerived) OR b.flag) & b.flag",
  714. "~(b IS PDerived) OR ~(b2 IS PDerived) OR b2.flag",
  715. "~(b IS PDerived) OR b.flag OR ~(b2 IS PDerived) OR b2.flag",
  716. "~((b IS PDerived) & b.flag) OR b.flag OR ~(b2 IS PDerived) OR b2.flag",
  717. "~((b IS PDerived) & b.flag) OR b.flag OR ~((b2 IS PDerived) & b.flag & b2.flag) OR b2.flag"
  718. ),
  719. temporaryValues.failExpressions(
  720. "(~(b IS PDerived) OR bVar) & b.flag",
  721. "(ORD(~(b IS PDerived)) + ORD(b.flag)",
  722. "~(~(b IS PDerived) OR bVar) OR b.flag",
  723. "~(~(b IS PDerived) & bVar) & b.flag",
  724. "~(b IS PDerived) OR b.flag = b.flag"
  725. )
  726. ),
  727. "type promotion in separate statements": testWithContext(
  728. temporaryValues.context,
  729. pass(),
  730. temporaryValues.failStatements(
  731. "bVar := b IS PDerived; ASSERT(b.flag)",
  732. "bVar := (b IS PDerived) & bVar; ASSERT(b.flag)"
  733. )
  734. ),
  735. "type promotion in IF": testWithContext(
  736. temporaryValues.context,
  737. temporaryValues.passStatements(
  738. "IF b IS PDerived THEN b.flag := FALSE; END;",
  739. "IF (b IS PDerived) & bVar THEN b.flag := FALSE; END;",
  740. "IF bVar & (b IS PDerived) THEN b.flag := FALSE; END;",
  741. "IF FALSE THEN ELSIF b IS PDerived THEN b.flag := FALSE; END;",
  742. "IF b IS PDerived THEN bVar := (b IS PDerived2) & b.flag2; b.flag := FALSE; END;",
  743. "IF bVar THEN ELSIF b IS PDerived2 THEN ELSIF b IS PDerived THEN END;",
  744. "IF bVar THEN ELSIF b IS PDerived THEN ELSIF b IS PDerived THEN ELSIF b IS PDerived THEN END;",
  745. "IF b IS PDerived THEN IF bVar OR (b IS PDerived2) THEN b.flag := FALSE; END; END"
  746. ),
  747. temporaryValues.failStatements(
  748. "IF (b IS PDerived) OR bVar THEN b.flag := FALSE; END",
  749. "IF bVar OR (b IS PDerived) THEN b.flag := FALSE; END",
  750. "IF (b2 IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  751. "IF (b IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  752. "IF (b IS PDerived) OR (b IS PDerived) OR b.flag THEN END",
  753. "IF (b IS PDerived) OR (b IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  754. "IF ((b IS PDerived) OR (b IS PDerived)) THEN b.flag := FALSE; END",
  755. "IF (b IS PDerived) OR (b IS PDerived2) THEN b.flag := FALSE; END",
  756. "IF (b IS PDerived2) OR (b IS PDerived) THEN b.flag := FALSE; END",
  757. "IF b IS PDerived THEN END; b.flag := FALSE",
  758. "IF ~(b IS PDerived) THEN END; b.flag := FALSE",
  759. "IF ~(b IS PDerived) THEN ELSIF bVar THEN END; b.flag := FALSE",
  760. "IF ~(b IS PDerived) THEN ELSIF bVar THEN ELSE END; b.flag := FALSE",
  761. "IF bVar THEN ELSIF b IS PDerived THEN ELSE END; b.flag := FALSE",
  762. "IF b IS PDerived THEN ELSE b.flag := FALSE; END",
  763. "IF bVar OR (b IS PDerived) THEN b.flag := FALSE; END;",
  764. "IF bVar OR (b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  765. "IF bVar OR ~(b IS PDerived) THEN b.flag := FALSE; END;",
  766. "IF b IS PDerived THEN ELSIF TRUE THEN b.flag := FALSE; END",
  767. "IF bVar THEN bVar := (b IS PDerived) & bVar; ASSERT(b.flag); END"
  768. )
  769. ),
  770. "invert type promotion in IF": testWithContext(
  771. temporaryValues.context,
  772. temporaryValues.passStatements(
  773. "IF ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  774. "IF ~(b IS PDerived) THEN ELSIF bVar THEN b.flag := FALSE; ELSE b.flag := FALSE; END;",
  775. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b.flag := FALSE; ELSE b.flag := FALSE; b2.flag := FALSE; END;",
  776. "IF ~(b IS PDerived) OR bVar THEN ELSE b.flag := FALSE; END;",
  777. "IF ~(b IS PDerived) OR b.flag THEN ELSE b.flag := FALSE; END;",
  778. "IF ~(b IS PDerived) OR (b2 IS PDerived) THEN ELSE b.flag := FALSE; END;",
  779. "IF ~(b IS PDerived) OR ~(b2 IS PDerived) THEN ELSE b2.flag := FALSE; END;",
  780. "IF ~(b IS PDerived) THEN bVar := b IS PDerived; ELSE b.flag := FALSE; END;",
  781. "IF ~(b IS PDerived) THEN ASSERT((b IS PDerived) & b.flag); ELSE b.flag := FALSE; END;",
  782. "IF bVar OR ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;"
  783. ),
  784. temporaryValues.failStatements(
  785. "IF ~(b IS PDerived) & bVar THEN ELSE b.flag := FALSE; END; END p;",
  786. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b2.flag := FALSE; END;",
  787. "IF bVar OR (b IS PDerived) THEN ELSE b.flag := FALSE; END;"
  788. )
  789. ),
  790. "type promotion in WHILE": testWithContext(
  791. temporaryValues.context,
  792. temporaryValues.passStatements(
  793. "WHILE (b IS PDerived) & b.flag DO END;",
  794. "WHILE ~(b IS PDerived) OR b.flag DO END;",
  795. "WHILE b IS PDerived DO b.flag := FALSE; END;",
  796. "WHILE ~(b IS PDerived) DO ELSIF b.flag DO END;",
  797. "WHILE ~(b IS PDerived) DO ELSIF bVar DO b.flag := FALSE; END;"
  798. ),
  799. temporaryValues.failStatements(
  800. "WHILE b IS PDerived DO END; b.flag := FALSE;"
  801. )
  802. ),
  803. "type promotion cannot be reset by assignment": testWithContext(
  804. temporaryValues.context,
  805. pass(),
  806. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN b := pBase; b.flag := FALSE; END; END p;",
  807. "type mismatch: 'b' is 'PDerived' and cannot be assigned to 'POINTER TO Base' expression"]
  808. )
  809. ),
  810. "type promotion cannot be reset by passing as VAR argument": testWithContext(
  811. temporaryValues.context,
  812. pass(),
  813. fail(["PROCEDURE p(); PROCEDURE procBaseAsVar(VAR p: PBase); END procBaseAsVar; BEGIN b <- pBase; IF b IS PDerived THEN procBaseAsVar(b); b.flag := FALSE; END; END p;",
  814. "type mismatch for argument 1: cannot pass 'PDerived' as VAR parameter of type 'PBase'"]
  815. )
  816. ),
  817. "type promotion after dereferencing": testWithContext(
  818. temporaryValues.context,
  819. temporaryValues.passExpressions(
  820. "(b^ IS Derived) & b.flag")
  821. ),
  822. "IS expression after type promotion": testWithContext(
  823. temporaryValues.context,
  824. pass(),
  825. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN bVar := b IS PDerived; b.flag := FALSE; END; END p;",
  826. "invalid type test: 'Derived' is not an extension of 'Derived'"]
  827. )
  828. ),
  829. "record types as values": testWithContext(
  830. context(grammar.declarationSequence,
  831. "TYPE Base = RECORD pBase: POINTER TO Base END;"
  832. + "Derived = RECORD (Base) END;"
  833. + "VAR base: Base;"
  834. + "PROCEDURE procBaseVar(VAR b: Base); END procBaseVar;"
  835. ),
  836. pass("PROCEDURE p(b: Base); BEGIN base <- b; procBaseVar(base); base := b; END p;"),
  837. fail(["PROCEDURE p(); BEGIN baseVar <- base.pBase^; ASSERT(base IS Derived); END p;",
  838. "invalid type test: a value variable cannot be used"],
  839. ["PROCEDURE p(VAR b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  840. "invalid type test: a value variable cannot be used"],
  841. ["PROCEDURE p(b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  842. "invalid type test: a value variable cannot be used"],
  843. ["PROCEDURE p(); TYPE Abstract = RECORD PROCEDURE method() END; PROCEDURE test(a: Abstract); BEGIN v <- a; END test; END p;",
  844. "cannot instantiate 'Abstract' because it has abstract method(s): method"],
  845. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE method() END; PROCEDURE T.method(); BEGIN ASSERT(SELF(POINTER) # NIL); END T.method; PROCEDURE test(r: T); BEGIN v <- r; END test; END p;",
  846. "cannot declare a variable of type 'T' (and derived types) because SELF(POINTER) was used in its method(s)"]
  847. )
  848. ),
  849. "arrays as values": testWithContext(
  850. context(grammar.declarationSequence,
  851. "TYPE A = ARRAY 3 OF INTEGER; T = RECORD a: A END;"
  852. + "VAR r: T;"
  853. + "PROCEDURE procArrayVar(VAR a: A); END procArrayVar;"
  854. ),
  855. pass("PROCEDURE p(r: T); BEGIN a <- r.a; a[0] := 123; procArrayVar(a); END p;",
  856. "PROCEDURE p(a: A); BEGIN tmp <- a; END p;",
  857. "PROCEDURE p(); VAR a: A; BEGIN tmp <- a; END p;",
  858. "PROCEDURE p(); VAR a: ARRAY 3 OF BOOLEAN; BEGIN tmp <- a; END p;"
  859. ),
  860. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN v <- a; END p;",
  861. "cannot initialize variable 'v' with open array"]
  862. )
  863. ),
  864. "FOR variable": testWithContext(
  865. context(grammar.statement, ""),
  866. pass("FOR i <- 0 TO 10 DO END",
  867. "FOR i <- 0 TO 10 DO FOR j <- 0 TO 10 BY 1 DO END END",
  868. "IF TRUE THEN FOR i <- 0 TO 10 DO END; FOR i <- 0 TO 10 BY 1 DO END; END"
  869. ),
  870. fail(["FOR i <- 0.0 TO 10 DO END", "'INTEGER' expression expected to assign 'i', got 'REAL'"],
  871. ["IF TRUE THEN FOR i <- 0 TO 10 DO END; i := 1; END", "undeclared identifier: 'i'"]
  872. )
  873. )
  874. },
  875. "type promotion for VAR arguments": testWithContext(
  876. context(grammar.declarationSequence,
  877. "TYPE Base = RECORD END; PBase = POINTER TO Base;"
  878. + "Derived = RECORD (Base) flag: BOOLEAN END; PDerived = POINTER TO Derived;"),
  879. pass("PROCEDURE p(VAR b: Base); BEGIN ASSERT((b IS Derived) & b.flag); END p;"),
  880. fail(["PROCEDURE p(VAR b: PBase); BEGIN ASSERT((b IS PDerived) & b.flag); END p;",
  881. "type 'Base' has no 'flag' field"])
  882. ),
  883. "type promotion for non-VAR arguments": testWithContext(
  884. context(grammar.declarationSequence,
  885. "TYPE Base = RECORD END; PBase = POINTER TO Base;"
  886. + "Derived = RECORD (Base) flag: BOOLEAN END; PDerived = POINTER TO Derived;"),
  887. pass("PROCEDURE p(b: PBase); BEGIN ASSERT((b IS PDerived) & b.flag); END p;")
  888. ),
  889. "Non-VAR arguments cannot be modified": testWithContext(
  890. context(grammar.declarationSequence,
  891. "TYPE PBase = POINTER TO RECORD END; T = RECORD i: INTEGER END;"
  892. + "PROCEDURE pArrayRef(VAR a: ARRAY OF INTEGER); END pArrayRef;"
  893. + "PROCEDURE recordVar(VAR r: T); END recordVar;"),
  894. pass("PROCEDURE p(VAR i: INTEGER); BEGIN i := 0; END p;",
  895. "PROCEDURE p(VAR b: PBase); BEGIN b := NIL; END p;"),
  896. fail(["PROCEDURE p(i: INTEGER); BEGIN i := 0; END p;",
  897. "cannot assign to non-VAR formal parameter"],
  898. ["PROCEDURE p(b: PBase); BEGIN b := NIL; END p;",
  899. "cannot assign to non-VAR formal parameter"],
  900. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pArrayRef(a) END p",
  901. "non-VAR formal parameter cannot be used as VAR parameter"],
  902. ["PROCEDURE p(r: T); BEGIN recordVar(r); END p",
  903. "non-VAR formal parameter cannot be used as VAR parameter"],
  904. ["PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  905. "cannot assign to non-VAR formal parameter"],
  906. ["PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p",
  907. "cannot assign to non-VAR formal parameter"]
  908. )
  909. ),
  910. "array": {
  911. "static array indexOf": testWithContext(
  912. context(grammar.expression,
  913. "TYPE "
  914. + "T = RECORD END;"
  915. + "VAR "
  916. + "r: T;"
  917. + "intArray: ARRAY 3 OF INTEGER;"
  918. + "boolDynArray: ARRAY * OF BOOLEAN;"
  919. + "recordArray: ARRAY 3 OF T;"
  920. + "arrayOfArray: ARRAY 3, 4 OF INTEGER;"
  921. ),
  922. pass("intArray.indexOf(0)",
  923. "boolDynArray.indexOf(FALSE) = -1"
  924. ),
  925. fail(["intArray.indexOf(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  926. ["recordArray.indexOf(r)", "'indexOf' is not defined for array of 'T'"],
  927. ["arrayOfArray.indexOf(intArray)", "'indexOf' is not defined for array of 'ARRAY 4 OF INTEGER'"],
  928. ["intArray.indexOf", "array method 'indexOf' cannot be referenced"]
  929. )
  930. ),
  931. "open array indexOf": testWithGrammar(
  932. grammar.declarationSequence,
  933. pass("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN a.indexOf(123) END p;"
  934. )
  935. ),
  936. "dynamic": {
  937. "declaration": testWithContext(
  938. context(grammar.declarationSequence,
  939. "TYPE DA = ARRAY * OF INTEGER;"),
  940. pass("TYPE A = ARRAY * OF INTEGER;",
  941. "TYPE A = ARRAY * OF ARRAY * OF INTEGER;",
  942. "TYPE A = ARRAY *, * OF INTEGER;",
  943. "TYPE A = ARRAY 3, * OF INTEGER;",
  944. "TYPE A = ARRAY *, 3 OF INTEGER;",
  945. "TYPE P = PROCEDURE(): DA;",
  946. "TYPE P = PROCEDURE(VAR a: DA): DA;",
  947. "TYPE P = PROCEDURE(VAR a: ARRAY * OF INTEGER): DA;",
  948. "VAR a: ARRAY * OF INTEGER;",
  949. "PROCEDURE p(VAR a: ARRAY * OF INTEGER);END p;",
  950. "PROCEDURE p(VAR a: ARRAY * OF ARRAY * OF INTEGER);END p;",
  951. "PROCEDURE p(VAR a: ARRAY OF ARRAY * OF INTEGER);END p;"
  952. ),
  953. fail(["TYPE A = ARRAY OF INTEGER;", "not parsed"],
  954. ["TYPE P = PROCEDURE(): ARRAY OF INTEGER;", "';' expected"],
  955. ["TYPE P = PROCEDURE(a: DA);", "dynamic array has no use as non-VAR argument 'a'"],
  956. ["TYPE P = PROCEDURE(a: ARRAY * OF INTEGER);", "dynamic array has no use as non-VAR argument 'a'"],
  957. ["PROCEDURE p(a: DA);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  958. ["PROCEDURE p(a: ARRAY * OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  959. ["PROCEDURE p(a: ARRAY OF ARRAY * OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  960. ["PROCEDURE p(a: ARRAY * OF ARRAY OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"]
  961. )
  962. ),
  963. "return": testWithContext(
  964. context(grammar.declarationSequence,
  965. "TYPE A = ARRAY * OF INTEGER; B = ARRAY * OF BOOLEAN;"
  966. + "VAR a: A; b: B;"),
  967. pass("PROCEDURE p(): A; RETURN a END p;",
  968. "PROCEDURE p(): A; VAR static: ARRAY 3 OF INTEGER; RETURN static END p;"),
  969. fail(["PROCEDURE p(): ARRAY OF INTEGER; RETURN a; END p;", "not parsed"],
  970. ["PROCEDURE p(): A; RETURN b; END p;", "RETURN 'ARRAY * OF INTEGER' expected, got 'ARRAY * OF BOOLEAN'"])
  971. ),
  972. "pass as non-VAR argument": testWithContext(
  973. context(grammar.statement,
  974. "TYPE Int3 = ARRAY 3 OF INTEGER;"
  975. + "VAR dInt: ARRAY * OF INTEGER;"
  976. + "dIntInt: ARRAY *,* OF INTEGER;"
  977. + "PROCEDURE pOpenInt(a: ARRAY OF INTEGER); END pOpenInt;"
  978. + "PROCEDURE pOpenIntOfInt(a: ARRAY OF ARRAY OF INTEGER); END pOpenIntOfInt;"
  979. + "PROCEDURE pInt3(a: Int3); END pInt3;"),
  980. pass("pOpenInt(dInt)",
  981. "pOpenIntOfInt(dIntInt)"),
  982. fail(["pInt3(dInt)", "type mismatch for argument 1: 'ARRAY * OF INTEGER' cannot be converted to 'ARRAY 3 OF INTEGER'"])
  983. ),
  984. "pass as VAR argument": testWithContext(
  985. context(grammar.statement,
  986. "TYPE A = ARRAY * OF INTEGER; B = ARRAY * OF BOOLEAN;"
  987. + "VAR a: A; b: B; aStatic: ARRAY 3 OF INTEGER;"
  988. + "aIntInt: ARRAY * OF ARRAY * OF INTEGER;"
  989. + "aInt3Int: ARRAY * OF ARRAY 3 OF INTEGER;"
  990. + "PROCEDURE paVar(VAR a: A); END paVar;"
  991. + "PROCEDURE paVarOpen(VAR a: ARRAY OF INTEGER); END paVarOpen;"
  992. + "PROCEDURE pDynamicIntOfInt(VAR a: ARRAY * OF ARRAY * OF INTEGER); END pDynamicIntOfInt;"
  993. + "PROCEDURE pDynamicIntOfOpenInt(VAR a: ARRAY * OF ARRAY OF INTEGER); END pDynamicIntOfOpenInt;"
  994. ),
  995. pass("paVar(a)",
  996. "paVarOpen(a)",
  997. "pDynamicIntOfInt(aIntInt)",
  998. "pDynamicIntOfOpenInt(aIntInt)",
  999. "pDynamicIntOfOpenInt(aInt3Int)"
  1000. ),
  1001. fail(["paVar(aStatic)", "type mismatch for argument 1: cannot pass 'ARRAY 3 OF INTEGER' as VAR parameter of type 'ARRAY * OF INTEGER'"],
  1002. ["pDynamicIntOfInt(aInt3Int)", "type mismatch for argument 1: 'ARRAY *, 3 OF INTEGER' cannot be converted to 'ARRAY *, * OF INTEGER'"]
  1003. )
  1004. ),
  1005. "assign": testWithContext(
  1006. context(grammar.statement,
  1007. "VAR stat: ARRAY 3 OF INTEGER; dynamic: ARRAY * OF INTEGER;"),
  1008. pass("dynamic := stat"),
  1009. fail(["stat := dynamic", "type mismatch: 'stat' is 'ARRAY 3 OF INTEGER' and cannot be assigned to 'ARRAY * OF INTEGER' expression"],
  1010. ["dynamic := NIL", "type mismatch: 'dynamic' is 'ARRAY * OF INTEGER' and cannot be assigned to 'NIL' expression"])
  1011. ),
  1012. "indexing": testWithContext(
  1013. context(grammar.expression,
  1014. "VAR a: ARRAY * OF INTEGER;"),
  1015. pass("a[0]", "a[1]"),
  1016. fail(["a[-1]", "index is negative: -1"],
  1017. ["a[-2]", "index is negative: -2"])
  1018. ),
  1019. "indexOf": testWithContext(
  1020. context(grammar.expression,
  1021. "VAR intArray: ARRAY * OF INTEGER;"
  1022. ),
  1023. pass("intArray.indexOf(0)"),
  1024. fail()
  1025. ),
  1026. "add": testWithContext(
  1027. context(grammar.statement,
  1028. "VAR a: ARRAY * OF INTEGER;"
  1029. + "a2: ARRAY * OF ARRAY * OF INTEGER;"
  1030. + "aStatic: ARRAY 3 OF INTEGER;"
  1031. + "byte: BYTE;"),
  1032. pass("a.add(123)",
  1033. "a.add(byte)",
  1034. "a2.add(a)",
  1035. "a2.add(aStatic)"
  1036. ),
  1037. fail(["a.add := NIL", "cannot assign to method"],
  1038. ["v <- a.add", "dynamic array method 'add' cannot be referenced"],
  1039. ["a.add()", "method 'add' expects one argument, got nothing"],
  1040. ["a.add(1, 2)", "method 'add' expects one argument, got many"],
  1041. ["a.add(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
  1042. )
  1043. ),
  1044. "add open array to dynamic array of static arrays": testWithContext(
  1045. context(grammar.declarationSequence,
  1046. "VAR a: ARRAY * OF ARRAY 3 OF INTEGER;"),
  1047. pass(),
  1048. fail(["PROCEDURE p(paramA: ARRAY OF INTEGER); BEGIN a.add(paramA); END p",
  1049. "type mismatch for argument 1: 'ARRAY OF INTEGER' cannot be converted to 'ARRAY 3 OF INTEGER'"]
  1050. )
  1051. ),
  1052. "remove": testWithContext(
  1053. context(grammar.statement,
  1054. "VAR a: ARRAY * OF INTEGER;"),
  1055. pass("a.remove(0)"),
  1056. fail(["a.remove(-1)", "index is negative: -1"],
  1057. ["a.remove()", "1 argument(s) expected, got 0"],
  1058. ["a.remove(0, 1)", "1 argument(s) expected, got 2"],
  1059. ["a.remove(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  1060. ["a.Remove(0)", "selector '.Remove' cannot be applied to 'ARRAY * OF INTEGER'"]
  1061. )
  1062. ),
  1063. "clear": testWithContext(
  1064. context(grammar.statement,
  1065. "VAR a: ARRAY * OF INTEGER;"),
  1066. pass("a.clear()"),
  1067. fail(["a.clear(0)", "0 argument(s) expected, got 1"])
  1068. )
  1069. }
  1070. },
  1071. "syntax relaxation": testWithGrammar(
  1072. grammar.declarationSequence,
  1073. pass("PROCEDURE p; END;",
  1074. "TYPE T = RECORD field: INTEGER; END;",
  1075. "TYPE T = RECORD PROCEDURE method(); END;",
  1076. "TYPE T = RECORD PROCEDURE method(); END; PROCEDURE T.method(); END;",
  1077. "PROCEDURE p(): INTEGER; RETURN 0; END;"
  1078. )
  1079. ),
  1080. "constructor": {
  1081. "declaration": testWithGrammar(
  1082. grammar.declarationSequence,
  1083. pass("TYPE T = RECORD PROCEDURE T(); END; PROCEDURE T.T(); END;",
  1084. "TYPE T = RECORD PROCEDURE T(); i: INTEGER; END; PROCEDURE T.T(); BEGIN SELF.i := 0; END;",
  1085. "TYPE T = RECORD PROCEDURE T(); END; PROCEDURE T.T(); END T.T;",
  1086. "TYPE T = RECORD PROCEDURE T(a: INTEGER); END; PROCEDURE T.T(a: INTEGER); END;"
  1087. ),
  1088. fail(["TYPE T = RECORD END; PROCEDURE T(); END;", "'T' already declared"],
  1089. ["TYPE T = RECORD END; PROCEDURE T.T(); END;", "constructor was not declared for 'T'"],
  1090. ["TYPE T = RECORD PROCEDURE T(); END; PROCEDURE T.T(a: INTEGER); END;", "constructor 'T' signature mismatch: declared as 'PROCEDURE' but defined as 'PROCEDURE(INTEGER)'"],
  1091. ["TYPE T = RECORD PROCEDURE T(); PROCEDURE T(); END;", "constructor 'T' already declared"],
  1092. ["TYPE T = RECORD PROCEDURE T(); END; PROCEDURE T.T(); END T;", "mismatched method names: expected 'T.T' at the end (or nothing), got 'T'"],
  1093. ["TYPE T = RECORD PROCEDURE T(); END; PROCEDURE p(); PROCEDURE T.T(); END; END;", "method should be defined in the same scope as its bound type 'T'"],
  1094. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE T(); END; END;", "constructor was declared for 'T' but was not defined"],
  1095. ["TYPE T = RECORD PROCEDURE T(); END; PROCEDURE T.T(); END; PROCEDURE T.T(); END;", "constructor already defined for 'T'"],
  1096. ["TYPE T = RECORD PROCEDURE T(): INTEGER; END;", "constructor 'T' cannot have result type specified"],
  1097. ["TYPE T = ARRAY 3 OF INTEGER; PROCEDURE T(); END;", "'T' already declared"]
  1098. )
  1099. ),
  1100. "as expression": testWithContext(
  1101. context(grammar.expression,
  1102. "TYPE T = RECORD i: INTEGER; END; PT = POINTER TO T;"
  1103. + "ConsWithArguments = RECORD PROCEDURE ConsWithArguments(a: INTEGER); END;"
  1104. + "PROCEDURE ConsWithArguments.ConsWithArguments(a: INTEGER); END;"
  1105. + "PROCEDURE byVar(VAR a: T): INTEGER; RETURN 0; END;"
  1106. + "PROCEDURE byNonVar(a: T): INTEGER; RETURN 0; END;"
  1107. ),
  1108. pass("T()",
  1109. "byNonVar(T())",
  1110. "T().i",
  1111. "ConsWithArguments(123)"
  1112. ),
  1113. fail(["PT()", "PROCEDURE expected, got 'type PT'"],
  1114. ["byVar(T())", "expression cannot be used as VAR parameter"],
  1115. ["T(0)", "0 argument(s) expected, got 1"],
  1116. ["ConsWithArguments()", "1 argument(s) expected, got 0"],
  1117. ["ConsWithArguments(FALSE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
  1118. )
  1119. ),
  1120. "initialize in place variable": testWithContext(
  1121. context(grammar.statement,
  1122. "TYPE T = RECORD END;"),
  1123. pass("r <- T()"),
  1124. fail()
  1125. ),
  1126. "call base - correct": testWithContext(
  1127. context(grammar.declarationSequence,
  1128. "TYPE T = RECORD PROCEDURE T(a: INTEGER); END;"
  1129. + "Derived = RECORD(T) PROCEDURE Derived(); END;"
  1130. + "PROCEDURE T.T(a: INTEGER); END;"
  1131. ),
  1132. pass("PROCEDURE Derived.Derived() | SUPER(0); END;")
  1133. ),
  1134. "call base - incorrect": testWithContext(
  1135. context(grammar.declarationSequence,
  1136. "TYPE T = RECORD PROCEDURE T(a: INTEGER); END;"
  1137. + "RecordWthoutBase = RECORD END;"
  1138. + "Derived = RECORD(T) PROCEDURE Derived(); END;"
  1139. + "DerivedWthoutConstructor = RECORD(RecordWthoutBase) PROCEDURE DerivedWthoutConstructor(); END;"
  1140. + "RecordWthConstructorNoParameters = RECORD PROCEDURE RecordWthConstructorNoParameters(); END;"
  1141. + "DerivedWthConstructorNoParameters = RECORD(RecordWthConstructorNoParameters) PROCEDURE DerivedWthConstructorNoParameters(); END;"
  1142. + "PROCEDURE T.T(a: INTEGER); END;"
  1143. + "PROCEDURE RecordWthConstructorNoParameters.RecordWthConstructorNoParameters(); END;"
  1144. ),
  1145. pass(),
  1146. fail(["PROCEDURE Derived.Derived(); END;", "base record constructor has parameters but was not called (use '| SUPER' to pass parameters to base constructor)"],
  1147. ["PROCEDURE Derived.Derived() | SUPER(1, 2); END;", "1 argument(s) expected, got 2"],
  1148. ["PROCEDURE Derived.Derived() | SUPER(FALSE); END;", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  1149. ["PROCEDURE Derived.Derived() | SUPER(); END;", "1 argument(s) expected, got 0"],
  1150. ["PROCEDURE Derived.Derived(); BEGIN SUPER(0); END;", "cannot call base constructor from procedure body (use '| SUPER' to pass parameters to base constructor)"],
  1151. ["PROCEDURE RecordWthoutBase.RecordWthConstructorNoParametersthoutBase() | SUPER(0); END;", "'RecordWthoutBase' has no base type - SUPER cannot be used"],
  1152. ["PROCEDURE DerivedWthoutConstructor.DerivedWthoutConstructor() | SUPER(); END;", "base record constructor has no parameters and will be called automatically (do not use '| SUPER' to call base constructor)"],
  1153. ["PROCEDURE DerivedWthConstructorNoParameters.DerivedWthConstructorNoParameters() | SUPER(); END;", "base record constructor has no parameters and will be called automatically (do not use '| SUPER' to call base constructor)"]
  1154. )
  1155. ),
  1156. "initialize fields (of non record type)": testWithContext(
  1157. context(grammar.declarationSequence,
  1158. "TYPE T = RECORD PROCEDURE T(); i: INTEGER; END;"),
  1159. pass("PROCEDURE T.T() | i(123); END;"),
  1160. fail(["PROCEDURE T.T() | i(); END;", "single argument expected to initialize field 'i'"],
  1161. ["PROCEDURE T.T() | i(123, 456); END;", "single argument expected to initialize field 'i'"],
  1162. ["PROCEDURE T.T() | i(TRUE); END;", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression"]
  1163. )
  1164. ),
  1165. "initialize array fields": testWithContext(
  1166. context(grammar.declarationSequence,
  1167. "TYPE RecordWithArray = RECORD PROCEDURE RecordWithArray(a: ARRAY OF INTEGER); aStatic: ARRAY 3 OF INTEGER; aDynamic: ARRAY * OF INTEGER; END;"
  1168. ),
  1169. pass("PROCEDURE RecordWithArray.RecordWithArray(a: ARRAY OF INTEGER) | aDynamic(a); END;"),
  1170. fail(["PROCEDURE RecordWithArray.RecordWithArray(a: ARRAY OF INTEGER) | aStatic(a); END;",
  1171. "type mismatch: 'aStatic' is 'ARRAY 3 OF INTEGER' and cannot be assigned to 'ARRAY OF INTEGER' expression"]
  1172. )
  1173. ),
  1174. "initialize fields (of record type)": testWithContext(
  1175. context(grammar.declarationSequence,
  1176. "TYPE Field = RECORD PROCEDURE Field(a: INTEGER); END;"
  1177. + "PROCEDURE Field.Field(a: INTEGER); END;"),
  1178. pass("TYPE T = RECORD PROCEDURE T(); f: Field; END; PROCEDURE T.T() | f(123); END;"),
  1179. fail(["TYPE T = RECORD PROCEDURE T(); f: Field; END; PROCEDURE T.T(); END;",
  1180. "constructor 'T' must initialize fields: f"],
  1181. ["TYPE T = RECORD PROCEDURE T(); END; PROCEDURE T.T() | unknownField(123); END;",
  1182. "'unknownField' is not record 'T' own field"],
  1183. ["TYPE T = RECORD f: Field; END; Derived = RECORD(T) PROCEDURE Derived(); END; PROCEDURE Derived.Derived() | f(123); END;",
  1184. "'f' is not record 'Derived' own field"],
  1185. ["TYPE T = RECORD PROCEDURE T(); f: Field; END; PROCEDURE T.T() | f(123), f(123); END;",
  1186. "field 'f' is already initialized"]
  1187. )
  1188. ),
  1189. "initialize fields using SELF": testWithContext(
  1190. context(grammar.declarationSequence,
  1191. "TYPE T = RECORD PROCEDURE T(); i1, i2: INTEGER; END;"
  1192. ),
  1193. pass("PROCEDURE T.T() | i2(SELF.i1); END;"),
  1194. fail()
  1195. ),
  1196. "call base and initialize fields": testWithContext(
  1197. context(grammar.declarationSequence,
  1198. "TYPE Field = RECORD PROCEDURE Field(a: INTEGER); END;"
  1199. + "T = RECORD PROCEDURE T(a: INTEGER); END;"
  1200. + "Derived = RECORD(T) PROCEDURE Derived(); f: Field; END;"
  1201. + "PROCEDURE Field.Field(a: INTEGER); END;"
  1202. + "PROCEDURE T.T(a: INTEGER); END;"
  1203. ),
  1204. pass("PROCEDURE Derived.Derived() | SUPER(123), f(456); END;"),
  1205. fail(["PROCEDURE Derived.Derived() | f(456), SUPER(123); END;", "not parsed"])
  1206. ),
  1207. "fields initialization order": testWithContext(
  1208. context(grammar.declarationSequence,
  1209. "TYPE Field = RECORD PROCEDURE Field(a: INTEGER); END;"
  1210. + "T = RECORD PROCEDURE T(); f1: Field; f2, f3: Field; END;"
  1211. + "PROCEDURE Field.Field(a: INTEGER); END;"
  1212. ),
  1213. pass("PROCEDURE T.T() | f1(1), f2(2), f3(3); END;"),
  1214. fail(["PROCEDURE T.T() | f2(2), f1(1), f3(3); END;", "field 'f1' must be initialized before 'f2'"],
  1215. ["PROCEDURE T.T() | f1(1), f3(3), f2(2); END;", "field 'f2' must be initialized before 'f3'"]
  1216. )
  1217. ),
  1218. "fields with constructor but record without constructor": testWithContext(
  1219. context(grammar.declarationSequence,
  1220. "TYPE Field = RECORD PROCEDURE Field(a: INTEGER); END;"
  1221. + "PROCEDURE Field.Field(a: INTEGER); END;"
  1222. ),
  1223. pass(),
  1224. fail(["TYPE T = RECORD f: Field; END;", "constructor 'T' must initialize fields: f"])
  1225. ),
  1226. "inherit constructor parameters": testWithContext(
  1227. context(grammar.expression,
  1228. "TYPE Base = RECORD PROCEDURE Base(i: INTEGER); END;"
  1229. + "Derived = RECORD(Base) END;"
  1230. + "PROCEDURE Base.Base(a: INTEGER); END;"
  1231. ),
  1232. pass("Derived(123)"),
  1233. fail(["Derived()", "1 argument(s) expected, got 0"])
  1234. ),
  1235. "ARRAY OF record with constructor": testWithContext(
  1236. context(grammar.declarationSequence,
  1237. "TYPE NoParams = RECORD PROCEDURE NoParams(); END;"
  1238. + "WithParams = RECORD PROCEDURE WithParams(i: INTEGER); END;"
  1239. + "PROCEDURE NoParams.NoParams(); END;"
  1240. + "PROCEDURE WithParams.WithParams(i: INTEGER); END;"
  1241. ),
  1242. pass("TYPE T = ARRAY * OF NoParams;",
  1243. "TYPE T = ARRAY 3 OF NoParams;",
  1244. "TYPE T = ARRAY * OF WithParams;",
  1245. "VAR a: ARRAY 3 OF NoParams;",
  1246. "VAR a: ARRAY * OF WithParams;",
  1247. "PROCEDURE p(); VAR a: ARRAY * OF WithParams; BEGIN a.add(WithParams(123)); END;"
  1248. ),
  1249. fail(["TYPE T = ARRAY 3 OF WithParams;", "cannot use 'WithParams' as an element of static array because it has constructor with parameters"],
  1250. ["VAR a: ARRAY 3 OF WithParams;", "cannot use 'WithParams' as an element of static array because it has constructor with parameters"]
  1251. )
  1252. ),
  1253. "NEW": testWithContext(
  1254. context(grammar.statement,
  1255. "TYPE WithParams = RECORD PROCEDURE WithParams(i: INTEGER); END;"
  1256. + "DerivedWithParams = RECORD(WithParams) END;"
  1257. + "VAR p: POINTER TO WithParams; pd: POINTER TO DerivedWithParams;"
  1258. + "PROCEDURE WithParams.WithParams(i: INTEGER); END;"
  1259. ),
  1260. pass(),
  1261. fail(["NEW(p)", "cannot use procedure NEW for 'WithParams' because it has constructor with parameters, use operator NEW instead"],
  1262. ["NEW(pd)", "cannot use procedure NEW for 'DerivedWithParams' because it has constructor with parameters, use operator NEW instead"]
  1263. )
  1264. ),
  1265. "export": testWithModule(
  1266. "MODULE test;"
  1267. + "TYPE Exported* = RECORD PROCEDURE Exported*(); END;"
  1268. + "NotExported* = RECORD PROCEDURE NotExported(); END;"
  1269. + "DerivedNotExportedWithoutConstructor* = RECORD (NotExported) END;"
  1270. + "NoConstructor* = RECORD END;"
  1271. + "PROCEDURE Exported.Exported(); END;"
  1272. + "PROCEDURE NotExported.NotExported(); END;"
  1273. + "END test.",
  1274. pass("MODULE m; IMPORT test; VAR r: test.Exported; p: POINTER TO test.Exported; BEGIN p := NEW test.Exported(); NEW(p); END m.",
  1275. "MODULE m; IMPORT test; TYPE T = RECORD(test.Exported) END; END m.",
  1276. "MODULE m; IMPORT test; TYPE T = RECORD(test.NoConstructor) END; END m.",
  1277. "MODULE m; IMPORT test; TYPE T = RECORD(test.DerivedNotExportedWithoutConstructor) END; END m.",
  1278. "MODULE m; IMPORT test; PROCEDURE p(r: test.NotExported); BEGIN copy <- r; END; END m."
  1279. ),
  1280. fail(["MODULE m; TYPE T = RECORD PROCEDURE T*(); END; END m.",
  1281. "constructor 'T' cannot be exported because record itslef is not exported"],
  1282. ["MODULE m; IMPORT test; TYPE T = RECORD(test.NotExported) END; END m.",
  1283. "cannot extend 'NotExported' - its constructor was not exported"],
  1284. ["MODULE m; IMPORT test; VAR r: test.NotExported; END m.",
  1285. "cannot instantiate 'NotExported' - its constructor was not exported"],
  1286. ["MODULE m; IMPORT test; VAR p: POINTER TO test.NotExported; BEGIN NEW(p); END m.",
  1287. "cannot instantiate 'NotExported' - its constructor was not exported"],
  1288. ["MODULE m; IMPORT test; VAR p: POINTER TO test.NotExported; BEGIN p := NEW test.NotExported(); END m.",
  1289. "cannot instantiate 'NotExported' - its constructor was not exported"],
  1290. ["MODULE m; IMPORT test; VAR a: ARRAY 3 OF test.NotExported; END m.",
  1291. "cannot instantiate 'NotExported' - its constructor was not exported"]
  1292. )
  1293. )
  1294. },
  1295. "operator NEW": testWithContext(
  1296. context(grammar.expression,
  1297. "TYPE T = RECORD field: INTEGER; END; Proc = PROCEDURE();"
  1298. + "ParamCons = RECORD PROCEDURE ParamCons(i: INTEGER); END;"
  1299. + "Abstract = RECORD PROCEDURE abstract(); END;"
  1300. + "PROCEDURE ParamCons.ParamCons(i: INTEGER); END;"
  1301. + "PROCEDURE proc(); END;"
  1302. ),
  1303. pass("NEW T()",
  1304. "NEW ParamCons(123)",
  1305. "NEW T().field",
  1306. "NEW T()^"
  1307. ),
  1308. fail(["NEW INTEGER()", "record type is expected in operator NEW, got 'INTEGER'"],
  1309. ["NEW proc()", "record type is expected in operator NEW, got 'procedure'"],
  1310. ["NEW Proc()", "record type is expected in operator NEW, got 'Proc'"],
  1311. ["NEW T().unknownField", "type 'T' has no 'unknownField' field"],
  1312. ["NEW T(123)", "0 argument(s) expected, got 1"],
  1313. ["NEW Abstract()", "cannot instantiate 'Abstract' because it has abstract method(s): abstract"],
  1314. ["NEW undeclared()", "undeclared identifier: 'undeclared'"]
  1315. )
  1316. ),
  1317. "map": {
  1318. "declaration": testWithGrammar(
  1319. grammar.declarationSequence,
  1320. pass("TYPE M = MAP OF INTEGER;",
  1321. "TYPE M = MAP OF PROCEDURE;",
  1322. "TYPE M = MAP OF PROCEDURE();",
  1323. "TYPE M = MAP OF PROCEDURE(): INTEGER;",
  1324. "TYPE M = MAP OF PROCEDURE(): M;",
  1325. "TYPE M = MAP OF RECORD END;",
  1326. "TYPE M = MAP OF POINTER TO RECORD END;",
  1327. "TYPE M = MAP OF MAP OF INTEGER;",
  1328. "TYPE M = MAP OF ARRAY * OF INTEGER;",
  1329. "TYPE M = MAP OF M;",
  1330. "TYPE T = RECORD field: MAP OF T; END;",
  1331. "VAR v: MAP OF SET;"
  1332. ),
  1333. fail(["TYPE P = POINTER TO MAP OF INTEGER;", "RECORD is expected as a POINTER base type, got 'MAP OF INTEGER'"],
  1334. ["TYPE M = MAP OF Undeclared;", "undeclared identifier: 'Undeclared'"],
  1335. ["VAR MAP: INTEGER;", "not parsed"]
  1336. )
  1337. ),
  1338. "put": testWithContext(
  1339. context(grammar.statement,
  1340. "VAR m: MAP OF INTEGER;"
  1341. + "sIndex: STRING; aIndex: ARRAY 3 OF CHAR;"),
  1342. pass("m[\"abc\"] := 123",
  1343. "m[sIndex] := 123",
  1344. "m[aIndex] := 123"
  1345. ),
  1346. fail(["m[123] := 123", "invalid MAP key type: STRING or string literal or ARRAY OF CHAR expected, got 'INTEGER'"])
  1347. ),
  1348. "get": testWithContext(
  1349. context(grammar.expression,
  1350. "VAR m: MAP OF INTEGER;"
  1351. + "sIndex: STRING; aIndex: ARRAY 3 OF CHAR;"),
  1352. pass("m[\"abc\"]",
  1353. "m[sIndex]",
  1354. "m[aIndex]"
  1355. ),
  1356. fail(["m[123]", "invalid MAP key type: STRING or string literal or ARRAY OF CHAR expected, got 'INTEGER'"])
  1357. ),
  1358. "IN": testWithContext(
  1359. context(grammar.expression,
  1360. "VAR m: MAP OF INTEGER;"
  1361. + "sIndex: STRING; aIndex: ARRAY 3 OF CHAR;"),
  1362. pass("\"abc\" IN m",
  1363. "(\"abc\" IN m) = FALSE",
  1364. "sIndex IN m",
  1365. "aIndex IN m"
  1366. ),
  1367. fail(["123 IN m", "invalid MAP key type: STRING or string literal or ARRAY OF CHAR expected, got 'INTEGER'"])
  1368. ),
  1369. "non-VAR parameter": testWithContext(
  1370. context(grammar.declarationSequence,
  1371. "TYPE M = MAP OF INTEGER;"),
  1372. pass(),
  1373. fail(["PROCEDURE p(m: M); BEGIN m[\"abc\"] := 123; END;", "cannot assign to read-only variable"])
  1374. ),
  1375. "FOREACH": testWithContext(
  1376. context(grammar.statement,
  1377. "TYPE T = RECORD END;"
  1378. + "VAR m: MAP OF INTEGER; r: T;"),
  1379. pass("FOREACH v, k IN m DO END",
  1380. "FOREACH v, k IN m DO ASSERT(k # \"abc\"); END",
  1381. "FOREACH v, k IN m DO ASSERT(v # 123); END"
  1382. ),
  1383. fail(["FOREACH k, k IN m DO END", "'k' already declared"],
  1384. ["FOREACH m, k IN m DO END", "'m' already declared in module scope"],
  1385. ["FOREACH v, m IN m DO END", "'m' already declared in module scope"],
  1386. ["FOREACH v, k IN m DO k := \"\"; END", "cannot assign to FOREACH variable"],
  1387. ["FOREACH v, k IN m DO v := 0; END", "cannot assign to FOREACH variable"],
  1388. ["FOREACH v, k IN r DO END", "variable of type MAP is expected in FOREACH, got 'T'"],
  1389. ["FOREACH v, k IN T DO END", "variable of type MAP is expected in FOREACH, got 'type'"]
  1390. )
  1391. ),
  1392. "FOREACH scope": testWithContext(
  1393. context(grammar.declarationSequence,
  1394. "VAR m: MAP OF INTEGER;"),
  1395. pass(),
  1396. fail(["PROCEDURE p(); BEGIN FOREACH k, v IN m DO END; ASSERT(k # \"abc\"); END;", "undeclared identifier: 'k'"],
  1397. ["PROCEDURE p(); BEGIN FOREACH k, v IN m DO END; ASSERT(v # 123); END;", "undeclared identifier: 'v'"]
  1398. )
  1399. )
  1400. }
  1401. };