test_unit.js 67 KB

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