test_unit.js 59 KB

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