test_unit.js 68 KB

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