test_unit.js 76 KB

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