test_unit.js 52 KB

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