test_unit.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. "array declaration": testWithContext(
  271. context(Grammar.typeDeclaration,
  272. "CONST c1 = 5; VAR v1: INTEGER;"),
  273. pass("T = ARRAY 10 OF INTEGER",
  274. "T = ARRAY 10 OF BOOLEAN",
  275. "T = ARRAY 1 + 2 OF INTEGER",
  276. "T = ARRAY c1 OF INTEGER",
  277. "T = ARRAY ORD({0..5} <= {0..8}) OF INTEGER",
  278. "T = ARRAY 1, 2 OF ARRAY 3, 4 OF INTEGER"
  279. ),
  280. fail(["T = ARRAY 0 OF INTEGER",
  281. "array size must be greater than 0, got 0"],
  282. ["T = ARRAY TRUE OF INTEGER",
  283. "'INTEGER' constant expression expected, got 'BOOLEAN'"],
  284. ["T = ARRAY v1 OF INTEGER",
  285. "constant expression expected as ARRAY size"],
  286. ["T = ARRAY c1 - 10 OF INTEGER",
  287. "array size must be greater than 0, got -5"],
  288. ["T = ARRAY ORD({0..5} >= {0..8}) OF INTEGER",
  289. "array size must be greater than 0, got 0"]
  290. )
  291. ),
  292. "multi-dimensional array declaration": testWithGrammar(
  293. Grammar.typeDeclaration,
  294. pass("T = ARRAY 10 OF ARRAY 5 OF INTEGER",
  295. "T = ARRAY 10, 5 OF INTEGER")
  296. ),
  297. "PROCEDURE type declaration": testWithGrammar(
  298. Grammar.typeDeclaration,
  299. pass("T = PROCEDURE",
  300. "T = PROCEDURE()",
  301. "T = PROCEDURE(a: INTEGER)",
  302. "T = PROCEDURE(a: INTEGER; b: BOOLEAN)",
  303. "T = PROCEDURE(): T")
  304. ),
  305. "POINTER declaration": testWithGrammar(
  306. Grammar.typeDeclaration,
  307. pass("T = POINTER TO RECORD END",
  308. "T = RECORD p: POINTER TO T END",
  309. "T = POINTER TO RECORD p: T END"),
  310. fail(["T = POINTER TO INTEGER",
  311. "RECORD is expected as a POINTER base type, got 'INTEGER'"],
  312. ["T = POINTER TO POINTER TO RECORD END",
  313. "RECORD is expected as a POINTER base type, got 'POINTER TO anonymous RECORD'"],
  314. ["T = POINTER TO RECORD p: POINTER TO T END",
  315. "RECORD is expected as a POINTER base type, got 'T'"]
  316. )
  317. ),
  318. "POINTER forward declaration": testWithContext(
  319. context(Grammar.module, ""),
  320. pass("MODULE m; TYPE T = POINTER TO NotDeclaredYet; NotDeclaredYet = RECORD END; END m.",
  321. "MODULE m; TYPE T1 = POINTER TO NotDeclaredYet; T2 = POINTER TO NotDeclaredYet; NotDeclaredYet = RECORD END; END m."
  322. ),
  323. fail(["MODULE m; TYPE T = POINTER TO NotDeclaredYet; END m.",
  324. "no declaration found for 'NotDeclaredYet'"],
  325. ["MODULE m; TYPE T1 = POINTER TO NotDeclaredYet1; T2 = POINTER TO NotDeclaredYet2; END m.",
  326. "no declaration found for 'NotDeclaredYet1', 'NotDeclaredYet2'"],
  327. ["MODULE m; TYPE T1 = POINTER TO Forward; Forward = PROCEDURE; END m.",
  328. "'Forward' must be of RECORD type because it was used before in the declation of POINTER"])
  329. ),
  330. "POINTER dereference": testWithContext(
  331. context(Grammar.statement,
  332. "VAR p: POINTER TO RECORD field: INTEGER END; i: INTEGER; r: RECORD END;"),
  333. pass("p^.field := 1",
  334. "p.field := 0"),
  335. fail(["i^", "POINTER TO type expected, got 'INTEGER'"],
  336. ["r^", "POINTER TO type expected, got 'anonymous RECORD'"])
  337. ),
  338. "POINTER assignment": testWithContext(
  339. context(Grammar.statement,
  340. "TYPE Base = RECORD END; Derived = RECORD (Base) END;"
  341. + "VAR p1, p2: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived;"),
  342. pass("p1 := NIL",
  343. "p1 := p2",
  344. "pBase := pDerived"),
  345. fail(["p1 := pBase",
  346. "type mismatch: 'p1' is 'POINTER TO anonymous RECORD' and cannot be assigned to 'POINTER TO Base' expression"],
  347. ["pDerived := pBase",
  348. "type mismatch: 'pDerived' is 'POINTER TO Derived' and cannot be assigned to 'POINTER TO Base' expression"],
  349. ["NIL := p1", "not parsed"])
  350. ),
  351. "typeguard": testWithContext(
  352. context(Grammar.expression,
  353. "TYPE Base = RECORD END; PBase = POINTER TO Base; Derived = RECORD (Base) END; PDerived = POINTER TO Derived;"
  354. + "VAR p1, p2: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived;"
  355. + "vb: Base; i: INTEGER;"),
  356. pass("pBase(PDerived)",
  357. "pBase^(Derived)"),
  358. fail(["pDerived(PDerived)",
  359. "invalid type cast: 'Derived' is not an extension of 'Derived'"],
  360. ["p1(PBase)",
  361. "invalid type cast: 'Base' is not an extension of 'anonymous RECORD'"],
  362. ["p1(INTEGER)",
  363. "invalid type cast: POINTER type expected as an argument of POINTER type guard, got 'INTEGER'"],
  364. ["i(Derived)",
  365. "invalid type cast: 'Derived' is not an extension of 'INTEGER'"],
  366. ["vb(Derived)",
  367. "invalid type cast: a value variable and cannot be used in typeguard"],
  368. ["vb(PDerived)",
  369. "invalid type cast: a value variable and cannot be used in typeguard"])
  370. ),
  371. "typeguard for VAR argument": testWithContext(
  372. context(Grammar.procedureDeclaration,
  373. "TYPE Base = RECORD END; Derived = RECORD (Base) i: INTEGER END;"
  374. + "T = RECORD END; TD = RECORD(T) b: Base END;"),
  375. pass("PROCEDURE proc(VAR p: Base); BEGIN p(Derived).i := 1; END proc"),
  376. fail(["PROCEDURE proc(p: Base); BEGIN p(Derived).i := 1; END proc",
  377. "invalid type cast: a value variable and cannot be used in typeguard"],
  378. ["PROCEDURE proc(p: TD); BEGIN p.b(Derived).i := 1; END proc",
  379. "invalid type cast: a value variable and cannot be used in typeguard"],
  380. ["PROCEDURE proc(VAR p: T); BEGIN p(TD).b(Derived).i := 1; END proc",
  381. "invalid type cast: a value variable and cannot be used in typeguard"])
  382. ),
  383. "POINTER relations": testWithContext(
  384. context(Grammar.expression,
  385. "TYPE B = RECORD END; D = RECORD(B) END;"
  386. + "VAR p1, p2: POINTER TO RECORD END; pb: POINTER TO B; pd: POINTER TO D;"),
  387. pass("p1 = p2",
  388. "p1 # p2",
  389. "pb = pd",
  390. "pd # pb"
  391. ),
  392. fail(["p1 < p2", "operator '<' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  393. ["p1 <= p2", "operator '<=' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  394. ["p1 > p2", "operator '>' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  395. ["p1 >= p2", "operator '>=' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  396. ["p1 = pb", "type mismatch: expected 'POINTER TO anonymous RECORD', got 'POINTER TO B'"]
  397. )
  398. ),
  399. "IS expression": testWithContext(
  400. context(Grammar.expression,
  401. "TYPE Base = RECORD END; Derived = RECORD (Base) END; PDerived = POINTER TO Derived;"
  402. + "VAR p: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived; vDerived: Derived; i: INTEGER;"),
  403. pass("pBase IS Derived"),
  404. fail(["pBase IS pDerived", "type name expected"],
  405. ["pBase IS TRUE", "type name expected"],
  406. ["pBase IS vDerived", "type name expected"],
  407. ["Derived IS Derived", "POINTER to type expected before 'IS'"],
  408. ["i IS Derived", "POINTER to type expected before 'IS'"],
  409. ["p IS Derived",
  410. "invalid type test: 'Derived' is not an extension of 'anonymous RECORD'"],
  411. ["pDerived IS Derived",
  412. "invalid type test: 'Derived' is not an extension of 'Derived'"],
  413. ["pDerived IS Base",
  414. "invalid type test: 'Base' is not an extension of 'Derived'"],
  415. ["pDerived IS INTEGER", "RECORD type expected after 'IS'"])
  416. ),
  417. "NEW": testWithContext(
  418. context(Grammar.statement,
  419. "TYPE P = POINTER TO RECORD END;"
  420. + "VAR p: P; i: INTEGER;"
  421. + "PROCEDURE proc(): P; RETURN NIL END proc;"
  422. ),
  423. pass("NEW(p)"),
  424. fail(["NEW.NEW(p)", "cannot designate 'standard procedure NEW'"],
  425. ["NEW(i)", "POINTER variable expected, got 'INTEGER'"],
  426. ["NEW()", "1 argument(s) expected, got 0"],
  427. ["NEW(p, p)", "1 argument(s) expected, got 2"],
  428. ["NEW(proc())", "expression cannot be used as VAR parameter"])
  429. ),
  430. "NEW for read only array element fails": testWithContext(
  431. context(Grammar.procedureDeclaration,
  432. "TYPE P = POINTER TO RECORD END;"),
  433. pass(),
  434. fail(["PROCEDURE readOnlyPointers(a: ARRAY OF P); BEGIN NEW(a[0]) END readOnlyPointers",
  435. "read-only variable cannot be used as VAR parameter"])
  436. ),
  437. "LEN": testWithGrammar(
  438. Grammar.procedureDeclaration,
  439. pass("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a) END p",
  440. "PROCEDURE p(VAR a: ARRAY OF BOOLEAN): INTEGER; RETURN LEN(a) END p",
  441. "PROCEDURE p(): INTEGER; RETURN LEN(\"abc\") END p"),
  442. fail(["PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a[0]) END p",
  443. "type mismatch for argument 1: 'INTEGER' cannot be converted to 'ARRAY OF any type'"])
  444. ),
  445. "ABS": testWithContext(
  446. context(Grammar.statement,
  447. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  448. pass("i := ABS(i)",
  449. "r := ABS(r)"),
  450. fail(["i := ABS(r)", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'REAL' expression"],
  451. ["i := ABS(c)", "type mismatch: expected numeric type, got 'CHAR'"],
  452. ["i := ABS(i, i)", "1 argument(s) expected, got 2"]
  453. )
  454. ),
  455. "FLOOR": testWithContext(
  456. context(Grammar.statement, "VAR i: INTEGER; r: REAL;"),
  457. pass("i := FLOOR(r)"),
  458. fail(["i := FLOOR(i)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'REAL'"],
  459. ["i := FLOOR(r, r)", "1 argument(s) expected, got 2"]
  460. )
  461. ),
  462. "FLT": testWithContext(
  463. context(Grammar.statement, "VAR i: INTEGER; r: REAL;"),
  464. pass("r := FLT(i)"),
  465. fail(["r := FLT(r)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  466. ["i := FLT(i, i)", "1 argument(s) expected, got 2"]
  467. )
  468. ),
  469. "LONG": testWithContext(
  470. context(Grammar.statement, "VAR i: INTEGER; r: REAL; lr: LONGREAL;"),
  471. pass("lr := LONG(r)"),
  472. fail(["lr := LONG(i)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'REAL'"],
  473. ["lr := LONG(r, r)", "1 argument(s) expected, got 2"]
  474. )
  475. ),
  476. "SHORT": testWithContext(
  477. context(Grammar.statement, "VAR i: INTEGER; r: REAL; lr: LONGREAL;"),
  478. pass("r := SHORT(lr)"),
  479. fail(["r := SHORT(i)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'REAL'"],
  480. ["r := SHORT(lr, lr)", "1 argument(s) expected, got 2"]
  481. )
  482. ),
  483. "LSL": testWithContext(
  484. context(Grammar.statement,
  485. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  486. pass("i := LSL(i, i)"),
  487. fail(["i := LSL(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  488. ["i := LSL(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  489. ["r := LSL(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  490. ["i := LSL(i)", "2 argument(s) expected, got 1"]
  491. )
  492. ),
  493. "ASR": testWithContext(
  494. context(Grammar.statement,
  495. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  496. pass("i := ASR(i, i)"),
  497. fail(["i := ASR(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  498. ["i := ASR(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  499. ["r := ASR(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  500. ["i := ASR(i)", "2 argument(s) expected, got 1"]
  501. )
  502. ),
  503. "ROR": testWithContext(
  504. context(Grammar.statement,
  505. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  506. pass("i := ROR(i, i)"),
  507. fail(["i := ROR(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  508. ["i := ROR(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  509. ["r := ROR(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  510. ["i := ROR(i)", "2 argument(s) expected, got 1"]
  511. )
  512. ),
  513. "ODD": testWithContext(
  514. context(Grammar.statement, "VAR b: BOOLEAN;"),
  515. pass("b := ODD(1)",
  516. "b := ODD(123)"
  517. ),
  518. fail(["b := ODD(1.2)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  519. ["b := ODD(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
  520. )
  521. ),
  522. "ORD": testWithContext(
  523. context(Grammar.statement, "VAR ch: CHAR; i: INTEGER; b: BOOLEAN;"),
  524. pass("i := ORD(ch)",
  525. "i := ORD(TRUE)",
  526. "i := ORD({1})",
  527. "i := ORD(\"a\")",
  528. "b := ORD(22X) = 022H"),
  529. fail(["i := ORD(1.2)", "type mismatch for argument 1: 'REAL' cannot be converted to 'CHAR or BOOLEAN or SET'"],
  530. ["i := ORD(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'CHAR or BOOLEAN or SET'"]
  531. )
  532. ),
  533. "CHR": testWithContext(
  534. context(Grammar.statement, "VAR i: INTEGER; ch: CHAR;"),
  535. pass("ch := CHR(i)"),
  536. fail(["ch := CHR(ch)", "type mismatch for argument 1: 'CHAR' cannot be converted to 'INTEGER'"])
  537. ),
  538. "INC": testWithContext(
  539. context(Grammar.statement, "VAR i: INTEGER;"),
  540. pass("INC(i)",
  541. "INC(i, 3)"),
  542. fail(["INC(i + i)", "expression cannot be used as VAR parameter"],
  543. ["INC(i, i)", "constant expected as second argument of INC"],
  544. ["INC()", "at least 1 argument expected, got 0"],
  545. ["INC(i, 1, 2)", "at most 2 arguments expected, got 3"]
  546. )
  547. ),
  548. "DEC": testWithContext(
  549. context(Grammar.statement, "VAR i: INTEGER;"),
  550. pass("DEC(i)",
  551. "DEC(i, 3)"),
  552. fail(["DEC(i + i)", "expression cannot be used as VAR parameter"],
  553. ["DEC(i, i)", "constant expected as second argument of DEC"],
  554. ["DEC()", "at least 1 argument expected, got 0"],
  555. ["DEC(i, 1, 2)", "at most 2 arguments expected, got 3"]
  556. )
  557. ),
  558. "COPY": testWithContext(
  559. context(Grammar.statement, "VAR ac3: ARRAY 3 OF CHAR; ac4: ARRAY 4 OF CHAR;"),
  560. pass("COPY(\"abc\", ac3)",
  561. "COPY(ac3, ac3)"
  562. ),
  563. fail(["COPY(ac3, \"abc\")", "expression cannot be used as VAR parameter"],
  564. ["COPY(\"abcd\", ac3)", "3-character ARRAY is too small for 4-character string"],
  565. ["COPY(ac3, ac4)", "type mismatch: 'ac4' is 'ARRAY 4 OF CHAR' and cannot be assigned to 'ARRAY 3 OF CHAR' expression"]
  566. )
  567. ),
  568. "PACK": testWithContext(
  569. context(Grammar.statement, "VAR r: REAL; i: INTEGER;"),
  570. pass("PACK(r, i)",
  571. "PACK(r, 3)"),
  572. fail(["PACK(r, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"])
  573. ),
  574. "UNPACK": testWithContext(
  575. context(Grammar.statement, "VAR r: REAL; i: INTEGER;"),
  576. pass("UNPACK(r, i)"),
  577. fail(["UNPACK(r, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  578. ["UNPACK(r, 3)", "expression cannot be used as VAR parameter"],
  579. ["UNPACK(123.456, i)", "expression cannot be used as VAR parameter"]
  580. )
  581. ),
  582. "standard procedure cannot be referenced" : testWithContext(
  583. context(Grammar.expression, "VAR chr: PROCEDURE(c: CHAR): INTEGER;"),
  584. pass(),
  585. fail(["CHR", "standard procedure CHR cannot be referenced"])
  586. ),
  587. "assignment statement": testWithContext(
  588. context(Grammar.statement,
  589. "CONST c = 15;"
  590. + "VAR ch: CHAR; i, n: INTEGER; b: BOOLEAN;"
  591. + "proc1: PROCEDURE; proc2: PROCEDURE(): INTEGER;"
  592. + "a: ARRAY 5 OF INTEGER;"
  593. + "PROCEDURE p(): INTEGER; RETURN 1 END p;"
  594. + "PROCEDURE noResult(); END noResult;"),
  595. pass("i := 0",
  596. "i := n",
  597. "i := c",
  598. "b := TRUE",
  599. "ch := \"A\"",
  600. "i := p()",
  601. "proc1 := proc1",
  602. "proc2 := NIL",
  603. "a[1] := 2"),
  604. fail(["i := b", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression"],
  605. ["c := i", "cannot assign to constant"],
  606. ["ch := \"AB\"",
  607. "type mismatch: 'ch' is 'CHAR' and cannot be assigned to 'multi-character string' expression"],
  608. ["ch := CHAR",
  609. "type mismatch: 'ch' is 'CHAR' and cannot be assigned to 'type CHAR' expression"],
  610. ["i := .1", "expression expected"],
  611. ["proc1 := proc2",
  612. "type mismatch: 'proc1' is 'PROCEDURE' and cannot be assigned to 'PROCEDURE(): INTEGER' expression"],
  613. ["i := noResult()", "procedure returning no result cannot be used in an expression"])
  614. ),
  615. "array expression": testWithGrammar(
  616. Grammar.procedureBody,
  617. pass("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1 END",
  618. "VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1; a[1] := a[0] END"),
  619. fail(["VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := TRUE END",
  620. "type mismatch: 'a[0]' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression"],
  621. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[TRUE] := 1 END",
  622. "'INTEGER' expression expected, got 'BOOLEAN'"],
  623. ["VAR i: INTEGER; BEGIN i[0] := 1 END",
  624. "ARRAY expected, got 'INTEGER'"],
  625. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[0][0] := 1 END",
  626. "ARRAY expected, got 'INTEGER'"],
  627. ["VAR a: ARRAY 10 OF BOOLEAN; BEGIN a[0,0] := TRUE END",
  628. "ARRAY expected, got 'BOOLEAN'"],
  629. ["VAR a: ARRAY 10, 20 OF BOOLEAN; BEGIN a[0] := TRUE END",
  630. "type mismatch: 'a[0]' is 'ARRAY 20 OF BOOLEAN' and cannot be assigned to 'BOOLEAN' expression"],
  631. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[10] := 0 END",
  632. "index out of bounds: maximum possible index is 9, got 10"],
  633. ["CONST c1 = 5; VAR a: ARRAY 10 OF INTEGER; BEGIN a[10 + c1] := 0 END",
  634. "index out of bounds: maximum possible index is 9, got 15"])
  635. ),
  636. "multi-dimensional array expression": testWithGrammar(
  637. Grammar.procedureBody,
  638. pass("VAR a: ARRAY 10 OF ARRAY 5 OF INTEGER; BEGIN a[0][0] := 1 END",
  639. "VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0][0] := TRUE END",
  640. "VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0, 0] := TRUE END")
  641. ),
  642. "INTEGER number": testWithGrammar(
  643. Grammar.expression,
  644. pass("0",
  645. "123",
  646. "1H",
  647. "1FH",
  648. "0FFH",
  649. "0H"),
  650. fail(["FFH", "undeclared identifier: 'FFH'"],
  651. ["FF", "undeclared identifier: 'FF'"],
  652. ["1HH", "not parsed"],
  653. ["1H0", "not parsed"],
  654. ["1 23", "not parsed"],
  655. ["1F FH", "not parsed"])
  656. ),
  657. "SET statement": testWithContext(
  658. context(Grammar.statement, "VAR s: SET;"),
  659. pass("s := {}",
  660. "s := {0}",
  661. "s := {0, 1}",
  662. "s := {1 + 2, 5..10}")
  663. //fail("s := {32}", "0..31")
  664. ),
  665. "REAL number": testWithGrammar(
  666. Grammar.expression,
  667. pass("1.2345",
  668. "1.",
  669. "1.2345E6",
  670. "1.2345E+6",
  671. "1.2345E-12"),
  672. fail(["1. 2345E-12", "not parsed"],
  673. ["1.23 45E-12", "not parsed"],
  674. ["1.2345 E-12", "not parsed"],
  675. ["1.2345E-1 2", "not parsed"])
  676. ),
  677. "LONGREAL number": testWithGrammar(
  678. Grammar.expression,
  679. pass("1.2345D6",
  680. "1.2345D+6",
  681. "1.2345D-6")
  682. ),
  683. "IF statement": testWithContext(
  684. context(Grammar.statement,
  685. "VAR b1: BOOLEAN; i1: INTEGER;"),
  686. pass("IF b1 THEN i1 := 0 END",
  687. "IF FALSE THEN i1 := 0 ELSE i1 := 1 END",
  688. "IF TRUE THEN i1 := 0 ELSIF FALSE THEN i1 := 1 ELSE i1 := 2 END"),
  689. fail(["IF i1 THEN i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'"],
  690. ["IF b1 THEN i1 := 0 ELSIF i1 THEN i1 := 2 END",
  691. "'BOOLEAN' expression expected, got 'INTEGER'"])
  692. ),
  693. "CASE statement": testWithContext(
  694. context(Grammar.statement,
  695. "CONST ci = 15; cc = \"A\"; VAR c1: CHAR; b1: BOOLEAN; i1, i2: INTEGER;"),
  696. pass("CASE i1 OF END",
  697. "CASE i1 OF 0: b1 := TRUE END",
  698. "CASE c1 OF \"A\": b1 := TRUE END",
  699. "CASE i1 OF 0: b1 := TRUE | 1: b1 := FALSE END",
  700. "CASE i1 OF 0, 1: b1 := TRUE END",
  701. "CASE c1 OF \"A\", \"B\": b1 := TRUE END",
  702. "CASE i1 OF 0..2: b1 := TRUE END",
  703. "CASE i1 OF ci..2: b1 := TRUE END",
  704. "CASE c1 OF cc..\"Z\": b1 := TRUE END",
  705. "CASE i1 OF 1, 2, 3: b1 := TRUE | 4..10: b1 := FALSE | 11: c1 := \"A\" END",
  706. "CASE i1 OF 1, 2, 5..9: b1 := TRUE END"),
  707. fail(["CASE i1 OF undefined: b1 := TRUE END",
  708. "undeclared identifier: 'undefined'"],
  709. ["CASE i1 OF i2: b1 := TRUE END",
  710. "'i2' is not a constant"],
  711. ["CASE b1 OF END", "'INTEGER' or 'CHAR' expected as CASE expression"],
  712. ["CASE i1 OF \"A\": b1 := TRUE END",
  713. "label must be 'INTEGER' (the same as case expression), got 'CHAR'"],
  714. ["CASE c1 OF \"A\", 1: b1 := TRUE END",
  715. "label must be 'CHAR' (the same as case expression), got 'INTEGER'"],
  716. ["CASE c1 OF \"A\"..1: b1 := TRUE END",
  717. "label must be 'CHAR' (the same as case expression), got 'INTEGER'"])
  718. ),
  719. "WHILE statement": testWithContext(
  720. context(Grammar.statement,
  721. "VAR b1: BOOLEAN; i1: INTEGER;"),
  722. pass("WHILE TRUE DO i1 := 0 END",
  723. "WHILE b1 DO i1 := 0 ELSIF FALSE DO i1 := 1 END"),
  724. fail(["WHILE i1 DO i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'"],
  725. ["WHILE b1 DO i1 := 0 ELSIF i1 DO i1 := 1 END", "'BOOLEAN' expression expected, got 'INTEGER'"])
  726. ),
  727. "REPEAT statement": testWithContext(
  728. context(Grammar.statement,
  729. "VAR b1: BOOLEAN; i1: INTEGER;"),
  730. pass("REPEAT i1 := 0 UNTIL TRUE",
  731. "REPEAT i1 := 0 UNTIL b1"),
  732. fail(["REPEAT i1 := 0 UNTIL i1", "'BOOLEAN' expression expected, got 'INTEGER'"])
  733. ),
  734. "FOR statement": testWithContext(
  735. context(Grammar.statement,
  736. "CONST c = 15; VAR b: BOOLEAN; i, n: INTEGER;"),
  737. pass("FOR i := 0 TO 10 DO n := 1 END",
  738. "FOR i := 0 TO 10 BY 5 DO b := TRUE END",
  739. "FOR i := 0 TO n DO b := TRUE END",
  740. "FOR i := 0 TO n BY c DO n := 1; b := FALSE END"),
  741. fail(["FOR undefined := 0 TO 10 DO n := 1 END",
  742. "undeclared identifier: 'undefined'"],
  743. ["FOR b := TRUE TO 10 DO n := 1 END",
  744. "'b' is a 'BOOLEAN' variable, 'FOR' control variable must be 'INTEGER'"],
  745. ["FOR c := 0 TO 10 DO END", "'c' is not a variable"],
  746. ["FOR i := TRUE TO 10 DO n := 1 END",
  747. "'INTEGER' expression expected to assign 'i', got 'BOOLEAN'"],
  748. ["FOR i := 0 TO TRUE DO END",
  749. "'INTEGER' expression expected as 'TO' parameter, got 'BOOLEAN'"],
  750. ["FOR i := 0 TO 10 BY n DO END",
  751. "constant expression expected as 'BY' parameter"],
  752. ["FOR i := 0 TO 10 BY TRUE DO END",
  753. "'INTEGER' expression expected as 'BY' parameter, got 'BOOLEAN'"],
  754. ["FOR i := 0 TO 10 DO - END",
  755. "END expected (FOR)"])
  756. ),
  757. "logical operators": testWithContext(
  758. context(Grammar.statement, "VAR b1, b2: BOOLEAN; i1: INTEGER;"),
  759. pass("b1 := b1 OR b2",
  760. "b1 := b1 & b2",
  761. "b1 := ~b2"),
  762. fail(["b1 := i1 OR b2", "BOOLEAN expected as operand of 'OR', got 'INTEGER'"],
  763. ["b1 := b1 OR i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'"],
  764. ["b1 := i1 & b2", "BOOLEAN expected as operand of '&', got 'INTEGER'"],
  765. ["b1 := b1 & i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'"],
  766. ["b1 := ~i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'"])
  767. ),
  768. "arithmetic operators": testWithContext(
  769. context(Grammar.statement,
  770. "VAR b1: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL; c1: CHAR; s1: SET;"
  771. + "p1: PROCEDURE; ptr1: POINTER TO RECORD END;"),
  772. pass("i1 := i1 + i2",
  773. "i1 := i1 - i2",
  774. "i1 := i1 * i2",
  775. "i1 := i1 DIV i2",
  776. "i1 := i1 MOD i2",
  777. "r1 := r1 + r2",
  778. "r1 := r1 - r2",
  779. "r1 := r1 * r2",
  780. "r1 := r1 / r2"),
  781. fail(["i1 := i1 / i2", "operator DIV expected for integer division"],
  782. ["r1 := r1 DIV r1", "operator 'DIV' type mismatch: INTEGER expected, got 'REAL'"],
  783. ["b1 := b1 + b1", "operator '+' type mismatch: numeric type expected, got 'BOOLEAN'"],
  784. ["c1 := c1 - c1", "operator '-' type mismatch: numeric type expected, got 'CHAR'"],
  785. ["p1 := p1 * p1", "operator '*' type mismatch: numeric type expected, got 'PROCEDURE'"],
  786. ["ptr1 := ptr1 / ptr1", "operator '/' type mismatch: numeric type expected, got 'POINTER TO anonymous RECORD'"],
  787. ["s1 := +s1", "operator '+' type mismatch: numeric type expected, got 'SET'"],
  788. ["b1 := -b1", "operator '-' type mismatch: numeric type expected, got 'BOOLEAN'"],
  789. ["s1 := +b1", "operator '+' type mismatch: numeric type expected, got 'BOOLEAN'"])
  790. ),
  791. "relations are BOOLEAN": testWithContext(
  792. context(Grammar.statement,
  793. "TYPE Base = RECORD END; Derived = RECORD (Base) END;"
  794. + "VAR pBase: POINTER TO Base; proc1, proc2: PROCEDURE;"
  795. + "set1, set2: SET;"
  796. + "b: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL; c1, c2: CHAR; ca1, ca2: ARRAY 10 OF CHAR;"),
  797. pass("b := pBase IS Derived",
  798. "b := pBase = pBase",
  799. "b := proc1 # proc2",
  800. "b := set1 <= set2",
  801. "b := i1 IN set2",
  802. "b := i1 < i2",
  803. "IF i1 > i2 THEN END",
  804. "b := c1 > c2",
  805. "b := ca1 <= ca2",
  806. "b := r1 >= r2")
  807. ),
  808. "SET relations": testWithContext(
  809. context(Grammar.expression,
  810. "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;"),
  811. pass("set1 <= set2",
  812. "set1 >= set2",
  813. "set1 = set2",
  814. "set1 # set2",
  815. "i IN set1"),
  816. fail(["set1 <= i", "type mismatch: expected 'SET', got 'INTEGER'"],
  817. ["b IN set1", "'INTEGER' expected as an element of SET, got 'BOOLEAN'"],
  818. ["i IN b", "type mismatch: expected 'SET', got 'BOOLEAN'"])
  819. ),
  820. "SET operators": testWithContext(
  821. context(Grammar.expression,
  822. "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;"),
  823. pass("set1 + set2",
  824. "set1 - set2",
  825. "set1 * set2",
  826. "set1 / set2",
  827. "-set1"),
  828. fail(["set1 + i", "type mismatch: expected 'SET', got 'INTEGER'"],
  829. ["set1 - b", "type mismatch: expected 'SET', got 'BOOLEAN'"],
  830. ["set1 * b", "type mismatch: expected 'SET', got 'BOOLEAN'"],
  831. ["set1 / b", "type mismatch: expected 'SET', got 'BOOLEAN'"])
  832. ),
  833. "SET functions": testWithContext(
  834. context(Grammar.statement,
  835. "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;"),
  836. pass("INCL(set1, 0)",
  837. "EXCL(set1, 3)"),
  838. fail(["INCL({}, i)", "expression cannot be used as VAR parameter"],
  839. ["INCL(set1, i)", "constant (0..31) expected as second argument of INCL"],
  840. ["EXCL(set1, i)", "constant (0..31) expected as second argument of EXCL"],
  841. ["INCL(set1, 32)", "constant (0..31) expected as second argument of INCL"],
  842. ["EXCL(set1, -1)", "constant (0..31) expected as second argument of EXCL"]
  843. )
  844. ),
  845. "procedure body": testWithGrammar(
  846. Grammar.procedureBody,
  847. pass("END",
  848. "VAR END",
  849. "VAR i: INTEGER; END",
  850. "VAR a: ARRAY 10 OF INTEGER; END",
  851. "VAR i: INTEGER; BEGIN i := 1 END",
  852. "VAR b: BOOLEAN; BEGIN b := TRUE END",
  853. "VAR i, j: INTEGER; BEGIN i := 1; j := 2; i := 1 + i + j - 2 END",
  854. "TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.field := 1 END",
  855. "TYPE T1 = RECORD field: INTEGER END; T2 = RECORD field: T1 END; VAR v1: T1; v2: T2; BEGIN v1.field := v2.field.field END",
  856. "TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field2: INTEGER END; VAR v: T2; BEGIN v.field2 := v.field1 END"),
  857. fail(["VAR i: INTEGER;", "END expected (PROCEDURE)"],
  858. ["VAR i: INTEGER; i := 1; END", "END expected (PROCEDURE)"],
  859. ["VAR i: INTEGER; BEGIN j := 1 END", "undeclared identifier: 'j'"],
  860. ["VAR i: INTEGER; BEGIN i.field := 1 END",
  861. "cannot designate 'INTEGER'"],
  862. ["VAR i: INTEGER; BEGIN i := j END", "undeclared identifier: 'j'"],
  863. ["TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v := 1 END",
  864. "type mismatch: 'v' is 'T' and cannot be assigned to 'INTEGER' expression"],
  865. ["TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.unknown := 1 END",
  866. "Type 'T' has no 'unknown' field"],
  867. ["TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field1: INTEGER END; END",
  868. "base record already has field: 'field1'"])
  869. ),
  870. "procedure heading": testWithSetup(
  871. function(){
  872. function innerMakeContext(cx){return new Context.ProcDecl(makeContext());}
  873. return setupParser(Grammar.procedureHeading, innerMakeContext);
  874. },
  875. pass("PROCEDURE p",
  876. "PROCEDURE p(a1: INTEGER)",
  877. "PROCEDURE p(a1, a2: INTEGER; b1: BOOLEAN)"),
  878. fail(["PROCEDURE p(a1: INTEGER; a1: BOOLEAN)", "'a1' already declared"],
  879. ["PROCEDURE p(p: INTEGER)", "argument 'p' has the same name as procedure"])
  880. ),
  881. "procedure": testWithContext(
  882. context(Grammar.procedureDeclaration,
  883. "TYPE ProcType = PROCEDURE(): ProcType;"),
  884. pass("PROCEDURE p; END p",
  885. "PROCEDURE p; VAR i: INTEGER; BEGIN i := i + 1 END p",
  886. "PROCEDURE p(a: INTEGER); BEGIN a := a + 1 END p",
  887. "PROCEDURE p; BEGIN p() END p",
  888. "PROCEDURE p(a: INTEGER); BEGIN p(a) END p",
  889. "PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(a, b) END p",
  890. "PROCEDURE p(): ProcType; RETURN p END p"),
  891. fail(["PROCEDURE p; END", "not parsed"],
  892. ["PROCEDURE p1; END p2",
  893. "mismatched procedure names: 'p1' at the begining and 'p2' at the end"],
  894. ["PROCEDURE p(a: INTEGER); VAR a: INTEGER END p", "'a' already declared"],
  895. ["PROCEDURE p(a: INTEGER); BEGIN p() END p", "1 argument(s) expected, got 0"],
  896. ["PROCEDURE p(a: INTEGER); BEGIN p(1, 2) END p", "1 argument(s) expected, got 2"],
  897. ["PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(b, a) END p",
  898. "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  899. ["PROCEDURE p; BEGIN p1() END p", "undeclared identifier: 'p1'"])
  900. ),
  901. "procedure RETURN": testWithContext(
  902. context(
  903. Grammar.procedureDeclaration,
  904. "TYPE A = ARRAY 3 OF INTEGER; R = RECORD END; PR = POINTER TO R;"
  905. + "VAR i: INTEGER; PROCEDURE int(): INTEGER; RETURN 1 END int;"),
  906. pass("PROCEDURE p(): BOOLEAN; RETURN TRUE END p",
  907. "PROCEDURE p(): BOOLEAN; RETURN int() = 1 END p",
  908. "PROCEDURE p; BEGIN END p" ,
  909. "PROCEDURE p(): INTEGER; BEGIN RETURN 0 END p"),
  910. fail(["PROCEDURE p; RETURN TRUE END p", "unexpected RETURN in PROCEDURE declared with no result type"],
  911. ["PROCEDURE p(): BOOLEAN; END p", "RETURN expected at the end of PROCEDURE declared with 'BOOLEAN' result type"],
  912. ["PROCEDURE p(): undeclared; END p", "undeclared identifier: 'undeclared'"],
  913. ["PROCEDURE p(): i; END p", "type name expected"],
  914. ["PROCEDURE p(): INTEGER; RETURN TRUE END p", "RETURN 'INTEGER' expected, got 'BOOLEAN'"],
  915. ["PROCEDURE p(a: A): A; RETURN a END p", "the result type of a procedure cannot be an ARRAY"],
  916. ["PROCEDURE p(): A; VAR a: A; RETURN a END p", "the result type of a procedure cannot be an ARRAY"],
  917. ["PROCEDURE p(r: R): R; RETURN r END p", "the result type of a procedure cannot be a RECORD"],
  918. ["PROCEDURE p(): R; VAR r: R; RETURN r END p", "the result type of a procedure cannot be a RECORD"],
  919. ["PROCEDURE p(pr: PR): R; RETURN pr END p", "the result type of a procedure cannot be a RECORD"]
  920. )
  921. ),
  922. "PROCEDURE relations": testWithContext(
  923. context(Grammar.expression,
  924. "VAR p1: PROCEDURE; p2: PROCEDURE;"),
  925. pass("p1 = p2",
  926. "p1 # p2",
  927. "p1 = NIL",
  928. "NIL # p1"
  929. )
  930. ),
  931. "pass VAR argument as VAR parameter": testWithContext(
  932. context(Grammar.procedureDeclaration,
  933. "PROCEDURE p1(VAR i: INTEGER); END p1;"
  934. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"),
  935. pass("PROCEDURE p(VAR i1: INTEGER); BEGIN p1(i1) END p"),
  936. fail(["PROCEDURE p(VAR b: BOOLEAN); BEGIN p2(~b) END p", "expression cannot be used as VAR parameter"])
  937. ),
  938. "VAR parameter": testWithContext(
  939. context(Grammar.statement,
  940. "CONST c = 123;"
  941. + "VAR i1: INTEGER; b1: BOOLEAN; a1: ARRAY 5 OF INTEGER;"
  942. + "r1: RECORD f1: INTEGER END;"
  943. + "PROCEDURE p1(VAR i: INTEGER); END p1;"
  944. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"
  945. ),
  946. pass("p1(i1)",
  947. "p1(a1[0])",
  948. "p1(r1.f1)"),
  949. fail(["p1(c)", "constant cannot be used as VAR parameter"],
  950. ["p1(123)", "expression cannot be used as VAR parameter"],
  951. ["p2(TRUE)", "expression cannot be used as VAR parameter"],
  952. ["p1(i1 + i1)", "expression cannot be used as VAR parameter"],
  953. ["p1(i1 * i1)", "expression cannot be used as VAR parameter"],
  954. ["p1(+i1)", "expression cannot be used as VAR parameter"],
  955. ["p1(-i1)", "expression cannot be used as VAR parameter"],
  956. ["p2(~b1)", "expression cannot be used as VAR parameter"])
  957. ),
  958. "ARRAY parameter": testWithContext(
  959. context(Grammar.procedureDeclaration,
  960. "TYPE T = RECORD i: INTEGER; p: POINTER TO T END;"
  961. + "PROCEDURE p1(i: INTEGER); END p1;"
  962. + "PROCEDURE varInteger(VAR i: INTEGER); END varInteger;"
  963. + "PROCEDURE p2(a: ARRAY OF INTEGER); END p2;"
  964. + "PROCEDURE p3(VAR a: ARRAY OF INTEGER); END p3;"
  965. ),
  966. pass("PROCEDURE p(a: ARRAY OF INTEGER); END p",
  967. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); END p",
  968. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN p1(a[0][0]) END p",
  969. "PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p2(a) END p",
  970. "PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].p.i) END p"),
  971. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN a[0] := 0 END p",
  972. "cannot assign to read-only variable"],
  973. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p3(a) END p",
  974. "read-only variable cannot be used as VAR parameter"],
  975. ["PROCEDURE p(a: ARRAY OF T); BEGIN a[0].i := 0 END p",
  976. "cannot assign to read-only variable"],
  977. ["PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].i) END p",
  978. "read-only variable cannot be used as VAR parameter"])
  979. ),
  980. "procedure call": testWithContext(
  981. context(Grammar.statement,
  982. "TYPE ProcType = PROCEDURE;" +
  983. "VAR notProcedure: INTEGER;" +
  984. "PROCEDURE p; END p;" +
  985. "PROCEDURE p1(i: INTEGER); END p1;" +
  986. "PROCEDURE p2(i: INTEGER; b: BOOLEAN); END p2;" +
  987. "PROCEDURE p3(): ProcType; RETURN p END p3;"),
  988. pass("p",
  989. "p()",
  990. "p1(1)",
  991. "p1(1 + 2)",
  992. "p2(1, TRUE)"),
  993. fail(["notProcedure", "PROCEDURE expected, got 'INTEGER'"],
  994. ["p2(TRUE, 1)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  995. ["p2(1, 1)", "type mismatch for argument 2: 'INTEGER' cannot be converted to 'BOOLEAN'"],
  996. ["p()()", "not parsed"],
  997. ["p3", "procedure returning a result cannot be used as a statement"],
  998. ["p3()", "procedure returning a result cannot be used as a statement"]
  999. )
  1000. ),
  1001. "local procedure": testWithContext(
  1002. context(Grammar.procedureDeclaration,
  1003. "TYPE ProcType = PROCEDURE;" +
  1004. "VAR procVar: ProcType;" +
  1005. "PROCEDURE procWithProcArg(p: ProcType); END procWithProcArg;"),
  1006. pass("PROCEDURE p; PROCEDURE innerP; END innerP; END p",
  1007. "PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN innerP() END p"),
  1008. fail(["PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN procVar := innerP END p",
  1009. "local procedure 'innerP' cannot be referenced"],
  1010. ["PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN procWithProcArg(innerP) END p",
  1011. "local procedure 'innerP' cannot be referenced"],
  1012. ["PROCEDURE p; PROCEDURE innerP; VAR innerV: INTEGER; END innerP; BEGIN innerV := 0 END p",
  1013. "undeclared identifier: 'innerV'"])
  1014. ),
  1015. "procedure assignment": testWithContext(
  1016. context(Grammar.statement,
  1017. "TYPE ProcType1 = PROCEDURE(): ProcType1;"
  1018. + "ProcType2 = PROCEDURE(): ProcType2;"
  1019. + "ProcType3 = PROCEDURE(p: ProcType3): ProcType3;"
  1020. + "ProcType4 = PROCEDURE(p: ProcType4): ProcType4;"
  1021. + "ProcType4VAR = PROCEDURE(VAR p: ProcType4VAR): ProcType4VAR;"
  1022. + "ProcType5 = PROCEDURE(p: ProcType3): ProcType4;"
  1023. + "ProcType6 = PROCEDURE(p: INTEGER);"
  1024. + "ProcType7 = PROCEDURE(VAR p: INTEGER);"
  1025. + "VAR v1: ProcType1; v2: ProcType2;"
  1026. + "v3: PROCEDURE(i: INTEGER): ProcType1; v4: PROCEDURE(b: BOOLEAN): ProcType1;"
  1027. + "v5: PROCEDURE(p: ProcType1); v6: PROCEDURE(p: ProcType2);"
  1028. + "v7: ProcType3; v8: ProcType4; v8VAR: ProcType4VAR; v9: ProcType5; v10: ProcType6; v11: ProcType7;"
  1029. + "PROCEDURE p1(): ProcType1; RETURN p1 END p1;"
  1030. ),
  1031. pass("v1 := v2",
  1032. "v5 := v6",
  1033. "v7 := v8",
  1034. "v7 := v9",
  1035. "v8 := v9",
  1036. "v1 := p1"),
  1037. fail(["p1 := v1", "cannot assign to procedure"],
  1038. ["v3 := v1",
  1039. "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'ProcType1' expression"],
  1040. ["v3 := v4",
  1041. "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'PROCEDURE(BOOLEAN): ProcType1' expression"],
  1042. ["v10 := NEW",
  1043. "standard procedure NEW cannot be referenced"],
  1044. ["v10 := v11", "type mismatch: 'v10' is 'ProcType6' and cannot be assigned to 'ProcType7' expression" ],
  1045. ["v8 := v8VAR", "type mismatch: 'v8' is 'ProcType4' and cannot be assigned to 'ProcType4VAR' expression" ])
  1046. ),
  1047. "string assignment": testWithContext(
  1048. context(Grammar.statement,
  1049. "VAR a1: ARRAY 3 OF CHAR;"
  1050. + "ch1: CHAR;"
  1051. + "intArray: ARRAY 10 OF INTEGER;"
  1052. ),
  1053. pass("a1 := \"abc\"",
  1054. "a1 := \"ab\"",
  1055. "a1 := \"a\"",
  1056. "a1 := 22X",
  1057. "ch1 := \"A\"",
  1058. "ch1 := 22X"),
  1059. fail(["a1 := \"abcd\"", "3-character ARRAY is too small for 4-character string"],
  1060. ["intArray := \"abcd\"",
  1061. "type mismatch: 'intArray' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'multi-character string' expression"])
  1062. ),
  1063. "string relations": testWithContext(
  1064. context(Grammar.expression,
  1065. "VAR ch: CHAR;"),
  1066. pass("ch = \"a\"",
  1067. "\"a\" = ch",
  1068. "ch # \"a\"",
  1069. "\"a\" # ch"
  1070. ),
  1071. fail(["ch = \"ab\"", "type mismatch: expected 'CHAR', got 'multi-character string'"])
  1072. ),
  1073. "array assignment": testWithContext(
  1074. context(Grammar.statement,
  1075. "VAR charArray: ARRAY 3 OF CHAR;"
  1076. + "intArray: ARRAY 10 OF INTEGER;"
  1077. + "intArray2: ARRAY 10 OF INTEGER;"
  1078. + "intArray3: ARRAY 5 OF INTEGER;"
  1079. + "intArray23m1: ARRAY 2 OF ARRAY 3 OF INTEGER;"
  1080. + "intArray23m2: ARRAY 2, 3 OF INTEGER;"
  1081. + "intArray24m: ARRAY 2, 4 OF INTEGER;"
  1082. + "intArray43m: ARRAY 4, 3 OF INTEGER;"
  1083. ),
  1084. pass("intArray := intArray2",
  1085. "intArray23m1 := intArray23m2",
  1086. "intArray23m2 := intArray23m1",
  1087. "intArray43m[0] := intArray23m1[0]"
  1088. ),
  1089. fail(["intArray := charArray",
  1090. "type mismatch: 'intArray' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'ARRAY 3 OF CHAR' expression"],
  1091. ["intArray2 := intArray3",
  1092. "type mismatch: 'intArray2' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'ARRAY 5 OF INTEGER' expression"],
  1093. ["intArray3 := charArray",
  1094. "type mismatch: 'intArray3' is 'ARRAY 5 OF INTEGER' and cannot be assigned to 'ARRAY 3 OF CHAR' expression"],
  1095. ["intArray24m := intArray23m1",
  1096. "type mismatch: 'intArray24m' is 'ARRAY 2, 4 OF INTEGER' and cannot be assigned to 'ARRAY 2, 3 OF INTEGER' expression"]
  1097. )
  1098. ),
  1099. "record assignment": testWithContext(
  1100. context(Grammar.statement,
  1101. "TYPE Base1 = RECORD END;"
  1102. + "T1 = RECORD (Base1) END;"
  1103. + "T2 = RECORD END;"
  1104. + "VAR b1: Base1; r1: T1; r2: T2;"
  1105. ),
  1106. pass("r1 := r1",
  1107. "b1 := r1"),
  1108. fail(["r1 := r2", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'T2' expression"],
  1109. ["r1 := b1", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'Base1' expression"])
  1110. ),
  1111. "open array assignment fails": testWithGrammar(
  1112. Grammar.procedureDeclaration,
  1113. pass(),
  1114. fail(["PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  1115. "cannot assign to read-only variable"],
  1116. ["PROCEDURE p(VAR s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  1117. "'s1' is open 'ARRAY OF CHAR' and cannot be assigned"],
  1118. ["PROCEDURE p(s1: ARRAY OF CHAR); VAR s2: ARRAY 10 OF CHAR; BEGIN s2 := s1 END p",
  1119. "type mismatch: 's2' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'ARRAY OF CHAR' expression"])
  1120. ),
  1121. "open array type as procedure parameter": testWithContext(
  1122. context(Grammar.procedureDeclaration,
  1123. "TYPE A = ARRAY 3 OF INTEGER;"
  1124. ),
  1125. pass("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN END p",
  1126. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN END p",
  1127. "PROCEDURE p(a: ARRAY OF A); BEGIN END p"
  1128. ),
  1129. fail(["PROCEDURE p(a: ARRAY OF ARRAY 3 OF INTEGER); BEGIN END p",
  1130. "not parsed"]
  1131. )
  1132. ),
  1133. "non-open array type as procedure parameter": testWithContext(
  1134. context(Grammar.procedureDeclaration,
  1135. "TYPE A = ARRAY 2 OF INTEGER;"
  1136. + "VAR a: A;"
  1137. + "PROCEDURE pa(a: A); BEGIN END pa;"
  1138. ),
  1139. pass("PROCEDURE p(a: A); BEGIN END p",
  1140. "PROCEDURE p(); VAR a: A; BEGIN pa(a) END p",
  1141. "PROCEDURE p(); VAR a: ARRAY 2 OF INTEGER; BEGIN pa(a) END p"
  1142. ),
  1143. fail(["PROCEDURE p(a: ARRAY 3 OF INTEGER); BEGIN END p",
  1144. "not parsed"],
  1145. ["PROCEDURE p(a: A): INTEGER; BEGIN RETURN a[2] END p",
  1146. "index out of bounds: maximum possible index is 1, got 2"],
  1147. ["PROCEDURE p(); VAR a: ARRAY 1 OF INTEGER; BEGIN pa(a) END p",
  1148. "type mismatch for argument 1: 'ARRAY 1 OF INTEGER' cannot be converted to 'ARRAY 2 OF INTEGER'"],
  1149. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pa(a) END p",
  1150. "type mismatch for argument 1: 'ARRAY OF INTEGER' cannot be converted to 'ARRAY 2 OF INTEGER'"]
  1151. )
  1152. ),
  1153. "string assignment to open array fails": testWithGrammar(
  1154. Grammar.procedureDeclaration,
  1155. pass(),
  1156. fail(["PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "cannot assign to read-only variable"],
  1157. ["PROCEDURE p(VAR s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "string cannot be assigned to open ARRAY OF CHAR"])
  1158. ),
  1159. "string argument": testWithContext(
  1160. context(Grammar.statement,
  1161. "PROCEDURE p1(s: ARRAY OF CHAR); END p1;"
  1162. + "PROCEDURE p2(VAR s: ARRAY OF CHAR); END p2;"
  1163. + "PROCEDURE p3(i: INTEGER); END p3;"
  1164. + "PROCEDURE p4(a: ARRAY OF INTEGER); END p4;"
  1165. ),
  1166. pass("p1(\"abc\")"),
  1167. fail(["p2(\"abc\")", "expression cannot be used as VAR parameter"],
  1168. ["p3(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'INTEGER'"],
  1169. ["p4(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'ARRAY OF INTEGER'"])
  1170. ),
  1171. "scope": testWithGrammar(
  1172. Grammar.declarationSequence,
  1173. pass("PROCEDURE p1(a1: INTEGER); END p1; PROCEDURE p2(a1: BOOLEAN); END p2;")
  1174. ),
  1175. "module": testWithGrammar(
  1176. Grammar.module,
  1177. pass("MODULE m; END m."),
  1178. fail(["MODULE m; END undeclared.",
  1179. "original module name 'm' expected, got 'undeclared'"],
  1180. ["MODULE m; BEGIN - END m.", "END expected (MODULE)"])
  1181. ),
  1182. "assert": testWithGrammar(
  1183. Grammar.statement,
  1184. pass("ASSERT(TRUE)",
  1185. "ASSERT(TRUE, 123)"),
  1186. fail(["ASSERT()", "at least 1 argument expected, got 0"],
  1187. ["ASSERT(123, TRUE)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'BOOLEAN'"])
  1188. ),
  1189. "export": testWithGrammar(
  1190. Grammar.declarationSequence,
  1191. pass("CONST i* = 1;",
  1192. "TYPE T* = RECORD END;",
  1193. "VAR i*: INTEGER;",
  1194. "PROCEDURE p*; END p;"
  1195. ),
  1196. fail(["VAR r*: RECORD END;",
  1197. "only scalar type variables can be exported"],
  1198. ["VAR a*: ARRAY 5 OF INTEGER;",
  1199. "only scalar type variables can be exported"],
  1200. ["TYPE T = RECORD f*: INTEGER END;",
  1201. "field 'f' can be exported only if record 'T' itself is exported too"],
  1202. ["TYPE PT* = POINTER TO RECORD f*: INTEGER END;",
  1203. "cannot export anonymous RECORD field: 'f'"],
  1204. ["VAR p: POINTER TO RECORD f*: INTEGER END;",
  1205. "cannot export anonymous RECORD field: 'f'"],
  1206. ["VAR p*: POINTER TO RECORD r: RECORD f*: INTEGER END END;",
  1207. "field 'f' can be exported only if field 'r' itself is exported too"],
  1208. ["VAR i*: POINTER TO RECORD f*: INTEGER END;",
  1209. "cannot export anonymous RECORD field: 'f'"],
  1210. ["VAR i*: POINTER TO RECORD r*: RECORD f*: INTEGER END END;",
  1211. "cannot export anonymous RECORD field: 'r'"],
  1212. ["PROCEDURE p*; VAR i*: INTEGER; END p;",
  1213. "cannot export from within procedure: variable 'i'"]
  1214. )
  1215. ),
  1216. "import JS": testWithGrammar(
  1217. Grammar.module,
  1218. pass("MODULE m; IMPORT JS; END m.",
  1219. "MODULE m; IMPORT JS; BEGIN JS.alert(\"test\") END m.",
  1220. "MODULE m; IMPORT JS; BEGIN JS.console.info(123) END m.",
  1221. "MODULE m; IMPORT JS; BEGIN JS.do(\"throw new Error()\") END m."
  1222. ),
  1223. fail(["MODULE m; IMPORT JS; BEGIN JS.do(123) END m.",
  1224. "string is expected as an argument of JS predefined procedure 'do', got INTEGER"],
  1225. ["MODULE m; IMPORT JS; BEGIN JS.do(\"a\", \"b\") END m.",
  1226. "1 argument(s) expected, got 2"],
  1227. ["MODULE m; IMPORT JS; VAR s: ARRAY 10 OF CHAR; BEGIN JS.do(s) END m.",
  1228. "string is expected as an argument of JS predefined procedure 'do', got ARRAY 10 OF CHAR"]
  1229. )
  1230. ),
  1231. "import unknown module": testWithGrammar(
  1232. Grammar.module,
  1233. pass(),
  1234. fail(["MODULE m; IMPORT unknown; END m.", "module(s) not found: unknown"],
  1235. ["MODULE m; IMPORT unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2"]
  1236. )
  1237. ),
  1238. "self import is failed": testWithGrammar(
  1239. Grammar.module,
  1240. pass(),
  1241. fail(["MODULE test; IMPORT test; END test.", "module 'test' cannot import itself"])
  1242. ),
  1243. "import aliases": testWithGrammar(
  1244. Grammar.module,
  1245. pass("MODULE m; IMPORT J := JS; END m.",
  1246. "MODULE m; IMPORT J := JS; BEGIN J.alert(\"test\") END m."),
  1247. fail(["MODULE m; IMPORT u1 := unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2"],
  1248. ["MODULE m; IMPORT a1 := m1, a2 := m1; END m.", "module already imported: 'm1'"],
  1249. ["MODULE m; IMPORT a1 := u1, a1 := u2; END m.", "duplicated alias: 'a1'"],
  1250. ["MODULE m; IMPORT J := JS; BEGIN JS.alert(\"test\") END m.", "undeclared identifier: 'JS'"]
  1251. )
  1252. ),
  1253. "imported module without exports": testWithModule(
  1254. "MODULE test; END test.",
  1255. pass("MODULE m; IMPORT test; END m."),
  1256. fail(["MODULE m; IMPORT test; BEGIN test.p(); END m.",
  1257. "identifier 'p' is not exported by module 'test'"],
  1258. ["MODULE m; IMPORT t := test; BEGIN t.p(); END m.",
  1259. "identifier 'p' is not exported by module 'test'"]
  1260. )),
  1261. "imported variables are read-only": testWithModule(
  1262. "MODULE test; VAR i*: INTEGER; END test.",
  1263. pass("MODULE m; IMPORT test; PROCEDURE p(i: INTEGER); END p; BEGIN p(test.i); END m."),
  1264. fail(["MODULE m; IMPORT test; BEGIN test.i := 123; END m.",
  1265. "cannot assign to imported variable"],
  1266. ["MODULE m; IMPORT test; PROCEDURE p(VAR i: INTEGER); END p; BEGIN p(test.i); END m.",
  1267. "imported variable cannot be used as VAR parameter"]
  1268. )
  1269. ),
  1270. "import pointer type": testWithModule(
  1271. "MODULE test; TYPE TP* = POINTER TO RECORD END; END test.",
  1272. pass("MODULE m; IMPORT test; VAR p: test.TP; END m.")
  1273. ),
  1274. "import array type": testWithModule(
  1275. "MODULE test; TYPE TA* = ARRAY 3 OF INTEGER; END test.",
  1276. pass("MODULE m; IMPORT test; VAR a: test.TA; END m.")
  1277. ),
  1278. "import procedure type": testWithModule(
  1279. "MODULE test; TYPE TProc* = PROCEDURE; END test.",
  1280. pass("MODULE m; IMPORT test; VAR proc: test.TProc; END m.")
  1281. ),
  1282. "imported pointer type cannot be used in NEW if base type is not exported": testWithModule(
  1283. "MODULE test;"
  1284. + "TYPE T = RECORD END; TP* = POINTER TO T;"
  1285. + "TPAnonymous* = POINTER TO RECORD END; END test.",
  1286. pass(),
  1287. fail(["MODULE m; IMPORT test; VAR p: test.TPAnonymous; BEGIN NEW(p) END m.",
  1288. "non-exported RECORD type cannot be used in NEW"],
  1289. ["MODULE m; IMPORT test; VAR p: test.TP; BEGIN NEW(p) END m.",
  1290. "non-exported RECORD type cannot be used in NEW"])
  1291. ),
  1292. "imported pointer variable: anonymous record field cannot be used": testWithModule(
  1293. "MODULE test; VAR p*: POINTER TO RECORD i: INTEGER END; END test.",
  1294. pass(),
  1295. fail(["MODULE m; IMPORT test; BEGIN ASSERT(test.p.i = 0) END m.",
  1296. "non-exported RECORD type cannot be dereferenced"])
  1297. )
  1298. };
  1299. Test.run(testSuite);