test_unit.js 64 KB

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