test_unit_eberon.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. "use strict";
  2. var Class = require("rtl.js").Class;
  3. var language = require("eberon/eberon_grammar.js").language;
  4. var TestUnitCommon = require("test_unit_common.js");
  5. var TypePromotion = require("eberon/eberon_type_promotion.js");
  6. var assert = TestUnitCommon.assert;
  7. var pass = TestUnitCommon.pass;
  8. var fail = TestUnitCommon.fail;
  9. var context = TestUnitCommon.context;
  10. var grammar = language.grammar;
  11. function testWithContext(context, pass, fail){
  12. return TestUnitCommon.testWithContext(context, grammar.declarationSequence, language, pass, fail);
  13. }
  14. function testWithModule(src, pass, fail){
  15. return TestUnitCommon.testWithModule(src, language, pass, fail);
  16. }
  17. function testWithGrammar(parser, pass, faile){
  18. return TestUnitCommon.testWithGrammar(parser, language, pass, fail);
  19. }
  20. var temporaryValues = {
  21. context: context(
  22. grammar.declarationSequence,
  23. "TYPE Base = RECORD END;"
  24. + "Derived = RECORD (Base) flag: BOOLEAN END;"
  25. + "Derived2 = RECORD (Derived) flag2: BOOLEAN END;"
  26. + "PBase = POINTER TO Base;"
  27. + "PDerived = POINTER TO Derived;"
  28. + "PDerived2 = POINTER TO Derived2;"
  29. + "VAR pBase: POINTER TO Base; bVar: BOOLEAN;"
  30. + "PROCEDURE proc(b: BOOLEAN): BOOLEAN; RETURN b END proc;"),
  31. __expression: function(e){
  32. return "PROCEDURE p(); BEGIN b <- pBase; b2 <- pBase; ASSERT(" + e + "); END p;";
  33. },
  34. __statement: function(e){
  35. return "PROCEDURE p(); BEGIN b <- pBase; b2 <- pBase; " + e + " END p;";
  36. },
  37. passExpressions: function(){
  38. return this.__pass(this.__expression.bind(this), arguments);
  39. },
  40. passStatements: function(){
  41. return this.__pass(this.__statement.bind(this), arguments);
  42. },
  43. failExpressions: function(){
  44. return this.__fail(this.__expression.bind(this), arguments);
  45. },
  46. failStatements: function(){
  47. return this.__fail(this.__statement.bind(this), arguments);
  48. },
  49. __pass: function(make, cases){
  50. var result = [];
  51. for(var i = 0; i < cases.length; ++i)
  52. result.push(make(cases[i]));
  53. return pass.apply(this, result);
  54. },
  55. __fail: function(make, cases){
  56. var result = [];
  57. for(var i = 0; i < cases.length; ++i)
  58. result.push([make(cases[i]), "type 'Base' has no 'flag' field"]);
  59. return fail.apply(this, result);
  60. }
  61. };
  62. var TestVar = Class.extend({
  63. init: function(){
  64. this.__type = "type";
  65. },
  66. type: function(){return this.__type;},
  67. setType: function(type){this.__type = type;}
  68. });
  69. exports.suite = {
  70. "arithmetic operators": testWithContext(
  71. context(grammar.statement, "VAR b1: BOOLEAN;"),
  72. pass(),
  73. fail(["b1 := b1 + b1", "operator '+' type mismatch: numeric type or SET or STRING expected, got 'BOOLEAN'"])
  74. ),
  75. "key words": testWithGrammar(
  76. grammar.variableDeclaration,
  77. pass(),
  78. fail(["SELF: INTEGER", "not parsed"],
  79. ["SUPER: INTEGER", "not parsed"],
  80. ["STRING: INTEGER", "not parsed"]
  81. )
  82. ),
  83. "abstract method declaration": testWithContext(
  84. context(grammar.declarationSequence,
  85. "TYPE T = RECORD PROCEDURE p() END;"
  86. + "D = RECORD(T) END;"
  87. + "T2 = RECORD PROCEDURE p1(); PROCEDURE p2(i: INTEGER): BOOLEAN END;"
  88. ),
  89. pass(),
  90. fail(["VAR r: T;",
  91. "cannot instantiate 'T' because it has abstract method(s): p"],
  92. ["VAR r: T2;",
  93. "cannot instantiate 'T2' because it has abstract method(s): p1, p2"],
  94. ["PROCEDURE p(); VAR p: POINTER TO T; BEGIN NEW(p); END p;",
  95. "cannot instantiate 'T' because it has abstract method(s): p"],
  96. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR r: LocalT; END p;",
  97. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  98. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR p: POINTER TO LocalT; BEGIN NEW(p) END p;",
  99. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  100. ["VAR r: D;",
  101. "cannot instantiate 'D' because it has abstract method(s): p"],
  102. ["PROCEDURE p(); VAR p: POINTER TO D; BEGIN NEW(p); END p;",
  103. "cannot instantiate 'D' because it has abstract method(s): p"]
  104. )
  105. ),
  106. "new method declaration": testWithContext(
  107. context(grammar.declarationSequence,
  108. "TYPE T = RECORD PROCEDURE p(); intField: INTEGER END; A = ARRAY 1 OF INTEGER;"),
  109. pass("PROCEDURE T.p(); END T.p;"
  110. ),
  111. fail(["PROCEDURE TUnk.p(), NEW; END TUnk.p;", "undeclared identifier: 'TUnk'"],
  112. ["PROCEDURE A.p(), NEW; END A.p;",
  113. "RECORD type expected in method declaration, got 'ARRAY 1 OF INTEGER'"],
  114. ["PROCEDURE T.p(), NEW; END;", "not parsed"],
  115. ["PROCEDURE T.p(); END p;",
  116. "mismatched procedure names: 'T.p' at the begining and 'p.' at the end"],
  117. ["PROCEDURE T.p(); END T2.p;",
  118. "mismatched procedure names: 'T.p' at the begining and 'T2.p' at the end"],
  119. ["PROCEDURE T.p(); END T.p2;",
  120. "mismatched procedure names: 'T.p' at the begining and 'T.p2' at the end"],
  121. ["PROCEDURE T.intField(); END T.intField;",
  122. "'T' has no declaration for method 'intField'"],
  123. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(), NEW; END T.p;",
  124. "'T.p' already declared"],
  125. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); PROCEDURE m() END; END p;",
  126. "cannot declare a new method 'm': method already was declared"],
  127. ["PROCEDURE p(); TYPE T = RECORD m: INTEGER; PROCEDURE m() END; END p;",
  128. "cannot declare method, record already has field 'm'"],
  129. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); m: INTEGER END; END p;",
  130. "cannot declare field, record already has method 'm'"]
  131. )
  132. ),
  133. "overridden method declaration": testWithContext(
  134. context(grammar.declarationSequence,
  135. "TYPE Base = RECORD PROCEDURE p() END; T = RECORD (Base) END;"
  136. + "PROCEDURE Base.p(); END Base.p;"),
  137. pass("PROCEDURE T.p(); END T.p;"),
  138. fail(["PROCEDURE T.pUnk(); END T.pUnk;",
  139. "'T' has no declaration for method 'pUnk'"],
  140. ["PROCEDURE proc(); TYPE T = RECORD (Base) PROCEDURE p() END; END proc;",
  141. "cannot declare a new method 'p': method already was declared"],
  142. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(); END T.p;",
  143. "'T.p' already declared"],
  144. ["PROCEDURE T.p(a: INTEGER); END T.p;",
  145. "overridden method 'p' signature mismatch: should be 'PROCEDURE', got 'PROCEDURE(INTEGER)'"],
  146. ["PROCEDURE p(); PROCEDURE T.p(); END T.p; END p;",
  147. "method should be defined in the same scope as its bound type 'T'"]
  148. )
  149. ),
  150. "SELF": testWithContext(
  151. context(grammar.declarationSequence,
  152. "TYPE T = RECORD PROCEDURE p(); i: INTEGER END;"
  153. + "PROCEDURE proc(i: INTEGER); END proc;"),
  154. pass("PROCEDURE T.p(); BEGIN SELF.i := 0; END T.p;",
  155. "PROCEDURE T.p(); BEGIN proc(SELF.i); END T.p;"
  156. ),
  157. fail(["PROCEDURE p(); BEGIN SELF.i := 0; END p;",
  158. "SELF can be used only in methods"])
  159. ),
  160. "SELF as pointer": testWithContext(
  161. context(grammar.declarationSequence,
  162. "TYPE T = RECORD PROCEDURE method() END;"
  163. + "PT = POINTER TO T;"
  164. + "VAR pVar: PT;"
  165. + "PROCEDURE refProc(VAR p: PT); END refProc;"
  166. ),
  167. pass("PROCEDURE T.method(); BEGIN pVar := SELF(POINTER) END T.method;",
  168. "PROCEDURE p();"
  169. + "TYPE Derived = RECORD(T) END; VAR pd: POINTER TO Derived;"
  170. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN NEW(pd); pVar := SELF(POINTER); END Derived.method;"
  171. + "END p;"),
  172. fail(["PROCEDURE T.method(); BEGIN refProc(SELF(POINTER)) END T.method;",
  173. "read-only variable cannot be used as VAR parameter"],
  174. ["PROCEDURE T.method(); BEGIN SELF(POINTER) := pVar; END T.method;",
  175. "cannot assign to read-only variable"],
  176. ["PROCEDURE p();"
  177. + "TYPE Derived = RECORD(T) END; VAR d: Derived;"
  178. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN pVar := SELF(POINTER); END Derived.method;"
  179. + "END p;",
  180. "cannot declare a variable of type 'Derived' (and derived types) because SELF(POINTER) was used in its method(s)"],
  181. ["PROCEDURE p();"
  182. + "TYPE Derived = RECORD(T) END; Derived2 = RECORD(Derived) END;"
  183. + "VAR d: Derived2;"
  184. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN pVar := SELF(POINTER); END Derived.method;"
  185. + "END p;",
  186. "cannot declare a variable of type 'Derived' (and derived types) because SELF(POINTER) was used in its method(s)"]
  187. )
  188. ),
  189. "method call": testWithContext(
  190. context(grammar.expression,
  191. "TYPE T = RECORD PROCEDURE p(); PROCEDURE f(): INTEGER END;"
  192. + "VAR o: T;"
  193. + "PROCEDURE T.p(); END T.p;"
  194. + "PROCEDURE T.f(): INTEGER; RETURN 0 END T.f;"
  195. ),
  196. pass("o.f()"),
  197. fail(["o.p()", "procedure returning no result cannot be used in an expression"])
  198. ),
  199. "cannot assign to method": testWithContext(
  200. context(grammar.statement,
  201. "TYPE T = RECORD PROCEDURE p() END;"
  202. + "VAR o: T;"
  203. + "PROCEDURE T.p(); END T.p;"
  204. ),
  205. pass(),
  206. fail(["o.p := o.p", "cannot assign to method"],
  207. ["o.p := NIL", "cannot assign to method"])
  208. ),
  209. "method cannot be referenced": testWithContext(
  210. context(grammar.statement,
  211. "TYPE T = RECORD PROCEDURE p() END;"
  212. + "Proc = PROCEDURE();"
  213. + "VAR o: T;"
  214. + "PROCEDURE T.p(); END T.p;"
  215. + "PROCEDURE proc(p: Proc); END proc;"
  216. ),
  217. pass(),
  218. fail(["proc(o.p)", "type mismatch for argument 1: 'method p' cannot be converted to 'Proc'"])
  219. ),
  220. "method super call": testWithContext(
  221. context(grammar.declarationSequence,
  222. "TYPE T = RECORD PROCEDURE p(); PROCEDURE pAbstract(); PROCEDURE pAbstract2() END;"
  223. + "D = RECORD(T) PROCEDURE pNoSuper() END;"
  224. + "PROCEDURE T.p(); END T.p;"
  225. ),
  226. pass("PROCEDURE D.p(); BEGIN SUPER() END D.p;"),
  227. fail(["PROCEDURE D.pNoSuper(); BEGIN SUPER() END D.pNoSuper;",
  228. "there is no method 'pNoSuper' in base type(s)"],
  229. ["PROCEDURE p(); BEGIN SUPER() END p;",
  230. "SUPER can be used only in methods"],
  231. ["PROCEDURE T.pNoBase(); BEGIN SUPER() END T.pNoBase;",
  232. "'T' has no base type - SUPER cannot be used"],
  233. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract;",
  234. "cannot use abstract method(s) in SUPER calls: pAbstract"],
  235. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract; PROCEDURE D.pAbstract2(); BEGIN SUPER() END D.pAbstract2;",
  236. "cannot use abstract method(s) in SUPER calls: pAbstract, pAbstract2"]
  237. )
  238. ),
  239. "export method": testWithContext(
  240. context(grammar.declarationSequence,
  241. "TYPE T = RECORD PROCEDURE p() END;"
  242. ),
  243. pass(),
  244. fail(["PROCEDURE T.p*(); END T.p;",
  245. "method implementation cannot be exported: p"])
  246. ),
  247. "import method": testWithModule(
  248. "MODULE test;"
  249. + "TYPE T* = RECORD PROCEDURE m*(); PROCEDURE mNotExported() END;"
  250. + "PROCEDURE T.m(); END T.m; PROCEDURE T.mNotExported(); END T.mNotExported;"
  251. + "END test.",
  252. pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.m(); END m.",
  253. "MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.m(); END T.m; END m."
  254. ),
  255. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.mNotExported(); END m.",
  256. "type 'T' has no 'mNotExported' field"],
  257. ["MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.mNotExported(); END T.mNotExported; END m.",
  258. "'T' has no declaration for method 'mNotExported'"])
  259. ),
  260. "non-scalar variables can be exported": testWithContext(
  261. context(grammar.declarationSequence,
  262. "TYPE T = RECORD END; A = ARRAY 3 OF INTEGER;"
  263. ),
  264. pass("VAR r*: T;",
  265. "VAR a*: A;"),
  266. fail()
  267. ),
  268. "export as read-only": testWithContext(
  269. context(grammar.declarationSequence, ""),
  270. pass("TYPE T* = RECORD i-: INTEGER END;"),
  271. fail(["TYPE T- = RECORD END;",
  272. "type cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  273. ["PROCEDURE p-(); END p;",
  274. "procedure cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  275. ["CONST c- = 123;",
  276. "constant cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  277. ["VAR i-: INTEGER;",
  278. "variable cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  279. ["TYPE T* = RECORD PROCEDURE p-() END;",
  280. "method cannot be exported as read-only using '-' mark (did you mean '*'?)"]
  281. )
  282. ),
  283. "field exported as read-only is writable in current module": testWithContext(
  284. context(grammar.statement,
  285. "TYPE T* = RECORD i-: INTEGER END;"
  286. + "VAR r: T;"
  287. ),
  288. pass("r.i := 123"),
  289. fail()
  290. ),
  291. "import as read-only": testWithModule(
  292. "MODULE test; TYPE T* = RECORD f-: INTEGER END; END test.",
  293. pass(),
  294. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 123; END m.",
  295. "cannot assign to read-only variable"],
  296. ["MODULE m; IMPORT test; TYPE D = RECORD(test.T) END; VAR r: D; BEGIN r.f := 123; END m.",
  297. "cannot assign to read-only variable"]
  298. )),
  299. "STRING variable": testWithGrammar(
  300. grammar.variableDeclaration,
  301. pass("s: STRING")
  302. ),
  303. "STRING expression": testWithContext(
  304. context(grammar.expression,
  305. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  306. pass("s1 + s2",
  307. "s1 + \"abc\"",
  308. "\"abc\" + s1",
  309. "s1 = s2",
  310. "s1 = \"abc\"",
  311. "s1 = 22X",
  312. "\"abc\" = s1",
  313. "22X = s1",
  314. "s1 # s2",
  315. "s1 # \"abc\"",
  316. "s1 # 22X",
  317. "\"abc\" # s1",
  318. "22X # s1",
  319. "s1 < s2",
  320. "s1 < \"abc\"",
  321. "s1 < 22X",
  322. "\"abc\" < s1",
  323. "22X < s1",
  324. "s1 > s2",
  325. "s1 > \"abc\"",
  326. "s1 > 22X",
  327. "\"abc\" > s1",
  328. "22X > s1",
  329. "s1 <= s2",
  330. "s1 <= \"abc\"",
  331. "\"abc\" <=s1",
  332. "22X <= s1",
  333. "s1 >= \"abc\"",
  334. "s1 >= 22X",
  335. "s1 >= s2",
  336. "\"abc\" >= s1",
  337. "22X >= s1"
  338. ),
  339. fail(["s1 = NIL", "type mismatch: expected 'STRING', got 'NIL'"],
  340. ["s1 = a", "type mismatch: expected 'STRING', got 'ARRAY 10 OF CHAR'"],
  341. ["a = s1", "type mismatch: expected 'ARRAY 10 OF CHAR', got 'STRING'"]
  342. )
  343. ),
  344. "STRING literal expression": testWithContext(
  345. context(grammar.expression,
  346. "CONST cs = \"abc\";"
  347. + "PROCEDURE pString(s: STRING): STRING; RETURN s END pString;"
  348. + "PROCEDURE pStringByRef(VAR s: STRING): STRING; RETURN s END pStringByRef;"
  349. ),
  350. pass("\"abc\" + \"cde\"",
  351. "cs + cs",
  352. "cs + \"abc\"",
  353. "cs = \"abc\"",
  354. "cs # \"abc\"",
  355. "cs < \"abc\"",
  356. "cs > \"abc\"",
  357. "cs <= \"abc\"",
  358. "cs >= \"abc\"",
  359. "pString(cs)",
  360. "pString(\"abc\")"
  361. ),
  362. fail(["pStringByRef(cs)", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"],
  363. ["pStringByRef(\"abc\")", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"]
  364. )
  365. ),
  366. "STRING assignment": testWithContext(
  367. context(grammar.statement,
  368. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  369. pass("s1 := s2",
  370. "s1 := \"abc\"",
  371. "s1 := 22X"
  372. ),
  373. fail(["a := s1", "type mismatch: 'a' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'STRING' expression"],
  374. ["s1 := a", "type mismatch: 's1' is 'STRING' and cannot be assigned to 'ARRAY 10 OF CHAR' expression"]
  375. )
  376. ),
  377. "STRING and ARRAY OF CHAR": testWithContext(
  378. context(grammar.expression,
  379. "VAR s: STRING; a: ARRAY 10 OF CHAR;"
  380. + "PROCEDURE pArray(a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pArray;"
  381. + "PROCEDURE pString(s: STRING): BOOLEAN; RETURN FALSE END pString;"
  382. + "PROCEDURE pVar(VAR a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pVar;"
  383. + "PROCEDURE pIntArray(a: ARRAY OF INTEGER): BOOLEAN; RETURN FALSE END pIntArray;"
  384. ),
  385. pass("pArray(s)"),
  386. fail(["pVar(s)", "type mismatch for argument 1: cannot pass 'STRING' as VAR parameter of type 'ARRAY OF CHAR'"],
  387. ["pString(a)", "type mismatch for argument 1: 'ARRAY 10 OF CHAR' cannot be converted to 'STRING'"],
  388. ["pIntArray(s)", "type mismatch for argument 1: 'STRING' cannot be converted to 'ARRAY OF INTEGER'"]
  389. )
  390. ),
  391. "STRING LEN": testWithContext(
  392. context(grammar.expression,
  393. "VAR s: STRING;"),
  394. pass("LEN(s)"),
  395. fail()
  396. ),
  397. "STRING indexing": testWithContext(
  398. context(grammar.expression,
  399. "VAR s: STRING;"
  400. + "PROCEDURE pCharByVar(VAR c: CHAR): CHAR; RETURN c END pCharByVar;"),
  401. pass("s[0]"),
  402. fail(["s[-1]", "index is negative: -1"],
  403. ["pCharByVar(s[0])", "string element cannot be used as VAR parameter"]
  404. )
  405. ),
  406. "designate call result in expression": testWithContext(
  407. context(grammar.expression,
  408. "TYPE PT = POINTER TO RECORD field: INTEGER END;"
  409. + "VAR p: PT;"
  410. + "PROCEDURE proc(): PT; RETURN p END proc;"
  411. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  412. + "PROCEDURE intVar(VAR i: INTEGER): INTEGER; RETURN i END intVar;"),
  413. pass("proc().field",
  414. "intVar(proc().field)"),
  415. fail(["intVar(int())", "expression cannot be used as VAR parameter"])
  416. ),
  417. "designate call result in statement": testWithContext(
  418. context(grammar.statement,
  419. "TYPE PT = POINTER TO RECORD field: INTEGER; proc: PROCEDURE END;"
  420. + "ProcType = PROCEDURE;"
  421. + "VAR p: PT;"
  422. + "PROCEDURE procVoid(); END procVoid;"
  423. + "PROCEDURE proc(): PT; RETURN p END proc;"
  424. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  425. + "PROCEDURE intVar(VAR i: INTEGER); END intVar;"
  426. + "PROCEDURE returnProc(): ProcType; RETURN procVoid END returnProc;"
  427. ),
  428. pass("proc().field := 0",
  429. "proc().proc()",
  430. "proc().proc"
  431. ),
  432. fail(["int() := 0", "cannot assign to procedure call result"],
  433. ["intVar(int())", "expression cannot be used as VAR parameter"],
  434. ["procVoid()()", "PROCEDURE expected, got 'procedure call statement'"],
  435. ["int()()", "PROCEDURE expected, got 'INTEGER'"],
  436. ["returnProc()", "procedure returning a result cannot be used as a statement"] // call is not applied implicitly to result
  437. )
  438. ),
  439. "type promotion": {
  440. "or" : pass(
  441. function(){
  442. var or = new TypePromotion.OrPromotions();
  443. var a = new TestVar();
  444. var p = or.next();
  445. assert(a.type() == "type");
  446. p.invert();
  447. p.promote(a, "type1");
  448. assert(a.type() == "type");
  449. or.next();
  450. assert(a.type() == "type1");
  451. or.reset();
  452. assert(a.type() == "type");
  453. },
  454. function(){
  455. var or = new TypePromotion.OrPromotions();
  456. var a = new TestVar();
  457. var p = or.next(p);
  458. p.promote(a, "type1");
  459. or.next();
  460. assert(a.type() == "type");
  461. },
  462. function(){
  463. var or = new TypePromotion.OrPromotions();
  464. var a = new TestVar();
  465. var p1 = or.next();
  466. p1.promote(a, "type1");
  467. var p2 = or.next();
  468. p2.invert();
  469. p2.promote(a, "type2");
  470. assert(a.type() == "type");
  471. assert(a.type() == "type");
  472. or.next();
  473. assert(a.type() == "type2");
  474. or.reset();
  475. assert(a.type() == "type");
  476. }
  477. ),
  478. "and": pass(
  479. function(){
  480. var and = new TypePromotion.AndPromotions();
  481. var a = new TestVar();
  482. var p = and.next();
  483. p.promote(a, "type1");
  484. and.next();
  485. assert(a.type() == "type1");
  486. and.reset();
  487. assert(a.type() == "type");
  488. },
  489. function(){ // (a IS type1) & (v OR (a IS type2)) & v
  490. var and = new TypePromotion.AndPromotions();
  491. var a = new TestVar();
  492. var p = and.next();
  493. p.promote(a, "type1");
  494. var subOr = and.next().makeOr();
  495. subOr.next();
  496. subOr.next().promote(a, "type2");
  497. and.next();
  498. assert(a.type() == "type1");
  499. and.reset();
  500. assert(a.type() == "type");
  501. },
  502. function(){ // (a IS type1) & ~(v OR ~(a IS type2)) & v
  503. var and = new TypePromotion.AndPromotions();
  504. var a = new TestVar();
  505. and.next().promote(a, "type1");
  506. var subOr = and.next();
  507. subOr.invert();
  508. subOr = subOr.makeOr();
  509. subOr.next();
  510. var p = subOr.next();
  511. p.invert();
  512. p.promote(a, "type2");
  513. and.next();
  514. assert(a.type() == "type2");
  515. and.reset();
  516. assert(a.type() == "type");
  517. },
  518. function(){ // (a IS type1) & (v & (a IS type2))
  519. var and = new TypePromotion.AndPromotions();
  520. var a = new TestVar();
  521. and.next().promote(a, "type1");
  522. var sub = and.next().makeAnd();
  523. sub.next();
  524. assert(a.type() == "type1");
  525. sub.next().promote(a, "type2");
  526. assert(a.type() == "type1");
  527. and.and();
  528. assert(a.type() == "type2");
  529. and.or();
  530. assert(a.type() == "type");
  531. },
  532. function(){ // (~(~(a IS type1)) & v) OR v
  533. var a = new TestVar();
  534. var or = new TypePromotion.OrPromotions();
  535. var and = or.next().makeAnd();
  536. var p1 = and.next();
  537. p1.invert();
  538. var p2 = p1.makeOr().next().makeAnd().next();
  539. p2.invert();
  540. p2.promote(a, "type1");
  541. and.next();
  542. assert(a.type() == "type1");
  543. or.next();
  544. assert(a.type() == "type");
  545. },
  546. function(){ // (v OR (a IS type1)) & v)
  547. var a = new TestVar();
  548. var and = new TypePromotion.AndPromotions();
  549. var or = and.next().makeOr();
  550. or.next();
  551. or.next().makeAnd().next().promote(a, "type1");
  552. and.next();
  553. assert(a.type() == "type");
  554. }
  555. )
  556. },
  557. "in place variables": {
  558. "initialization": testWithContext(
  559. context(grammar.statement,
  560. "VAR i: INTEGER;"
  561. + "PROCEDURE p(): BOOLEAN; RETURN FALSE END p;"
  562. + "PROCEDURE void(); END void;"
  563. ),
  564. pass("v <- 0",
  565. "v <- 1.23",
  566. "v <- \"abc\"",
  567. "v <- TRUE",
  568. "v <- i",
  569. "v <- i + i",
  570. "v <- p()",
  571. "v <- void" // procedure type
  572. ),
  573. fail(["v <-", "initialization expression expected"],
  574. ["v <- void()", "procedure returning no result cannot be used in an expression"],
  575. ["v <- NIL", "cannot use NIL to initialize variable"])
  576. ),
  577. "read-only if initialized with string literal": testWithContext(
  578. context(grammar.declarationSequence, ""),
  579. pass(),
  580. fail(["PROCEDURE p(); BEGIN s <- \"abc\"; s := \"def\"; END p;", "cannot assign to string literal"],
  581. ["PROCEDURE p(); BEGIN s <- \"abc\"; s[0] := \"d\"; END p;", "cannot assign to read-only variable"])
  582. ),
  583. "scope": testWithContext(
  584. temporaryValues.context,
  585. temporaryValues.passStatements(
  586. "v1 <- 0; v2 <-0;",
  587. "i <- 0; ASSERT(i = 0);",
  588. "WHILE FALSE DO v <- 0; ASSERT(v = 0); END; WHILE FALSE DO v <- 0; END;",
  589. "WHILE FALSE DO i1 <- 0; WHILE FALSE DO i2 <- 0; ASSERT(i1 = 0); ASSERT(i2 = 0); END; END;",
  590. "WHILE bVar DO v <- 0; ELSIF ~bVar DO v <- 0 END;",
  591. "IF FALSE THEN v <- 0; ASSERT(v = 0); END; IF FALSE THEN v <- 0; END;",
  592. "IF FALSE THEN v <- 0; END; IF FALSE THEN v <- 0; END;",
  593. "IF FALSE THEN v <- 0; ELSIF FALSE THEN v <- 0; ELSE v <- 0; END;",
  594. "i <- 0; CASE i OF 0: v <- 0 | 1: v <- 1; ; ASSERT(v = 1); END;",
  595. "REPEAT v <- 0; UNTIL FALSE; REPEAT v <- 0; UNTIL FALSE;",
  596. "REPEAT v <- 0; ASSERT(v = 0); UNTIL v # 0;",
  597. "i <- 0; FOR i := 0 TO 10 DO v <- 0; END; FOR i := 0 TO 10 DO v <- 0; END;"
  598. ),
  599. fail(["PROCEDURE p(); BEGIN v <- 0; v <-0; END p;", "'v' already declared"],
  600. ["PROCEDURE p(); VAR v: INTEGER; BEGIN v <- 0; END p;", "'v' already declared"],
  601. ["PROCEDURE p(); BEGIN v <- 0; WHILE FALSE DO v <- 0; END; END p;",
  602. "'v' already declared in procedure scope"],
  603. ["PROCEDURE p(); BEGIN i <- 0; IF FALSE THEN i <- 0; END; END p;",
  604. "'i' already declared in procedure scope"],
  605. ["PROCEDURE p(); BEGIN i <- 0; IF TRUE THEN IF TRUE THEN i <- 0; END; END; END p;",
  606. "'i' already declared in procedure scope"],
  607. ["PROCEDURE p(); BEGIN WHILE FALSE DO i <- 0; WHILE FALSE DO i <- 0; END; END; END p;",
  608. "'i' already declared in operator scope"]
  609. )
  610. ),
  611. "type promotion in expression": testWithContext(
  612. temporaryValues.context,
  613. temporaryValues.passExpressions(
  614. "(b IS PDerived) & b.flag",
  615. "(b IS PDerived) & bVar & b.flag",
  616. "(b IS PDerived) & (bVar OR b.flag)",
  617. "(b IS PDerived) & (b2 IS PDerived) & b.flag & b2.flag",
  618. "(b IS PDerived) & proc(TRUE) & b.flag",
  619. "(b IS PDerived) & ~proc(TRUE) & b.flag",
  620. "~(~(b IS PDerived)) & b.flag",
  621. "~~(b IS PDerived) & b.flag",
  622. "(b IS PDerived) & ((b IS PDerived2) OR bVar) & b.flag",
  623. "(b IS PDerived) & (bVar OR (b IS PDerived2)) & b.flag",
  624. "(b IS PDerived) & ~(bVar OR ~(b IS PDerived2)) & b.flag2",
  625. "~(bVar & (b IS PDerived)) OR b.flag"
  626. //TODO: "((b IS PDerived) = TRUE) & b.flag); END p;",
  627. ),
  628. temporaryValues.failExpressions(
  629. "(b IS PDerived) OR b.flag",
  630. "(bVar OR (b IS PDerived)) & b.flag",
  631. "(b IS PDerived) OR bVar & b.flag",
  632. "~(b IS PDerived) & b.flag",
  633. "((b IS PDerived) & (b2 IS PDerived) OR bVar) & b.flag",
  634. "proc(b IS PDerived) & proc(b.flag)",
  635. "ORD(b IS PDerived) * ORD(b.flag) = 0",
  636. "((b IS PDerived) = FALSE) & b.flag",
  637. "((b IS PDerived) & bVar) = b.flag",
  638. "b IS PDerived); ASSERT(b.flag",
  639. "((b IS PDerived) OR (b IS PDerived)) & b.flag",
  640. "(b IS PDerived) OR (b IS PDerived) OR b.flag",
  641. "(bVar OR (b IS PDerived)) & b.flag",
  642. "~(bVar & ~(b IS PDerived)) & b.flag"
  643. )
  644. ),
  645. "invert type promotion in expression": testWithContext(
  646. temporaryValues.context,
  647. temporaryValues.passExpressions(
  648. "~(b IS PDerived) OR b.flag",
  649. "~(b IS PDerived) OR b.flag OR bVar",
  650. "~(b IS PDerived) OR b.flag & bVar",
  651. "~(b IS PDerived) OR bVar & b.flag",
  652. "~(b IS PDerived) OR (bVar & b.flag)",
  653. "~(b IS PDerived) OR bVar OR b.flag",
  654. "~(b IS PDerived) OR (bVar = b.flag)",
  655. "~(~(b IS PDerived) OR bVar) & b.flag",
  656. "~(~(b IS PDerived) OR b.flag) & b.flag",
  657. "~(b IS PDerived) OR ~(b2 IS PDerived) OR b2.flag",
  658. "~(b IS PDerived) OR b.flag OR ~(b2 IS PDerived) OR b2.flag",
  659. "~((b IS PDerived) & b.flag) OR b.flag OR ~(b2 IS PDerived) OR b2.flag",
  660. "~((b IS PDerived) & b.flag) OR b.flag OR ~((b2 IS PDerived) & b.flag & b2.flag) OR b2.flag"
  661. ),
  662. temporaryValues.failExpressions(
  663. "(~(b IS PDerived) OR bVar) & b.flag",
  664. "(ORD(~(b IS PDerived)) + ORD(b.flag)",
  665. "~(~(b IS PDerived) OR bVar) OR b.flag",
  666. "~(~(b IS PDerived) & bVar) & b.flag",
  667. "~(b IS PDerived) OR b.flag = b.flag"
  668. )
  669. ),
  670. "type promotion in separate statements": testWithContext(
  671. temporaryValues.context,
  672. pass(),
  673. temporaryValues.failStatements(
  674. "bVar := b IS PDerived; ASSERT(b.flag)",
  675. "bVar := (b IS PDerived) & bVar; ASSERT(b.flag)"
  676. )
  677. ),
  678. "type promotion in IF": testWithContext(
  679. temporaryValues.context,
  680. temporaryValues.passStatements(
  681. "IF b IS PDerived THEN b.flag := FALSE; END;",
  682. "IF (b IS PDerived) & bVar THEN b.flag := FALSE; END;",
  683. "IF bVar & (b IS PDerived) THEN b.flag := FALSE; END;",
  684. "IF FALSE THEN ELSIF b IS PDerived THEN b.flag := FALSE; END;",
  685. "IF b IS PDerived THEN bVar := (b IS PDerived2) & b.flag2; b.flag := FALSE; END;",
  686. "IF bVar THEN ELSIF b IS PDerived2 THEN ELSIF b IS PDerived THEN END;",
  687. "IF bVar THEN ELSIF b IS PDerived THEN ELSIF b IS PDerived THEN ELSIF b IS PDerived THEN END;",
  688. "IF b IS PDerived THEN IF bVar OR (b IS PDerived2) THEN b.flag := FALSE; END; END"
  689. ),
  690. temporaryValues.failStatements(
  691. "IF (b IS PDerived) OR bVar THEN b.flag := FALSE; END",
  692. "IF bVar OR (b IS PDerived) THEN b.flag := FALSE; END",
  693. "IF (b2 IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  694. "IF (b IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  695. "IF (b IS PDerived) OR (b IS PDerived) OR b.flag THEN END",
  696. "IF (b IS PDerived) OR (b IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  697. "IF ((b IS PDerived) OR (b IS PDerived)) THEN b.flag := FALSE; END",
  698. "IF (b IS PDerived) OR (b IS PDerived2) THEN b.flag := FALSE; END",
  699. "IF (b IS PDerived2) OR (b IS PDerived) THEN b.flag := FALSE; END",
  700. "IF b IS PDerived THEN END; b.flag := FALSE",
  701. "IF ~(b IS PDerived) THEN END; b.flag := FALSE",
  702. "IF ~(b IS PDerived) THEN ELSIF bVar THEN END; b.flag := FALSE",
  703. "IF ~(b IS PDerived) THEN ELSIF bVar THEN ELSE END; b.flag := FALSE",
  704. "IF bVar THEN ELSIF b IS PDerived THEN ELSE END; b.flag := FALSE",
  705. "IF b IS PDerived THEN ELSE b.flag := FALSE; END",
  706. "IF bVar OR (b IS PDerived) THEN b.flag := FALSE; END;",
  707. "IF bVar OR (b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  708. "IF bVar OR ~(b IS PDerived) THEN b.flag := FALSE; END;",
  709. "IF b IS PDerived THEN ELSIF TRUE THEN b.flag := FALSE; END",
  710. "IF bVar THEN bVar := (b IS PDerived) & bVar; ASSERT(b.flag); END"
  711. )
  712. ),
  713. "invert type promotion in IF": testWithContext(
  714. temporaryValues.context,
  715. temporaryValues.passStatements(
  716. "IF ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  717. "IF ~(b IS PDerived) THEN ELSIF bVar THEN b.flag := FALSE; ELSE b.flag := FALSE; END;",
  718. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b.flag := FALSE; ELSE b.flag := FALSE; b2.flag := FALSE; END;",
  719. "IF ~(b IS PDerived) OR bVar THEN ELSE b.flag := FALSE; END;",
  720. "IF ~(b IS PDerived) OR b.flag THEN ELSE b.flag := FALSE; END;",
  721. "IF ~(b IS PDerived) OR (b2 IS PDerived) THEN ELSE b.flag := FALSE; END;",
  722. "IF ~(b IS PDerived) OR ~(b2 IS PDerived) THEN ELSE b2.flag := FALSE; END;",
  723. "IF ~(b IS PDerived) THEN bVar := b IS PDerived; ELSE b.flag := FALSE; END;",
  724. "IF ~(b IS PDerived) THEN ASSERT((b IS PDerived) & b.flag); ELSE b.flag := FALSE; END;",
  725. "IF bVar OR ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;"
  726. ),
  727. temporaryValues.failStatements(
  728. "IF ~(b IS PDerived) & bVar THEN ELSE b.flag := FALSE; END; END p;",
  729. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b2.flag := FALSE; END;",
  730. "IF bVar OR (b IS PDerived) THEN ELSE b.flag := FALSE; END;"
  731. )
  732. ),
  733. "type promotion in WHILE": testWithContext(
  734. temporaryValues.context,
  735. temporaryValues.passStatements(
  736. "WHILE (b IS PDerived) & b.flag DO END;",
  737. "WHILE ~(b IS PDerived) OR b.flag DO END;",
  738. "WHILE b IS PDerived DO b.flag := FALSE; END;",
  739. "WHILE ~(b IS PDerived) DO ELSIF b.flag DO END;",
  740. "WHILE ~(b IS PDerived) DO ELSIF bVar DO b.flag := FALSE; END;"
  741. ),
  742. temporaryValues.failStatements(
  743. "WHILE b IS PDerived DO END; b.flag := FALSE;"
  744. )
  745. ),
  746. "type promotion cannot be reset by assignment": testWithContext(
  747. temporaryValues.context,
  748. pass(),
  749. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN b := pBase; b.flag := FALSE; END; END p;",
  750. "type mismatch: 'b' is 'PDerived' and cannot be assigned to 'POINTER TO Base' expression"]
  751. )
  752. ),
  753. "type promotion cannot be reset by passing as VAR argument": testWithContext(
  754. temporaryValues.context,
  755. pass(),
  756. 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;",
  757. "type mismatch for argument 1: cannot pass 'PDerived' as VAR parameter of type 'PBase'"]
  758. )
  759. ),
  760. "type promotion after dereferencing": testWithContext(
  761. temporaryValues.context,
  762. temporaryValues.passExpressions(
  763. "(b^ IS Derived) & b.flag")
  764. ),
  765. "IS expression after type promotion": testWithContext(
  766. temporaryValues.context,
  767. pass(),
  768. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN bVar := b IS PDerived; b.flag := FALSE; END; END p;",
  769. "invalid type test: 'Derived' is not an extension of 'Derived'"]
  770. )
  771. ),
  772. "record types as values": testWithContext(
  773. context(grammar.declarationSequence,
  774. "TYPE Base = RECORD pBase: POINTER TO Base END;"
  775. + "Derived = RECORD (Base) END;"
  776. + "VAR base: Base;"
  777. + "PROCEDURE procBaseVar(VAR b: Base); END procBaseVar;"
  778. ),
  779. pass("PROCEDURE p(b: Base); BEGIN base <- b; procBaseVar(base); base := b; END p;"),
  780. fail(["PROCEDURE p(); BEGIN baseVar <- base.pBase^; ASSERT(base IS Derived); END p;",
  781. "invalid type test: a value variable cannot be used"],
  782. ["PROCEDURE p(VAR b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  783. "invalid type test: a value variable cannot be used"],
  784. ["PROCEDURE p(b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  785. "invalid type test: a value variable cannot be used"],
  786. ["PROCEDURE p(); TYPE Abstract = RECORD PROCEDURE method() END; PROCEDURE test(a: Abstract); BEGIN v <- a; END test; END p;",
  787. "cannot instantiate 'Abstract' because it has abstract method(s): method"],
  788. ["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;",
  789. "cannot declare a variable of type 'T' (and derived types) because SELF(POINTER) was used in its method(s)"]
  790. )
  791. ),
  792. "arrays as values": testWithContext(
  793. context(grammar.declarationSequence,
  794. "TYPE A = ARRAY 3 OF INTEGER; T = RECORD a: A END;"
  795. + "VAR r: T;"
  796. + "PROCEDURE procArrayVar(VAR a: A); END procArrayVar;"
  797. ),
  798. pass("PROCEDURE p(r: T); BEGIN a <- r.a; a[0] := 123; procArrayVar(a); END p;",
  799. "PROCEDURE p(a: A); BEGIN tmp <- a; END p;",
  800. "PROCEDURE p(); VAR a: A; BEGIN tmp <- a; END p;",
  801. "PROCEDURE p(); VAR a: ARRAY 3 OF BOOLEAN; BEGIN tmp <- a; END p;"
  802. ),
  803. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN v <- a; END p;",
  804. "cannot initialize variable 'v' with open array"]
  805. )
  806. ),
  807. "FOR variable": testWithContext(
  808. context(grammar.statement, ""),
  809. pass("FOR i <- 0 TO 10 DO END",
  810. "FOR i <- 0 TO 10 DO FOR j <- 0 TO 10 BY 1 DO END END",
  811. "IF TRUE THEN FOR i <- 0 TO 10 DO END; FOR i <- 0 TO 10 BY 1 DO END; END"
  812. ),
  813. fail(["FOR i <- 0.0 TO 10 DO END", "'INTEGER' expression expected to assign 'i', got 'REAL'"],
  814. ["IF TRUE THEN FOR i <- 0 TO 10 DO END; i := 1; END", "undeclared identifier: 'i'"]
  815. )
  816. )
  817. },
  818. "type promotion for VAR arguments": testWithContext(
  819. context(grammar.declarationSequence,
  820. "TYPE Base = RECORD END; PBase = POINTER TO Base;"
  821. + "Derived = RECORD (Base) flag: BOOLEAN END; PDerived = POINTER TO Derived;"),
  822. pass("PROCEDURE p(VAR b: Base); BEGIN ASSERT((b IS Derived) & b.flag); END p;"),
  823. fail(["PROCEDURE p(VAR b: PBase); BEGIN ASSERT((b IS PDerived) & b.flag); END p;",
  824. "type 'Base' has no 'flag' field"])
  825. ),
  826. "type promotion for non-VAR arguments": testWithContext(
  827. context(grammar.declarationSequence,
  828. "TYPE Base = RECORD END; PBase = POINTER TO Base;"
  829. + "Derived = RECORD (Base) flag: BOOLEAN END; PDerived = POINTER TO Derived;"),
  830. pass("PROCEDURE p(b: PBase); BEGIN ASSERT((b IS PDerived) & b.flag); END p;")
  831. ),
  832. "Non-VAR arguments cannot be modified": testWithContext(
  833. context(grammar.declarationSequence,
  834. "TYPE PBase = POINTER TO RECORD END; T = RECORD i: INTEGER END;"
  835. + "PROCEDURE pArrayRef(VAR a: ARRAY OF INTEGER); END pArrayRef;"
  836. + "PROCEDURE recordVar(VAR r: T); END recordVar;"),
  837. pass("PROCEDURE p(VAR i: INTEGER); BEGIN i := 0; END p;",
  838. "PROCEDURE p(VAR b: PBase); BEGIN b := NIL; END p;"),
  839. fail(["PROCEDURE p(i: INTEGER); BEGIN i := 0; END p;",
  840. "cannot assign to non-VAR formal parameter"],
  841. ["PROCEDURE p(b: PBase); BEGIN b := NIL; END p;",
  842. "cannot assign to non-VAR formal parameter"],
  843. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pArrayRef(a) END p",
  844. "non-VAR formal parameter cannot be used as VAR parameter"],
  845. ["PROCEDURE p(r: T); BEGIN recordVar(r); END p",
  846. "non-VAR formal parameter cannot be used as VAR parameter"],
  847. ["PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  848. "cannot assign to non-VAR formal parameter"],
  849. ["PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p",
  850. "cannot assign to non-VAR formal parameter"]
  851. )
  852. ),
  853. "dynamic ARRAY": {
  854. "declaration": testWithContext(
  855. context(grammar.declarationSequence,
  856. "TYPE DA = ARRAY * OF INTEGER;"),
  857. pass("TYPE A = ARRAY * OF INTEGER;",
  858. "TYPE A = ARRAY * OF ARRAY * OF INTEGER;",
  859. "TYPE A = ARRAY *, * OF INTEGER;",
  860. "TYPE A = ARRAY 3, * OF INTEGER;",
  861. "TYPE A = ARRAY *, 3 OF INTEGER;",
  862. "TYPE P = PROCEDURE(): DA;",
  863. "TYPE P = PROCEDURE(VAR a: DA): DA;",
  864. "TYPE P = PROCEDURE(VAR a: ARRAY * OF INTEGER): DA;",
  865. "VAR a: ARRAY * OF INTEGER;",
  866. "PROCEDURE p(VAR a: ARRAY * OF INTEGER);END p;",
  867. "PROCEDURE p(VAR a: ARRAY * OF ARRAY * OF INTEGER);END p;",
  868. "PROCEDURE p(VAR a: ARRAY OF ARRAY * OF INTEGER);END p;"
  869. ),
  870. fail(["TYPE A = ARRAY OF INTEGER;", "not parsed"],
  871. ["TYPE P = PROCEDURE(): ARRAY OF INTEGER;", "';' expected"],
  872. ["TYPE P = PROCEDURE(a: DA);", "dynamic array has no use as non-VAR argument 'a'"],
  873. ["TYPE P = PROCEDURE(a: ARRAY * OF INTEGER);", "dynamic array has no use as non-VAR argument 'a'"],
  874. ["PROCEDURE p(a: DA);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  875. ["PROCEDURE p(a: ARRAY * OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  876. ["PROCEDURE p(a: ARRAY OF ARRAY * OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  877. ["PROCEDURE p(a: ARRAY * OF ARRAY OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"]
  878. )
  879. ),
  880. "return": testWithContext(
  881. context(grammar.declarationSequence,
  882. "TYPE A = ARRAY * OF INTEGER; B = ARRAY * OF BOOLEAN;"
  883. + "VAR a: A; b: B;"),
  884. pass("PROCEDURE p(): A; RETURN a END p;",
  885. "PROCEDURE p(): A; VAR static: ARRAY 3 OF INTEGER; RETURN static END p;"),
  886. fail(["PROCEDURE p(): ARRAY OF INTEGER; RETURN a; END p;", "not parsed"],
  887. ["PROCEDURE p(): A; RETURN b; END p;", "RETURN 'ARRAY * OF INTEGER' expected, got 'ARRAY * OF BOOLEAN'"])
  888. ),
  889. "pass as VAR argument": testWithContext(
  890. context(grammar.statement,
  891. "TYPE A = ARRAY * OF INTEGER; B = ARRAY * OF BOOLEAN;"
  892. + "VAR a: A; b: B; aStatic: ARRAY 3 OF INTEGER;"
  893. + "PROCEDURE paVar(VAR a: A); END paVar;"
  894. + "PROCEDURE paVarOpen(VAR a: ARRAY OF INTEGER); END paVarOpen;"),
  895. pass("paVar(a)",
  896. "paVarOpen(a)"),
  897. fail(["paVar(aStatic)", "type mismatch for argument 1: cannot pass 'ARRAY 3 OF INTEGER' as VAR parameter of type 'ARRAY * OF INTEGER'"])
  898. ),
  899. "assign": testWithContext(
  900. context(grammar.statement,
  901. "VAR stat: ARRAY 3 OF INTEGER; dynamic: ARRAY * OF INTEGER;"),
  902. pass("dynamic := stat"),
  903. fail(["stat := dynamic", "type mismatch: 'stat' is 'ARRAY 3 OF INTEGER' and cannot be assigned to 'ARRAY * OF INTEGER' expression"])
  904. ),
  905. "indexing": testWithContext(
  906. context(grammar.expression,
  907. "VAR a: ARRAY * OF INTEGER;"),
  908. pass("a[0]", "a[1]"),
  909. fail(["a[-1]", "index is negative: -1"],
  910. ["a[-2]", "index is negative: -2"])
  911. )
  912. }
  913. };