2
0

test_unit_eberon.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. + "PDerived = POINTER TO Derived;"
  24. + "PDerived2 = POINTER TO Derived2;"
  25. + "VAR pBase: POINTER TO Base; bVar: BOOLEAN;"
  26. + "PROCEDURE proc(b: BOOLEAN): BOOLEAN; RETURN b END proc;"),
  27. __expression: function(e){
  28. return "PROCEDURE p(); BEGIN b <- pBase; b2 <- pBase; ASSERT(" + e + "); END p;";
  29. },
  30. __statement: function(e){
  31. return "PROCEDURE p(); BEGIN b <- pBase; b2 <- pBase; " + e + " END p;";
  32. },
  33. passExpressions: function(){
  34. return this.__pass(this.__expression.bind(this), arguments);
  35. },
  36. passStatements: function(){
  37. return this.__pass(this.__statement.bind(this), arguments);
  38. },
  39. failExpressions: function(){
  40. return this.__fail(this.__expression.bind(this), arguments);
  41. },
  42. failStatements: function(){
  43. return this.__fail(this.__statement.bind(this), arguments);
  44. },
  45. __pass: function(make, cases){
  46. var result = [];
  47. for(var i = 0; i < cases.length; ++i)
  48. result.push(make(cases[i]));
  49. return pass.apply(this, result);
  50. },
  51. __fail: function(make, cases){
  52. var result = [];
  53. for(var i = 0; i < cases.length; ++i)
  54. result.push([make(cases[i]), "type 'Base' has no 'flag' field"]);
  55. return fail.apply(this, result);
  56. }
  57. };
  58. exports.suite = {
  59. "arithmetic operators": testWithContext(
  60. context(grammar.statement, "VAR b1: BOOLEAN;"),
  61. pass(),
  62. fail(["b1 := b1 + b1", "operator '+' type mismatch: numeric type or SET or STRING expected, got 'BOOLEAN'"])
  63. ),
  64. "key words": testWithGrammar(
  65. grammar.variableDeclaration,
  66. pass(),
  67. fail(["SELF: INTEGER", "not parsed"],
  68. ["SUPER: INTEGER", "not parsed"],
  69. ["STRING: INTEGER", "not parsed"]
  70. )
  71. ),
  72. "abstract method declaration": testWithContext(
  73. context(grammar.declarationSequence,
  74. "TYPE T = RECORD PROCEDURE p() END;"
  75. + "D = RECORD(T) END;"
  76. + "T2 = RECORD PROCEDURE p1(); PROCEDURE p2(i: INTEGER): BOOLEAN END;"
  77. ),
  78. pass(),
  79. fail(["VAR r: T;",
  80. "cannot instantiate 'T' because it has abstract method(s): p"],
  81. ["VAR r: T2;",
  82. "cannot instantiate 'T2' because it has abstract method(s): p1, p2"],
  83. ["PROCEDURE p(); VAR p: POINTER TO T; BEGIN NEW(p); END p;",
  84. "cannot instantiate 'T' because it has abstract method(s): p"],
  85. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR r: LocalT; END p;",
  86. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  87. ["PROCEDURE p(); TYPE LocalT = RECORD(T) END; VAR p: POINTER TO LocalT; BEGIN NEW(p) END p;",
  88. "cannot instantiate 'LocalT' because it has abstract method(s): p"],
  89. ["VAR r: D;",
  90. "cannot instantiate 'D' because it has abstract method(s): p"],
  91. ["PROCEDURE p(); VAR p: POINTER TO D; BEGIN NEW(p); END p;",
  92. "cannot instantiate 'D' because it has abstract method(s): p"]
  93. )
  94. ),
  95. "new method declaration": testWithContext(
  96. context(grammar.declarationSequence,
  97. "TYPE T = RECORD PROCEDURE p(); intField: INTEGER END; A = ARRAY 1 OF INTEGER;"),
  98. pass("PROCEDURE T.p(); END T.p;"
  99. ),
  100. fail(["PROCEDURE TUnk.p(), NEW; END TUnk.p;", "undeclared identifier: 'TUnk'"],
  101. ["PROCEDURE A.p(), NEW; END A.p;",
  102. "RECORD type expected in method declaration, got 'ARRAY 1 OF INTEGER'"],
  103. ["PROCEDURE T.p(), NEW; END;", "not parsed"],
  104. ["PROCEDURE T.p(); END p;",
  105. "mismatched procedure names: 'T.p' at the begining and 'p.' at the end"],
  106. ["PROCEDURE T.p(); END T2.p;",
  107. "mismatched procedure names: 'T.p' at the begining and 'T2.p' at the end"],
  108. ["PROCEDURE T.p(); END T.p2;",
  109. "mismatched procedure names: 'T.p' at the begining and 'T.p2' at the end"],
  110. ["PROCEDURE T.intField(); END T.intField;",
  111. "'T' has no declaration for method 'intField'"],
  112. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(), NEW; END T.p;",
  113. "'T.p' already declared"],
  114. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); PROCEDURE m() END; END p;",
  115. "cannot declare a new method 'm': method already was declared"],
  116. ["PROCEDURE p(); TYPE T = RECORD m: INTEGER; PROCEDURE m() END; END p;",
  117. "cannot declare method, record already has field 'm'"],
  118. ["PROCEDURE p(); TYPE T = RECORD PROCEDURE m(); m: INTEGER END; END p;",
  119. "cannot declare field, record already has method 'm'"]
  120. )
  121. ),
  122. "overridden method declaration": testWithContext(
  123. context(grammar.declarationSequence,
  124. "TYPE Base = RECORD PROCEDURE p() END; T = RECORD (Base) END;"
  125. + "PROCEDURE Base.p(); END Base.p;"),
  126. pass("PROCEDURE T.p(); END T.p;"),
  127. fail(["PROCEDURE T.pUnk(); END T.pUnk;",
  128. "'T' has no declaration for method 'pUnk'"],
  129. ["PROCEDURE proc(); TYPE T = RECORD (Base) PROCEDURE p() END; END proc;",
  130. "cannot declare a new method 'p': method already was declared"],
  131. ["PROCEDURE T.p(); END T.p; PROCEDURE T.p(); END T.p;",
  132. "'T.p' already declared"],
  133. ["PROCEDURE T.p(a: INTEGER); END T.p;",
  134. "overridden method 'p' signature mismatch: should be 'PROCEDURE', got 'PROCEDURE(INTEGER)'"],
  135. ["PROCEDURE p(); PROCEDURE T.p(); END T.p; END p;",
  136. "method should be defined in the same scope as its bound type 'T'"]
  137. )
  138. ),
  139. "SELF": testWithContext(
  140. context(grammar.declarationSequence,
  141. "TYPE T = RECORD PROCEDURE p(); i: INTEGER END;"
  142. + "PROCEDURE proc(i: INTEGER); END proc;"),
  143. pass("PROCEDURE T.p(); BEGIN SELF.i := 0; END T.p;",
  144. "PROCEDURE T.p(); BEGIN proc(SELF.i); END T.p;"
  145. ),
  146. fail(["PROCEDURE p(); BEGIN SELF.i := 0; END p;",
  147. "SELF can be used only in methods"])
  148. ),
  149. "method call": testWithContext(
  150. context(grammar.expression,
  151. "TYPE T = RECORD PROCEDURE p(); PROCEDURE f(): INTEGER END;"
  152. + "VAR o: T;"
  153. + "PROCEDURE T.p(); END T.p;"
  154. + "PROCEDURE T.f(): INTEGER; RETURN 0 END T.f;"
  155. ),
  156. pass("o.f()"),
  157. fail(["o.p()", "procedure returning no result cannot be used in an expression"])
  158. ),
  159. "cannot assign to method": testWithContext(
  160. context(grammar.statement,
  161. "TYPE T = RECORD PROCEDURE p() END;"
  162. + "VAR o: T;"
  163. + "PROCEDURE T.p(); END T.p;"
  164. ),
  165. pass(),
  166. fail(["o.p := o.p", "cannot assign to method"],
  167. ["o.p := NIL", "cannot assign to method"])
  168. ),
  169. "method cannot be referenced": testWithContext(
  170. context(grammar.statement,
  171. "TYPE T = RECORD PROCEDURE p() END;"
  172. + "Proc = PROCEDURE();"
  173. + "VAR o: T;"
  174. + "PROCEDURE T.p(); END T.p;"
  175. + "PROCEDURE proc(p: Proc); END proc;"
  176. ),
  177. pass(),
  178. fail(["proc(o.p)", "type mismatch for argument 1: 'method p' cannot be converted to 'Proc'"])
  179. ),
  180. "method super call": testWithContext(
  181. context(grammar.declarationSequence,
  182. "TYPE T = RECORD PROCEDURE p(); PROCEDURE pAbstract(); PROCEDURE pAbstract2() END;"
  183. + "D = RECORD(T) PROCEDURE pNoSuper() END;"
  184. + "PROCEDURE T.p(); END T.p;"
  185. ),
  186. pass("PROCEDURE D.p(); BEGIN SUPER() END D.p;"),
  187. fail(["PROCEDURE D.pNoSuper(); BEGIN SUPER() END D.pNoSuper;",
  188. "there is no method 'pNoSuper' in base type(s)"],
  189. ["PROCEDURE p(); BEGIN SUPER() END p;",
  190. "SUPER can be used only in methods"],
  191. ["PROCEDURE T.pNoBase(); BEGIN SUPER() END T.pNoBase;",
  192. "'T' has no base type - SUPER cannot be used"],
  193. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract;",
  194. "cannot use abstract method(s) in SUPER calls: pAbstract"],
  195. ["PROCEDURE D.pAbstract(); BEGIN SUPER() END D.pAbstract; PROCEDURE D.pAbstract2(); BEGIN SUPER() END D.pAbstract2;",
  196. "cannot use abstract method(s) in SUPER calls: pAbstract, pAbstract2"]
  197. )
  198. ),
  199. "export method": testWithContext(
  200. context(grammar.declarationSequence,
  201. "TYPE T = RECORD PROCEDURE p() END;"
  202. ),
  203. pass(),
  204. fail(["PROCEDURE T.p*(); END T.p;",
  205. "method implementation cannot be exported: p"])
  206. ),
  207. "import method": testWithModule(
  208. "MODULE test;"
  209. + "TYPE T* = RECORD PROCEDURE m*(); PROCEDURE mNotExported() END;"
  210. + "PROCEDURE T.m(); END T.m; PROCEDURE T.mNotExported(); END T.mNotExported;"
  211. + "END test.",
  212. pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.m(); END m.",
  213. "MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.m(); END T.m; END m."
  214. ),
  215. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.mNotExported(); END m.",
  216. "type 'T' has no 'mNotExported' field"],
  217. ["MODULE m; IMPORT test; TYPE T = RECORD(test.T) END; PROCEDURE T.mNotExported(); END T.mNotExported; END m.",
  218. "'T' has no declaration for method 'mNotExported'"])
  219. ),
  220. "non-scalar variables can be exported": testWithContext(
  221. context(grammar.declarationSequence,
  222. "TYPE T = RECORD END; A = ARRAY 3 OF INTEGER;"
  223. ),
  224. pass("VAR r*: T;",
  225. "VAR a*: A;"),
  226. fail()
  227. ),
  228. "export as read-only": testWithContext(
  229. context(grammar.declarationSequence, ""),
  230. pass("TYPE T* = RECORD i-: INTEGER END;"),
  231. fail(["TYPE T- = RECORD END;",
  232. "type cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  233. ["PROCEDURE p-(); END p;",
  234. "procedure cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  235. ["CONST c- = 123;",
  236. "constant cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  237. ["VAR i-: INTEGER;",
  238. "variable cannot be exported as read-only using '-' mark (did you mean '*'?)"],
  239. ["TYPE T* = RECORD PROCEDURE p-() END;",
  240. "method cannot be exported as read-only using '-' mark (did you mean '*'?)"]
  241. )
  242. ),
  243. "field exported as read-only is writable in current module": testWithContext(
  244. context(grammar.statement,
  245. "TYPE T* = RECORD i-: INTEGER END;"
  246. + "VAR r: T;"
  247. ),
  248. pass("r.i := 123"),
  249. fail()
  250. ),
  251. "import as read-only": testWithModule(
  252. "MODULE test; TYPE T* = RECORD f-: INTEGER END; END test.",
  253. pass(),
  254. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 123; END m.",
  255. "cannot assign to read-only variable"],
  256. ["MODULE m; IMPORT test; TYPE D = RECORD(test.T) END; VAR r: D; BEGIN r.f := 123; END m.",
  257. "cannot assign to read-only variable"]
  258. )),
  259. "STRING variable": testWithGrammar(
  260. grammar.variableDeclaration,
  261. pass("s: STRING")
  262. ),
  263. "STRING expression": testWithContext(
  264. context(grammar.expression,
  265. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  266. pass("s1 + s2",
  267. "s1 + \"abc\"",
  268. "\"abc\" + s1",
  269. "s1 = s2",
  270. "s1 # s2",
  271. "s1 < s2",
  272. "s1 > s2",
  273. "s1 <= s2",
  274. "s1 >= s2"
  275. ),
  276. fail(["s1 = NIL", "type mismatch: expected 'STRING', got 'NIL'"],
  277. ["s1 = a", "type mismatch: expected 'STRING', got 'ARRAY 10 OF CHAR'"],
  278. ["a = s1", "type mismatch: expected 'ARRAY 10 OF CHAR', got 'STRING'"]
  279. )
  280. ),
  281. "STRING literal expression": testWithContext(
  282. context(grammar.expression,
  283. "CONST cs = \"abc\";"
  284. + "PROCEDURE pString(s: STRING): STRING; RETURN s END pString;"
  285. + "PROCEDURE pStringByRef(VAR s: STRING): STRING; RETURN s END pStringByRef;"
  286. ),
  287. pass("\"abc\" + \"cde\"",
  288. "cs + cs",
  289. "cs + \"abc\"",
  290. "cs = \"abc\"",
  291. "cs # \"abc\"",
  292. "cs < \"abc\"",
  293. "cs > \"abc\"",
  294. "cs <= \"abc\"",
  295. "cs >= \"abc\"",
  296. "pString(cs)",
  297. "pString(\"abc\")"
  298. ),
  299. fail(["pStringByRef(cs)", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"],
  300. ["pStringByRef(\"abc\")", "type mismatch for argument 1: cannot pass 'multi-character string' as VAR parameter of type 'STRING'"]
  301. )
  302. ),
  303. "STRING assignment": testWithContext(
  304. context(grammar.statement,
  305. "VAR s1, s2: STRING; a: ARRAY 10 OF CHAR;"),
  306. pass("s1 := s2",
  307. "s1 := \"abc\"",
  308. "s1 := 22X"
  309. ),
  310. fail(["a := s1", "type mismatch: 'a' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'STRING' expression"],
  311. ["s1 := a", "type mismatch: 's1' is 'STRING' and cannot be assigned to 'ARRAY 10 OF CHAR' expression"]
  312. )
  313. ),
  314. "STRING and ARRAY OF CHAR": testWithContext(
  315. context(grammar.expression,
  316. "VAR s: STRING; a: ARRAY 10 OF CHAR;"
  317. + "PROCEDURE pArray(a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pArray;"
  318. + "PROCEDURE pString(s: STRING): BOOLEAN; RETURN FALSE END pString;"
  319. + "PROCEDURE pVar(VAR a: ARRAY OF CHAR): BOOLEAN; RETURN FALSE END pVar;"
  320. + "PROCEDURE pIntArray(a: ARRAY OF INTEGER): BOOLEAN; RETURN FALSE END pIntArray;"
  321. ),
  322. pass("pArray(s)"),
  323. fail(["pVar(s)", "type mismatch for argument 1: cannot pass 'STRING' as VAR parameter of type 'ARRAY OF CHAR'"],
  324. ["pString(a)", "type mismatch for argument 1: 'ARRAY 10 OF CHAR' cannot be converted to 'STRING'"],
  325. ["pIntArray(s)", "type mismatch for argument 1: 'STRING' cannot be converted to 'ARRAY OF INTEGER'"]
  326. )
  327. ),
  328. "STRING LEN": testWithContext(
  329. context(grammar.expression,
  330. "VAR s: STRING;"),
  331. pass("LEN(s)"),
  332. fail()
  333. ),
  334. "STRING indexing": testWithContext(
  335. context(grammar.expression,
  336. "VAR s: STRING;"
  337. + "PROCEDURE pCharByVar(VAR c: CHAR): CHAR; RETURN c END pCharByVar;"),
  338. pass("s[0]"),
  339. fail(["s[-1]", "index is negative: -1"],
  340. ["pCharByVar(s[0])", "string element cannot be used as VAR parameter"]
  341. )
  342. ),
  343. "designate call result in expression": testWithContext(
  344. context(grammar.expression,
  345. "TYPE PT = POINTER TO RECORD field: INTEGER END;"
  346. + "VAR p: PT;"
  347. + "PROCEDURE proc(): PT; RETURN p END proc;"
  348. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  349. + "PROCEDURE intVar(VAR i: INTEGER): INTEGER; RETURN i END intVar;"),
  350. pass("proc().field",
  351. "intVar(proc().field)"),
  352. fail(["intVar(int())", "expression cannot be used as VAR parameter"])
  353. ),
  354. "designate call result in statement": testWithContext(
  355. context(grammar.statement,
  356. "TYPE PT = POINTER TO RECORD field: INTEGER; proc: PROCEDURE END;"
  357. + "ProcType = PROCEDURE;"
  358. + "VAR p: PT;"
  359. + "PROCEDURE procVoid(); END procVoid;"
  360. + "PROCEDURE proc(): PT; RETURN p END proc;"
  361. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  362. + "PROCEDURE intVar(VAR i: INTEGER); END intVar;"
  363. + "PROCEDURE returnProc(): ProcType; RETURN procVoid END returnProc;"
  364. ),
  365. pass("proc().field := 0",
  366. "proc().proc()",
  367. "proc().proc"
  368. ),
  369. fail(["int() := 0", "cannot assign to procedure call result"],
  370. ["intVar(int())", "expression cannot be used as VAR parameter"],
  371. ["procVoid()()", "PROCEDURE expected, got 'procedure call statement'"],
  372. ["int()()", "PROCEDURE expected, got 'INTEGER'"],
  373. ["returnProc()", "procedure returning a result cannot be used as a statement"] // call is not applied implicitly to result
  374. )
  375. ),
  376. "temporary values": {
  377. "initialization": testWithContext(
  378. context(grammar.statement,
  379. "VAR i: INTEGER;"
  380. + "PROCEDURE p(): BOOLEAN; RETURN FALSE END p;"
  381. + "PROCEDURE void(); END void;"
  382. ),
  383. pass("v <- 0",
  384. "v <- 1.23",
  385. "v <- \"abc\"",
  386. "v <- TRUE",
  387. "v <- i",
  388. "v <- i + i",
  389. "v <- p()",
  390. "v <- void" // procedure type
  391. ),
  392. fail(["v <-", "initialization expression expected"],
  393. ["v <- void()", "procedure returning no result cannot be used in an expression"])
  394. ),
  395. "scope": testWithContext(
  396. temporaryValues.context,
  397. temporaryValues.passStatements(
  398. "v1 <- 0; v2 <-0;",
  399. "i <- 0; ASSERT(i = 0);",
  400. "WHILE FALSE DO v <- 0; ASSERT(v = 0); END; WHILE FALSE DO v <- 0; END;",
  401. "WHILE FALSE DO i1 <- 0; WHILE FALSE DO i2 <- 0; ASSERT(i1 = 0); ASSERT(i2 = 0); END; END;",
  402. "WHILE bVar DO v <- 0; ELSIF ~bVar DO v <- 0 END;",
  403. "IF FALSE THEN v <- 0; ASSERT(v = 0); END; IF FALSE THEN v <- 0; END;",
  404. "IF FALSE THEN v <- 0; END; IF FALSE THEN v <- 0; END;",
  405. "IF FALSE THEN v <- 0; ELSIF FALSE THEN v <- 0; ELSE v <- 0; END;",
  406. "i <- 0; CASE i OF 0: v <- 0 | 1: v <- 1; ; ASSERT(v = 1); END;",
  407. "REPEAT v <- 0; UNTIL FALSE; REPEAT v <- 0; UNTIL FALSE;",
  408. "REPEAT v <- 0; ASSERT(v = 0); UNTIL v # 0;",
  409. "i <- 0; FOR i := 0 TO 10 DO v <- 0; END; FOR i := 0 TO 10 DO v <- 0; END;"
  410. ),
  411. fail(["PROCEDURE p(); BEGIN v <- 0; v <-0; END p;", "'v' already declared"],
  412. ["PROCEDURE p(); VAR v: INTEGER; BEGIN v <- 0; END p;", "'v' already declared"],
  413. ["PROCEDURE p(); BEGIN v <- 0; WHILE FALSE DO v <- 0; END; END p;",
  414. "'v' already declared in procedure scope"],
  415. ["PROCEDURE p(); BEGIN i <- 0; IF FALSE THEN i <- 0; END; END p;",
  416. "'i' already declared in procedure scope"],
  417. ["PROCEDURE p(); BEGIN WHILE FALSE DO i <- 0; WHILE FALSE DO i <- 0; END; END; END p;",
  418. "'i' already declared in procedure scope"]
  419. )
  420. ),
  421. "read-only": testWithContext(
  422. context(grammar.declarationSequence,
  423. ""),
  424. pass(),
  425. fail(["PROCEDURE p(); BEGIN v <- 0; v := 0; END p;",
  426. "cannot assign to temporary variable"]
  427. )
  428. ),
  429. "type promotion in expression": testWithContext(
  430. temporaryValues.context,
  431. temporaryValues.passExpressions(
  432. "(b IS PDerived) & b.flag",
  433. "(b IS PDerived) & bVar & b.flag",
  434. "(b IS PDerived) & (bVar OR b.flag)",
  435. "(b IS PDerived) & (b2 IS PDerived) & b.flag & b2.flag",
  436. "(b IS PDerived) & proc(TRUE) & b.flag",
  437. "(b IS PDerived) & ~proc(TRUE) & b.flag",
  438. "~(~(b IS PDerived)) & b.flag",
  439. "~~(b IS PDerived) & b.flag",
  440. "(b IS PDerived) & ((b IS PDerived2) OR bVar) & b.flag"
  441. //TODO: "((b IS PDerived) = TRUE) & b.flag); END p;",
  442. ),
  443. temporaryValues.failExpressions(
  444. "(b IS PDerived) OR b.flag",
  445. "(b IS PDerived) OR bVar & b.flag",
  446. "~(b IS PDerived) & b.flag",
  447. "((b IS PDerived) & (b2 IS PDerived) OR bVar) & b.flag",
  448. "proc(b IS PDerived) & proc(b.flag)",
  449. "ORD(b IS PDerived) * ORD(b.flag) = 0",
  450. "((b IS PDerived) = FALSE) & b.flag",
  451. "b IS PDerived); ASSERT(b.flag"
  452. // TODO: move to statements test "bVar := b IS PDerived; ASSERT(b.flag)",
  453. )
  454. ),
  455. "invert type promotion in expression": testWithContext(
  456. temporaryValues.context,
  457. temporaryValues.passExpressions(
  458. "~(b IS PDerived) OR b.flag",
  459. "~(b IS PDerived) OR b.flag OR bVar",
  460. "~(b IS PDerived) OR b.flag & bVar",
  461. "~(b IS PDerived) OR bVar & b.flag",
  462. "~(b IS PDerived) OR (bVar & b.flag)",
  463. "~(b IS PDerived) OR bVar OR b.flag",
  464. "~(b IS PDerived) OR (bVar = b.flag)",
  465. "~(~(b IS PDerived) OR bVar) & b.flag",
  466. "~(~(b IS PDerived) OR b.flag) & b.flag"
  467. ),
  468. temporaryValues.failExpressions(
  469. "(~(b IS PDerived) OR bVar) & b.flag",
  470. "(ORD(~(b IS PDerived)) + ORD(b.flag)",
  471. "~(~(b IS PDerived) OR bVar) OR b.flag",
  472. "~(~(b IS PDerived) & bVar) & b.flag",
  473. "~(b IS PDerived) OR b.flag = b.flag"
  474. )
  475. ),
  476. "type promotion in IF": testWithContext(
  477. temporaryValues.context,
  478. temporaryValues.passStatements(
  479. "IF b IS PDerived THEN b.flag := FALSE; END;",
  480. "IF (b IS PDerived) & bVar THEN b.flag := FALSE; END;",
  481. "IF FALSE THEN ELSIF b IS PDerived THEN b.flag := FALSE; END;",
  482. "IF b IS PDerived THEN bVar := (b IS PDerived2) & b.flag2; b.flag := FALSE; END;"
  483. ),
  484. temporaryValues.failStatements(
  485. "IF (b IS PDerived) OR bVar THEN b.flag := FALSE; END",
  486. "IF b IS PDerived THEN END; b.flag := FALSE",
  487. "IF ~(b IS PDerived) THEN END; b.flag := FALSE",
  488. "IF ~(b IS PDerived) THEN ELSIF bVar THEN END; b.flag := FALSE",
  489. "IF ~(b IS PDerived) THEN ELSIF bVar THEN ELSE END; b.flag := FALSE",
  490. "IF bVar THEN ELSIF b IS PDerived THEN ELSE END; b.flag := FALSE",
  491. "IF b IS PDerived THEN ELSE b.flag := FALSE; END",
  492. "IF b IS PDerived THEN ELSIF TRUE THEN b.flag := FALSE; END"
  493. //TODO: separate test
  494. //["PROCEDURE p(); BEGIN b <- pBase; IF b IS PDerived THEN bVar := b IS PDerived; b.flag := FALSE; END; END p;",
  495. // "invalid type test: 'Derived' is not an extension of 'Derived'"]
  496. )
  497. ),
  498. "invert type promotion in IF": testWithContext(
  499. temporaryValues.context,
  500. temporaryValues.passStatements(
  501. "IF ~(b IS PDerived) THEN ELSE b.flag := FALSE; END;",
  502. "IF ~(b IS PDerived) THEN ELSIF bVar THEN b.flag := FALSE; ELSE b.flag := FALSE; END;",
  503. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b.flag := FALSE; ELSE b.flag := FALSE; b2.flag := FALSE; END;",
  504. "IF ~(b IS PDerived) OR bVar THEN ELSE b.flag := FALSE; END;",
  505. "IF ~(b IS PDerived) OR b.flag THEN ELSE b.flag := FALSE; END;",
  506. "IF ~(b IS PDerived) THEN bVar := b IS PDerived; ELSE b.flag := FALSE; END;",
  507. "IF ~(b IS PDerived) THEN ASSERT((b IS PDerived) & b.flag); ELSE b.flag := FALSE; END;"
  508. ),
  509. temporaryValues.failStatements(
  510. "IF ~(b IS PDerived) & bVar THEN ELSE b.flag := FALSE; END; END p;",
  511. "IF ~(b IS PDerived) THEN ELSIF ~(b2 IS PDerived) THEN b2.flag := FALSE; END;"
  512. )
  513. ),
  514. "type promotion in WHILE": testWithContext(
  515. temporaryValues.context,
  516. temporaryValues.passStatements(
  517. "WHILE (b IS PDerived) & b.flag DO END;",
  518. "WHILE ~(b IS PDerived) OR b.flag DO END;",
  519. "WHILE b IS PDerived DO b.flag := FALSE; END;",
  520. "WHILE ~(b IS PDerived) DO ELSIF b.flag DO END;",
  521. "WHILE ~(b IS PDerived) DO ELSIF bVar DO b.flag := FALSE; END;"
  522. ),
  523. temporaryValues.failStatements(
  524. "WHILE b IS PDerived DO END; b.flag := FALSE;"
  525. )
  526. )
  527. }
  528. };