2
0

test_unit.js 62 KB

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