2
0

test_unit.js 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. "use strict";
  2. var assert = require("rtl.js").assert;
  3. var Class = require("rtl.js").Class;
  4. var CodeGenerator = require("js/CodeGenerator.js");
  5. var Grammar = require("grammar.js");
  6. var Test = require("test.js");
  7. var TestUnitCommon = require("test_unit_common.js");
  8. var TestUnitEberon = require("test_unit_eberon.js");
  9. var TestUnitOberon = require("test_unit_oberon.js");
  10. var eberon = require("eberon/eberon_grammar.js").language;
  11. var oberon = require("oberon/oberon_grammar.js").language;
  12. var context = TestUnitCommon.context;
  13. var pass = TestUnitCommon.pass;
  14. var fail = TestUnitCommon.fail;
  15. var setupParser = TestUnitCommon.setupParser;
  16. var testWithSetup = TestUnitCommon.testWithSetup;
  17. function makeSuiteForGrammar(language){
  18. var grammar = language.grammar;
  19. function testWithContext(context, pass, fail){
  20. return TestUnitCommon.testWithContext(context, grammar.declarationSequence, language, pass, fail);
  21. }
  22. function testWithModule(src, pass, fail){
  23. return TestUnitCommon.testWithModule(src, language, pass, fail);
  24. }
  25. function testWithGrammar(parser, pass, fail){
  26. return TestUnitCommon.testWithGrammar(parser, language, pass, fail);
  27. }
  28. return {
  29. "comment": testWithGrammar(
  30. grammar.expression,
  31. pass("(**)123",
  32. "(*abc*)123",
  33. "(*abc*)(*def*)123",
  34. "(*a(*b*)c*)123"),
  35. fail(["(*123", "comment was not closed"])
  36. ),
  37. "spaces are required to separate keywords and integers": testWithGrammar(
  38. grammar.typeDeclaration,
  39. pass(),
  40. fail(["T = ARRAY10OFARRAY5OFINTEGER", "not parsed"],
  41. ["T = ARRAY10 OF ARRAY 5 OF INTEGER", "not parsed"],
  42. ["T = ARRAY 10OF ARRAY 5 OF INTEGER", "not parsed"],
  43. ["T = ARRAY 10 OFARRAY 5 OF INTEGER", "not parsed"],
  44. ["T = ARRAY 10 OF ARRAY5 OF INTEGER", "undeclared identifier: 'ARRAY5'"],
  45. ["T = ARRAY 10 OF ARRAY 5OF INTEGER", "not parsed"],
  46. ["T = ARRAY 10 OF ARRAY 5 OFINTEGER", "not parsed"])
  47. ),
  48. "expression": testWithContext(
  49. context(grammar.expression,
  50. "TYPE ProcType = PROCEDURE(): INTEGER;"
  51. + "PROCEDURE p1(): INTEGER; RETURN 1 END p1;"
  52. + "PROCEDURE p2(): ProcType; RETURN p1 END p2;"
  53. + "PROCEDURE noResult(); END noResult;"),
  54. pass("123",
  55. "1+2",
  56. "1 + 2",
  57. "1 + 2 + 3",
  58. "-1",
  59. "+1",
  60. "p1()",
  61. "p1() + p1()",
  62. "p2()"),
  63. fail(["", "not parsed"],
  64. ["12a", "not parsed"],
  65. ["noResult()", "procedure returning no result cannot be used in an expression"],
  66. ["1 + INTEGER", "type name 'INTEGER' cannot be used as an expression"],
  67. ["INTEGER + 1", "type name 'INTEGER' cannot be used as an expression"],
  68. ["1 * INTEGER", "type name 'INTEGER' cannot be used as an expression"],
  69. ["INTEGER * 1", "type name 'INTEGER' cannot be used as an expression"],
  70. ["-INTEGER", "type name 'INTEGER' cannot be used as an expression"],
  71. ["+INTEGER", "type name 'INTEGER' cannot be used as an expression"],
  72. ["~BOOLEAN", "type name 'BOOLEAN' cannot be used as an expression"],
  73. ["INTEGER", "type name 'INTEGER' cannot be used as an expression"]
  74. )
  75. ),
  76. "string expression": testWithContext(
  77. context(grammar.expression,
  78. "CONST cs = \"abc\";"
  79. + "PROCEDURE charByRef(VAR c: CHAR): CHAR; RETURN c END charByRef;"
  80. ),
  81. pass("\"\"",
  82. "\"a\"",
  83. "\"abc\"",
  84. "0FFX",
  85. "0AX",
  86. "22X",
  87. "0X"),
  88. fail(["\"", "unexpected end of string"],
  89. ["\"abc", "unexpected end of string"],
  90. ["FFX", "undeclared identifier: 'FFX'"],
  91. ["charByRef(cs[1])", "read-only array's element cannot be passed as VAR actual parameter"]
  92. )
  93. ),
  94. "parentheses": testWithGrammar(
  95. grammar.expression,
  96. pass("(1)",
  97. "(1 + 2)",
  98. "(1 + 2) * 3",
  99. "3 * (1 + 2)"),
  100. fail(["(1 + 2", "no matched ')'"])
  101. ),
  102. "identifier": testWithSetup(
  103. function(){
  104. var IdentDeclarationContext = Class.extend({
  105. init: function IdentDeclarationContext(){this.__ident = undefined;},
  106. handleIdent: function(id){this.__ident = id;},
  107. ident: function() {return this.__ident;},
  108. getResult: function() {return this.__ident;},
  109. currentScope: function(){return {close: function(){}};}
  110. });
  111. function makeContext() {return new IdentDeclarationContext();}
  112. return setupParser(grammar.ident, language, makeContext);},
  113. pass("i", "abc1"),
  114. fail(["", "not parsed"],
  115. [";", "not parsed"],
  116. ["1", "not parsed"]
  117. )
  118. ),
  119. "variable declaration": testWithGrammar(
  120. grammar.variableDeclaration,
  121. pass("i: INTEGER",
  122. "i, j: INTEGER"),
  123. fail(["i: T", "undeclared identifier: 'T'"],
  124. ["p: POINTER TO T", "type 'T' was not declared"])
  125. ),
  126. "record declaration": testWithGrammar(
  127. grammar.typeDeclaration,
  128. pass("T = RECORD END",
  129. "T = RECORD i: INTEGER END",
  130. "T = RECORD i, j: INTEGER END",
  131. "T = RECORD i, j: INTEGER; b: BOOLEAN END",
  132. "T = RECORD p: PROCEDURE(r: T) END",
  133. "T = POINTER TO RECORD p: PROCEDURE(): T END"
  134. ),
  135. fail(["T = RECORD i, j, i: INTEGER END", "duplicated field: 'i'"],
  136. ["T = RECORD r: T END", "recursive field definition: 'r'"],
  137. ["T = RECORD a: ARRAY 10 OF T END", "recursive field definition: 'a'"],
  138. ["T = RECORD a: ARRAY 3 OF ARRAY 5 OF T END", "recursive field definition: 'a'"],
  139. ["T = RECORD r: RECORD rr: T END END", "recursive field definition: 'r'"],
  140. ["T = RECORD (T) END", "recursive inheritance: 'T'"],
  141. ["T = RECORD r: RECORD (T) END END", "recursive field definition: 'r'"]
  142. )
  143. ),
  144. "record extension": testWithContext(
  145. context(grammar.typeDeclaration,
  146. "TYPE B = RECORD END;"),
  147. pass("T = RECORD(B) END"
  148. ),
  149. fail(["T = RECORD(INTEGER) END", "RECORD type is expected as a base type, got 'INTEGER'"],
  150. ["T = RECORD(INTEGER) m: INTEGER END", "RECORD type is expected as a base type, got 'INTEGER'"]
  151. )
  152. ),
  153. "array declaration": testWithContext(
  154. context(grammar.typeDeclaration,
  155. "CONST c1 = 5; VAR v1: INTEGER; p: POINTER TO RECORD END;"),
  156. pass("T = ARRAY 10 OF INTEGER",
  157. "T = ARRAY 10 OF BOOLEAN",
  158. "T = ARRAY 1 + 2 OF INTEGER",
  159. "T = ARRAY c1 OF INTEGER",
  160. "T = ARRAY ORD({0..5} <= {0..8}) OF INTEGER",
  161. "T = ARRAY 1, 2 OF ARRAY 3, 4 OF INTEGER"
  162. ),
  163. fail(["T = ARRAY 0 OF INTEGER",
  164. "array size must be greater than 0, got 0"],
  165. ["T = ARRAY TRUE OF INTEGER",
  166. "'INTEGER' constant expression expected, got 'BOOLEAN'"],
  167. ["T = ARRAY v1 OF INTEGER",
  168. "constant expression expected as ARRAY size"],
  169. ["T = ARRAY p OF INTEGER",
  170. "'INTEGER' constant expression expected, got 'POINTER TO anonymous RECORD'"],
  171. ["T = ARRAY c1 - 10 OF INTEGER",
  172. "array size must be greater than 0, got -5"],
  173. ["T = ARRAY ORD({0..5} >= {0..8}) OF INTEGER",
  174. "array size must be greater than 0, got 0"]
  175. )
  176. ),
  177. "multi-dimensional array declaration": testWithGrammar(
  178. grammar.typeDeclaration,
  179. pass("T = ARRAY 10 OF ARRAY 5 OF INTEGER",
  180. "T = ARRAY 10, 5 OF INTEGER")
  181. ),
  182. "PROCEDURE type declaration": testWithContext(
  183. context(grammar.typeDeclaration, "TYPE R = RECORD END; A = ARRAY 3 OF INTEGER;"),
  184. pass("T = PROCEDURE",
  185. "T = PROCEDURE()",
  186. "T = PROCEDURE(a: INTEGER)",
  187. "T = PROCEDURE(a: INTEGER; b: BOOLEAN)",
  188. "T = PROCEDURE(): T"),
  189. fail(["T = PROCEDURE(): A;", "the result type of a procedure cannot be an ARRAY"],
  190. ["T = PROCEDURE(): R;", "the result type of a procedure cannot be a RECORD"]
  191. )
  192. ),
  193. "POINTER declaration": testWithGrammar(
  194. grammar.typeDeclaration,
  195. pass("T = POINTER TO RECORD END",
  196. "T = RECORD p: POINTER TO T END",
  197. "T = POINTER TO RECORD p: T END"),
  198. fail(["T = POINTER TO INTEGER",
  199. "RECORD is expected as a POINTER base type, got 'INTEGER'"],
  200. ["T = POINTER TO POINTER TO RECORD END",
  201. "RECORD is expected as a POINTER base type, got 'POINTER TO anonymous RECORD'"],
  202. ["T = POINTER TO RECORD p: POINTER TO T END",
  203. "RECORD is expected as a POINTER base type, got 'T'"],
  204. ["T = POINTER TO T",
  205. "RECORD is expected as a POINTER base type"]
  206. )
  207. ),
  208. "POINTER dereference": testWithContext(
  209. context(grammar.statement,
  210. "TYPE PT = POINTER TO RECORD END;"
  211. + "VAR pt: PT; p: POINTER TO RECORD field: INTEGER END; i: INTEGER; r: RECORD END;"),
  212. pass("p^.field := 1",
  213. "p.field := 0"),
  214. fail(["i^", "POINTER TO type expected, got 'INTEGER'"],
  215. ["r^", "POINTER TO type expected, got 'anonymous RECORD'"],
  216. ["p.unknown := 0", "type 'anonymous RECORD' has no 'unknown' field"],
  217. ["pt.constructor := 0", "type 'PT' has no 'constructor' field"], // "constructor" is JS predefined property
  218. ["pt.prototype := 0", "type 'PT' has no 'prototype' field"], // "prototype" is JS predefined property
  219. ["pt.unknown := 0", "type 'PT' has no 'unknown' field"])
  220. ),
  221. "POINTER assignment": testWithContext(
  222. context(grammar.statement,
  223. "TYPE Base = RECORD END;"
  224. + "Derived = RECORD (Base) END;"
  225. + "PDerivedAnonymous = POINTER TO RECORD(Base) END;"
  226. + "VAR p1, p2: POINTER TO RECORD END;"
  227. + "pBase: POINTER TO Base; pDerived: POINTER TO Derived;"
  228. + "pDerivedAnonymous: PDerivedAnonymous;"
  229. + "pDerivedAnonymous2: POINTER TO RECORD(Base) END;"
  230. ),
  231. pass("p1 := NIL",
  232. "p1 := p2",
  233. "pBase := pDerived",
  234. "pBase := pDerivedAnonymous",
  235. "pBase := pDerivedAnonymous2"
  236. ),
  237. fail(["p1 := pBase",
  238. "type mismatch: 'p1' is 'POINTER TO anonymous RECORD' and cannot be assigned to 'POINTER TO Base' expression"],
  239. ["pDerived := pBase",
  240. "type mismatch: 'pDerived' is 'POINTER TO Derived' and cannot be assigned to 'POINTER TO Base' expression"],
  241. ["NIL := p1", "not parsed"])
  242. ),
  243. "typeguard": testWithContext(
  244. context(grammar.expression,
  245. "TYPE Base = RECORD END; PBase = POINTER TO Base; Derived = RECORD (Base) END; PDerived = POINTER TO Derived;"
  246. + "VAR p1, p2: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived;"
  247. + "vb: Base; i: INTEGER;"),
  248. pass("pBase(PDerived)",
  249. "pBase^(Derived)"),
  250. fail(["pDerived(PDerived)",
  251. "invalid type cast: 'Derived' is not an extension of 'Derived'"],
  252. ["p1(PBase)",
  253. "invalid type cast: 'Base' is not an extension of 'anonymous RECORD'"],
  254. ["p1(INTEGER)",
  255. "invalid type cast: POINTER type expected as an argument of POINTER type cast, got 'INTEGER'"],
  256. ["i(Derived)",
  257. "invalid type cast: POINTER to type or RECORD expected, got 'INTEGER'"],
  258. ["vb(Derived)",
  259. "invalid type cast: a value variable cannot be used"],
  260. ["vb(PDerived)",
  261. "invalid type cast: a value variable cannot be used"])
  262. ),
  263. "NIL": testWithContext(
  264. context(grammar.expression,
  265. "VAR i: INTEGER;"),
  266. pass(),
  267. fail(["i = NIL", "type mismatch: expected 'INTEGER', got 'NIL'"])
  268. ),
  269. "POINTER relations": testWithContext(
  270. context(grammar.expression,
  271. "TYPE B = RECORD END; D = RECORD(B) END; PB = POINTER TO B;"
  272. + "VAR p1, p2: POINTER TO RECORD END; pb: POINTER TO B; pd: POINTER TO D;"),
  273. pass("p1 = p2",
  274. "p1 # p2",
  275. "pb = pd",
  276. "pd # pb"
  277. ),
  278. fail(["p1 < p2", "operator '<' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  279. ["p1 <= p2", "operator '<=' type mismatch: numeric type or SET or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  280. ["p1 > p2", "operator '>' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  281. ["p1 >= p2", "operator '>=' type mismatch: numeric type or SET or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  282. ["p1 = pb", "type mismatch: expected 'POINTER TO anonymous RECORD', got 'POINTER TO B'"],
  283. ["pb = PB", "type name 'PB' cannot be used as an expression"],
  284. ["PB = pb", "type name 'PB' cannot be used as an expression"]
  285. )
  286. ),
  287. "IS expression": testWithContext(
  288. context(grammar.expression,
  289. "TYPE Base = RECORD END; Derived = RECORD (Base) END; PDerived = POINTER TO Derived;"
  290. + "VAR p: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived; vDerived: Derived; i: INTEGER;"),
  291. pass("pBase IS PDerived",
  292. "pBase^ IS Derived"
  293. ),
  294. fail(["pBase IS pDerived", "type name expected"],
  295. ["pBase IS TRUE", "type name expected"],
  296. ["pBase IS vDerived", "type name expected"],
  297. ["Derived IS Derived",
  298. "type name 'Derived' cannot be used as an expression"],
  299. ["i IS Derived",
  300. "invalid type test: POINTER to type or RECORD expected, got 'INTEGER'"],
  301. ["p^ IS Derived",
  302. "invalid type test: 'Derived' is not an extension of 'anonymous RECORD'"],
  303. ["p IS PDerived",
  304. "invalid type test: 'Derived' is not an extension of 'anonymous RECORD'"],
  305. ["pDerived^ IS Derived",
  306. "invalid type test: 'Derived' is not an extension of 'Derived'"],
  307. ["pDerived IS PDerived",
  308. "invalid type test: 'Derived' is not an extension of 'Derived'"],
  309. ["pDerived^ IS Base",
  310. "invalid type test: 'Base' is not an extension of 'Derived'"],
  311. ["pDerived IS INTEGER",
  312. "invalid type test: POINTER type expected as an argument of POINTER type test, got 'INTEGER'"],
  313. ["pBase IS Derived",
  314. "invalid type test: POINTER type expected as an argument of POINTER type test, got 'Derived'"],
  315. ["pBase^ IS PDerived",
  316. "invalid type test: RECORD type expected as an argument of RECORD type test, got 'PDerived'"]
  317. )
  318. ),
  319. "IS for VAR argument": testWithContext(
  320. context(grammar.procedureDeclaration,
  321. "TYPE Base = RECORD END; Derived = RECORD (Base) i: INTEGER END;"
  322. + "T = RECORD END; TD = RECORD(T) b: Base END;"),
  323. pass("PROCEDURE proc(VAR p: Base): BOOLEAN; RETURN p IS Derived END proc"),
  324. fail(["PROCEDURE proc(p: Base): BOOLEAN; RETURN p IS Derived END proc",
  325. "invalid type test: a value variable cannot be used"],
  326. ["PROCEDURE proc(p: TD): BOOLEAN; RETURN p.b IS Derived END proc",
  327. "invalid type test: a value variable cannot be used"],
  328. ["PROCEDURE proc(VAR p: T):BOOLEAN; RETURN p(TD).b IS Derived END proc",
  329. "invalid type test: a value variable cannot be used"])
  330. ),
  331. "BYTE": testWithContext(
  332. context(grammar.statement,
  333. "VAR b1, b2: BYTE; i: INTEGER; set: SET; a: ARRAY 3 OF BYTE; ai: ARRAY 3 OF INTEGER;"
  334. + "PROCEDURE varIntParam(VAR i: INTEGER); END varIntParam;"
  335. + "PROCEDURE varByteParam(VAR b: BYTE); END varByteParam;"
  336. + "PROCEDURE arrayParam(b: ARRAY OF BYTE); END arrayParam;"
  337. + "PROCEDURE arrayIntParam(i: ARRAY OF INTEGER); END arrayIntParam;"
  338. ),
  339. pass("b1 := b2",
  340. "i := b1",
  341. "b2 := i",
  342. "a[b1] := i",
  343. "ASSERT(i = b1)",
  344. "ASSERT(b1 = i)",
  345. "ASSERT(i < b1)",
  346. "ASSERT(b1 > i)",
  347. "ASSERT(b1 IN set)",
  348. "i := b1 DIV i",
  349. "i := i DIV b1",
  350. "b1 := b1 MOD i",
  351. "b1 := i MOD b1",
  352. "b1 := b1 + i",
  353. "b1 := i - b1",
  354. "i := b1 * i",
  355. "i := -b1",
  356. "i := +b1",
  357. "arrayParam(a)",
  358. "arrayIntParam(ai)"
  359. ),
  360. fail(["i := b1 / i", "operator DIV expected for integer division"],
  361. ["varIntParam(b1)", "type mismatch for argument 1: cannot pass 'BYTE' as VAR parameter of type 'INTEGER'"],
  362. ["varByteParam(i)", "type mismatch for argument 1: cannot pass 'INTEGER' as VAR parameter of type 'BYTE'"],
  363. ["arrayParam(ai)", "type mismatch for argument 1: 'ARRAY 3 OF INTEGER' cannot be converted to 'ARRAY OF BYTE'"],
  364. ["arrayIntParam(a)", "type mismatch for argument 1: 'ARRAY 3 OF BYTE' cannot be converted to 'ARRAY OF INTEGER'"]
  365. )
  366. ),
  367. "NEW": testWithContext(
  368. context(grammar.statement,
  369. "TYPE P = POINTER TO RECORD END;"
  370. + "VAR p: P; i: INTEGER; r: RECORD END;"
  371. + "PROCEDURE proc(): P; RETURN NIL END proc;"
  372. ),
  373. pass("NEW(p)"),
  374. fail(["NEW.NEW(p)", "selector '.NEW' cannot be applied to 'standard procedure NEW'"],
  375. ["NEW(i)", "POINTER variable expected, got 'INTEGER'"],
  376. ["NEW(r)", "POINTER variable expected, got 'anonymous RECORD'"],
  377. ["NEW()", "1 argument(s) expected, got 0"],
  378. ["NEW(p, p)", "1 argument(s) expected, got 2"],
  379. ["NEW(proc())", "expression cannot be used as VAR parameter"])
  380. ),
  381. "ABS": testWithContext(
  382. context(grammar.statement,
  383. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  384. pass("i := ABS(i)",
  385. "r := ABS(r)"),
  386. fail(["i := ABS(r)", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'REAL' expression"],
  387. ["i := ABS(c)", "type mismatch: expected numeric type, got 'CHAR'"],
  388. ["i := ABS(i, i)", "1 argument(s) expected, got 2"]
  389. )
  390. ),
  391. "FLOOR": testWithContext(
  392. context(grammar.statement, "VAR i: INTEGER; r: REAL;"),
  393. pass("i := FLOOR(r)"),
  394. fail(["i := FLOOR(i)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'REAL'"],
  395. ["i := FLOOR(r, r)", "1 argument(s) expected, got 2"]
  396. )
  397. ),
  398. "FLT": testWithContext(
  399. context(grammar.statement, "VAR i: INTEGER; r: REAL;"),
  400. pass("r := FLT(i)"),
  401. fail(["r := FLT(r)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  402. ["i := FLT(i, i)", "1 argument(s) expected, got 2"]
  403. )
  404. ),
  405. "LSL": testWithContext(
  406. context(grammar.statement,
  407. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  408. pass("i := LSL(i, i)"),
  409. fail(["i := LSL(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  410. ["i := LSL(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  411. ["r := LSL(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  412. ["i := LSL(i)", "2 argument(s) expected, got 1"]
  413. )
  414. ),
  415. "ASR": testWithContext(
  416. context(grammar.statement,
  417. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  418. pass("i := ASR(i, i)"),
  419. fail(["i := ASR(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  420. ["i := ASR(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  421. ["r := ASR(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  422. ["i := ASR(i)", "2 argument(s) expected, got 1"]
  423. )
  424. ),
  425. "ROR": testWithContext(
  426. context(grammar.statement,
  427. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  428. pass("i := ROR(i, i)"),
  429. fail(["i := ROR(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  430. ["i := ROR(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  431. ["r := ROR(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  432. ["i := ROR(i)", "2 argument(s) expected, got 1"]
  433. )
  434. ),
  435. "ODD": testWithContext(
  436. context(grammar.statement, "VAR b: BOOLEAN;"),
  437. pass("b := ODD(1)",
  438. "b := ODD(123)"
  439. ),
  440. fail(["b := ODD(1.2)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  441. ["b := ODD(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
  442. )
  443. ),
  444. "ODD const expression": testWithGrammar(
  445. grammar.typeDeclaration,
  446. pass("T = ARRAY ORD(ODD(1)) OF INTEGER",
  447. "T = ARRAY ORD(ODD(3)) OF INTEGER"
  448. ),
  449. fail(["T = ARRAY ORD(ODD(0)) OF INTEGER", "array size must be greater than 0, got 0"],
  450. ["T = ARRAY ORD(ODD(2)) OF INTEGER", "array size must be greater than 0, got 0"]
  451. )
  452. ),
  453. "ORD": testWithContext(
  454. context(grammar.statement, "VAR ch: CHAR; i: INTEGER; b: BOOLEAN;"),
  455. pass("i := ORD(ch)",
  456. "i := ORD(TRUE)",
  457. "i := ORD(b)",
  458. "i := ORD(b = FALSE)",
  459. "i := ORD({1})",
  460. "i := ORD(\"a\")",
  461. "b := ORD(22X) = 022H"),
  462. fail(["i := ORD(1.2)", "ORD function expects CHAR or BOOLEAN or SET as an argument, got 'REAL'"],
  463. ["i := ORD(\"abc\")", "ORD function expects CHAR or BOOLEAN or SET as an argument, got 'multi-character string'"]
  464. )
  465. ),
  466. "ORD const expression": testWithGrammar(
  467. grammar.typeDeclaration,
  468. pass("T = ARRAY ORD({0}) OF INTEGER",
  469. "T = ARRAY ORD({0}) + 1 OF INTEGER",
  470. "T = ARRAY ORD(TRUE) OF INTEGER",
  471. "T = ARRAY ORD(TRUE) + 1 OF INTEGER",
  472. "T = ARRAY ORD(\"A\") OF INTEGER",
  473. "T = ARRAY ORD(\"A\") + 1 OF INTEGER"
  474. ),
  475. fail(["T = ARRAY ORD({}) OF INTEGER", "array size must be greater than 0, got 0"],
  476. ["T = ARRAY ORD(FALSE) OF INTEGER", "array size must be greater than 0, got 0"],
  477. ["T = ARRAY ORD(0X) OF INTEGER", "array size must be greater than 0, got 0"]
  478. )
  479. ),
  480. "CHR": testWithContext(
  481. context(grammar.statement, "VAR i: INTEGER; ch: CHAR;"),
  482. pass("ch := CHR(i)"),
  483. fail(["ch := CHR(ch)", "type mismatch for argument 1: 'CHAR' cannot be converted to 'INTEGER'"])
  484. ),
  485. "INC": testWithContext(
  486. context(grammar.statement, "VAR i: INTEGER;"),
  487. pass("INC(i)",
  488. "INC(i, 3)",
  489. "INC(i, i)"),
  490. fail(["INC(i + i)", "expression cannot be used as VAR parameter"],
  491. ["INC()", "at least 1 argument expected, got 0"],
  492. ["INC(i, 1, 2)", "at most 2 arguments expected, got 3"]
  493. )
  494. ),
  495. "DEC": testWithContext(
  496. context(grammar.statement, "VAR i: INTEGER;"),
  497. pass("DEC(i)",
  498. "DEC(i, 3)",
  499. "DEC(i, i)"),
  500. fail(["DEC(i + i)", "expression cannot be used as VAR parameter"],
  501. ["DEC()", "at least 1 argument expected, got 0"],
  502. ["DEC(i, 1, 2)", "at most 2 arguments expected, got 3"]
  503. )
  504. ),
  505. "PACK": testWithContext(
  506. context(grammar.statement, "VAR r: REAL; i: INTEGER;"),
  507. pass("PACK(r, i)",
  508. "PACK(r, 3)"),
  509. fail(["PACK(r, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"])
  510. ),
  511. "UNPK": testWithContext(
  512. context(grammar.statement, "VAR r: REAL; i: INTEGER;"),
  513. pass("UNPK(r, i)"),
  514. fail(["UNPK(r, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  515. ["UNPK(r, 3)", "expression cannot be used as VAR parameter"],
  516. ["UNPK(123.456, i)", "expression cannot be used as VAR parameter"]
  517. )
  518. ),
  519. "standard procedure cannot be referenced" : testWithContext(
  520. context(grammar.expression, "VAR chr: PROCEDURE(c: CHAR): INTEGER;"),
  521. pass(),
  522. fail(["CHR", "standard procedure CHR cannot be referenced"])
  523. ),
  524. "assignment statement": testWithContext(
  525. context(grammar.statement,
  526. "CONST c = 15;"
  527. + "VAR ch: CHAR; i, n: INTEGER; b: BOOLEAN;"
  528. + "proc1: PROCEDURE; proc2: PROCEDURE(): INTEGER;"
  529. + "a: ARRAY 5 OF INTEGER;"
  530. + "PROCEDURE p(): INTEGER; RETURN 1 END p;"
  531. + "PROCEDURE noResult(); END noResult;"),
  532. pass("i := 0",
  533. "i := n",
  534. "i := c",
  535. "b := TRUE",
  536. "ch := \"A\"",
  537. "i := p()",
  538. "proc1 := proc1",
  539. "proc2 := NIL",
  540. "a[1] := 2"),
  541. fail(["i = 0", "did you mean ':=' (statement expected, got expression)?"],
  542. ["i := b", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression"],
  543. ["c := i", "cannot assign to constant"],
  544. ["ch := \"AB\"",
  545. "type mismatch: 'ch' is 'CHAR' and cannot be assigned to 'multi-character string' expression"],
  546. ["ch := CHAR",
  547. "type name 'CHAR' cannot be used as an expression"],
  548. ["i := .1", "expression expected"],
  549. ["proc1 := proc2",
  550. "type mismatch: 'proc1' is 'PROCEDURE' and cannot be assigned to 'PROCEDURE(): INTEGER' expression"],
  551. ["i := noResult()", "procedure returning no result cannot be used in an expression"])
  552. ),
  553. "INTEGER number": testWithGrammar(
  554. grammar.expression,
  555. pass("0",
  556. "123",
  557. "1H",
  558. "1FH",
  559. "0FFH",
  560. "0H"),
  561. fail(["FFH", "undeclared identifier: 'FFH'"],
  562. ["FF", "undeclared identifier: 'FF'"],
  563. ["1HH", "not parsed"],
  564. ["1H0", "not parsed"],
  565. ["1 23", "not parsed"],
  566. ["1F FH", "integer constant looks like having hexadecimal format but 'H' suffix is missing"]
  567. )
  568. ),
  569. "INTEGER number in statement": testWithGrammar(
  570. grammar.statement,
  571. pass("IF 1 < 2345 THEN END"),
  572. fail(["IF 1 < 2345THEN END", "'BOOLEAN' expression expected, got 'INTEGER'"],
  573. ["IF 1 < 2345HTHEN END", "'BOOLEAN' expression expected, got 'INTEGER'"])
  574. ),
  575. "SET statement": testWithContext(
  576. context(grammar.statement, "VAR s: SET;"),
  577. pass("s := {}",
  578. "s := {0}",
  579. "s := {0, 1}",
  580. "s := {1 + 2, 5..10}")
  581. //fail("s := {32}", "0..31")
  582. ),
  583. "REAL number": testWithGrammar(
  584. grammar.expression,
  585. pass("1.2345",
  586. "1.",
  587. "1.2345E6",
  588. "1.2345E+6",
  589. "1.2345E-12"),
  590. fail(["1..", "not parsed"],
  591. ["1..2", "not parsed"],
  592. ["1. 2345E-12", "not parsed"],
  593. ["1.23 45E-12", "not parsed"],
  594. ["1.2345 E-12", "not parsed"],
  595. ["1.2345E-1 2", "not parsed"])
  596. ),
  597. "LONGREAL number": testWithGrammar(
  598. grammar.expression,
  599. pass("1.2345D6",
  600. "1.2345D+6",
  601. "1.2345D-6")
  602. ),
  603. "REAL number in statement": testWithGrammar(
  604. grammar.statement,
  605. pass("IF 1. < 1.2345 THEN END"),
  606. fail(["IF 1. < 1.2345THEN END", "'BOOLEAN' expression expected, got 'REAL'"])
  607. ),
  608. "IF statement": testWithContext(
  609. context(grammar.statement,
  610. "VAR b1: BOOLEAN; i1: INTEGER; p: POINTER TO RECORD END;"),
  611. pass("IF b1 THEN i1 := 0 END",
  612. "IF FALSE THEN i1 := 0 ELSE i1 := 1 END",
  613. "IF TRUE THEN i1 := 0 ELSIF FALSE THEN i1 := 1 ELSE i1 := 2 END"),
  614. fail(["IF i1 THEN i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'"],
  615. ["IF b1 THEN i1 := 0 ELSIF i1 THEN i1 := 2 END",
  616. "'BOOLEAN' expression expected, got 'INTEGER'"],
  617. ["IF p THEN i1 := 0 END",
  618. "'BOOLEAN' expression expected, got 'POINTER TO anonymous RECORD'"],
  619. ["IF b1 (*THEN*) i1 := 0 END", "THEN expected"],
  620. ["IF b1 THEN i1 := 0 ELSIF ~b1 (*THEN*) i1 := 0 END", "THEN expected"])
  621. ),
  622. "CASE statement": testWithContext(
  623. context(grammar.statement,
  624. "CONST ci = 15; cc = \"A\"; cb = TRUE; cs = \"abc\";"
  625. + "VAR c1: CHAR; b1: BOOLEAN; i1, i2: INTEGER; byte: BYTE; p: POINTER TO RECORD END;"),
  626. pass("CASE i1 OF END",
  627. "CASE i1 OF | END",
  628. "CASE i1 OF | 0: b1 := TRUE END",
  629. "CASE i1 OF 0: b1 := TRUE END",
  630. "CASE cc OF \"A\": b1 := TRUE END",
  631. "CASE \"A\" OF \"A\": b1 := TRUE END",
  632. "CASE c1 OF \"A\": b1 := TRUE END",
  633. "CASE byte OF 3: b1 := TRUE END",
  634. "CASE i1 OF 0: b1 := TRUE | 1: b1 := FALSE END",
  635. "CASE i1 OF 0, 1: b1 := TRUE END",
  636. "CASE c1 OF \"A\", \"B\": b1 := TRUE END",
  637. "CASE i1 OF 0..2: b1 := TRUE END",
  638. "CASE i1 OF ci..2: b1 := TRUE END",
  639. "CASE c1 OF cc..\"Z\": b1 := TRUE END",
  640. "CASE i1 OF 1, 2, 3: b1 := TRUE | 4..10: b1 := FALSE | 11: c1 := \"A\" END",
  641. "CASE i1 OF 1, 2, 5..9: b1 := TRUE END"
  642. ),
  643. fail(["CASE i1 OF undefined: b1 := TRUE END",
  644. "undeclared identifier: 'undefined'"],
  645. ["CASE i1 OF i2: b1 := TRUE END",
  646. "'i2' is not a constant"],
  647. ["CASE b1 OF END", "'INTEGER' or 'BYTE' or 'CHAR' expected as CASE expression"],
  648. ["CASE \"AA\" OF \"A\": b1 := TRUE END", "'INTEGER' or 'BYTE' or 'CHAR' expected as CASE expression"],
  649. ["CASE i1 OF \"A\": b1 := TRUE END",
  650. "label must be 'INTEGER' (the same as case expression), got 'CHAR'"],
  651. ["CASE i1 OF p: b1 := TRUE END",
  652. "'p' is not a constant"],
  653. ["CASE c1 OF \"A\", 1: b1 := TRUE END",
  654. "label must be 'CHAR' (the same as case expression), got 'INTEGER'"],
  655. ["CASE c1 OF \"A\"..1: b1 := TRUE END",
  656. "label must be 'CHAR' (the same as case expression), got 'INTEGER'"],
  657. ["CASE c1 OF cs: b1 := TRUE END", "single-character string expected"],
  658. ["CASE ci OF cb: b1 := TRUE END", "label must be 'INTEGER' (the same as case expression), got 'BOOLEAN'"],
  659. ["CASE ci OF TRUE: b1 := TRUE END", "not parsed"]
  660. )
  661. ),
  662. "WHILE statement": testWithContext(
  663. context(grammar.statement,
  664. "VAR b1: BOOLEAN; i1: INTEGER;"),
  665. pass("WHILE TRUE DO i1 := 0 END",
  666. "WHILE b1 DO i1 := 0 ELSIF FALSE DO i1 := 1 END"),
  667. fail(["WHILE i1 DO i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'"],
  668. ["WHILE b1 DO i1 := 0 ELSIF i1 DO i1 := 1 END", "'BOOLEAN' expression expected, got 'INTEGER'"])
  669. ),
  670. "REPEAT statement": testWithContext(
  671. context(grammar.statement,
  672. "VAR b1: BOOLEAN; i1: INTEGER;"),
  673. pass("REPEAT i1 := 0 UNTIL TRUE",
  674. "REPEAT i1 := 0 UNTIL b1"),
  675. fail(["REPEAT i1 := 0 UNTIL i1", "'BOOLEAN' expression expected, got 'INTEGER'"])
  676. ),
  677. "FOR statement": testWithContext(
  678. context(grammar.statement,
  679. "CONST c = 15; zero = 1 - 1;"
  680. + "VAR b: BOOLEAN; i, n: INTEGER; ch: CHAR; p: POINTER TO RECORD END;"),
  681. pass("FOR i := 0 TO 10 DO n := 1 END",
  682. "FOR i := 0 TO 10 BY 5 DO b := TRUE END",
  683. "FOR i := 0 TO n DO b := TRUE END",
  684. "FOR i := 0 TO n BY c DO n := 1; b := FALSE END",
  685. "FOR i := 0 TO 10 BY 0 DO b := TRUE END",
  686. "FOR i := 0 TO 10 BY zero DO b := TRUE END"
  687. ),
  688. fail(["FOR undefined := 0 TO 10 DO n := 1 END",
  689. "undeclared identifier: 'undefined'"],
  690. ["FOR b := TRUE TO 10 DO n := 1 END",
  691. "'b' is a 'BOOLEAN' variable, 'FOR' control variable must be 'INTEGER'"],
  692. ["FOR ch := 'a' TO 10 DO n := 1 END",
  693. "'ch' is a 'CHAR' variable, 'FOR' control variable must be 'INTEGER'"],
  694. ["FOR c := 0 TO 10 DO END", "'c' is not a variable"],
  695. ["FOR i := TRUE TO 10 DO n := 1 END",
  696. "'INTEGER' expression expected to assign 'i', got 'BOOLEAN'"],
  697. ["FOR i := p TO 10 DO n := 1 END",
  698. "'INTEGER' expression expected to assign 'i', got 'POINTER TO anonymous RECORD'"],
  699. ["FOR i := 0 TO p DO n := 1 END",
  700. "'INTEGER' expression expected as 'TO' parameter, got 'POINTER TO anonymous RECORD'"],
  701. ["FOR i := 0 TO TRUE DO END",
  702. "'INTEGER' expression expected as 'TO' parameter, got 'BOOLEAN'"],
  703. ["FOR i := 0 TO 10 BY n DO END",
  704. "constant expression expected as 'BY' parameter"],
  705. ["FOR i := 0 TO 10 BY p DO END",
  706. "'INTEGER' expression expected as 'BY' parameter, got 'POINTER TO anonymous RECORD'"],
  707. ["FOR i := 0 TO 10 BY TRUE DO END",
  708. "'INTEGER' expression expected as 'BY' parameter, got 'BOOLEAN'"],
  709. ["FOR i := 0 TO 10 DO - END", "END expected (FOR)"]
  710. )
  711. ),
  712. "logical operators": testWithContext(
  713. context(grammar.statement, "VAR b1, b2: BOOLEAN; i1: INTEGER; p: POINTER TO RECORD END;"),
  714. pass("b1 := b1 OR b2",
  715. "b1 := b1 & b2",
  716. "b1 := ~b2"),
  717. fail(["b1 := i1 OR b2", "BOOLEAN expected as operand of 'OR', got 'INTEGER'"],
  718. ["b1 := b1 OR i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'"],
  719. ["b1 := p OR b1", "BOOLEAN expected as operand of 'OR', got 'POINTER TO anonymous RECORD'"],
  720. ["b1 := i1 & b2", "BOOLEAN expected as operand of '&', got 'INTEGER'"],
  721. ["b1 := b1 & i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'"],
  722. ["b1 := ~i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'"])
  723. ),
  724. "arithmetic operators": testWithContext(
  725. context(grammar.statement,
  726. "VAR b1: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL; c1: CHAR; s1: SET;"
  727. + "p1: PROCEDURE; ptr1: POINTER TO RECORD END;"),
  728. pass("i1 := i1 + i2",
  729. "i1 := i1 - i2",
  730. "i1 := i1 * i2",
  731. "i1 := i1 DIV i2",
  732. "i1 := i1 MOD i2",
  733. "r1 := r1 + r2",
  734. "r1 := r1 - r2",
  735. "r1 := r1 * r2",
  736. "r1 := r1 / r2"),
  737. fail(["i1 := i1 / i2", "operator DIV expected for integer division"],
  738. ["r1 := r1 DIV r1", "operator 'DIV' type mismatch: 'INTEGER' or 'BYTE' expected, got 'REAL'"],
  739. ["c1 := c1 - c1", "operator '-' type mismatch: numeric type or SET expected, got 'CHAR'"],
  740. ["p1 := p1 * p1", "operator '*' type mismatch: numeric type or SET expected, got 'PROCEDURE'"],
  741. ["ptr1 := ptr1 / ptr1", "operator '/' type mismatch: numeric type or SET expected, got 'POINTER TO anonymous RECORD'"],
  742. ["s1 := +s1", "operator '+' type mismatch: numeric type expected, got 'SET'"],
  743. ["b1 := -b1", "operator '-' type mismatch: numeric type or SET expected, got 'BOOLEAN'"],
  744. ["s1 := +b1", "operator '+' type mismatch: numeric type expected, got 'BOOLEAN'"])
  745. ),
  746. "relations are BOOLEAN": testWithContext(
  747. context(grammar.statement,
  748. "TYPE Base = RECORD END; Derived = RECORD (Base) END;"
  749. + "VAR pBase: POINTER TO Base; proc1, proc2: PROCEDURE;"
  750. + "set1, set2: SET;"
  751. + "b: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL; c1, c2: CHAR; ca1, ca2: ARRAY 10 OF CHAR;"),
  752. pass("b := pBase^ IS Derived",
  753. "b := pBase = pBase",
  754. "b := proc1 # proc2",
  755. "b := set1 <= set2",
  756. "b := i1 IN set2",
  757. "b := i1 < i2",
  758. "IF i1 > i2 THEN END",
  759. "b := c1 > c2",
  760. "b := ca1 <= ca2",
  761. "b := r1 >= r2")
  762. ),
  763. "SET relations": testWithContext(
  764. context(grammar.expression,
  765. "CONST constSet1 = {}; constSet2 = {};"
  766. + "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;"),
  767. pass("set1 <= set2",
  768. "set1 >= set2",
  769. "set1 = set2",
  770. "set1 # set2",
  771. "constSet1 = constSet2",
  772. "constSet1 # constSet2",
  773. "i IN set1"),
  774. fail(["set1 <= i", "type mismatch: expected 'SET', got 'INTEGER'"],
  775. ["b IN set1", "'INTEGER' or 'BYTE' expected as an element of SET, got 'BOOLEAN'"],
  776. ["i IN b", "type mismatch: expected 'SET', got 'BOOLEAN'"],
  777. ["set1 < set2", "operator '<' type mismatch: numeric type or CHAR or character array expected, got 'SET'"],
  778. ["set1 > set2", "operator '>' type mismatch: numeric type or CHAR or character array expected, got 'SET'"]
  779. )
  780. ),
  781. "SET operators": testWithContext(
  782. context(grammar.expression,
  783. "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;"),
  784. pass("set1 + set2",
  785. "set1 - set2",
  786. "set1 * set2",
  787. "set1 / set2",
  788. "-set1"),
  789. fail(["set1 + i", "type mismatch: expected 'SET', got 'INTEGER'"],
  790. ["set1 - b", "type mismatch: expected 'SET', got 'BOOLEAN'"],
  791. ["set1 * b", "type mismatch: expected 'SET', got 'BOOLEAN'"],
  792. ["set1 / b", "type mismatch: expected 'SET', got 'BOOLEAN'"])
  793. ),
  794. "SET functions": testWithContext(
  795. context(grammar.statement,
  796. "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;"),
  797. pass("INCL(set1, 0)",
  798. "EXCL(set1, 3)",
  799. "INCL(set1, i)",
  800. "EXCL(set1, i)"),
  801. fail(["INCL({}, i)", "expression cannot be used as VAR parameter"],
  802. ["INCL(set1, 32)", "value (0..31) expected as a second argument of INCL, got 32"],
  803. ["EXCL(set1, -1)", "value (0..31) expected as a second argument of EXCL, got -1"]
  804. )
  805. ),
  806. "PROCEDURE relations": testWithContext(
  807. context(grammar.expression,
  808. "VAR p1: PROCEDURE; p2: PROCEDURE;"),
  809. pass("p1 = p2",
  810. "p1 # p2",
  811. "p1 = NIL",
  812. "NIL # p1"
  813. )
  814. ),
  815. "VAR parameter": testWithContext(
  816. context(grammar.statement,
  817. "CONST c = 123;"
  818. + "TYPE Base = RECORD END; Derived = RECORD (Base) END; PBase = POINTER TO Base; PDerived = POINTER TO Derived;"
  819. + "VAR i1: INTEGER; b1: BOOLEAN; a1: ARRAY 5 OF INTEGER;"
  820. + "r1: RECORD f1: INTEGER END;"
  821. + "pBase: PBase; pDerived: PDerived;"
  822. + "PROCEDURE p1(VAR i: INTEGER); END p1;"
  823. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"
  824. + "PROCEDURE procBasePointer(VAR p: PBase); END procBasePointer;"
  825. + "PROCEDURE int(): INTEGER; RETURN 0 END int;"
  826. ),
  827. pass("p1(i1)",
  828. "p1(a1[0])",
  829. "p1(r1.f1)"),
  830. fail(["p1(c)", "constant cannot be passed as VAR actual parameter"],
  831. ["p1(123)", "expression cannot be used as VAR parameter"],
  832. ["p2(TRUE)", "expression cannot be used as VAR parameter"],
  833. ["procBasePointer(NIL)", "expression cannot be used as VAR parameter"],
  834. ["p1(i1 + i1)", "expression cannot be used as VAR parameter"],
  835. ["p1(i1 * i1)", "expression cannot be used as VAR parameter"],
  836. ["p1(+i1)", "expression cannot be used as VAR parameter"],
  837. ["p1(-i1)", "expression cannot be used as VAR parameter"],
  838. ["p2(~b1)", "expression cannot be used as VAR parameter"],
  839. ["p1(int())", "expression cannot be used as VAR parameter"],
  840. ["procBasePointer(pDerived)",
  841. "type mismatch for argument 1: cannot pass 'PDerived' as VAR parameter of type 'PBase'"]
  842. )
  843. ),
  844. "procedure call": testWithContext(
  845. context(grammar.statement,
  846. "TYPE ProcType = PROCEDURE;" +
  847. "VAR notProcedure: INTEGER; ptr: POINTER TO RECORD END;" +
  848. "PROCEDURE p; END p;" +
  849. "PROCEDURE p1(i: INTEGER); END p1;" +
  850. "PROCEDURE p2(i: INTEGER; b: BOOLEAN); END p2;" +
  851. "PROCEDURE p3(): ProcType; RETURN p END p3;"),
  852. pass("p",
  853. "p()",
  854. "p1(1)",
  855. "p1(1 + 2)",
  856. "p2(1, TRUE)"),
  857. fail(["notProcedure", "PROCEDURE expected, got 'INTEGER'"],
  858. ["ptr()", "PROCEDURE expected, got 'POINTER TO anonymous RECORD'"],
  859. ["p2(TRUE, 1)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  860. ["p2(1, 1)", "type mismatch for argument 2: 'INTEGER' cannot be converted to 'BOOLEAN'"],
  861. ["p3", "procedure returning a result cannot be used as a statement"],
  862. ["p3()", "procedure returning a result cannot be used as a statement"],
  863. ["IF p() THEN END", "procedure returning no result cannot be used in an expression"],
  864. ["IF ~p() THEN END", "type mismatch: expected 'BOOLEAN', got no type (proper procedure call)"]
  865. )
  866. ),
  867. "procedure assignment": testWithContext(
  868. context(grammar.statement,
  869. "TYPE ProcType1 = PROCEDURE(): ProcType1;"
  870. + "ProcType2 = PROCEDURE(): ProcType2;"
  871. + "ProcType3 = PROCEDURE(p: ProcType3): ProcType3;"
  872. + "ProcType4 = PROCEDURE(p: ProcType4): ProcType4;"
  873. + "ProcType4VAR = PROCEDURE(VAR p: ProcType4VAR): ProcType4VAR;"
  874. + "ProcType5 = PROCEDURE(p: ProcType3): ProcType4;"
  875. + "ProcType6 = PROCEDURE(p: INTEGER);"
  876. + "ProcType7 = PROCEDURE(VAR p: INTEGER);"
  877. + "VAR v1: ProcType1; v2: ProcType2;"
  878. + "v3: PROCEDURE(i: INTEGER): ProcType1; v4: PROCEDURE(b: BOOLEAN): ProcType1;"
  879. + "v5: PROCEDURE(p: ProcType1); v6: PROCEDURE(p: ProcType2);"
  880. + "v7: ProcType3; v8: ProcType4; v8VAR: ProcType4VAR; v9: ProcType5; v10: ProcType6; v11: ProcType7;"
  881. + "vProcCharArray: PROCEDURE (a: ARRAY OF CHAR);"
  882. + "vProcInt: PROCEDURE (i: INTEGER);"
  883. + "vProcReturnInt: PROCEDURE(): INTEGER;"
  884. + "PROCEDURE p1(): ProcType1; RETURN p1 END p1;"
  885. + "PROCEDURE procCharArray(a: ARRAY OF CHAR); END procCharArray;"
  886. + "PROCEDURE procIntArray(a: ARRAY OF INTEGER); END procIntArray;"
  887. + "PROCEDURE procByte(b: BYTE); END procByte;"
  888. + "PROCEDURE procReturnByte(): BYTE; RETURN 0 END procReturnByte;"
  889. ),
  890. pass("v1 := v2",
  891. "v5 := v6",
  892. "v7 := v8",
  893. "v7 := v9",
  894. "v8 := v9",
  895. "v1 := p1",
  896. "vProcCharArray := procCharArray"),
  897. fail(["p1 := v1", "cannot assign to procedure"],
  898. ["v3 := v1",
  899. "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'ProcType1' expression"],
  900. ["v3 := v4",
  901. "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'PROCEDURE(BOOLEAN): ProcType1' expression"],
  902. ["v10 := NEW",
  903. "standard procedure NEW cannot be referenced"],
  904. ["v10 := v11", "type mismatch: 'v10' is 'ProcType6' and cannot be assigned to 'ProcType7' expression" ],
  905. ["v8 := v8VAR", "type mismatch: 'v8' is 'ProcType4' and cannot be assigned to 'ProcType4VAR' expression" ],
  906. ["vProcCharArray := procIntArray",
  907. "type mismatch: 'vProcCharArray' is 'PROCEDURE(ARRAY OF CHAR)' and cannot be assigned to 'PROCEDURE(ARRAY OF INTEGER)' expression"],
  908. ["vProcInt := procByte",
  909. "type mismatch: 'vProcInt' is 'PROCEDURE(INTEGER)' and cannot be assigned to 'PROCEDURE(BYTE)' expression"],
  910. ["vProcReturnInt := procReturnByte",
  911. "type mismatch: 'vProcReturnInt' is 'PROCEDURE(): INTEGER' and cannot be assigned to 'PROCEDURE(): BYTE' expression"]
  912. )
  913. ),
  914. "string assignment": testWithContext(
  915. context(grammar.statement,
  916. "VAR a1: ARRAY 3 OF CHAR;"
  917. + "ch1: CHAR;"
  918. + "intArray: ARRAY 10 OF INTEGER;"
  919. ),
  920. pass("a1 := \"abc\"",
  921. "a1 := \"ab\"",
  922. "a1 := \"a\"",
  923. "a1 := 22X",
  924. "ch1 := \"A\"",
  925. "ch1 := 22X"),
  926. fail(["a1 := \"abcd\"", "3-character ARRAY is too small for 4-character string"],
  927. ["intArray := \"abcd\"",
  928. "type mismatch: 'intArray' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'multi-character string' expression"])
  929. ),
  930. "string relations": testWithContext(
  931. context(grammar.expression,
  932. "VAR ch: CHAR;"),
  933. pass("ch = \"a\"",
  934. "\"a\" = ch",
  935. "ch # \"a\"",
  936. "\"a\" # ch"
  937. ),
  938. fail(["ch = \"ab\"", "type mismatch: expected 'CHAR', got 'multi-character string'"])
  939. ),
  940. "array assignment": testWithContext(
  941. context(grammar.statement,
  942. "VAR charArray: ARRAY 3 OF CHAR;"
  943. + "intArray: ARRAY 10 OF INTEGER;"
  944. + "intArray2: ARRAY 10 OF INTEGER;"
  945. + "intArray3: ARRAY 5 OF INTEGER;"
  946. + "intArray23m1: ARRAY 2 OF ARRAY 3 OF INTEGER;"
  947. + "intArray23m2: ARRAY 2, 3 OF INTEGER;"
  948. + "intArray24m: ARRAY 2, 4 OF INTEGER;"
  949. + "intArray43m: ARRAY 4, 3 OF INTEGER;"
  950. ),
  951. pass("intArray := intArray2",
  952. "intArray23m1 := intArray23m2",
  953. "intArray23m2 := intArray23m1",
  954. "intArray43m[0] := intArray23m1[0]"
  955. ),
  956. fail(["intArray := charArray",
  957. "type mismatch: 'intArray' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'ARRAY 3 OF CHAR' expression"],
  958. ["intArray2 := intArray3",
  959. "type mismatch: 'intArray2' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'ARRAY 5 OF INTEGER' expression"],
  960. ["intArray3 := charArray",
  961. "type mismatch: 'intArray3' is 'ARRAY 5 OF INTEGER' and cannot be assigned to 'ARRAY 3 OF CHAR' expression"],
  962. ["intArray24m := intArray23m1",
  963. "type mismatch: 'intArray24m' is 'ARRAY 2, 4 OF INTEGER' and cannot be assigned to 'ARRAY 2, 3 OF INTEGER' expression"]
  964. )
  965. ),
  966. "record assignment": testWithContext(
  967. context(grammar.statement,
  968. "TYPE Base1 = RECORD END;"
  969. + "T1 = RECORD (Base1) END;"
  970. + "T2 = RECORD END;"
  971. + "VAR b1: Base1; r1: T1; r2: T2;"
  972. ),
  973. pass("r1 := r1",
  974. "b1 := r1"),
  975. fail(["r1 := r2", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'T2' expression"],
  976. ["r1 := b1", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'Base1' expression"])
  977. ),
  978. "string argument": testWithContext(
  979. context(grammar.statement,
  980. "PROCEDURE p1(s: ARRAY OF CHAR); END p1;"
  981. + "PROCEDURE p2(VAR s: ARRAY OF CHAR); END p2;"
  982. + "PROCEDURE p3(i: INTEGER); END p3;"
  983. + "PROCEDURE p4(a: ARRAY OF INTEGER); END p4;"
  984. ),
  985. pass("p1(\"abc\")"),
  986. fail(["p2(\"abc\")", "expression cannot be used as VAR parameter"],
  987. ["p3(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'INTEGER'"],
  988. ["p4(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'ARRAY OF INTEGER'"])
  989. ),
  990. "assert": testWithGrammar(
  991. grammar.statement,
  992. pass("ASSERT(TRUE)"),
  993. fail(["ASSERT()", "1 argument(s) expected, got 0"],
  994. ["ASSERT(TRUE, 123)", "1 argument(s) expected, got 2"],
  995. ["ASSERT(123)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'BOOLEAN'"])
  996. ),
  997. "imported module without exports": testWithModule(
  998. "MODULE test; END test.",
  999. pass("MODULE m; IMPORT test; END m."),
  1000. fail(["MODULE m; IMPORT test; BEGIN test.p(); END m.",
  1001. "identifier 'p' is not exported by module 'test'"],
  1002. ["MODULE m; IMPORT t := test; BEGIN t.p(); END m.",
  1003. "identifier 'p' is not exported by module 'test'"],
  1004. ["MODULE m; IMPORT test; BEGIN test(); END m.",
  1005. "PROCEDURE expected, got 'MODULE'"]
  1006. )),
  1007. "import record type": testWithModule(
  1008. "MODULE test; TYPE T* = RECORD f*: INTEGER; notExported: BOOLEAN END; END test.",
  1009. pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 0; END m."),
  1010. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.notExported := FALSE; END m.",
  1011. "type 'T' has no 'notExported' field"]
  1012. )),
  1013. "imported variables are read-only": testWithModule(
  1014. "MODULE test; VAR i*: INTEGER; END test.",
  1015. pass("MODULE m; IMPORT test; PROCEDURE p(i: INTEGER); END p; BEGIN p(test.i); END m."),
  1016. fail(["MODULE m; IMPORT test; BEGIN test.i := 123; END m.",
  1017. "cannot assign to imported variable"],
  1018. ["MODULE m; IMPORT test; PROCEDURE p(VAR i: INTEGER); END p; BEGIN p(test.i); END m.",
  1019. "imported variable cannot be passed as VAR actual parameter"]
  1020. )
  1021. ),
  1022. "import pointer type": testWithModule(
  1023. "MODULE test;"
  1024. + "TYPE TPAnonymous1* = POINTER TO RECORD END; TPAnonymous2* = POINTER TO RECORD END;"
  1025. + "Base* = RECORD END; TPDerived* = POINTER TO RECORD(Base) END;"
  1026. + "END test.",
  1027. pass("MODULE m; IMPORT test; VAR p1: test.TPAnonymous1; p2: test.TPAnonymous2; END m.",
  1028. "MODULE m; IMPORT test;"
  1029. + "VAR pb: POINTER TO test.Base; pd: test.TPDerived;"
  1030. + "BEGIN pb := pd; END m."),
  1031. fail(["MODULE m; IMPORT test; VAR p1: test.TPAnonymous1; p2: test.TPAnonymous2; BEGIN p1 := p2; END m.",
  1032. "type mismatch: 'p1' is 'TPAnonymous1' and cannot be assigned to 'TPAnonymous2' expression"]
  1033. )
  1034. ),
  1035. "import array type": testWithModule(
  1036. "MODULE test; TYPE TA* = ARRAY 3 OF INTEGER; END test.",
  1037. pass("MODULE m; IMPORT test; VAR a: test.TA; END m.")
  1038. ),
  1039. "import procedure type": testWithModule(
  1040. "MODULE test; TYPE TProc* = PROCEDURE; END test.",
  1041. pass("MODULE m; IMPORT test; VAR proc: test.TProc; END m.")
  1042. ),
  1043. "imported pointer type cannot be used in NEW if base type is not exported": testWithModule(
  1044. "MODULE test;"
  1045. + "TYPE T = RECORD END; TP* = POINTER TO T;"
  1046. + "TPAnonymous* = POINTER TO RECORD END; END test.",
  1047. pass(),
  1048. fail(["MODULE m; IMPORT test; VAR p: test.TPAnonymous; BEGIN NEW(p) END m.",
  1049. "non-exported RECORD type cannot be used in NEW"],
  1050. ["MODULE m; IMPORT test; VAR p: test.TP; BEGIN NEW(p) END m.",
  1051. "non-exported RECORD type cannot be used in NEW"])
  1052. ),
  1053. "imported pointer type cannot be dereferenced if base type is not exported (even if base of base type is exported)": testWithModule(
  1054. "MODULE test;"
  1055. + "TYPE B* = RECORD i: INTEGER END; T = RECORD(B) END; TP* = POINTER TO T;"
  1056. + "TPAnonymous* = POINTER TO RECORD(B) END;"
  1057. + "PROCEDURE makeTP*(): TP; VAR result: TP; BEGIN NEW(result); RETURN result END makeTP;"
  1058. + "PROCEDURE makeTPA*(): TPAnonymous; VAR result: TPAnonymous; BEGIN NEW(result); RETURN result END makeTPA;"
  1059. + "END test.",
  1060. pass(),
  1061. fail(["MODULE m; IMPORT test; VAR p: test.TPAnonymous; BEGIN p := test.makeTPA(); p.i := 123; END m.",
  1062. "POINTER TO non-exported RECORD type cannot be dereferenced"],
  1063. ["MODULE m; IMPORT test; VAR p: test.TP; BEGIN p := test.makeTP(); p.i := 123; END m.",
  1064. "POINTER TO non-exported RECORD type cannot be dereferenced"])
  1065. ),
  1066. "imported pointer variable: anonymous record field cannot be used": testWithModule(
  1067. "MODULE test; VAR p*: POINTER TO RECORD i: INTEGER END; END test.",
  1068. pass(),
  1069. fail(["MODULE m; IMPORT test; BEGIN ASSERT(test.p.i = 0) END m.",
  1070. "POINTER TO non-exported RECORD type cannot be dereferenced"])
  1071. ),
  1072. "procedure VAR section": testWithGrammar(
  1073. grammar.declarationSequence,
  1074. pass("VAR",
  1075. "VAR i: INTEGER;",
  1076. "VAR i, j: INTEGER;",
  1077. "VAR i, j: INTEGER; b: BOOLEAN;")
  1078. ),
  1079. "const declaration": testWithContext(
  1080. context(grammar.declarationSequence,
  1081. "CONST ci = 1; VAR v1: INTEGER;"),
  1082. pass("CONST i = 10;",
  1083. "CONST i = 1 + 2;",
  1084. "CONST i = ci + 2;",
  1085. "CONST i = ci * 2;",
  1086. "CONST i = ORD({0..5});",
  1087. "CONST i = ORD({0..5} <= {0..8});",
  1088. "CONST b = TRUE;",
  1089. "CONST b = {0..5} <= {0..8};",
  1090. "CONST c = \"a\";",
  1091. "CONST s = \"abc\";",
  1092. "CONST s0 = \"\";",
  1093. "CONST set = {};",
  1094. "CONST set = {1 + 2};",
  1095. "CONST set = {0..32 - 1};",
  1096. "CONST set = {ci};",
  1097. "CONST i1 = 1; b1 = TRUE;",
  1098. "CONST i1 = 1; i2 = i1 + 1;",
  1099. "CONST i1 = 1; i2 = i1 + 1; i3 = i2 + 2;"),
  1100. fail(["CONST i1 = v1;", "constant expression expected"],
  1101. ["CONST i1 = v1 * 2;", "constant expression expected"],
  1102. ["CONST i1 = v1 - 10;", "constant expression expected"],
  1103. ["CONST i1 = 10 - v1;", "constant expression expected"],
  1104. ["CONST s = {v1};", "constant expression expected"],
  1105. ["CONST s = {1, v1};", "constant expression expected"],
  1106. ["CONST s = {1..v1};", "constant expression expected"],
  1107. ["CONST s = {10 - v1..15};", "constant expression expected"])
  1108. ),
  1109. "POINTER forward declaration": testWithContext(
  1110. context(grammar.module, ""),
  1111. pass("MODULE m; TYPE T = POINTER TO NotDeclaredYet; NotDeclaredYet = RECORD END; END m.",
  1112. "MODULE m; TYPE T1 = POINTER TO NotDeclaredYet; T2 = POINTER TO NotDeclaredYet; NotDeclaredYet = RECORD END; END m."
  1113. ),
  1114. fail(["MODULE m; TYPE T = POINTER TO NotDeclaredYet; END m.",
  1115. "no declaration found for 'NotDeclaredYet'"],
  1116. ["MODULE m; TYPE T1 = POINTER TO NotDeclaredYet1; T2 = POINTER TO NotDeclaredYet2; END m.",
  1117. "no declaration found for 'NotDeclaredYet1', 'NotDeclaredYet2'"],
  1118. ["MODULE m; TYPE T1 = POINTER TO Forward; Forward = PROCEDURE; END m.",
  1119. "'Forward' must be of RECORD type because it was used before in the declation of POINTER"])
  1120. ),
  1121. "typeguard for VAR argument": testWithContext(
  1122. context(grammar.procedureDeclaration,
  1123. "TYPE Base = RECORD END; Derived = RECORD (Base) i: INTEGER END;"
  1124. + "T = RECORD END; TD = RECORD(T) b: Base END;"),
  1125. pass("PROCEDURE proc(VAR p: Base); BEGIN p(Derived).i := 1; END proc"),
  1126. fail(["PROCEDURE proc(p: Base); BEGIN p(Derived).i := 1; END proc",
  1127. "invalid type cast: a value variable cannot be used"],
  1128. ["PROCEDURE proc(p: TD); BEGIN p.b(Derived).i := 1; END proc",
  1129. "invalid type cast: a value variable cannot be used"],
  1130. ["PROCEDURE proc(VAR p: T); BEGIN p(TD).b(Derived).i := 1; END proc",
  1131. "invalid type cast: a value variable cannot be used"])
  1132. ),
  1133. "NEW for read only array element fails": testWithContext(
  1134. context(grammar.procedureDeclaration,
  1135. "TYPE P = POINTER TO RECORD END;"),
  1136. pass(),
  1137. fail(["PROCEDURE readOnlyPointers(a: ARRAY OF P); BEGIN NEW(a[0]) END readOnlyPointers",
  1138. "read-only array's element cannot be passed as VAR actual parameter"])
  1139. ),
  1140. "LEN": testWithGrammar(
  1141. grammar.procedureDeclaration,
  1142. pass("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a) END p",
  1143. "PROCEDURE p(VAR a: ARRAY OF BOOLEAN): INTEGER; RETURN LEN(a) END p",
  1144. "PROCEDURE p(): INTEGER; RETURN LEN(\"abc\") END p"),
  1145. fail(["PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a[0]) END p",
  1146. "ARRAY or string is expected as an argument of LEN, got 'INTEGER'"])
  1147. ),
  1148. "array expression": testWithGrammar(
  1149. grammar.procedureBody,
  1150. pass("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1 END",
  1151. "VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1; a[1] := a[0] END",
  1152. "VAR a1, a2: ARRAY 3 OF CHAR; BEGIN ASSERT(a1 = a2); END",
  1153. "VAR a1: ARRAY 2 OF CHAR; a2: ARRAY 3 OF CHAR; BEGIN ASSERT(a1 = a2); END",
  1154. "CONST cs = \"a\"; VAR a: ARRAY 3 OF CHAR; BEGIN ASSERT(a = cs); ASSERT(cs # a); ASSERT(a < cs); ASSERT(cs > a); END",
  1155. "CONST cs = \"a\"; BEGIN ASSERT(cs[0] = \"a\"); END"
  1156. ),
  1157. fail(["VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := TRUE END",
  1158. "type mismatch: 'a[0]' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression"],
  1159. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[TRUE] := 1 END",
  1160. "'INTEGER' or 'BYTE' expression expected, got 'BOOLEAN'"],
  1161. ["VAR a: ARRAY 10 OF INTEGER; p: POINTER TO RECORD END; BEGIN a[p] := 1 END",
  1162. "'INTEGER' or 'BYTE' expression expected, got 'POINTER TO anonymous RECORD'"],
  1163. ["VAR i: INTEGER; BEGIN i[0] := 1 END",
  1164. "ARRAY or string expected, got 'INTEGER'"],
  1165. ["VAR p: POINTER TO RECORD END; BEGIN p[0] := 1 END",
  1166. "ARRAY or string expected, got 'POINTER TO anonymous RECORD'"],
  1167. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[0][0] := 1 END",
  1168. "ARRAY or string expected, got 'INTEGER'"],
  1169. ["VAR a: ARRAY 10 OF BOOLEAN; BEGIN a[0,0] := TRUE END",
  1170. "ARRAY or string expected, got 'BOOLEAN'"],
  1171. ["VAR a: ARRAY 10, 20 OF BOOLEAN; BEGIN a[0] := TRUE END",
  1172. "type mismatch: 'a[0]' is 'ARRAY 20 OF BOOLEAN' and cannot be assigned to 'BOOLEAN' expression"],
  1173. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[10] := 0 END",
  1174. "index out of bounds: maximum possible index is 9, got 10"],
  1175. ["CONST c1 = 5; VAR a: ARRAY 10 OF INTEGER; BEGIN a[10 + c1] := 0 END",
  1176. "index out of bounds: maximum possible index is 9, got 15"],
  1177. ["VAR a1, a2: ARRAY 3 OF INTEGER; BEGIN ASSERT(a1 = a2); END",
  1178. "operator '=' type mismatch: numeric type or SET or BOOLEAN or CHAR or character array or POINTER or PROCEDURE expected, got 'ARRAY 3 OF INTEGER'"],
  1179. ["CONST cs = \"\"; BEGIN ASSERT(cs[0] = \"a\"); END",
  1180. "cannot index empty string"],
  1181. ["CONST cs = \"\"; VAR i: INTEGER; BEGIN ASSERT(cs[i] = \"a\"); END",
  1182. "cannot index empty string"],
  1183. ["CONST cs = \"a\"; BEGIN ASSERT(cs[1] = \"a\"); END",
  1184. "index out of bounds: maximum possible index is 0, got 1"],
  1185. ["CONST ci = -1; VAR a: ARRAY 10 OF INTEGER; BEGIN ASSERT(a[ci] = 0); END",
  1186. "index is negative: -1"],
  1187. ["CONST ci = -1; PROCEDURE p(a: ARRAY OF INTEGER); BEGIN ASSERT(a[ci] = 0); END p; END",
  1188. "index is negative: -1"]
  1189. )
  1190. ),
  1191. "multi-dimensional array expression": testWithGrammar(
  1192. grammar.procedureBody,
  1193. pass("VAR a: ARRAY 10 OF ARRAY 5 OF INTEGER; BEGIN a[0][0] := 1 END",
  1194. "VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0][0] := TRUE END",
  1195. "VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0, 0] := TRUE END")
  1196. ),
  1197. "selector": testWithContext(
  1198. context(grammar.expression,
  1199. "TYPE T = RECORD field: INTEGER END; VAR r: T; i: INTEGER;"),
  1200. pass("r.field"),
  1201. fail(["i.field",
  1202. "selector '.field' cannot be applied to 'INTEGER'"],
  1203. ["T.field", "selector '.field' cannot be applied to 'type T'"])
  1204. ),
  1205. "procedure body": testWithGrammar(
  1206. grammar.procedureBody,
  1207. pass("END",
  1208. "VAR END",
  1209. "VAR i: INTEGER; END",
  1210. "VAR a: ARRAY 10 OF INTEGER; END",
  1211. "VAR i: INTEGER; BEGIN i := 1 END",
  1212. "VAR b: BOOLEAN; BEGIN b := TRUE END",
  1213. "VAR i, j: INTEGER; BEGIN i := 1; j := 2; i := 1 + i + j - 2 END",
  1214. "TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.field := 1 END",
  1215. "TYPE T1 = RECORD field: INTEGER END; T2 = RECORD field: T1 END; VAR v1: T1; v2: T2; BEGIN v1.field := v2.field.field END",
  1216. "TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field2: INTEGER END; VAR v: T2; BEGIN v.field2 := v.field1 END"),
  1217. fail(["VAR i: INTEGER;", "END expected (PROCEDURE)"],
  1218. ["VAR i: INTEGER; i := 1; END", "END expected (PROCEDURE)"],
  1219. ["VAR i: INTEGER; BEGIN j := 1 END", "undeclared identifier: 'j'"],
  1220. ["VAR i: INTEGER; BEGIN i := j END", "undeclared identifier: 'j'"],
  1221. ["TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v := 1 END",
  1222. "type mismatch: 'v' is 'T' and cannot be assigned to 'INTEGER' expression"],
  1223. ["TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.unknown := 1 END",
  1224. "type 'T' has no 'unknown' field"],
  1225. ["TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field1: INTEGER END; END",
  1226. "base record already has field: 'field1'"])
  1227. ),
  1228. "procedure": testWithContext(
  1229. context(grammar.procedureDeclaration,
  1230. "TYPE ProcType = PROCEDURE(): ProcType;"),
  1231. pass("PROCEDURE p; END p",
  1232. "PROCEDURE p; VAR i: INTEGER; BEGIN i := i + 1 END p",
  1233. "PROCEDURE p(a1, a2: INTEGER); END p",
  1234. "PROCEDURE p; BEGIN p() END p",
  1235. "PROCEDURE p(a: INTEGER); BEGIN p(a) END p",
  1236. "PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(a, b) END p",
  1237. "PROCEDURE p(): ProcType; RETURN p END p"),
  1238. fail(["PROCEDURE p1; END p2",
  1239. "mismatched procedure names: 'p1' at the begining and 'p2' at the end"],
  1240. ["PROCEDURE p(a: INTEGER); VAR a: INTEGER END p", "'a' already declared"],
  1241. ["PROCEDURE p(a: INTEGER); BEGIN p() END p", "1 argument(s) expected, got 0"],
  1242. ["PROCEDURE p(a: INTEGER); BEGIN p(1, 2) END p", "1 argument(s) expected, got 2"],
  1243. ["PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(b, a) END p",
  1244. "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  1245. ["PROCEDURE p; BEGIN p1() END p", "undeclared identifier: 'p1'"],
  1246. ["PROCEDURE p(a1: INTEGER; a1: BOOLEAN)", "'a1' already declared"],
  1247. ["PROCEDURE p(p: INTEGER)", "argument 'p' has the same name as procedure"]
  1248. )
  1249. ),
  1250. "procedure RETURN": testWithContext(
  1251. context(
  1252. grammar.procedureDeclaration,
  1253. "TYPE A = ARRAY 3 OF INTEGER; R = RECORD END; PR = POINTER TO R;"
  1254. + "VAR i: INTEGER; PROCEDURE int(): INTEGER; RETURN 1 END int;"),
  1255. pass("PROCEDURE p(): BOOLEAN; RETURN TRUE END p",
  1256. "PROCEDURE p(): BOOLEAN; RETURN int() = 1 END p",
  1257. "PROCEDURE p; BEGIN END p" ,
  1258. "PROCEDURE p(): INTEGER; BEGIN RETURN 0 END p"),
  1259. fail(["PROCEDURE p; RETURN TRUE END p", "unexpected RETURN in PROCEDURE declared with no result type"],
  1260. ["PROCEDURE p(): BOOLEAN; END p", "RETURN expected at the end of PROCEDURE declared with 'BOOLEAN' result type"],
  1261. ["PROCEDURE p(): undeclared; END p", "undeclared identifier: 'undeclared'"],
  1262. ["PROCEDURE p(): i; END p", "type name expected"],
  1263. ["PROCEDURE p(): INTEGER; RETURN TRUE END p", "RETURN 'INTEGER' expected, got 'BOOLEAN'"],
  1264. ["PROCEDURE p(a: A): A; RETURN a END p", "the result type of a procedure cannot be an ARRAY"],
  1265. ["PROCEDURE p(): A; VAR a: A; RETURN a END p", "the result type of a procedure cannot be an ARRAY"],
  1266. ["PROCEDURE p(r: R): R; RETURN r END p", "the result type of a procedure cannot be a RECORD"],
  1267. ["PROCEDURE p(): R; VAR r: R; RETURN r END p", "the result type of a procedure cannot be a RECORD"],
  1268. ["PROCEDURE p(pr: PR): R; RETURN pr END p", "the result type of a procedure cannot be a RECORD"]
  1269. )
  1270. ),
  1271. "pass VAR argument as VAR parameter": testWithContext(
  1272. context(grammar.procedureDeclaration,
  1273. "PROCEDURE p1(VAR i: INTEGER); END p1;"
  1274. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"),
  1275. pass("PROCEDURE p(VAR i1: INTEGER); BEGIN p1(i1) END p"),
  1276. fail(["PROCEDURE p(VAR b: BOOLEAN); BEGIN p2(~b) END p", "expression cannot be used as VAR parameter"])
  1277. ),
  1278. "ARRAY parameter": testWithContext(
  1279. context(grammar.procedureDeclaration,
  1280. "TYPE T = RECORD i: INTEGER; p: POINTER TO T END;"
  1281. + "PROCEDURE p1(i: INTEGER); END p1;"
  1282. + "PROCEDURE varInteger(VAR i: INTEGER); END varInteger;"
  1283. + "PROCEDURE p2(a: ARRAY OF INTEGER); END p2;"
  1284. + "PROCEDURE p3(VAR a: ARRAY OF INTEGER); END p3;"
  1285. ),
  1286. pass("PROCEDURE p(a: ARRAY OF INTEGER); END p",
  1287. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); END p",
  1288. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN p1(a[0][0]) END p",
  1289. "PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p2(a) END p",
  1290. "PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].p.i) END p"),
  1291. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN a[0] := 0 END p",
  1292. "cannot assign to read-only array's element"],
  1293. ["PROCEDURE p(a: ARRAY OF T); BEGIN a[0].i := 0 END p",
  1294. "cannot assign to read-only record's field"],
  1295. ["PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].i) END p",
  1296. "read-only record's field cannot be passed as VAR actual parameter"])
  1297. ),
  1298. "RECORD parameter": testWithContext(
  1299. context(grammar.procedureDeclaration,
  1300. "TYPE T = RECORD i: INTEGER; p: POINTER TO T END;"
  1301. + "PROCEDURE intValue(i: INTEGER); END intValue;"
  1302. + "PROCEDURE intVar(VAR i: INTEGER); END intVar;"
  1303. + "PROCEDURE recordValue(r: T); END recordValue;"
  1304. + "PROCEDURE recordVar(VAR r: T); END recordVar;"
  1305. ),
  1306. pass("PROCEDURE p(VAR r: T); BEGIN r.i := 0; intVar(r.i); END p",
  1307. "PROCEDURE p(VAR r: T); BEGIN recordValue(r); recordVar(r); END p",
  1308. "PROCEDURE p(r: T); BEGIN intValue(r.i); recordValue(r); END p"
  1309. ),
  1310. fail(["PROCEDURE p(r: T); BEGIN r.i := 0 END p",
  1311. "cannot assign to read-only record's field"],
  1312. ["PROCEDURE p(r: T); BEGIN intVar(r.i); END p",
  1313. "read-only record's field cannot be passed as VAR actual parameter"]
  1314. )
  1315. ),
  1316. "local procedure": testWithContext(
  1317. context(grammar.procedureDeclaration,
  1318. "TYPE ProcType = PROCEDURE;" +
  1319. "VAR procVar: ProcType;" +
  1320. "PROCEDURE procWithProcArg(p: ProcType); END procWithProcArg;"),
  1321. pass("PROCEDURE p; PROCEDURE innerP; END innerP; END p",
  1322. "PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN innerP() END p"),
  1323. fail(["PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN procVar := innerP END p",
  1324. "local procedure 'innerP' cannot be referenced"],
  1325. ["PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN procWithProcArg(innerP) END p",
  1326. "local procedure 'innerP' cannot be referenced"],
  1327. ["PROCEDURE p; PROCEDURE innerP; VAR innerV: INTEGER; END innerP; BEGIN innerV := 0 END p",
  1328. "undeclared identifier: 'innerV'"])
  1329. ),
  1330. "open array assignment fails": testWithGrammar(
  1331. grammar.procedureDeclaration,
  1332. pass(),
  1333. fail(["PROCEDURE p(VAR s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  1334. "'s1' is open 'ARRAY OF CHAR' and cannot be assigned"],
  1335. ["PROCEDURE p(s1: ARRAY OF CHAR); VAR s2: ARRAY 10 OF CHAR; BEGIN s2 := s1 END p",
  1336. "type mismatch: 's2' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'ARRAY OF CHAR' expression"])
  1337. ),
  1338. "open array type as procedure parameter": testWithContext(
  1339. context(grammar.procedureDeclaration,
  1340. "TYPE A = ARRAY 3 OF INTEGER;"
  1341. ),
  1342. pass("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN END p",
  1343. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN END p",
  1344. "PROCEDURE p(a: ARRAY OF A); BEGIN END p"
  1345. ),
  1346. fail(["PROCEDURE p(a: ARRAY OF ARRAY 3 OF INTEGER); BEGIN END p",
  1347. "')' expected"]
  1348. )
  1349. ),
  1350. "non-open array type as procedure parameter": testWithContext(
  1351. context(grammar.procedureDeclaration,
  1352. "TYPE A = ARRAY 2 OF INTEGER;"
  1353. + "VAR a: A;"
  1354. + "PROCEDURE pa(a: A); BEGIN END pa;"
  1355. ),
  1356. pass("PROCEDURE p(a: A); BEGIN END p",
  1357. "PROCEDURE p(); VAR a: A; BEGIN pa(a) END p",
  1358. "PROCEDURE p(); VAR a: ARRAY 2 OF INTEGER; BEGIN pa(a) END p"
  1359. ),
  1360. fail(["PROCEDURE p(a: ARRAY 3 OF INTEGER); BEGIN END p",
  1361. "')' expected"],
  1362. ["PROCEDURE p(a: A): INTEGER; BEGIN RETURN a[2] END p",
  1363. "index out of bounds: maximum possible index is 1, got 2"],
  1364. ["PROCEDURE p(); VAR a: ARRAY 1 OF INTEGER; BEGIN pa(a) END p",
  1365. "type mismatch for argument 1: 'ARRAY 1 OF INTEGER' cannot be converted to 'ARRAY 2 OF INTEGER'"],
  1366. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pa(a) END p",
  1367. "type mismatch for argument 1: 'ARRAY OF INTEGER' cannot be converted to 'ARRAY 2 OF INTEGER'"]
  1368. )
  1369. ),
  1370. "string assignment to open array fails": testWithGrammar(
  1371. grammar.procedureDeclaration,
  1372. pass(),
  1373. fail(["PROCEDURE p(VAR s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "string cannot be assigned to open ARRAY OF CHAR"])
  1374. ),
  1375. "scope": testWithGrammar(
  1376. grammar.declarationSequence,
  1377. pass("PROCEDURE p1(a1: INTEGER); END p1; PROCEDURE p2(a1: BOOLEAN); END p2;")
  1378. ),
  1379. "module": testWithGrammar(
  1380. grammar.module,
  1381. pass("MODULE m; END m."),
  1382. fail(["MODULE m; END undeclared.",
  1383. "original module name 'm' expected, got 'undeclared'"],
  1384. ["MODULE m; BEGIN - END m.", "END expected (MODULE)"])
  1385. ),
  1386. "export": testWithGrammar(
  1387. grammar.declarationSequence,
  1388. pass("CONST i* = 1;",
  1389. "TYPE T* = RECORD END;",
  1390. "VAR i*: INTEGER;",
  1391. "PROCEDURE p*; END p;"
  1392. ),
  1393. fail(["TYPE T = RECORD f*: INTEGER END;",
  1394. "field 'f' can be exported only if record 'T' itself is exported too"],
  1395. ["TYPE PT* = POINTER TO RECORD f*: INTEGER END;",
  1396. "cannot export anonymous RECORD field: 'f'"],
  1397. ["VAR p: POINTER TO RECORD f*: INTEGER END;",
  1398. "cannot export anonymous RECORD field: 'f'"],
  1399. ["VAR p*: POINTER TO RECORD r: RECORD f*: INTEGER END END;",
  1400. "field 'f' can be exported only if field 'r' itself is exported too"],
  1401. ["VAR i*: POINTER TO RECORD f*: INTEGER END;",
  1402. "cannot export anonymous RECORD field: 'f'"],
  1403. ["VAR i*: POINTER TO RECORD r*: RECORD f*: INTEGER END END;",
  1404. "cannot export anonymous RECORD field: 'r'"],
  1405. ["PROCEDURE p*; VAR i*: INTEGER; END p;",
  1406. "cannot export from within procedure: variable 'i'"]
  1407. //["TYPE PT = POINTER TO RECORD END; PROCEDURE p*(): PT; RETURN NIL END p;",
  1408. //"exported PROCEDURE 'p' uses non-exported type 'PT'"]
  1409. )
  1410. ),
  1411. "import JS": testWithGrammar(
  1412. grammar.module,
  1413. pass("MODULE m; IMPORT JS; END m.",
  1414. "MODULE m; IMPORT JS; BEGIN JS.alert(\"test\") END m.",
  1415. "MODULE m; IMPORT JS; BEGIN JS.console.info(123) END m.",
  1416. "MODULE m; IMPORT JS; BEGIN JS.do(\"throw new Error()\") END m."
  1417. ),
  1418. fail(["MODULE m; IMPORT JS; BEGIN JS.do(123) END m.",
  1419. "string is expected as an argument of JS predefined procedure 'do', got INTEGER"],
  1420. ["MODULE m; IMPORT JS; BEGIN JS.do(\"a\", \"b\") END m.",
  1421. "1 argument(s) expected, got 2"],
  1422. ["MODULE m; IMPORT JS; VAR s: ARRAY 10 OF CHAR; BEGIN JS.do(s) END m.",
  1423. "string is expected as an argument of JS predefined procedure 'do', got ARRAY 10 OF CHAR"]
  1424. )
  1425. ),
  1426. "JS.var": testWithGrammar(
  1427. grammar.module,
  1428. pass("MODULE m; IMPORT JS; VAR v: JS.var; END m.",
  1429. "MODULE m; IMPORT JS; VAR v: JS.var; BEGIN v := JS.f(); END m.",
  1430. "MODULE m; IMPORT JS; VAR v: JS.var; BEGIN v := JS.f1(); JS.f2(v); END m."
  1431. ),
  1432. fail(["MODULE m; IMPORT JS; VAR v: JS.var; i: INTEGER; BEGIN i := v; END m.",
  1433. "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'JS.var' expression"])
  1434. ),
  1435. "import unknown module": testWithGrammar(
  1436. grammar.module,
  1437. pass(),
  1438. fail(["MODULE m; IMPORT unknown; END m.", "module(s) not found: unknown"],
  1439. ["MODULE m; IMPORT unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2"]
  1440. )
  1441. ),
  1442. "self import is failed": testWithGrammar(
  1443. grammar.module,
  1444. pass(),
  1445. fail(["MODULE test; IMPORT test; END test.", "module 'test' cannot import itself"])
  1446. ),
  1447. "import aliases": testWithGrammar(
  1448. grammar.module,
  1449. pass("MODULE m; IMPORT J := JS; END m.",
  1450. "MODULE m; IMPORT J := JS; BEGIN J.alert(\"test\") END m."),
  1451. fail(["MODULE m; IMPORT u1 := unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2"],
  1452. ["MODULE m; IMPORT a1 := m1, a2 := m1; END m.", "module already imported: 'm1'"],
  1453. ["MODULE m; IMPORT a1 := u1, a1 := u2; END m.", "duplicated alias: 'a1'"],
  1454. ["MODULE m; IMPORT J := JS; BEGIN JS.alert(\"test\") END m.", "undeclared identifier: 'JS'"]
  1455. )
  1456. ),
  1457. "syntax errors": testWithGrammar(
  1458. grammar.module,
  1459. pass(),
  1460. fail(["MODULE m; CONST c = 1 END m.",
  1461. "';' expected"],
  1462. ["MODULE m; TYPE T = RECORD END END m.",
  1463. "';' expected"],
  1464. ["MODULE m; VAR v: INTEGER END m.",
  1465. "';' expected"],
  1466. ["MODULE m; PROCEDURE p(INTEGER) END m.",
  1467. "')' expected"])
  1468. )
  1469. };
  1470. }
  1471. function run(){
  1472. return Test.run({
  1473. "common": {
  1474. "oberon": makeSuiteForGrammar(oberon),
  1475. "eberon": makeSuiteForGrammar(eberon)
  1476. },
  1477. "eberon": TestUnitEberon.suite,
  1478. "oberon": TestUnitOberon.suite
  1479. });
  1480. }
  1481. exports.run = run;