test_unit.js 75 KB

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