test_unit_eberon.js 37 KB

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