test_unit_eberon.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. function makeCodeSuite(){
  71. return {
  72. "insertion": pass(
  73. function(){
  74. var g = EberonCodeGenerator.makeGenerator();
  75. g.write("a");
  76. var i = g.makeInsertion();
  77. g.write("b");
  78. g.insert(i, "c");
  79. assert(g.result() == "acb");
  80. },
  81. function(){
  82. var g = EberonCodeGenerator.makeGenerator();
  83. g.write("ab");
  84. var i1 = g.makeInsertion();
  85. var i2 = g.makeInsertion();
  86. g.write("cd");
  87. g.insert(i1, "123");
  88. g.insert(i2, "345");
  89. assert(g.result() == "ab123345cd");
  90. },
  91. function(){
  92. var g = EberonCodeGenerator.makeGenerator();
  93. g.write("ab");
  94. var i = g.makeInsertion();
  95. g.write("cd");
  96. assert(g.result() == "abcd");
  97. }
  98. )
  99. };
  100. }
  101. exports.suite = {
  102. "code": makeCodeSuite(),
  103. "arithmetic operators": testWithContext(
  104. context(grammar.statement, "VAR b1: BOOLEAN;"),
  105. pass(),
  106. fail(["b1 := b1 + b1", "operator '+' type mismatch: numeric type or SET or STRING expected, got 'BOOLEAN'"])
  107. ),
  108. "key words": testWithGrammar(
  109. grammar.variableDeclaration,
  110. pass(),
  111. fail(["SELF: INTEGER", "not parsed"],
  112. ["SUPER: INTEGER", "not parsed"],
  113. ["STRING: INTEGER", "'STRING' already declared"]
  114. )
  115. ),
  116. "abstract method declaration": testWithContext(
  117. context(grammar.declarationSequence,
  118. "TYPE T = RECORD PROCEDURE p() END;"
  119. + "D = RECORD(T) END;"
  120. + "T2 = RECORD PROCEDURE p1(); PROCEDURE p2(i: INTEGER): BOOLEAN END;"
  121. ),
  122. pass(),
  123. fail(["VAR r: T;",
  124. "cannot instantiate 'T' because it has abstract method(s): p"],
  125. ["VAR r: T2;",
  126. "cannot instantiate 'T2' because it has abstract method(s): p1, p2"],
  127. ["PROCEDURE p(); VAR p: POINTER TO T; BEGIN NEW(p); END p;",
  128. "cannot instantiate 'T' because it has abstract method(s): p"],
  129. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR r: LocalT; END p;",
  130. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  131. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR p: POINTER TO LocalT; BEGIN NEW(p) END p;",
  132. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  133. ["VAR r: D;",
  134. "cannot instantiate 'D' because it has abstract method(s): p"],
  135. ["PROCEDURE p(); VAR p: POINTER TO D; BEGIN NEW(p); END p;",
  136. "cannot instantiate 'D' because it has abstract method(s): p"]
  137. )
  138. ),
  139. "new method declaration": testWithContext(
  140. context(grammar.declarationSequence,
  141. "TYPE T = RECORD PROCEDURE p(); intField: INTEGER END; A = ARRAY 1 OF INTEGER;"),
  142. pass("PROCEDURE T.p(); END T.p;"
  143. ),
  144. fail(["PROCEDURE TUnk.p(), NEW; END TUnk.p;", "undeclared identifier: 'TUnk'"],
  145. ["PROCEDURE A.p(), NEW; END A.p;",
  146. "RECORD type expected in method declaration, got 'ARRAY 1 OF INTEGER'"],
  147. ["PROCEDURE T.p(), NEW; END;", "not parsed"],
  148. ["PROCEDURE T.p(); END p;",
  149. "mismatched method names: expected 'T.p' at the end (or nothing), got 'p'"],
  150. ["PROCEDURE T.p(); END T2.p;",
  151. "mismatched method names: expected 'T.p' at the end (or nothing), got 'T2.p'"],
  152. ["PROCEDURE T.p(); END T.p2;",
  153. "mismatched method names: expected 'T.p' at the end (or nothing), got 'T.p2'"],
  154. ["PROCEDURE T.intField(); END T.intField;",
  155. "'T' has no declaration for method 'intField'"],
  156. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(); END T.p;",
  157. "method 'T.p' already defined"],
  158. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); PROCEDURE m() END; END p;",
  159. "cannot declare a new method 'm': method already was declared"],
  160. ["PROCEDURE p(); TYPE T = RECORD m: INTEGER; PROCEDURE m() END; END p;",
  161. "cannot declare method, record already has field 'm'"],
  162. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); m: INTEGER END; END p;",
  163. "cannot declare field, record already has method 'm'"]
  164. )
  165. ),
  166. "overridden method declaration": testWithContext(
  167. context(grammar.declarationSequence,
  168. "TYPE Base = RECORD PROCEDURE p() END; T = RECORD (Base) END;"
  169. + "PROCEDURE Base.p(); END Base.p;"),
  170. pass("PROCEDURE T.p(); END T.p;"),
  171. fail(["PROCEDURE T.pUnk(); END T.pUnk;",
  172. "'T' has no declaration for method 'pUnk'"],
  173. ["PROCEDURE proc(); TYPE T = RECORD (Base) PROCEDURE p() END; END proc;",
  174. "cannot declare a new method 'p': method already was declared"],
  175. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(); END T.p;",
  176. "method 'T.p' already defined"],
  177. ["PROCEDURE T.p(a: INTEGER); END T.p;",
  178. "overridden method 'p' signature mismatch: should be 'PROCEDURE', got 'PROCEDURE(INTEGER)'"],
  179. ["PROCEDURE p(); PROCEDURE T.p(); END T.p; END p;",
  180. "method should be defined in the same scope as its bound type 'T'"]
  181. )
  182. ),
  183. "SELF": testWithContext(
  184. context(grammar.declarationSequence,
  185. "TYPE T = RECORD PROCEDURE p(); i: INTEGER END;"
  186. + "PROCEDURE proc(i: INTEGER); END proc;"),
  187. pass("PROCEDURE T.p(); BEGIN SELF.i := 0; END T.p;",
  188. "PROCEDURE T.p(); BEGIN proc(SELF.i); END T.p;"
  189. ),
  190. fail(["PROCEDURE p(); BEGIN SELF.i := 0; END p;",
  191. "SELF can be used only in methods"])
  192. ),
  193. "SELF as pointer": testWithContext(
  194. context(grammar.declarationSequence,
  195. "TYPE T = RECORD PROCEDURE method() END;"
  196. + "PT = POINTER TO T;"
  197. + "VAR pVar: PT;"
  198. + "PROCEDURE refProc(VAR p: PT); END refProc;"
  199. ),
  200. pass("PROCEDURE T.method(); BEGIN pVar := SELF(POINTER) END T.method;",
  201. "PROCEDURE p();"
  202. + "TYPE Derived = RECORD(T) END; VAR pd: POINTER TO Derived;"
  203. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN NEW(pd); pVar := SELF(POINTER); END Derived.method;"
  204. + "END p;"),
  205. fail(["PROCEDURE T.method(); BEGIN refProc(SELF(POINTER)) END T.method;",
  206. "read-only variable cannot be used as VAR parameter"],
  207. ["PROCEDURE T.method(); BEGIN SELF(POINTER) := pVar; END T.method;",
  208. "cannot assign to read-only variable"],
  209. ["PROCEDURE p();"
  210. + "TYPE Derived = RECORD(T) END; VAR d: Derived;"
  211. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN pVar := SELF(POINTER); END Derived.method;"
  212. + "END p;",
  213. "cannot declare a variable of type 'Derived' (and derived types) because SELF(POINTER) was used in its method(s)"],
  214. ["PROCEDURE p();"
  215. + "TYPE Derived = RECORD(T) END; Derived2 = RECORD(Derived) END;"
  216. + "VAR d: Derived2;"
  217. + "PROCEDURE Derived.method(); VAR pVar: PT; BEGIN pVar := SELF(POINTER); END Derived.method;"
  218. + "END p;",
  219. "cannot declare a variable of type 'Derived' (and derived types) because SELF(POINTER) was used in its method(s)"]
  220. )
  221. ),
  222. "method call": testWithContext(
  223. context(grammar.expression,
  224. "TYPE T = RECORD PROCEDURE p(); PROCEDURE f(): INTEGER END;"
  225. + "VAR o: T;"
  226. + "PROCEDURE T.p(); END T.p;"
  227. + "PROCEDURE T.f(): INTEGER; RETURN 0 END T.f;"
  228. ),
  229. pass("o.f()"),
  230. fail(["o.p()", "procedure returning no result cannot be used in an expression"])
  231. ),
  232. "method call as statement": testWithContext(
  233. context(grammar.statement,
  234. "TYPE T = RECORD PROCEDURE p(); PROCEDURE f(): INTEGER END;"
  235. + "VAR o: T;"
  236. + "PROCEDURE T.p(); END T.p;"
  237. + "PROCEDURE T.f(): INTEGER; RETURN 0 END T.f;"
  238. ),
  239. pass("o.p"),
  240. fail(["o.f", "procedure returning a result cannot be used as a statement"])
  241. ),
  242. "cannot assign to method": testWithContext(
  243. context(grammar.statement,
  244. "TYPE T = RECORD PROCEDURE p() END;"
  245. + "VAR o: T;"
  246. + "PROCEDURE T.p(); END T.p;"
  247. ),
  248. pass(),
  249. fail(["o.p := o.p", "method 'p' cannot be referenced"],
  250. ["o.p := NIL", "cannot assign to method"])
  251. ),
  252. "method cannot be referenced": testWithContext(
  253. context(grammar.statement,
  254. "TYPE T = RECORD PROCEDURE p() END;"
  255. + "Proc = PROCEDURE();"
  256. + "VAR o: T;"
  257. + "PROCEDURE T.p(); END T.p;"
  258. + "PROCEDURE proc(p: Proc); END proc;"
  259. ),
  260. pass(),
  261. fail(["proc(o.p)", "method 'p' cannot be referenced"],
  262. ["v <- o.p", "method 'p' cannot be referenced"])
  263. ),
  264. "method super call": testWithContext(
  265. context(grammar.declarationSequence,
  266. "TYPE T = RECORD PROCEDURE p(); PROCEDURE pAbstract(); PROCEDURE pAbstract2() END;"
  267. + "D = RECORD(T) PROCEDURE pNoSuper() END;"
  268. + "PROCEDURE T.p(); END T.p;"
  269. ),
  270. pass("PROCEDURE D.p(); BEGIN SUPER() END D.p;"),
  271. fail(["PROCEDURE D.pNoSuper(); BEGIN SUPER() END D.pNoSuper;",
  272. "there is no method 'pNoSuper' in base type(s)"],
  273. ["PROCEDURE p(); BEGIN SUPER() END p;",
  274. "SUPER can be used only in methods"],
  275. ["PROCEDURE T.pNoBase(); BEGIN SUPER() END T.pNoBase;",
  276. "'T' has no base type - SUPER cannot be used"],
  277. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract;",
  278. "cannot use abstract method(s) in SUPER calls: pAbstract"],
  279. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract; PROCEDURE D.pAbstract2(); BEGIN SUPER() END D.pAbstract2;",
  280. "cannot use abstract method(s) in SUPER calls: pAbstract, pAbstract2"]
  281. )
  282. ),
  283. "export method": testWithContext(
  284. context(grammar.declarationSequence,
  285. "TYPE T = RECORD PROCEDURE p() END;"
  286. ),
  287. pass(),
  288. fail(["PROCEDURE T.p*(); END T.p;",
  289. "method implementation cannot be exported: p"])
  290. ),
  291. "import method": testWithModule(
  292. "MODULE test;"
  293. + "TYPE T* = RECORD PROCEDURE m*(); PROCEDURE mNotExported() END;"
  294. + "PROCEDURE T.m(); END T.m; PROCEDURE T.mNotExported(); END T.mNotExported;"
  295. + "END test.",
  296. pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.m(); END m.",
  297. "MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.m(); END T.m; END m."
  298. ),
  299. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.mNotExported(); END m.",
  300. "type 'T' has no 'mNotExported' field"],
  301. ["MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.mNotExported(); END T.mNotExported; END m.",
  302. "'T' has no declaration for method 'mNotExported'"])
  303. ),
  304. "non-scalar variables can be exported": testWithContext(
  305. context(grammar.declarationSequence,
  306. "TYPE T = RECORD END; A = ARRAY 3 OF INTEGER;"
  307. ),
  308. pass("VAR r*: T;",
  309. "VAR a*: A;"),
  310. fail()
  311. ),
  312. "export as read-only": testWithContext(
  313. context(grammar.declarationSequence, ""),
  314. pass("TYPE T* = RECORD i-: INTEGER END;"),
  315. fail(["TYPE T- = RECORD END;",
  316. "type cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  317. ["PROCEDURE p-(); END p;",
  318. "procedure cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  319. ["CONST c- = 123;",
  320. "constant cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  321. ["VAR i-: INTEGER;",
  322. "variable cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  323. ["TYPE T* = RECORD PROCEDURE p-() END;",
  324. "method cannot be exported as read-only using '-' mark (did you mean '*'?)"]
  325. )
  326. ),
  327. "field exported as read-only is writable in current module": testWithContext(
  328. context(grammar.statement,
  329. "TYPE T* = RECORD i-: INTEGER END;"
  330. + "VAR r: T;"
  331. ),
  332. pass("r.i := 123"),
  333. fail()
  334. ),
  335. "import as read-only": testWithModule(
  336. "MODULE test; TYPE T* = RECORD f-: INTEGER END; END test.",
  337. pass(),
  338. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 123; END m.",
  339. "cannot assign to read-only variable"],
  340. ["MODULE m; IMPORT test; TYPE D = RECORD(test.T) END; VAR r: D; BEGIN r.f := 123; END m.",
  341. "cannot assign to read-only variable"]
  342. )),
  343. "STRING variable": testWithGrammar(
  344. grammar.variableDeclaration,
  345. pass("s: STRING")
  346. ),
  347. "STRING expression": testWithContext(
  348. context(grammar.expression,
  349. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  350. pass("s1 + s2",
  351. "s1 + \"abc\"",
  352. "\"abc\" + s1",
  353. "s1 = s2",
  354. "s1 = \"abc\"",
  355. "s1 = 22X",
  356. "\"abc\" = s1",
  357. "22X = s1",
  358. "s1 # s2",
  359. "s1 # \"abc\"",
  360. "s1 # 22X",
  361. "\"abc\" # s1",
  362. "22X # s1",
  363. "s1 < s2",
  364. "s1 < \"abc\"",
  365. "s1 < 22X",
  366. "\"abc\" < s1",
  367. "22X < s1",
  368. "s1 > s2",
  369. "s1 > \"abc\"",
  370. "s1 > 22X",
  371. "\"abc\" > s1",
  372. "22X > s1",
  373. "s1 <= s2",
  374. "s1 <= \"abc\"",
  375. "\"abc\" <=s1",
  376. "22X <= s1",
  377. "s1 >= \"abc\"",
  378. "s1 >= 22X",
  379. "s1 >= s2",
  380. "\"abc\" >= s1",
  381. "22X >= s1"
  382. ),
  383. fail(["s1 = NIL", "type mismatch: expected 'STRING', got 'NIL'"],
  384. ["s1 = a", "type mismatch: expected 'STRING', got 'ARRAY 10 OF CHAR'"],
  385. ["a = s1", "type mismatch: expected 'ARRAY 10 OF CHAR', got 'STRING'"]
  386. )
  387. ),
  388. "STRING literal expression": testWithContext(
  389. context(grammar.expression,
  390. "CONST cs = \"abc\";"
  391. + "PROCEDURE pString(s: STRING): STRING; RETURN s END pString;"
  392. + "PROCEDURE pStringByRef(VAR s: STRING): STRING; RETURN s END pStringByRef;"
  393. ),
  394. pass("\"abc\" + \"cde\"",
  395. "cs + cs",
  396. "cs + \"abc\"",
  397. "cs = \"abc\"",
  398. "cs # \"abc\"",
  399. "cs < \"abc\"",
  400. "cs > \"abc\"",
  401. "cs <= \"abc\"",
  402. "cs >= \"abc\"",
  403. "pString(cs)",
  404. "pString(\"abc\")"
  405. ),
  406. fail(["pStringByRef(cs)", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"],
  407. ["pStringByRef(\"abc\")", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"]
  408. )
  409. ),
  410. "STRING assignment": testWithContext(
  411. context(grammar.statement,
  412. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  413. pass("s1 := s2",
  414. "s1 := \"abc\"",
  415. "s1 := 22X"
  416. ),
  417. fail(["a := s1", "type mismatch: 'a' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'STRING' expression"],
  418. ["s1 := a", "type mismatch: 's1' is 'STRING' and cannot be assigned to 'ARRAY 10 OF CHAR' expression"]
  419. )
  420. ),
  421. "STRING and ARRAY OF CHAR": testWithContext(
  422. context(grammar.expression,
  423. "VAR s: STRING; a: ARRAY 10 OF CHAR;"
  424. + "PROCEDURE pArray(a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pArray;"
  425. + "PROCEDURE pString(s: STRING): BOOLEAN; RETURN FALSE END pString;"
  426. + "PROCEDURE pVar(VAR a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pVar;"
  427. + "PROCEDURE pIntArray(a: ARRAY OF INTEGER): BOOLEAN; RETURN FALSE END pIntArray;"
  428. ),
  429. pass("pArray(s)"),
  430. fail(["pVar(s)", "type mismatch for argument 1: cannot pass 'STRING' as VAR parameter of type 'ARRAY OF CHAR'"],
  431. ["pString(a)", "type mismatch for argument 1: 'ARRAY 10 OF CHAR' cannot be converted to 'STRING'"],
  432. ["pIntArray(s)", "type mismatch for argument 1: 'STRING' cannot be converted to 'ARRAY OF INTEGER'"]
  433. )
  434. ),
  435. "STRING LEN": testWithContext(
  436. context(grammar.expression,
  437. "VAR s: STRING;"),
  438. pass("LEN(s)"),
  439. fail()
  440. ),
  441. "STRING indexing": testWithContext(
  442. context(grammar.expression,
  443. "VAR s: STRING;"
  444. + "PROCEDURE pCharByVar(VAR c: CHAR): CHAR; RETURN c END pCharByVar;"),
  445. pass("s[0]"),
  446. fail(["s[-1]", "index is negative: -1"],
  447. ["pCharByVar(s[0])", "string element cannot be used as VAR parameter"]
  448. )
  449. ),
  450. "designate call result in expression": testWithContext(
  451. context(grammar.expression,
  452. "TYPE PT = POINTER TO RECORD field: INTEGER END;"
  453. + "VAR p: PT;"
  454. + "PROCEDURE proc(): PT; RETURN p END proc;"
  455. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  456. + "PROCEDURE intVar(VAR i: INTEGER): INTEGER; RETURN i END intVar;"),
  457. pass("proc().field",
  458. "intVar(proc().field)"),
  459. fail(["intVar(int())", "expression cannot be used as VAR parameter"])
  460. ),
  461. "designate call result in statement": testWithContext(
  462. context(grammar.statement,
  463. "TYPE PT = POINTER TO RECORD field: INTEGER; proc: PROCEDURE END;"
  464. + "ProcType = PROCEDURE;"
  465. + "VAR p: PT;"
  466. + "PROCEDURE procVoid(); END procVoid;"
  467. + "PROCEDURE proc(): PT; RETURN p END proc;"
  468. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  469. + "PROCEDURE intVar(VAR i: INTEGER); END intVar;"
  470. + "PROCEDURE returnProc(): ProcType; RETURN procVoid END returnProc;"
  471. ),
  472. pass("proc().field := 0",
  473. "proc().proc()",
  474. "proc().proc"
  475. ),
  476. fail(["int() := 0", "cannot assign to procedure call result"],
  477. ["intVar(int())", "expression cannot be used as VAR parameter"],
  478. ["procVoid()()", "PROCEDURE expected, got 'procedure call statement'"],
  479. ["int()()", "PROCEDURE expected, got 'INTEGER'"],
  480. ["returnProc()", "procedure returning a result cannot be used as a statement"] // call is not applied implicitly to result
  481. )
  482. ),
  483. "type promotion": {
  484. "or" : pass(
  485. function(){
  486. var or = new TypePromotion.OrPromotions();
  487. var a = new TestVar();
  488. var p = or.next();
  489. assert(a.type() == "type");
  490. p.invert();
  491. p.promote(a, "type1");
  492. assert(a.type() == "type");
  493. or.next();
  494. assert(a.type() == "type1");
  495. or.reset();
  496. assert(a.type() == "type");
  497. },
  498. function(){
  499. var or = new TypePromotion.OrPromotions();
  500. var a = new TestVar();
  501. var p = or.next(p);
  502. p.promote(a, "type1");
  503. or.next();
  504. assert(a.type() == "type");
  505. },
  506. function(){
  507. var or = new TypePromotion.OrPromotions();
  508. var a = new TestVar();
  509. var p1 = or.next();
  510. p1.promote(a, "type1");
  511. var p2 = or.next();
  512. p2.invert();
  513. p2.promote(a, "type2");
  514. assert(a.type() == "type");
  515. assert(a.type() == "type");
  516. or.next();
  517. assert(a.type() == "type2");
  518. or.reset();
  519. assert(a.type() == "type");
  520. }
  521. ),
  522. "and": pass(
  523. function(){
  524. var and = new TypePromotion.AndPromotions();
  525. var a = new TestVar();
  526. var p = and.next();
  527. p.promote(a, "type1");
  528. and.next();
  529. assert(a.type() == "type1");
  530. and.reset();
  531. assert(a.type() == "type");
  532. },
  533. function(){ // (a IS type1) & (v OR (a IS type2)) & v
  534. var and = new TypePromotion.AndPromotions();
  535. var a = new TestVar();
  536. var p = and.next();
  537. p.promote(a, "type1");
  538. var subOr = and.next().makeOr();
  539. subOr.next();
  540. subOr.next().promote(a, "type2");
  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. and.next().promote(a, "type1");
  550. var subOr = and.next();
  551. subOr.invert();
  552. subOr = subOr.makeOr();
  553. subOr.next();
  554. var p = subOr.next();
  555. p.invert();
  556. p.promote(a, "type2");
  557. and.next();
  558. assert(a.type() == "type2");
  559. and.reset();
  560. assert(a.type() == "type");
  561. },
  562. function(){ // (a IS type1) & (v & (a IS type2))
  563. var and = new TypePromotion.AndPromotions();
  564. var a = new TestVar();
  565. and.next().promote(a, "type1");
  566. var sub = and.next().makeAnd();
  567. sub.next();
  568. assert(a.type() == "type1");
  569. sub.next().promote(a, "type2");
  570. assert(a.type() == "type1");
  571. and.and();
  572. assert(a.type() == "type2");
  573. and.or();
  574. assert(a.type() == "type");
  575. },
  576. function(){ // (~(~(a IS type1)) & v) OR v
  577. var a = new TestVar();
  578. var or = new TypePromotion.OrPromotions();
  579. var and = or.next().makeAnd();
  580. var p1 = and.next();
  581. p1.invert();
  582. var p2 = p1.makeOr().next().makeAnd().next();
  583. p2.invert();
  584. p2.promote(a, "type1");
  585. and.next();
  586. assert(a.type() == "type1");
  587. or.next();
  588. assert(a.type() == "type");
  589. },
  590. function(){ // (v OR (a IS type1)) & v)
  591. var a = new TestVar();
  592. var and = new TypePromotion.AndPromotions();
  593. var or = and.next().makeOr();
  594. or.next();
  595. or.next().makeAnd().next().promote(a, "type1");
  596. and.next();
  597. assert(a.type() == "type");
  598. }
  599. )
  600. },
  601. "in place variables": {
  602. "initialization": testWithContext(
  603. context(grammar.statement,
  604. "VAR i: INTEGER;"
  605. + "PROCEDURE p(): BOOLEAN; RETURN FALSE END p;"
  606. + "PROCEDURE void(); END void;"
  607. ),
  608. pass("v <- 0",
  609. "v <- 1.23",
  610. "v <- \"abc\"",
  611. "v <- TRUE",
  612. "v <- i",
  613. "v <- i + i",
  614. "v <- p()",
  615. "v <- void" // procedure type
  616. ),
  617. fail(["v <-", "initialization expression expected"],
  618. ["v <- void()", "procedure returning no result cannot be used in an expression"],
  619. ["v <- NIL", "cannot use NIL to initialize variable"])
  620. ),
  621. "read-only if initialized with string literal": testWithContext(
  622. context(grammar.declarationSequence, ""),
  623. pass(),
  624. fail(["PROCEDURE p(); BEGIN s <- \"abc\"; s := \"def\"; END p;", "cannot assign to string literal"],
  625. ["PROCEDURE p(); BEGIN s <- \"abc\"; s[0] := \"d\"; END p;", "cannot assign to read-only variable"])
  626. ),
  627. "scope": testWithContext(
  628. temporaryValues.context,
  629. temporaryValues.passStatements(
  630. "v1 <- 0; v2 <-0;",
  631. "i <- 0; ASSERT(i = 0);",
  632. "WHILE FALSE DO v <- 0; ASSERT(v = 0); END; WHILE FALSE DO v <- 0; END;",
  633. "WHILE FALSE DO i1 <- 0; WHILE FALSE DO i2 <- 0; ASSERT(i1 = 0); ASSERT(i2 = 0); END; END;",
  634. "WHILE bVar DO v <- 0; ELSIF ~bVar DO v <- 0 END;",
  635. "IF FALSE THEN v <- 0; ASSERT(v = 0); END; IF FALSE THEN v <- 0; END;",
  636. "IF FALSE THEN v <- 0; END; IF FALSE THEN v <- 0; END;",
  637. "IF FALSE THEN v <- 0; ELSIF FALSE THEN v <- 0; ELSE v <- 0; END;",
  638. "i <- 0; CASE i OF 0: v <- 0 | 1: v <- 1; ; ASSERT(v = 1); END;",
  639. "REPEAT v <- 0; UNTIL FALSE; REPEAT v <- 0; UNTIL FALSE;",
  640. "REPEAT v <- 0; ASSERT(v = 0); UNTIL v # 0;",
  641. "i <- 0; FOR i := 0 TO 10 DO v <- 0; END; FOR i := 0 TO 10 DO v <- 0; END;"
  642. ),
  643. fail(["PROCEDURE p(); BEGIN v <- 0; v <-0; END p;", "'v' already declared"],
  644. ["PROCEDURE p(); VAR v: INTEGER; BEGIN v <- 0; END p;", "'v' already declared"],
  645. ["PROCEDURE p(); BEGIN v <- 0; WHILE FALSE DO v <- 0; END; END p;",
  646. "'v' already declared in procedure scope"],
  647. ["PROCEDURE p(); BEGIN i <- 0; IF FALSE THEN i <- 0; END; END p;",
  648. "'i' already declared in procedure scope"],
  649. ["PROCEDURE p(); BEGIN i <- 0; IF TRUE THEN IF TRUE THEN i <- 0; END; END; END p;",
  650. "'i' already declared in procedure scope"],
  651. ["PROCEDURE p(); BEGIN WHILE FALSE DO i <- 0; WHILE FALSE DO i <- 0; END; END; END p;",
  652. "'i' already declared in operator scope"]
  653. )
  654. ),
  655. "type promotion in expression": testWithContext(
  656. temporaryValues.context,
  657. temporaryValues.passExpressions(
  658. "(b IS PDerived) & b.flag",
  659. "(b IS PDerived) & bVar & b.flag",
  660. "(b IS PDerived) & (bVar OR b.flag)",
  661. "(b IS PDerived) & (b2 IS PDerived) & b.flag & b2.flag",
  662. "(b IS PDerived) & proc(TRUE) & b.flag",
  663. "(b IS PDerived) & ~proc(TRUE) & b.flag",
  664. "~(~(b IS PDerived)) & b.flag",
  665. "~~(b IS PDerived) & b.flag",
  666. "(b IS PDerived) & ((b IS PDerived2) OR bVar) & b.flag",
  667. "(b IS PDerived) & (bVar OR (b IS PDerived2)) & b.flag",
  668. "(b IS PDerived) & ~(bVar OR ~(b IS PDerived2)) & b.flag2",
  669. "~(bVar & (b IS PDerived)) OR b.flag"
  670. //TODO: "((b IS PDerived) = TRUE) & b.flag); END p;",
  671. ),
  672. temporaryValues.failExpressions(
  673. "(b IS PDerived) OR b.flag",
  674. "(bVar OR (b IS PDerived)) & b.flag",
  675. "(b IS PDerived) OR bVar & b.flag",
  676. "~(b IS PDerived) & b.flag",
  677. "((b IS PDerived) & (b2 IS PDerived) OR bVar) & b.flag",
  678. "proc(b IS PDerived) & proc(b.flag)",
  679. "ORD(b IS PDerived) * ORD(b.flag) = 0",
  680. "((b IS PDerived) = FALSE) & b.flag",
  681. "((b IS PDerived) & bVar) = b.flag",
  682. "b IS PDerived); ASSERT(b.flag",
  683. "((b IS PDerived) OR (b IS PDerived)) & b.flag",
  684. "(b IS PDerived) OR (b IS PDerived) OR b.flag",
  685. "(bVar OR (b IS PDerived)) & b.flag",
  686. "~(bVar & ~(b IS PDerived)) & b.flag"
  687. )
  688. ),
  689. "invert type promotion in expression": testWithContext(
  690. temporaryValues.context,
  691. temporaryValues.passExpressions(
  692. "~(b IS PDerived) OR b.flag",
  693. "~(b IS PDerived) OR b.flag OR bVar",
  694. "~(b IS PDerived) OR b.flag & bVar",
  695. "~(b IS PDerived) OR bVar & b.flag",
  696. "~(b IS PDerived) OR (bVar & b.flag)",
  697. "~(b IS PDerived) OR bVar OR b.flag",
  698. "~(b IS PDerived) OR (bVar = b.flag)",
  699. "~(~(b IS PDerived) OR bVar) & b.flag",
  700. "~(~(b IS PDerived) OR b.flag) & b.flag",
  701. "~(b IS PDerived) OR ~(b2 IS PDerived) OR b2.flag",
  702. "~(b IS PDerived) OR b.flag OR ~(b2 IS PDerived) OR b2.flag",
  703. "~((b IS PDerived) & b.flag) OR b.flag OR ~(b2 IS PDerived) OR b2.flag",
  704. "~((b IS PDerived) & b.flag) OR b.flag OR ~((b2 IS PDerived) & b.flag & b2.flag) OR b2.flag"
  705. ),
  706. temporaryValues.failExpressions(
  707. "(~(b IS PDerived) OR bVar) & b.flag",
  708. "(ORD(~(b IS PDerived)) + ORD(b.flag)",
  709. "~(~(b IS PDerived) OR bVar) OR b.flag",
  710. "~(~(b IS PDerived) & bVar) & b.flag",
  711. "~(b IS PDerived) OR b.flag = b.flag"
  712. )
  713. ),
  714. "type promotion in separate statements": testWithContext(
  715. temporaryValues.context,
  716. pass(),
  717. temporaryValues.failStatements(
  718. "bVar := b IS PDerived; ASSERT(b.flag)",
  719. "bVar := (b IS PDerived) & bVar; ASSERT(b.flag)"
  720. )
  721. ),
  722. "type promotion in IF": testWithContext(
  723. temporaryValues.context,
  724. temporaryValues.passStatements(
  725. "IF b IS PDerived THEN b.flag := FALSE; END;",
  726. "IF (b IS PDerived) & bVar THEN b.flag := FALSE; END;",
  727. "IF bVar & (b IS PDerived) THEN b.flag := FALSE; END;",
  728. "IF FALSE THEN ELSIF b IS PDerived THEN b.flag := FALSE; END;",
  729. "IF b IS PDerived THEN bVar := (b IS PDerived2) & b.flag2; b.flag := FALSE; END;",
  730. "IF bVar THEN ELSIF b IS PDerived2 THEN ELSIF b IS PDerived THEN END;",
  731. "IF bVar THEN ELSIF b IS PDerived THEN ELSIF b IS PDerived THEN ELSIF b IS PDerived THEN END;",
  732. "IF b IS PDerived THEN IF bVar OR (b IS PDerived2) THEN b.flag := FALSE; END; END"
  733. ),
  734. temporaryValues.failStatements(
  735. "IF (b IS PDerived) OR bVar THEN b.flag := FALSE; END",
  736. "IF bVar OR (b IS PDerived) THEN b.flag := FALSE; END",
  737. "IF (b2 IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  738. "IF (b IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  739. "IF (b IS PDerived) OR (b IS PDerived) OR b.flag THEN END",
  740. "IF (b IS PDerived) OR (b IS PDerived) OR (b IS PDerived) THEN b.flag := FALSE; END",
  741. "IF ((b IS PDerived) OR (b IS PDerived)) THEN b.flag := FALSE; END",
  742. "IF (b IS PDerived) OR (b IS PDerived2) THEN b.flag := FALSE; END",
  743. "IF (b IS PDerived2) OR (b IS PDerived) THEN b.flag := FALSE; END",
  744. "IF b IS PDerived THEN END; b.flag := FALSE",
  745. "IF ~(b IS PDerived) THEN END; b.flag := FALSE",
  746. "IF ~(b IS PDerived) THEN ELSIF bVar THEN END; b.flag := FALSE",
  747. "IF ~(b IS PDerived) THEN ELSIF bVar THEN ELSE END; b.flag := FALSE",
  748. "IF bVar THEN ELSIF b IS PDerived THEN ELSE END; b.flag := FALSE",
  749. "IF b IS PDerived THEN ELSE b.flag := FALSE; END",
  750. "IF bVar OR (b IS PDerived) THEN b.flag := FALSE; END;",
  751. "IF bVar OR (b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  752. "IF bVar OR ~(b IS PDerived) THEN b.flag := FALSE; END;",
  753. "IF b IS PDerived THEN ELSIF TRUE THEN b.flag := FALSE; END",
  754. "IF bVar THEN bVar := (b IS PDerived) & bVar; ASSERT(b.flag); END"
  755. )
  756. ),
  757. "invert type promotion in IF": testWithContext(
  758. temporaryValues.context,
  759. temporaryValues.passStatements(
  760. "IF ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  761. "IF ~(b IS PDerived) THEN ELSIF bVar THEN b.flag := FALSE; ELSE b.flag := FALSE; END;",
  762. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b.flag := FALSE; ELSE b.flag := FALSE; b2.flag := FALSE; END;",
  763. "IF ~(b IS PDerived) OR bVar THEN ELSE b.flag := FALSE; END;",
  764. "IF ~(b IS PDerived) OR b.flag THEN ELSE b.flag := FALSE; END;",
  765. "IF ~(b IS PDerived) OR (b2 IS PDerived) THEN ELSE b.flag := FALSE; END;",
  766. "IF ~(b IS PDerived) OR ~(b2 IS PDerived) THEN ELSE b2.flag := FALSE; END;",
  767. "IF ~(b IS PDerived) THEN bVar := b IS PDerived; ELSE b.flag := FALSE; END;",
  768. "IF ~(b IS PDerived) THEN ASSERT((b IS PDerived) & b.flag); ELSE b.flag := FALSE; END;",
  769. "IF bVar OR ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;"
  770. ),
  771. temporaryValues.failStatements(
  772. "IF ~(b IS PDerived) & bVar THEN ELSE b.flag := FALSE; END; END p;",
  773. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b2.flag := FALSE; END;",
  774. "IF bVar OR (b IS PDerived) THEN ELSE b.flag := FALSE; END;"
  775. )
  776. ),
  777. "type promotion in WHILE": testWithContext(
  778. temporaryValues.context,
  779. temporaryValues.passStatements(
  780. "WHILE (b IS PDerived) & b.flag DO END;",
  781. "WHILE ~(b IS PDerived) OR b.flag DO END;",
  782. "WHILE b IS PDerived DO b.flag := FALSE; END;",
  783. "WHILE ~(b IS PDerived) DO ELSIF b.flag DO END;",
  784. "WHILE ~(b IS PDerived) DO ELSIF bVar DO b.flag := FALSE; END;"
  785. ),
  786. temporaryValues.failStatements(
  787. "WHILE b IS PDerived DO END; b.flag := FALSE;"
  788. )
  789. ),
  790. "type promotion cannot be reset by assignment": testWithContext(
  791. temporaryValues.context,
  792. pass(),
  793. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN b := pBase; b.flag := FALSE; END; END p;",
  794. "type mismatch: 'b' is 'PDerived' and cannot be assigned to 'POINTER TO Base' expression"]
  795. )
  796. ),
  797. "type promotion cannot be reset by passing as VAR argument": testWithContext(
  798. temporaryValues.context,
  799. pass(),
  800. 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;",
  801. "type mismatch for argument 1: cannot pass 'PDerived' as VAR parameter of type 'PBase'"]
  802. )
  803. ),
  804. "type promotion after dereferencing": testWithContext(
  805. temporaryValues.context,
  806. temporaryValues.passExpressions(
  807. "(b^ IS Derived) & b.flag")
  808. ),
  809. "IS expression after type promotion": testWithContext(
  810. temporaryValues.context,
  811. pass(),
  812. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN bVar := b IS PDerived; b.flag := FALSE; END; END p;",
  813. "invalid type test: 'Derived' is not an extension of 'Derived'"]
  814. )
  815. ),
  816. "record types as values": testWithContext(
  817. context(grammar.declarationSequence,
  818. "TYPE Base = RECORD pBase: POINTER TO Base END;"
  819. + "Derived = RECORD (Base) END;"
  820. + "VAR base: Base;"
  821. + "PROCEDURE procBaseVar(VAR b: Base); END procBaseVar;"
  822. ),
  823. pass("PROCEDURE p(b: Base); BEGIN base <- b; procBaseVar(base); base := b; END p;"),
  824. fail(["PROCEDURE p(); BEGIN baseVar <- base.pBase^; ASSERT(base IS Derived); END p;",
  825. "invalid type test: a value variable cannot be used"],
  826. ["PROCEDURE p(VAR b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  827. "invalid type test: a value variable cannot be used"],
  828. ["PROCEDURE p(b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  829. "invalid type test: a value variable cannot be used"],
  830. ["PROCEDURE p(); TYPE Abstract = RECORD PROCEDURE method() END; PROCEDURE test(a: Abstract); BEGIN v <- a; END test; END p;",
  831. "cannot instantiate 'Abstract' because it has abstract method(s): method"],
  832. ["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;",
  833. "cannot declare a variable of type 'T' (and derived types) because SELF(POINTER) was used in its method(s)"]
  834. )
  835. ),
  836. "arrays as values": testWithContext(
  837. context(grammar.declarationSequence,
  838. "TYPE A = ARRAY 3 OF INTEGER; T = RECORD a: A END;"
  839. + "VAR r: T;"
  840. + "PROCEDURE procArrayVar(VAR a: A); END procArrayVar;"
  841. ),
  842. pass("PROCEDURE p(r: T); BEGIN a <- r.a; a[0] := 123; procArrayVar(a); END p;",
  843. "PROCEDURE p(a: A); BEGIN tmp <- a; END p;",
  844. "PROCEDURE p(); VAR a: A; BEGIN tmp <- a; END p;",
  845. "PROCEDURE p(); VAR a: ARRAY 3 OF BOOLEAN; BEGIN tmp <- a; END p;"
  846. ),
  847. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN v <- a; END p;",
  848. "cannot initialize variable 'v' with open array"]
  849. )
  850. ),
  851. "FOR variable": testWithContext(
  852. context(grammar.statement, ""),
  853. pass("FOR i <- 0 TO 10 DO END",
  854. "FOR i <- 0 TO 10 DO FOR j <- 0 TO 10 BY 1 DO END END",
  855. "IF TRUE THEN FOR i <- 0 TO 10 DO END; FOR i <- 0 TO 10 BY 1 DO END; END"
  856. ),
  857. fail(["FOR i <- 0.0 TO 10 DO END", "'INTEGER' expression expected to assign 'i', got 'REAL'"],
  858. ["IF TRUE THEN FOR i <- 0 TO 10 DO END; i := 1; END", "undeclared identifier: 'i'"]
  859. )
  860. )
  861. },
  862. "type promotion for VAR arguments": testWithContext(
  863. context(grammar.declarationSequence,
  864. "TYPE Base = RECORD END; PBase = POINTER TO Base;"
  865. + "Derived = RECORD (Base) flag: BOOLEAN END; PDerived = POINTER TO Derived;"),
  866. pass("PROCEDURE p(VAR b: Base); BEGIN ASSERT((b IS Derived) & b.flag); END p;"),
  867. fail(["PROCEDURE p(VAR b: PBase); BEGIN ASSERT((b IS PDerived) & b.flag); END p;",
  868. "type 'Base' has no 'flag' field"])
  869. ),
  870. "type promotion for non-VAR arguments": testWithContext(
  871. context(grammar.declarationSequence,
  872. "TYPE Base = RECORD END; PBase = POINTER TO Base;"
  873. + "Derived = RECORD (Base) flag: BOOLEAN END; PDerived = POINTER TO Derived;"),
  874. pass("PROCEDURE p(b: PBase); BEGIN ASSERT((b IS PDerived) & b.flag); END p;")
  875. ),
  876. "Non-VAR arguments cannot be modified": testWithContext(
  877. context(grammar.declarationSequence,
  878. "TYPE PBase = POINTER TO RECORD END; T = RECORD i: INTEGER END;"
  879. + "PROCEDURE pArrayRef(VAR a: ARRAY OF INTEGER); END pArrayRef;"
  880. + "PROCEDURE recordVar(VAR r: T); END recordVar;"),
  881. pass("PROCEDURE p(VAR i: INTEGER); BEGIN i := 0; END p;",
  882. "PROCEDURE p(VAR b: PBase); BEGIN b := NIL; END p;"),
  883. fail(["PROCEDURE p(i: INTEGER); BEGIN i := 0; END p;",
  884. "cannot assign to non-VAR formal parameter"],
  885. ["PROCEDURE p(b: PBase); BEGIN b := NIL; END p;",
  886. "cannot assign to non-VAR formal parameter"],
  887. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pArrayRef(a) END p",
  888. "non-VAR formal parameter cannot be used as VAR parameter"],
  889. ["PROCEDURE p(r: T); BEGIN recordVar(r); END p",
  890. "non-VAR formal parameter cannot be used as VAR parameter"],
  891. ["PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  892. "cannot assign to non-VAR formal parameter"],
  893. ["PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p",
  894. "cannot assign to non-VAR formal parameter"]
  895. )
  896. ),
  897. "array": {
  898. "static array indexOf": testWithContext(
  899. context(grammar.expression,
  900. "TYPE "
  901. + "T = RECORD END;"
  902. + "VAR "
  903. + "r: T;"
  904. + "intArray: ARRAY 3 OF INTEGER;"
  905. + "boolDynArray: ARRAY * OF BOOLEAN;"
  906. + "recordArray: ARRAY 3 OF T;"
  907. + "arrayOfArray: ARRAY 3, 4 OF INTEGER;"
  908. ),
  909. pass("intArray.indexOf(0)",
  910. "boolDynArray.indexOf(FALSE) = -1"
  911. ),
  912. fail(["intArray.indexOf(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  913. ["recordArray.indexOf(r)", "'indexOf' is not defined for array of 'T'"],
  914. ["arrayOfArray.indexOf(intArray)", "'indexOf' is not defined for array of 'ARRAY 4 OF INTEGER'"],
  915. ["intArray.indexOf", "array method 'indexOf' cannot be referenced"]
  916. )
  917. ),
  918. "open array indexOf": testWithGrammar(
  919. grammar.declarationSequence,
  920. pass("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN a.indexOf(123) END p;"
  921. )
  922. ),
  923. "dynamic": {
  924. "declaration": testWithContext(
  925. context(grammar.declarationSequence,
  926. "TYPE DA = ARRAY * OF INTEGER;"),
  927. pass("TYPE A = ARRAY * OF INTEGER;",
  928. "TYPE A = ARRAY * OF ARRAY * OF INTEGER;",
  929. "TYPE A = ARRAY *, * OF INTEGER;",
  930. "TYPE A = ARRAY 3, * OF INTEGER;",
  931. "TYPE A = ARRAY *, 3 OF INTEGER;",
  932. "TYPE P = PROCEDURE(): DA;",
  933. "TYPE P = PROCEDURE(VAR a: DA): DA;",
  934. "TYPE P = PROCEDURE(VAR a: ARRAY * OF INTEGER): DA;",
  935. "VAR a: ARRAY * OF INTEGER;",
  936. "PROCEDURE p(VAR a: ARRAY * OF INTEGER);END p;",
  937. "PROCEDURE p(VAR a: ARRAY * OF ARRAY * OF INTEGER);END p;",
  938. "PROCEDURE p(VAR a: ARRAY OF ARRAY * OF INTEGER);END p;"
  939. ),
  940. fail(["TYPE A = ARRAY OF INTEGER;", "not parsed"],
  941. ["TYPE P = PROCEDURE(): ARRAY OF INTEGER;", "';' expected"],
  942. ["TYPE P = PROCEDURE(a: DA);", "dynamic array has no use as non-VAR argument 'a'"],
  943. ["TYPE P = PROCEDURE(a: ARRAY * OF INTEGER);", "dynamic array has no use as non-VAR argument 'a'"],
  944. ["PROCEDURE p(a: DA);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  945. ["PROCEDURE p(a: ARRAY * OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  946. ["PROCEDURE p(a: ARRAY OF ARRAY * OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"],
  947. ["PROCEDURE p(a: ARRAY * OF ARRAY OF INTEGER);END p;", "dynamic array has no use as non-VAR argument 'a'"]
  948. )
  949. ),
  950. "return": testWithContext(
  951. context(grammar.declarationSequence,
  952. "TYPE A = ARRAY * OF INTEGER; B = ARRAY * OF BOOLEAN;"
  953. + "VAR a: A; b: B;"),
  954. pass("PROCEDURE p(): A; RETURN a END p;",
  955. "PROCEDURE p(): A; VAR static: ARRAY 3 OF INTEGER; RETURN static END p;"),
  956. fail(["PROCEDURE p(): ARRAY OF INTEGER; RETURN a; END p;", "not parsed"],
  957. ["PROCEDURE p(): A; RETURN b; END p;", "RETURN 'ARRAY * OF INTEGER' expected, got 'ARRAY * OF BOOLEAN'"])
  958. ),
  959. "pass as non-VAR argument": testWithContext(
  960. context(grammar.statement,
  961. "TYPE Int3 = ARRAY 3 OF INTEGER;"
  962. + "VAR dInt: ARRAY * OF INTEGER;"
  963. + "dIntInt: ARRAY *,* OF INTEGER;"
  964. + "PROCEDURE pOpenInt(a: ARRAY OF INTEGER); END pOpenInt;"
  965. + "PROCEDURE pOpenIntOfInt(a: ARRAY OF ARRAY OF INTEGER); END pOpenIntOfInt;"
  966. + "PROCEDURE pInt3(a: Int3); END pInt3;"),
  967. pass("pOpenInt(dInt)",
  968. "pOpenIntOfInt(dIntInt)"),
  969. fail(["pInt3(dInt)", "type mismatch for argument 1: 'ARRAY * OF INTEGER' cannot be converted to 'ARRAY 3 OF INTEGER'"])
  970. ),
  971. "pass as VAR argument": testWithContext(
  972. context(grammar.statement,
  973. "TYPE A = ARRAY * OF INTEGER; B = ARRAY * OF BOOLEAN;"
  974. + "VAR a: A; b: B; aStatic: ARRAY 3 OF INTEGER;"
  975. + "aIntInt: ARRAY * OF ARRAY * OF INTEGER;"
  976. + "aInt3Int: ARRAY * OF ARRAY 3 OF INTEGER;"
  977. + "PROCEDURE paVar(VAR a: A); END paVar;"
  978. + "PROCEDURE paVarOpen(VAR a: ARRAY OF INTEGER); END paVarOpen;"
  979. + "PROCEDURE pDynamicIntOfInt(VAR a: ARRAY * OF ARRAY * OF INTEGER); END pDynamicIntOfInt;"
  980. + "PROCEDURE pDynamicIntOfOpenInt(VAR a: ARRAY * OF ARRAY OF INTEGER); END pDynamicIntOfOpenInt;"
  981. ),
  982. pass("paVar(a)",
  983. "paVarOpen(a)",
  984. "pDynamicIntOfInt(aIntInt)",
  985. "pDynamicIntOfOpenInt(aIntInt)",
  986. "pDynamicIntOfOpenInt(aInt3Int)"
  987. ),
  988. fail(["paVar(aStatic)", "type mismatch for argument 1: cannot pass 'ARRAY 3 OF INTEGER' as VAR parameter of type 'ARRAY * OF INTEGER'"],
  989. ["pDynamicIntOfInt(aInt3Int)", "type mismatch for argument 1: 'ARRAY *, 3 OF INTEGER' cannot be converted to 'ARRAY *, * OF INTEGER'"]
  990. )
  991. ),
  992. "assign": testWithContext(
  993. context(grammar.statement,
  994. "VAR stat: ARRAY 3 OF INTEGER; dynamic: ARRAY * OF INTEGER;"),
  995. pass("dynamic := stat"),
  996. fail(["stat := dynamic", "type mismatch: 'stat' is 'ARRAY 3 OF INTEGER' and cannot be assigned to 'ARRAY * OF INTEGER' expression"],
  997. ["dynamic := NIL", "type mismatch: 'dynamic' is 'ARRAY * OF INTEGER' and cannot be assigned to 'NIL' expression"])
  998. ),
  999. "indexing": testWithContext(
  1000. context(grammar.expression,
  1001. "VAR a: ARRAY * OF INTEGER;"),
  1002. pass("a[0]", "a[1]"),
  1003. fail(["a[-1]", "index is negative: -1"],
  1004. ["a[-2]", "index is negative: -2"])
  1005. ),
  1006. "indexOf": testWithContext(
  1007. context(grammar.expression,
  1008. "VAR intArray: ARRAY * OF INTEGER;"
  1009. ),
  1010. pass("intArray.indexOf(0)"),
  1011. fail()
  1012. ),
  1013. "add": testWithContext(
  1014. context(grammar.statement,
  1015. "VAR a: ARRAY * OF INTEGER;"
  1016. + "a2: ARRAY * OF ARRAY * OF INTEGER;"
  1017. + "aStatic: ARRAY 3 OF INTEGER;"
  1018. + "byte: BYTE;"),
  1019. pass("a.add(123)",
  1020. "a.add(byte)",
  1021. "a2.add(a)",
  1022. "a2.add(aStatic)"
  1023. ),
  1024. fail(["a.add := NIL", "cannot assign to method"],
  1025. ["v <- a.add", "dynamic array method 'add' cannot be referenced"],
  1026. ["a.add()", "method 'add' expects one argument, got nothing"],
  1027. ["a.add(1, 2)", "method 'add' expects one argument, got many"],
  1028. ["a.add(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
  1029. )
  1030. ),
  1031. "add open array to dynamic array of static arrays": testWithContext(
  1032. context(grammar.declarationSequence,
  1033. "VAR a: ARRAY * OF ARRAY 3 OF INTEGER;"),
  1034. pass(),
  1035. fail(["PROCEDURE p(paramA: ARRAY OF INTEGER); BEGIN a.add(paramA); END p",
  1036. "type mismatch for argument 1: 'ARRAY OF INTEGER' cannot be converted to 'ARRAY 3 OF INTEGER'"]
  1037. )
  1038. ),
  1039. "remove": testWithContext(
  1040. context(grammar.statement,
  1041. "VAR a: ARRAY * OF INTEGER;"),
  1042. pass("a.remove(0)"),
  1043. fail(["a.remove(-1)", "index is negative: -1"],
  1044. ["a.remove()", "1 argument(s) expected, got 0"],
  1045. ["a.remove(0, 1)", "1 argument(s) expected, got 2"],
  1046. ["a.remove(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  1047. ["a.Remove(0)", "selector '.Remove' cannot be applied to 'ARRAY * OF INTEGER'"]
  1048. )
  1049. ),
  1050. "clear": testWithContext(
  1051. context(grammar.statement,
  1052. "VAR a: ARRAY * OF INTEGER;"),
  1053. pass("a.clear()"),
  1054. fail(["a.clear(0)", "0 argument(s) expected, got 1"])
  1055. )
  1056. }
  1057. },
  1058. "syntax relaxation": testWithGrammar(
  1059. grammar.declarationSequence,
  1060. pass("PROCEDURE p; END;",
  1061. "TYPE T = RECORD field: INTEGER; END;",
  1062. "TYPE T = RECORD PROCEDURE method(); END;",
  1063. "TYPE T = RECORD PROCEDURE method(); END; PROCEDURE T.method(); END;",
  1064. "PROCEDURE p(): INTEGER; RETURN 0; END;"
  1065. )
  1066. ),
  1067. "constructor": {
  1068. "declaration": testWithGrammar(
  1069. grammar.declarationSequence,
  1070. pass("TYPE T = RECORD END; PROCEDURE T(); END;",
  1071. "TYPE T = RECORD i: INTEGER; END; PROCEDURE T(); BEGIN SELF.i := 0; END;",
  1072. "TYPE T = RECORD END; PROCEDURE T(); END T;",
  1073. "TYPE T = RECORD END; PROCEDURE p(); PROCEDURE T(); END; BEGIN T(); END;", /* local procedure name may match type name from outer scope*/
  1074. "TYPE T = RECORD END; PROCEDURE T(a: INTEGER); END T;"
  1075. ),
  1076. fail(["TYPE T = RECORD END; PROCEDURE T(); END; PROCEDURE T(); END;", "constructor 'T' already defined"],
  1077. ["TYPE T = RECORD END; PROCEDURE T(): INTEGER; RETURN 0; END;", "constructor 'T' cannot have result type specified"],
  1078. ["TYPE T = ARRAY 3 OF INTEGER; PROCEDURE T(); END;", "'T' already declared"],
  1079. ["TYPE T = RECORD END; PROCEDURE T(); END T.T;", "mismatched method names: expected 'T' at the end (or nothing), got 'T.T'"],
  1080. ["TYPE T = RECORD END; PROCEDURE T.T(); END;", "'T' has no declaration for method 'T'"],
  1081. ["TYPE T = RECORD END; PROCEDURE T(); END T2;", "mismatched method names: expected 'T' at the end (or nothing), got 'T2'"],
  1082. ["TYPE T = RECORD END; PROCEDURE T(); END T.T;", "mismatched method names: expected 'T' at the end (or nothing), got 'T.T'"]
  1083. )
  1084. ),
  1085. "as expression": testWithContext(
  1086. context(grammar.expression,
  1087. "TYPE T = RECORD i: INTEGER; END; PT = POINTER TO T;"
  1088. + "ConsWithArguments = RECORD END;"
  1089. + "PROCEDURE ConsWithArguments(a: INTEGER); END;"
  1090. + "PROCEDURE byVar(VAR a: T): INTEGER; RETURN 0; END;"
  1091. + "PROCEDURE byNonVar(a: T): INTEGER; RETURN 0; END;"
  1092. ),
  1093. pass("T()",
  1094. "byNonVar(T())",
  1095. "T().i",
  1096. "ConsWithArguments(123)"
  1097. ),
  1098. fail(["PT()", "PROCEDURE expected, got 'type PT'"],
  1099. ["byVar(T())", "expression cannot be used as VAR parameter"],
  1100. ["T(0)", "0 argument(s) expected, got 1"],
  1101. ["ConsWithArguments()", "1 argument(s) expected, got 0"],
  1102. ["ConsWithArguments(FALSE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
  1103. )
  1104. ),
  1105. "initialize in place variable": testWithContext(
  1106. context(grammar.statement,
  1107. "TYPE T = RECORD END;"),
  1108. pass("r <- T()"),
  1109. fail()
  1110. )
  1111. }
  1112. };