test_unit.js 70 KB

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