2
0

test_unit.js 53 KB

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