test_unit.js 64 KB

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