test_unit.js 62 KB

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