test_unit_eberon.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. "method call": testWithContext(
  151. context(grammar.expression,
  152. "TYPE T = RECORD PROCEDURE p(); PROCEDURE f(): INTEGER END;"
  153. + "VAR o: T;"
  154. + "PROCEDURE T.p(); END T.p;"
  155. + "PROCEDURE T.f(): INTEGER; RETURN 0 END T.f;"
  156. ),
  157. pass("o.f()"),
  158. fail(["o.p()", "procedure returning no result cannot be used in an expression"])
  159. ),
  160. "cannot assign to method": testWithContext(
  161. context(grammar.statement,
  162. "TYPE T = RECORD PROCEDURE p() END;"
  163. + "VAR o: T;"
  164. + "PROCEDURE T.p(); END T.p;"
  165. ),
  166. pass(),
  167. fail(["o.p := o.p", "cannot assign to method"],
  168. ["o.p := NIL", "cannot assign to method"])
  169. ),
  170. "method cannot be referenced": testWithContext(
  171. context(grammar.statement,
  172. "TYPE T = RECORD PROCEDURE p() END;"
  173. + "Proc = PROCEDURE();"
  174. + "VAR o: T;"
  175. + "PROCEDURE T.p(); END T.p;"
  176. + "PROCEDURE proc(p: Proc); END proc;"
  177. ),
  178. pass(),
  179. fail(["proc(o.p)", "type mismatch for argument 1: 'method p' cannot be converted to 'Proc'"])
  180. ),
  181. "method super call": testWithContext(
  182. context(grammar.declarationSequence,
  183. "TYPE T = RECORD PROCEDURE p(); PROCEDURE pAbstract(); PROCEDURE pAbstract2() END;"
  184. + "D = RECORD(T) PROCEDURE pNoSuper() END;"
  185. + "PROCEDURE T.p(); END T.p;"
  186. ),
  187. pass("PROCEDURE D.p(); BEGIN SUPER() END D.p;"),
  188. fail(["PROCEDURE D.pNoSuper(); BEGIN SUPER() END D.pNoSuper;",
  189. "there is no method 'pNoSuper' in base type(s)"],
  190. ["PROCEDURE p(); BEGIN SUPER() END p;",
  191. "SUPER can be used only in methods"],
  192. ["PROCEDURE T.pNoBase(); BEGIN SUPER() END T.pNoBase;",
  193. "'T' has no base type - SUPER cannot be used"],
  194. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract;",
  195. "cannot use abstract method(s) in SUPER calls: pAbstract"],
  196. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract; PROCEDURE D.pAbstract2(); BEGIN SUPER() END D.pAbstract2;",
  197. "cannot use abstract method(s) in SUPER calls: pAbstract, pAbstract2"]
  198. )
  199. ),
  200. "export method": testWithContext(
  201. context(grammar.declarationSequence,
  202. "TYPE T = RECORD PROCEDURE p() END;"
  203. ),
  204. pass(),
  205. fail(["PROCEDURE T.p*(); END T.p;",
  206. "method implementation cannot be exported: p"])
  207. ),
  208. "import method": testWithModule(
  209. "MODULE test;"
  210. + "TYPE T* = RECORD PROCEDURE m*(); PROCEDURE mNotExported() END;"
  211. + "PROCEDURE T.m(); END T.m; PROCEDURE T.mNotExported(); END T.mNotExported;"
  212. + "END test.",
  213. pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.m(); END m.",
  214. "MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.m(); END T.m; END m."
  215. ),
  216. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.mNotExported(); END m.",
  217. "type 'T' has no 'mNotExported' field"],
  218. ["MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.mNotExported(); END T.mNotExported; END m.",
  219. "'T' has no declaration for method 'mNotExported'"])
  220. ),
  221. "non-scalar variables can be exported": testWithContext(
  222. context(grammar.declarationSequence,
  223. "TYPE T = RECORD END; A = ARRAY 3 OF INTEGER;"
  224. ),
  225. pass("VAR r*: T;",
  226. "VAR a*: A;"),
  227. fail()
  228. ),
  229. "export as read-only": testWithContext(
  230. context(grammar.declarationSequence, ""),
  231. pass("TYPE T* = RECORD i-: INTEGER END;"),
  232. fail(["TYPE T- = RECORD END;",
  233. "type cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  234. ["PROCEDURE p-(); END p;",
  235. "procedure cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  236. ["CONST c- = 123;",
  237. "constant cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  238. ["VAR i-: INTEGER;",
  239. "variable cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  240. ["TYPE T* = RECORD PROCEDURE p-() END;",
  241. "method cannot be exported as read-only using '-' mark (did you mean '*'?)"]
  242. )
  243. ),
  244. "field exported as read-only is writable in current module": testWithContext(
  245. context(grammar.statement,
  246. "TYPE T* = RECORD i-: INTEGER END;"
  247. + "VAR r: T;"
  248. ),
  249. pass("r.i := 123"),
  250. fail()
  251. ),
  252. "import as read-only": testWithModule(
  253. "MODULE test; TYPE T* = RECORD f-: INTEGER END; END test.",
  254. pass(),
  255. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 123; END m.",
  256. "cannot assign to read-only variable"],
  257. ["MODULE m; IMPORT test; TYPE D = RECORD(test.T) END; VAR r: D; BEGIN r.f := 123; END m.",
  258. "cannot assign to read-only variable"]
  259. )),
  260. "STRING variable": testWithGrammar(
  261. grammar.variableDeclaration,
  262. pass("s: STRING")
  263. ),
  264. "STRING expression": testWithContext(
  265. context(grammar.expression,
  266. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  267. pass("s1 + s2",
  268. "s1 + \"abc\"",
  269. "\"abc\" + s1",
  270. "s1 = s2",
  271. "s1 # s2",
  272. "s1 < s2",
  273. "s1 > s2",
  274. "s1 <= s2",
  275. "s1 >= s2"
  276. ),
  277. fail(["s1 = NIL", "type mismatch: expected 'STRING', got 'NIL'"],
  278. ["s1 = a", "type mismatch: expected 'STRING', got 'ARRAY 10 OF CHAR'"],
  279. ["a = s1", "type mismatch: expected 'ARRAY 10 OF CHAR', got 'STRING'"]
  280. )
  281. ),
  282. "STRING literal expression": testWithContext(
  283. context(grammar.expression,
  284. "CONST cs = \"abc\";"
  285. + "PROCEDURE pString(s: STRING): STRING; RETURN s END pString;"
  286. + "PROCEDURE pStringByRef(VAR s: STRING): STRING; RETURN s END pStringByRef;"
  287. ),
  288. pass("\"abc\" + \"cde\"",
  289. "cs + cs",
  290. "cs + \"abc\"",
  291. "cs = \"abc\"",
  292. "cs # \"abc\"",
  293. "cs < \"abc\"",
  294. "cs > \"abc\"",
  295. "cs <= \"abc\"",
  296. "cs >= \"abc\"",
  297. "pString(cs)",
  298. "pString(\"abc\")"
  299. ),
  300. fail(["pStringByRef(cs)", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"],
  301. ["pStringByRef(\"abc\")", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"]
  302. )
  303. ),
  304. "STRING assignment": testWithContext(
  305. context(grammar.statement,
  306. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  307. pass("s1 := s2",
  308. "s1 := \"abc\"",
  309. "s1 := 22X"
  310. ),
  311. fail(["a := s1", "type mismatch: 'a' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'STRING' expression"],
  312. ["s1 := a", "type mismatch: 's1' is 'STRING' and cannot be assigned to 'ARRAY 10 OF CHAR' expression"]
  313. )
  314. ),
  315. "STRING and ARRAY OF CHAR": testWithContext(
  316. context(grammar.expression,
  317. "VAR s: STRING; a: ARRAY 10 OF CHAR;"
  318. + "PROCEDURE pArray(a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pArray;"
  319. + "PROCEDURE pString(s: STRING): BOOLEAN; RETURN FALSE END pString;"
  320. + "PROCEDURE pVar(VAR a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pVar;"
  321. + "PROCEDURE pIntArray(a: ARRAY OF INTEGER): BOOLEAN; RETURN FALSE END pIntArray;"
  322. ),
  323. pass("pArray(s)"),
  324. fail(["pVar(s)", "type mismatch for argument 1: cannot pass 'STRING' as VAR parameter of type 'ARRAY OF CHAR'"],
  325. ["pString(a)", "type mismatch for argument 1: 'ARRAY 10 OF CHAR' cannot be converted to 'STRING'"],
  326. ["pIntArray(s)", "type mismatch for argument 1: 'STRING' cannot be converted to 'ARRAY OF INTEGER'"]
  327. )
  328. ),
  329. "STRING LEN": testWithContext(
  330. context(grammar.expression,
  331. "VAR s: STRING;"),
  332. pass("LEN(s)"),
  333. fail()
  334. ),
  335. "STRING indexing": testWithContext(
  336. context(grammar.expression,
  337. "VAR s: STRING;"
  338. + "PROCEDURE pCharByVar(VAR c: CHAR): CHAR; RETURN c END pCharByVar;"),
  339. pass("s[0]"),
  340. fail(["s[-1]", "index is negative: -1"],
  341. ["pCharByVar(s[0])", "string element cannot be used as VAR parameter"]
  342. )
  343. ),
  344. "designate call result in expression": testWithContext(
  345. context(grammar.expression,
  346. "TYPE PT = POINTER TO RECORD field: INTEGER END;"
  347. + "VAR p: PT;"
  348. + "PROCEDURE proc(): PT; RETURN p END proc;"
  349. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  350. + "PROCEDURE intVar(VAR i: INTEGER): INTEGER; RETURN i END intVar;"),
  351. pass("proc().field",
  352. "intVar(proc().field)"),
  353. fail(["intVar(int())", "expression cannot be used as VAR parameter"])
  354. ),
  355. "designate call result in statement": testWithContext(
  356. context(grammar.statement,
  357. "TYPE PT = POINTER TO RECORD field: INTEGER; proc: PROCEDURE END;"
  358. + "ProcType = PROCEDURE;"
  359. + "VAR p: PT;"
  360. + "PROCEDURE procVoid(); END procVoid;"
  361. + "PROCEDURE proc(): PT; RETURN p END proc;"
  362. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  363. + "PROCEDURE intVar(VAR i: INTEGER); END intVar;"
  364. + "PROCEDURE returnProc(): ProcType; RETURN procVoid END returnProc;"
  365. ),
  366. pass("proc().field := 0",
  367. "proc().proc()",
  368. "proc().proc"
  369. ),
  370. fail(["int() := 0", "cannot assign to procedure call result"],
  371. ["intVar(int())", "expression cannot be used as VAR parameter"],
  372. ["procVoid()()", "PROCEDURE expected, got 'procedure call statement'"],
  373. ["int()()", "PROCEDURE expected, got 'INTEGER'"],
  374. ["returnProc()", "procedure returning a result cannot be used as a statement"] // call is not applied implicitly to result
  375. )
  376. ),
  377. "in place variables": {
  378. "initialization": testWithContext(
  379. context(grammar.statement,
  380. "VAR i: INTEGER;"
  381. + "PROCEDURE p(): BOOLEAN; RETURN FALSE END p;"
  382. + "PROCEDURE void(); END void;"
  383. ),
  384. pass("v <- 0",
  385. "v <- 1.23",
  386. "v <- \"abc\"",
  387. "v <- TRUE",
  388. "v <- i",
  389. "v <- i + i",
  390. "v <- p()",
  391. "v <- void" // procedure type
  392. ),
  393. fail(["v <-", "initialization expression expected"],
  394. ["v <- void()", "procedure returning no result cannot be used in an expression"])
  395. ),
  396. "scope": testWithContext(
  397. temporaryValues.context,
  398. temporaryValues.passStatements(
  399. "v1 <- 0; v2 <-0;",
  400. "i <- 0; ASSERT(i = 0);",
  401. "WHILE FALSE DO v <- 0; ASSERT(v = 0); END; WHILE FALSE DO v <- 0; END;",
  402. "WHILE FALSE DO i1 <- 0; WHILE FALSE DO i2 <- 0; ASSERT(i1 = 0); ASSERT(i2 = 0); END; END;",
  403. "WHILE bVar DO v <- 0; ELSIF ~bVar DO v <- 0 END;",
  404. "IF FALSE THEN v <- 0; ASSERT(v = 0); END; IF FALSE THEN v <- 0; END;",
  405. "IF FALSE THEN v <- 0; END; IF FALSE THEN v <- 0; END;",
  406. "IF FALSE THEN v <- 0; ELSIF FALSE THEN v <- 0; ELSE v <- 0; END;",
  407. "i <- 0; CASE i OF 0: v <- 0 | 1: v <- 1; ; ASSERT(v = 1); END;",
  408. "REPEAT v <- 0; UNTIL FALSE; REPEAT v <- 0; UNTIL FALSE;",
  409. "REPEAT v <- 0; ASSERT(v = 0); UNTIL v # 0;",
  410. "i <- 0; FOR i := 0 TO 10 DO v <- 0; END; FOR i := 0 TO 10 DO v <- 0; END;"
  411. ),
  412. fail(["PROCEDURE p(); BEGIN v <- 0; v <-0; END p;", "'v' already declared"],
  413. ["PROCEDURE p(); VAR v: INTEGER; BEGIN v <- 0; END p;", "'v' already declared"],
  414. ["PROCEDURE p(); BEGIN v <- 0; WHILE FALSE DO v <- 0; END; END p;",
  415. "'v' already declared in procedure scope"],
  416. ["PROCEDURE p(); BEGIN i <- 0; IF FALSE THEN i <- 0; END; END p;",
  417. "'i' already declared in procedure scope"],
  418. ["PROCEDURE p(); BEGIN WHILE FALSE DO i <- 0; WHILE FALSE DO i <- 0; END; END; END p;",
  419. "'i' already declared in procedure scope"]
  420. )
  421. ),
  422. "type promotion in expression": testWithContext(
  423. temporaryValues.context,
  424. temporaryValues.passExpressions(
  425. "(b IS PDerived) & b.flag",
  426. "(b IS PDerived) & bVar & b.flag",
  427. "(b IS PDerived) & (bVar OR b.flag)",
  428. "(b IS PDerived) & (b2 IS PDerived) & b.flag & b2.flag",
  429. "(b IS PDerived) & proc(TRUE) & b.flag",
  430. "(b IS PDerived) & ~proc(TRUE) & b.flag",
  431. "~(~(b IS PDerived)) & b.flag",
  432. "~~(b IS PDerived) & b.flag",
  433. "(b IS PDerived) & ((b IS PDerived2) OR bVar) & b.flag"
  434. //TODO: "((b IS PDerived) = TRUE) & b.flag); END p;",
  435. ),
  436. temporaryValues.failExpressions(
  437. "(b IS PDerived) OR b.flag",
  438. "(b IS PDerived) OR bVar & b.flag",
  439. "~(b IS PDerived) & b.flag",
  440. "((b IS PDerived) & (b2 IS PDerived) OR bVar) & b.flag",
  441. "proc(b IS PDerived) & proc(b.flag)",
  442. "ORD(b IS PDerived) * ORD(b.flag) = 0",
  443. "((b IS PDerived) = FALSE) & b.flag",
  444. "b IS PDerived); ASSERT(b.flag"
  445. )
  446. ),
  447. "invert type promotion in expression": testWithContext(
  448. temporaryValues.context,
  449. temporaryValues.passExpressions(
  450. "~(b IS PDerived) OR b.flag",
  451. "~(b IS PDerived) OR b.flag OR bVar",
  452. "~(b IS PDerived) OR b.flag & bVar",
  453. "~(b IS PDerived) OR bVar & b.flag",
  454. "~(b IS PDerived) OR (bVar & b.flag)",
  455. "~(b IS PDerived) OR bVar OR b.flag",
  456. "~(b IS PDerived) OR (bVar = b.flag)",
  457. "~(~(b IS PDerived) OR bVar) & b.flag",
  458. "~(~(b IS PDerived) OR b.flag) & b.flag"
  459. ),
  460. temporaryValues.failExpressions(
  461. "(~(b IS PDerived) OR bVar) & b.flag",
  462. "(ORD(~(b IS PDerived)) + ORD(b.flag)",
  463. "~(~(b IS PDerived) OR bVar) OR b.flag",
  464. "~(~(b IS PDerived) & bVar) & b.flag",
  465. "~(b IS PDerived) OR b.flag = b.flag"
  466. )
  467. ),
  468. "type promotion in separate statements": testWithContext(
  469. temporaryValues.context,
  470. pass(),
  471. temporaryValues.failStatements("bVar := b IS PDerived; ASSERT(b.flag)")
  472. ),
  473. "type promotion in IF": testWithContext(
  474. temporaryValues.context,
  475. temporaryValues.passStatements(
  476. "IF b IS PDerived THEN b.flag := FALSE; END;",
  477. "IF (b IS PDerived) & bVar THEN b.flag := FALSE; END;",
  478. "IF FALSE THEN ELSIF b IS PDerived THEN b.flag := FALSE; END;",
  479. "IF b IS PDerived THEN bVar := (b IS PDerived2) & b.flag2; b.flag := FALSE; END;"
  480. ),
  481. temporaryValues.failStatements(
  482. "IF (b IS PDerived) OR bVar THEN b.flag := FALSE; END",
  483. "IF b IS PDerived THEN END; b.flag := FALSE",
  484. "IF ~(b IS PDerived) THEN END; b.flag := FALSE",
  485. "IF ~(b IS PDerived) THEN ELSIF bVar THEN END; b.flag := FALSE",
  486. "IF ~(b IS PDerived) THEN ELSIF bVar THEN ELSE END; b.flag := FALSE",
  487. "IF bVar THEN ELSIF b IS PDerived THEN ELSE END; b.flag := FALSE",
  488. "IF b IS PDerived THEN ELSE b.flag := FALSE; END",
  489. "IF b IS PDerived THEN ELSIF TRUE THEN b.flag := FALSE; END"
  490. )
  491. ),
  492. "invert type promotion in IF": testWithContext(
  493. temporaryValues.context,
  494. temporaryValues.passStatements(
  495. "IF ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  496. "IF ~(b IS PDerived) THEN ELSIF bVar THEN b.flag := FALSE; ELSE b.flag := FALSE; END;",
  497. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b.flag := FALSE; ELSE b.flag := FALSE; b2.flag := FALSE; END;",
  498. "IF ~(b IS PDerived) OR bVar THEN ELSE b.flag := FALSE; END;",
  499. "IF ~(b IS PDerived) OR b.flag THEN ELSE b.flag := FALSE; END;",
  500. "IF ~(b IS PDerived) THEN bVar := b IS PDerived; ELSE b.flag := FALSE; END;",
  501. "IF ~(b IS PDerived) THEN ASSERT((b IS PDerived) & b.flag); ELSE b.flag := FALSE; END;"
  502. ),
  503. temporaryValues.failStatements(
  504. "IF ~(b IS PDerived) & bVar THEN ELSE b.flag := FALSE; END; END p;",
  505. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b2.flag := FALSE; END;"
  506. )
  507. ),
  508. "type promotion in WHILE": testWithContext(
  509. temporaryValues.context,
  510. temporaryValues.passStatements(
  511. "WHILE (b IS PDerived) & b.flag DO END;",
  512. "WHILE ~(b IS PDerived) OR b.flag DO END;",
  513. "WHILE b IS PDerived DO b.flag := FALSE; END;",
  514. "WHILE ~(b IS PDerived) DO ELSIF b.flag DO END;",
  515. "WHILE ~(b IS PDerived) DO ELSIF bVar DO b.flag := FALSE; END;"
  516. ),
  517. temporaryValues.failStatements(
  518. "WHILE b IS PDerived DO END; b.flag := FALSE;"
  519. )
  520. ),
  521. "type promotion cannot be reset by assignment": testWithContext(
  522. temporaryValues.context,
  523. pass(),
  524. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN b := pBase; b.flag := FALSE; END; END p;",
  525. "type mismatch: 'b' is 'PDerived' and cannot be assigned to 'POINTER TO Base' expression"]
  526. )
  527. ),
  528. "type promotion cannot be reset by passing as VAR argument": testWithContext(
  529. temporaryValues.context,
  530. pass(),
  531. 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;",
  532. "type mismatch for argument 1: cannot pass 'PDerived' as VAR parameter of type 'PBase'"]
  533. )
  534. ),
  535. "IS expression after type promotion": testWithContext(
  536. temporaryValues.context,
  537. pass(),
  538. fail(["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN bVar := b IS PDerived; b.flag := FALSE; END; END p;",
  539. "invalid type test: 'Derived' is not an extension of 'Derived'"]
  540. )
  541. ),
  542. "record types as values": testWithContext(
  543. context(grammar.declarationSequence,
  544. "TYPE Base = RECORD pBase: POINTER TO Base END; Derived = RECORD (Base) END;"
  545. + "VAR base: Base;"
  546. + "PROCEDURE procBaseVar(VAR b: Base); END procBaseVar;"
  547. ),
  548. pass("PROCEDURE p(b: Base); BEGIN base <- b; procBaseVar(base); base := b; END p;"),
  549. fail(["PROCEDURE p(); BEGIN baseVar <- base.pBase^; ASSERT(base IS Derived); END p;",
  550. "invalid type test: a value variable cannot be used"],
  551. ["PROCEDURE p(VAR b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  552. "invalid type test: a value variable cannot be used"],
  553. ["PROCEDURE p(b: Base); BEGIN base <- b; ASSERT(base IS Derived); END p;",
  554. "invalid type test: a value variable cannot be used"]
  555. )
  556. ),
  557. "arrays as values": testWithContext(
  558. context(grammar.declarationSequence,
  559. "TYPE A = ARRAY 3 OF INTEGER; T = RECORD a: A END;"
  560. + "VAR r: T;"
  561. + "PROCEDURE procArrayVar(VAR a: A); END procArrayVar;"
  562. ),
  563. pass("PROCEDURE p(r: T); BEGIN a <- r.a; a[0] := 123; procArrayVar(a); END p;",
  564. "PROCEDURE p(a: A); BEGIN tmp <- a; END p;",
  565. "PROCEDURE p(); VAR a: A; BEGIN tmp <- a; END p;",
  566. "PROCEDURE p(); VAR a: ARRAY 3 OF BOOLEAN; BEGIN tmp <- a; END p;"
  567. ),
  568. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN v <- a; END p;",
  569. "cannot initialize variable 'v' with open array"]
  570. )
  571. )/*
  572. /*
  573. "as references": testWithContext(
  574. context(grammar.declarationSequence,
  575. "TYPE Base = RECORD pBase: POINTER TO Base END; Derived = RECORD (Base) END;"
  576. + "VAR base: Base;"
  577. ),
  578. pass("PROCEDURE p(); BEGIN baseRef <- base.pBase^; ASSERT(baseRef IS Derived); END p;",
  579. "PROCEDURE p(VAR b: Base); BEGIN baseRef <- b; ASSERT(baseRef IS Derived); END p;"),
  580. fail(["PROCEDURE p(b: Base); BEGIN baseRef <- b; ASSERT(baseRef IS Derived); END p;",
  581. "invalid type test: a value variable cannot be used"])
  582. )*/
  583. }
  584. };