test_unit.js 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. + "VAR i1: INTEGER; b1: BOOLEAN; a1: ARRAY 5 OF INTEGER;"
  778. + "r1: RECORD f1: INTEGER END;"
  779. + "PROCEDURE p1(VAR i: INTEGER); END p1;"
  780. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"
  781. ),
  782. pass("p1(i1)",
  783. "p1(a1[0])",
  784. "p1(r1.f1)"),
  785. fail(["p1(c)", "constant cannot be used as VAR parameter"],
  786. ["p1(123)", "expression cannot be used as VAR parameter"],
  787. ["p2(TRUE)", "expression cannot be used as VAR parameter"],
  788. ["p1(i1 + i1)", "expression cannot be used as VAR parameter"],
  789. ["p1(i1 * i1)", "expression cannot be used as VAR parameter"],
  790. ["p1(+i1)", "expression cannot be used as VAR parameter"],
  791. ["p1(-i1)", "expression cannot be used as VAR parameter"],
  792. ["p2(~b1)", "expression cannot be used as VAR parameter"])
  793. ),
  794. "procedure call": testWithContext(
  795. context(grammar.statement,
  796. "TYPE ProcType = PROCEDURE;" +
  797. "VAR notProcedure: INTEGER; ptr: POINTER TO RECORD END;" +
  798. "PROCEDURE p; END p;" +
  799. "PROCEDURE p1(i: INTEGER); END p1;" +
  800. "PROCEDURE p2(i: INTEGER; b: BOOLEAN); END p2;" +
  801. "PROCEDURE p3(): ProcType; RETURN p END p3;"),
  802. pass("p",
  803. "p()",
  804. "p1(1)",
  805. "p1(1 + 2)",
  806. "p2(1, TRUE)"),
  807. fail(["notProcedure", "PROCEDURE expected, got 'INTEGER'"],
  808. ["ptr()", "PROCEDURE expected, got 'POINTER TO anonymous RECORD'"],
  809. ["p2(TRUE, 1)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  810. ["p2(1, 1)", "type mismatch for argument 2: 'INTEGER' cannot be converted to 'BOOLEAN'"],
  811. ["p3", "procedure returning a result cannot be used as a statement"],
  812. ["p3()", "procedure returning a result cannot be used as a statement"]
  813. )
  814. ),
  815. "procedure assignment": testWithContext(
  816. context(grammar.statement,
  817. "TYPE ProcType1 = PROCEDURE(): ProcType1;"
  818. + "ProcType2 = PROCEDURE(): ProcType2;"
  819. + "ProcType3 = PROCEDURE(p: ProcType3): ProcType3;"
  820. + "ProcType4 = PROCEDURE(p: ProcType4): ProcType4;"
  821. + "ProcType4VAR = PROCEDURE(VAR p: ProcType4VAR): ProcType4VAR;"
  822. + "ProcType5 = PROCEDURE(p: ProcType3): ProcType4;"
  823. + "ProcType6 = PROCEDURE(p: INTEGER);"
  824. + "ProcType7 = PROCEDURE(VAR p: INTEGER);"
  825. + "VAR v1: ProcType1; v2: ProcType2;"
  826. + "v3: PROCEDURE(i: INTEGER): ProcType1; v4: PROCEDURE(b: BOOLEAN): ProcType1;"
  827. + "v5: PROCEDURE(p: ProcType1); v6: PROCEDURE(p: ProcType2);"
  828. + "v7: ProcType3; v8: ProcType4; v8VAR: ProcType4VAR; v9: ProcType5; v10: ProcType6; v11: ProcType7;"
  829. + "vProcCharArray: PROCEDURE (a: ARRAY OF CHAR);"
  830. + "vProcInt: PROCEDURE (i: INTEGER);"
  831. + "vProcReturnInt: PROCEDURE(): INTEGER;"
  832. + "PROCEDURE p1(): ProcType1; RETURN p1 END p1;"
  833. + "PROCEDURE procCharArray(a: ARRAY OF CHAR); END procCharArray;"
  834. + "PROCEDURE procIntArray(a: ARRAY OF INTEGER); END procIntArray;"
  835. + "PROCEDURE procByte(b: BYTE); END procByte;"
  836. + "PROCEDURE procReturnByte(): BYTE; RETURN 0 END procReturnByte;"
  837. ),
  838. pass("v1 := v2",
  839. "v5 := v6",
  840. "v7 := v8",
  841. "v7 := v9",
  842. "v8 := v9",
  843. "v1 := p1",
  844. "vProcCharArray := procCharArray"),
  845. fail(["p1 := v1", "cannot assign to procedure"],
  846. ["v3 := v1",
  847. "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'ProcType1' expression"],
  848. ["v3 := v4",
  849. "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'PROCEDURE(BOOLEAN): ProcType1' expression"],
  850. ["v10 := NEW",
  851. "standard procedure NEW cannot be referenced"],
  852. ["v10 := v11", "type mismatch: 'v10' is 'ProcType6' and cannot be assigned to 'ProcType7' expression" ],
  853. ["v8 := v8VAR", "type mismatch: 'v8' is 'ProcType4' and cannot be assigned to 'ProcType4VAR' expression" ],
  854. ["vProcCharArray := procIntArray",
  855. "type mismatch: 'vProcCharArray' is 'PROCEDURE(ARRAY OF CHAR)' and cannot be assigned to 'PROCEDURE(ARRAY OF INTEGER)' expression"],
  856. ["vProcInt := procByte",
  857. "type mismatch: 'vProcInt' is 'PROCEDURE(INTEGER)' and cannot be assigned to 'PROCEDURE(BYTE)' expression"],
  858. ["vProcReturnInt := procReturnByte",
  859. "type mismatch: 'vProcReturnInt' is 'PROCEDURE(): INTEGER' and cannot be assigned to 'PROCEDURE(): BYTE' expression"]
  860. )
  861. ),
  862. "string assignment": testWithContext(
  863. context(grammar.statement,
  864. "VAR a1: ARRAY 3 OF CHAR;"
  865. + "ch1: CHAR;"
  866. + "intArray: ARRAY 10 OF INTEGER;"
  867. ),
  868. pass("a1 := \"abc\"",
  869. "a1 := \"ab\"",
  870. "a1 := \"a\"",
  871. "a1 := 22X",
  872. "ch1 := \"A\"",
  873. "ch1 := 22X"),
  874. fail(["a1 := \"abcd\"", "3-character ARRAY is too small for 4-character string"],
  875. ["intArray := \"abcd\"",
  876. "type mismatch: 'intArray' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'multi-character string' expression"])
  877. ),
  878. "string relations": testWithContext(
  879. context(grammar.expression,
  880. "VAR ch: CHAR;"),
  881. pass("ch = \"a\"",
  882. "\"a\" = ch",
  883. "ch # \"a\"",
  884. "\"a\" # ch"
  885. ),
  886. fail(["ch = \"ab\"", "type mismatch: expected 'CHAR', got 'multi-character string'"])
  887. ),
  888. "array assignment": testWithContext(
  889. context(grammar.statement,
  890. "VAR charArray: ARRAY 3 OF CHAR;"
  891. + "intArray: ARRAY 10 OF INTEGER;"
  892. + "intArray2: ARRAY 10 OF INTEGER;"
  893. + "intArray3: ARRAY 5 OF INTEGER;"
  894. + "intArray23m1: ARRAY 2 OF ARRAY 3 OF INTEGER;"
  895. + "intArray23m2: ARRAY 2, 3 OF INTEGER;"
  896. + "intArray24m: ARRAY 2, 4 OF INTEGER;"
  897. + "intArray43m: ARRAY 4, 3 OF INTEGER;"
  898. ),
  899. pass("intArray := intArray2",
  900. "intArray23m1 := intArray23m2",
  901. "intArray23m2 := intArray23m1",
  902. "intArray43m[0] := intArray23m1[0]"
  903. ),
  904. fail(["intArray := charArray",
  905. "type mismatch: 'intArray' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'ARRAY 3 OF CHAR' expression"],
  906. ["intArray2 := intArray3",
  907. "type mismatch: 'intArray2' is 'ARRAY 10 OF INTEGER' and cannot be assigned to 'ARRAY 5 OF INTEGER' expression"],
  908. ["intArray3 := charArray",
  909. "type mismatch: 'intArray3' is 'ARRAY 5 OF INTEGER' and cannot be assigned to 'ARRAY 3 OF CHAR' expression"],
  910. ["intArray24m := intArray23m1",
  911. "type mismatch: 'intArray24m' is 'ARRAY 2, 4 OF INTEGER' and cannot be assigned to 'ARRAY 2, 3 OF INTEGER' expression"]
  912. )
  913. ),
  914. "record assignment": testWithContext(
  915. context(grammar.statement,
  916. "TYPE Base1 = RECORD END;"
  917. + "T1 = RECORD (Base1) END;"
  918. + "T2 = RECORD END;"
  919. + "VAR b1: Base1; r1: T1; r2: T2;"
  920. ),
  921. pass("r1 := r1",
  922. "b1 := r1"),
  923. fail(["r1 := r2", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'T2' expression"],
  924. ["r1 := b1", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'Base1' expression"])
  925. ),
  926. "string argument": testWithContext(
  927. context(grammar.statement,
  928. "PROCEDURE p1(s: ARRAY OF CHAR); END p1;"
  929. + "PROCEDURE p2(VAR s: ARRAY OF CHAR); END p2;"
  930. + "PROCEDURE p3(i: INTEGER); END p3;"
  931. + "PROCEDURE p4(a: ARRAY OF INTEGER); END p4;"
  932. ),
  933. pass("p1(\"abc\")"),
  934. fail(["p2(\"abc\")", "expression cannot be used as VAR parameter"],
  935. ["p3(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'INTEGER'"],
  936. ["p4(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'ARRAY OF INTEGER'"])
  937. ),
  938. "assert": testWithGrammar(
  939. grammar.statement,
  940. pass("ASSERT(TRUE)"),
  941. fail(["ASSERT()", "1 argument(s) expected, got 0"],
  942. ["ASSERT(TRUE, 123)", "1 argument(s) expected, got 2"],
  943. ["ASSERT(123)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'BOOLEAN'"])
  944. ),
  945. "imported module without exports": testWithModule(
  946. "MODULE test; END test.",
  947. pass("MODULE m; IMPORT test; END m."),
  948. fail(["MODULE m; IMPORT test; BEGIN test.p(); END m.",
  949. "identifier 'p' is not exported by module 'test'"],
  950. ["MODULE m; IMPORT t := test; BEGIN t.p(); END m.",
  951. "identifier 'p' is not exported by module 'test'"],
  952. ["MODULE m; IMPORT test; BEGIN test(); END m.",
  953. "PROCEDURE expected, got 'MODULE'"]
  954. )),
  955. "import record type": testWithModule(
  956. "MODULE test; TYPE T* = RECORD f*: INTEGER; notExported: BOOLEAN END; END test.",
  957. pass("MODULE m; IMPORT test; VAR r: test.T; BEGIN r.f := 0; END m."),
  958. fail(["MODULE m; IMPORT test; VAR r: test.T; BEGIN r.notExported := FALSE; END m.",
  959. "type 'T' has no 'notExported' field"]
  960. )),
  961. "imported variables are read-only": testWithModule(
  962. "MODULE test; VAR i*: INTEGER; END test.",
  963. pass("MODULE m; IMPORT test; PROCEDURE p(i: INTEGER); END p; BEGIN p(test.i); END m."),
  964. fail(["MODULE m; IMPORT test; BEGIN test.i := 123; END m.",
  965. "cannot assign to imported variable"],
  966. ["MODULE m; IMPORT test; PROCEDURE p(VAR i: INTEGER); END p; BEGIN p(test.i); END m.",
  967. "imported variable cannot be used as VAR parameter"]
  968. )
  969. ),
  970. "import pointer type": testWithModule(
  971. "MODULE test;"
  972. + "TYPE TPAnonymous1* = POINTER TO RECORD END; TPAnonymous2* = POINTER TO RECORD END;"
  973. + "Base* = RECORD END; TPDerived* = POINTER TO RECORD(Base) END;"
  974. + "END test.",
  975. pass("MODULE m; IMPORT test; VAR p1: test.TPAnonymous1; p2: test.TPAnonymous2; END m.",
  976. "MODULE m; IMPORT test;"
  977. + "VAR pb: POINTER TO test.Base; pd: test.TPDerived;"
  978. + "BEGIN pb := pd; END m."),
  979. fail(["MODULE m; IMPORT test; VAR p1: test.TPAnonymous1; p2: test.TPAnonymous2; BEGIN p1 := p2; END m.",
  980. "type mismatch: 'p1' is 'TPAnonymous1' and cannot be assigned to 'TPAnonymous2' expression"]
  981. )
  982. ),
  983. "import array type": testWithModule(
  984. "MODULE test; TYPE TA* = ARRAY 3 OF INTEGER; END test.",
  985. pass("MODULE m; IMPORT test; VAR a: test.TA; END m.")
  986. ),
  987. "import procedure type": testWithModule(
  988. "MODULE test; TYPE TProc* = PROCEDURE; END test.",
  989. pass("MODULE m; IMPORT test; VAR proc: test.TProc; END m.")
  990. ),
  991. "imported pointer type cannot be used in NEW if base type is not exported": testWithModule(
  992. "MODULE test;"
  993. + "TYPE T = RECORD END; TP* = POINTER TO T;"
  994. + "TPAnonymous* = POINTER TO RECORD END; END test.",
  995. pass(),
  996. fail(["MODULE m; IMPORT test; VAR p: test.TPAnonymous; BEGIN NEW(p) END m.",
  997. "non-exported RECORD type cannot be used in NEW"],
  998. ["MODULE m; IMPORT test; VAR p: test.TP; BEGIN NEW(p) END m.",
  999. "non-exported RECORD type cannot be used in NEW"])
  1000. ),
  1001. "imported pointer type cannot be dereferenced if base type is not exported (even if base of base type is exported)": testWithModule(
  1002. "MODULE test;"
  1003. + "TYPE B* = RECORD i: INTEGER END; T = RECORD(B) END; TP* = POINTER TO T;"
  1004. + "TPAnonymous* = POINTER TO RECORD(B) END;"
  1005. + "PROCEDURE makeTP*(): TP; VAR result: TP; BEGIN NEW(result); RETURN result END makeTP;"
  1006. + "PROCEDURE makeTPA*(): TPAnonymous; VAR result: TPAnonymous; BEGIN NEW(result); RETURN result END makeTPA;"
  1007. + "END test.",
  1008. pass(),
  1009. fail(["MODULE m; IMPORT test; VAR p: test.TPAnonymous; BEGIN p := test.makeTPA(); p.i := 123; END m.",
  1010. "POINTER TO non-exported RECORD type cannot be dereferenced"],
  1011. ["MODULE m; IMPORT test; VAR p: test.TP; BEGIN p := test.makeTP(); p.i := 123; END m.",
  1012. "POINTER TO non-exported RECORD type cannot be dereferenced"])
  1013. ),
  1014. "imported pointer variable: anonymous record field cannot be used": testWithModule(
  1015. "MODULE test; VAR p*: POINTER TO RECORD i: INTEGER END; END test.",
  1016. pass(),
  1017. fail(["MODULE m; IMPORT test; BEGIN ASSERT(test.p.i = 0) END m.",
  1018. "POINTER TO non-exported RECORD type cannot be dereferenced"])
  1019. ),
  1020. "procedure VAR section": testWithGrammar(
  1021. grammar.declarationSequence,
  1022. pass("VAR",
  1023. "VAR i: INTEGER;",
  1024. "VAR i, j: INTEGER;",
  1025. "VAR i, j: INTEGER; b: BOOLEAN;")
  1026. ),
  1027. "const declaration": testWithContext(
  1028. context(grammar.declarationSequence,
  1029. "CONST ci = 1; VAR v1: INTEGER;"),
  1030. pass("CONST i = 10;",
  1031. "CONST i = 1 + 2;",
  1032. "CONST i = ci + 2;",
  1033. "CONST i = ci * 2;",
  1034. "CONST i = ORD({0..5});",
  1035. "CONST i = ORD({0..5} <= {0..8});",
  1036. "CONST b = TRUE;",
  1037. "CONST b = {0..5} <= {0..8};",
  1038. "CONST c = \"a\";",
  1039. "CONST s = \"abc\";",
  1040. "CONST s0 = \"\";",
  1041. "CONST set = {};",
  1042. "CONST set = {1 + 2};",
  1043. "CONST set = {0..32 - 1};",
  1044. "CONST set = {ci};",
  1045. "CONST i1 = 1; b1 = TRUE;",
  1046. "CONST i1 = 1; i2 = i1 + 1;",
  1047. "CONST i1 = 1; i2 = i1 + 1; i3 = i2 + 2;"),
  1048. fail(["CONST i1 = v1;", "constant expression expected"],
  1049. ["CONST i1 = v1 * 2;", "constant expression expected"],
  1050. ["CONST i1 = v1 - 10;", "constant expression expected"],
  1051. ["CONST i1 = 10 - v1;", "constant expression expected"],
  1052. ["CONST s = {v1};", "constant expression expected"],
  1053. ["CONST s = {1, v1};", "constant expression expected"],
  1054. ["CONST s = {1..v1};", "constant expression expected"],
  1055. ["CONST s = {10 - v1..15};", "constant expression expected"])
  1056. ),
  1057. "POINTER forward declaration": testWithContext(
  1058. context(grammar.module, ""),
  1059. pass("MODULE m; TYPE T = POINTER TO NotDeclaredYet; NotDeclaredYet = RECORD END; END m.",
  1060. "MODULE m; TYPE T1 = POINTER TO NotDeclaredYet; T2 = POINTER TO NotDeclaredYet; NotDeclaredYet = RECORD END; END m."
  1061. ),
  1062. fail(["MODULE m; TYPE T = POINTER TO NotDeclaredYet; END m.",
  1063. "no declaration found for 'NotDeclaredYet'"],
  1064. ["MODULE m; TYPE T1 = POINTER TO NotDeclaredYet1; T2 = POINTER TO NotDeclaredYet2; END m.",
  1065. "no declaration found for 'NotDeclaredYet1', 'NotDeclaredYet2'"],
  1066. ["MODULE m; TYPE T1 = POINTER TO Forward; Forward = PROCEDURE; END m.",
  1067. "'Forward' must be of RECORD type because it was used before in the declation of POINTER"])
  1068. ),
  1069. "typeguard for VAR argument": testWithContext(
  1070. context(grammar.procedureDeclaration,
  1071. "TYPE Base = RECORD END; Derived = RECORD (Base) i: INTEGER END;"
  1072. + "T = RECORD END; TD = RECORD(T) b: Base END;"),
  1073. pass("PROCEDURE proc(VAR p: Base); BEGIN p(Derived).i := 1; END proc"),
  1074. fail(["PROCEDURE proc(p: Base); BEGIN p(Derived).i := 1; END proc",
  1075. "invalid type cast: a value variable cannot be used"],
  1076. ["PROCEDURE proc(p: TD); BEGIN p.b(Derived).i := 1; END proc",
  1077. "invalid type cast: a value variable cannot be used"],
  1078. ["PROCEDURE proc(VAR p: T); BEGIN p(TD).b(Derived).i := 1; END proc",
  1079. "invalid type cast: a value variable cannot be used"])
  1080. ),
  1081. "NEW for read only array element fails": testWithContext(
  1082. context(grammar.procedureDeclaration,
  1083. "TYPE P = POINTER TO RECORD END;"),
  1084. pass(),
  1085. fail(["PROCEDURE readOnlyPointers(a: ARRAY OF P); BEGIN NEW(a[0]) END readOnlyPointers",
  1086. "read-only variable cannot be used as VAR parameter"])
  1087. ),
  1088. "LEN": testWithGrammar(
  1089. grammar.procedureDeclaration,
  1090. pass("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a) END p",
  1091. "PROCEDURE p(VAR a: ARRAY OF BOOLEAN): INTEGER; RETURN LEN(a) END p",
  1092. "PROCEDURE p(): INTEGER; RETURN LEN(\"abc\") END p"),
  1093. fail(["PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a[0]) END p",
  1094. "ARRAY or string is expected as an argument of LEN, got 'INTEGER'"])
  1095. ),
  1096. "array expression": testWithGrammar(
  1097. grammar.procedureBody,
  1098. pass("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1 END",
  1099. "VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1; a[1] := a[0] END",
  1100. "VAR a1, a2: ARRAY 3 OF CHAR; BEGIN ASSERT(a1 = a2); END",
  1101. "VAR a1: ARRAY 2 OF CHAR; a2: ARRAY 3 OF CHAR; BEGIN ASSERT(a1 = a2); END",
  1102. "CONST cs = \"a\"; VAR a: ARRAY 3 OF CHAR; BEGIN ASSERT(a = cs); ASSERT(cs # a); ASSERT(a < cs); ASSERT(cs > a); END",
  1103. "CONST cs = \"a\"; BEGIN ASSERT(cs[0] = \"a\"); END"
  1104. ),
  1105. fail(["VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := TRUE END",
  1106. "type mismatch: 'a[0]' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression"],
  1107. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[TRUE] := 1 END",
  1108. "'INTEGER' or 'BYTE' expression expected, got 'BOOLEAN'"],
  1109. ["VAR a: ARRAY 10 OF INTEGER; p: POINTER TO RECORD END; BEGIN a[p] := 1 END",
  1110. "'INTEGER' or 'BYTE' expression expected, got 'POINTER TO anonymous RECORD'"],
  1111. ["VAR i: INTEGER; BEGIN i[0] := 1 END",
  1112. "ARRAY or string expected, got 'INTEGER'"],
  1113. ["VAR p: POINTER TO RECORD END; BEGIN p[0] := 1 END",
  1114. "ARRAY or string expected, got 'POINTER TO anonymous RECORD'"],
  1115. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[0][0] := 1 END",
  1116. "ARRAY or string expected, got 'INTEGER'"],
  1117. ["VAR a: ARRAY 10 OF BOOLEAN; BEGIN a[0,0] := TRUE END",
  1118. "ARRAY or string expected, got 'BOOLEAN'"],
  1119. ["VAR a: ARRAY 10, 20 OF BOOLEAN; BEGIN a[0] := TRUE END",
  1120. "type mismatch: 'a[0]' is 'ARRAY 20 OF BOOLEAN' and cannot be assigned to 'BOOLEAN' expression"],
  1121. ["VAR a: ARRAY 10 OF INTEGER; BEGIN a[10] := 0 END",
  1122. "index out of bounds: maximum possible index is 9, got 10"],
  1123. ["CONST c1 = 5; VAR a: ARRAY 10 OF INTEGER; BEGIN a[10 + c1] := 0 END",
  1124. "index out of bounds: maximum possible index is 9, got 15"],
  1125. ["VAR a1, a2: ARRAY 3 OF INTEGER; BEGIN ASSERT(a1 = a2); END",
  1126. "operator '=' type mismatch: numeric type or SET or BOOLEAN or CHAR or character array or POINTER or PROCEDURE expected, got 'ARRAY 3 OF INTEGER'"],
  1127. ["CONST cs = \"\"; BEGIN ASSERT(cs[0] = \"a\"); END",
  1128. "cannot index empty string"],
  1129. ["CONST cs = \"\"; VAR i: INTEGER; BEGIN ASSERT(cs[i] = \"a\"); END",
  1130. "cannot index empty string"],
  1131. ["CONST cs = \"a\"; BEGIN ASSERT(cs[1] = \"a\"); END",
  1132. "index out of bounds: maximum possible index is 0, got 1"],
  1133. ["CONST ci = -1; VAR a: ARRAY 10 OF INTEGER; BEGIN ASSERT(a[ci] = 0); END",
  1134. "index is negative: -1"],
  1135. ["CONST ci = -1; PROCEDURE p(a: ARRAY OF INTEGER); BEGIN ASSERT(a[ci] = 0); END p; END",
  1136. "index is negative: -1"]
  1137. )
  1138. ),
  1139. "multi-dimensional array expression": testWithGrammar(
  1140. grammar.procedureBody,
  1141. pass("VAR a: ARRAY 10 OF ARRAY 5 OF INTEGER; BEGIN a[0][0] := 1 END",
  1142. "VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0][0] := TRUE END",
  1143. "VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0, 0] := TRUE END")
  1144. ),
  1145. "procedure body": testWithGrammar(
  1146. grammar.procedureBody,
  1147. pass("END",
  1148. "VAR END",
  1149. "VAR i: INTEGER; END",
  1150. "VAR a: ARRAY 10 OF INTEGER; END",
  1151. "VAR i: INTEGER; BEGIN i := 1 END",
  1152. "VAR b: BOOLEAN; BEGIN b := TRUE END",
  1153. "VAR i, j: INTEGER; BEGIN i := 1; j := 2; i := 1 + i + j - 2 END",
  1154. "TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.field := 1 END",
  1155. "TYPE T1 = RECORD field: INTEGER END; T2 = RECORD field: T1 END; VAR v1: T1; v2: T2; BEGIN v1.field := v2.field.field END",
  1156. "TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field2: INTEGER END; VAR v: T2; BEGIN v.field2 := v.field1 END"),
  1157. fail(["VAR i: INTEGER;", "END expected (PROCEDURE)"],
  1158. ["VAR i: INTEGER; i := 1; END", "END expected (PROCEDURE)"],
  1159. ["VAR i: INTEGER; BEGIN j := 1 END", "undeclared identifier: 'j'"],
  1160. ["VAR i: INTEGER; BEGIN i.field := 1 END",
  1161. "cannot designate 'INTEGER'"],
  1162. ["VAR i: INTEGER; BEGIN i := j END", "undeclared identifier: 'j'"],
  1163. ["TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v := 1 END",
  1164. "type mismatch: 'v' is 'T' and cannot be assigned to 'INTEGER' expression"],
  1165. ["TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.unknown := 1 END",
  1166. "type 'T' has no 'unknown' field"],
  1167. ["TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field1: INTEGER END; END",
  1168. "base record already has field: 'field1'"])
  1169. ),
  1170. "procedure": testWithContext(
  1171. context(grammar.procedureDeclaration,
  1172. "TYPE ProcType = PROCEDURE(): ProcType;"),
  1173. pass("PROCEDURE p; END p",
  1174. "PROCEDURE p; VAR i: INTEGER; BEGIN i := i + 1 END p",
  1175. "PROCEDURE p(a: INTEGER); BEGIN a := a + 1 END p",
  1176. "PROCEDURE p(a1, a2: INTEGER); END p",
  1177. "PROCEDURE p; BEGIN p() END p",
  1178. "PROCEDURE p(a: INTEGER); BEGIN p(a) END p",
  1179. "PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(a, b) END p",
  1180. "PROCEDURE p(): ProcType; RETURN p END p"),
  1181. fail(["PROCEDURE p; END", "not parsed"],
  1182. ["PROCEDURE p1; END p2",
  1183. "mismatched procedure names: 'p1' at the begining and 'p2' at the end"],
  1184. ["PROCEDURE p(a: INTEGER); VAR a: INTEGER END p", "'a' already declared"],
  1185. ["PROCEDURE p(a: INTEGER); BEGIN p() END p", "1 argument(s) expected, got 0"],
  1186. ["PROCEDURE p(a: INTEGER); BEGIN p(1, 2) END p", "1 argument(s) expected, got 2"],
  1187. ["PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(b, a) END p",
  1188. "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  1189. ["PROCEDURE p; BEGIN p1() END p", "undeclared identifier: 'p1'"],
  1190. ["PROCEDURE p(a1: INTEGER; a1: BOOLEAN)", "'a1' already declared"],
  1191. ["PROCEDURE p(p: INTEGER)", "argument 'p' has the same name as procedure"]
  1192. )
  1193. ),
  1194. "procedure RETURN": testWithContext(
  1195. context(
  1196. grammar.procedureDeclaration,
  1197. "TYPE A = ARRAY 3 OF INTEGER; R = RECORD END; PR = POINTER TO R;"
  1198. + "VAR i: INTEGER; PROCEDURE int(): INTEGER; RETURN 1 END int;"),
  1199. pass("PROCEDURE p(): BOOLEAN; RETURN TRUE END p",
  1200. "PROCEDURE p(): BOOLEAN; RETURN int() = 1 END p",
  1201. "PROCEDURE p; BEGIN END p" ,
  1202. "PROCEDURE p(): INTEGER; BEGIN RETURN 0 END p"),
  1203. fail(["PROCEDURE p; RETURN TRUE END p", "unexpected RETURN in PROCEDURE declared with no result type"],
  1204. ["PROCEDURE p(): BOOLEAN; END p", "RETURN expected at the end of PROCEDURE declared with 'BOOLEAN' result type"],
  1205. ["PROCEDURE p(): undeclared; END p", "undeclared identifier: 'undeclared'"],
  1206. ["PROCEDURE p(): i; END p", "type name expected"],
  1207. ["PROCEDURE p(): INTEGER; RETURN TRUE END p", "RETURN 'INTEGER' expected, got 'BOOLEAN'"],
  1208. ["PROCEDURE p(a: A): A; RETURN a END p", "the result type of a procedure cannot be an ARRAY"],
  1209. ["PROCEDURE p(): A; VAR a: A; RETURN a END p", "the result type of a procedure cannot be an ARRAY"],
  1210. ["PROCEDURE p(r: R): R; RETURN r END p", "the result type of a procedure cannot be a RECORD"],
  1211. ["PROCEDURE p(): R; VAR r: R; RETURN r END p", "the result type of a procedure cannot be a RECORD"],
  1212. ["PROCEDURE p(pr: PR): R; RETURN pr END p", "the result type of a procedure cannot be a RECORD"]
  1213. )
  1214. ),
  1215. "pass VAR argument as VAR parameter": testWithContext(
  1216. context(grammar.procedureDeclaration,
  1217. "PROCEDURE p1(VAR i: INTEGER); END p1;"
  1218. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"),
  1219. pass("PROCEDURE p(VAR i1: INTEGER); BEGIN p1(i1) END p"),
  1220. fail(["PROCEDURE p(VAR b: BOOLEAN); BEGIN p2(~b) END p", "expression cannot be used as VAR parameter"])
  1221. ),
  1222. "ARRAY parameter": testWithContext(
  1223. context(grammar.procedureDeclaration,
  1224. "TYPE T = RECORD i: INTEGER; p: POINTER TO T END;"
  1225. + "PROCEDURE p1(i: INTEGER); END p1;"
  1226. + "PROCEDURE varInteger(VAR i: INTEGER); END varInteger;"
  1227. + "PROCEDURE p2(a: ARRAY OF INTEGER); END p2;"
  1228. + "PROCEDURE p3(VAR a: ARRAY OF INTEGER); END p3;"
  1229. ),
  1230. pass("PROCEDURE p(a: ARRAY OF INTEGER); END p",
  1231. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); END p",
  1232. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN p1(a[0][0]) END p",
  1233. "PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p2(a) END p",
  1234. "PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].p.i) END p"),
  1235. fail(["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN a[0] := 0 END p",
  1236. "cannot assign to read-only variable"],
  1237. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p3(a) END p",
  1238. "read-only variable cannot be used as VAR parameter"],
  1239. ["PROCEDURE p(a: ARRAY OF T); BEGIN a[0].i := 0 END p",
  1240. "cannot assign to read-only variable"],
  1241. ["PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].i) END p",
  1242. "read-only variable cannot be used as VAR parameter"])
  1243. ),
  1244. "RECORD parameter": testWithContext(
  1245. context(grammar.procedureDeclaration,
  1246. "TYPE T = RECORD i: INTEGER; p: POINTER TO T END;"
  1247. + "PROCEDURE intValue(i: INTEGER); END intValue;"
  1248. + "PROCEDURE intVar(VAR i: INTEGER); END intVar;"
  1249. + "PROCEDURE recordValue(r: T); END recordValue;"
  1250. + "PROCEDURE recordVar(VAR r: T); END recordVar;"
  1251. ),
  1252. pass("PROCEDURE p(VAR r: T); BEGIN r.i := 0; intVar(r.i); END p",
  1253. "PROCEDURE p(VAR r: T); BEGIN recordValue(r); recordVar(r); END p",
  1254. "PROCEDURE p(r: T); BEGIN intValue(r.i); recordValue(r); END p"
  1255. ),
  1256. fail(["PROCEDURE p(r: T); BEGIN r.i := 0 END p",
  1257. "cannot assign to read-only variable"],
  1258. ["PROCEDURE p(r: T); BEGIN intVar(r.i); END p",
  1259. "read-only variable cannot be used as VAR parameter"],
  1260. ["PROCEDURE p(r: T); BEGIN recordVar(r); END p",
  1261. "read-only variable cannot be used as VAR parameter"]
  1262. )
  1263. ),
  1264. "local procedure": testWithContext(
  1265. context(grammar.procedureDeclaration,
  1266. "TYPE ProcType = PROCEDURE;" +
  1267. "VAR procVar: ProcType;" +
  1268. "PROCEDURE procWithProcArg(p: ProcType); END procWithProcArg;"),
  1269. pass("PROCEDURE p; PROCEDURE innerP; END innerP; END p",
  1270. "PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN innerP() END p"),
  1271. fail(["PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN procVar := innerP END p",
  1272. "local procedure 'innerP' cannot be referenced"],
  1273. ["PROCEDURE p; PROCEDURE innerP; END innerP; BEGIN procWithProcArg(innerP) END p",
  1274. "local procedure 'innerP' cannot be referenced"],
  1275. ["PROCEDURE p; PROCEDURE innerP; VAR innerV: INTEGER; END innerP; BEGIN innerV := 0 END p",
  1276. "undeclared identifier: 'innerV'"])
  1277. ),
  1278. "open array assignment fails": testWithGrammar(
  1279. grammar.procedureDeclaration,
  1280. pass(),
  1281. fail(["PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  1282. "cannot assign to read-only variable"],
  1283. ["PROCEDURE p(VAR s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p",
  1284. "'s1' is open 'ARRAY OF CHAR' and cannot be assigned"],
  1285. ["PROCEDURE p(s1: ARRAY OF CHAR); VAR s2: ARRAY 10 OF CHAR; BEGIN s2 := s1 END p",
  1286. "type mismatch: 's2' is 'ARRAY 10 OF CHAR' and cannot be assigned to 'ARRAY OF CHAR' expression"])
  1287. ),
  1288. "open array type as procedure parameter": testWithContext(
  1289. context(grammar.procedureDeclaration,
  1290. "TYPE A = ARRAY 3 OF INTEGER;"
  1291. ),
  1292. pass("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN END p",
  1293. "PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN END p",
  1294. "PROCEDURE p(a: ARRAY OF A); BEGIN END p"
  1295. ),
  1296. fail(["PROCEDURE p(a: ARRAY OF ARRAY 3 OF INTEGER); BEGIN END p",
  1297. "')' expected"]
  1298. )
  1299. ),
  1300. "non-open array type as procedure parameter": testWithContext(
  1301. context(grammar.procedureDeclaration,
  1302. "TYPE A = ARRAY 2 OF INTEGER;"
  1303. + "VAR a: A;"
  1304. + "PROCEDURE pa(a: A); BEGIN END pa;"
  1305. ),
  1306. pass("PROCEDURE p(a: A); BEGIN END p",
  1307. "PROCEDURE p(); VAR a: A; BEGIN pa(a) END p",
  1308. "PROCEDURE p(); VAR a: ARRAY 2 OF INTEGER; BEGIN pa(a) END p"
  1309. ),
  1310. fail(["PROCEDURE p(a: ARRAY 3 OF INTEGER); BEGIN END p",
  1311. "')' expected"],
  1312. ["PROCEDURE p(a: A): INTEGER; BEGIN RETURN a[2] END p",
  1313. "index out of bounds: maximum possible index is 1, got 2"],
  1314. ["PROCEDURE p(); VAR a: ARRAY 1 OF INTEGER; BEGIN pa(a) END p",
  1315. "type mismatch for argument 1: 'ARRAY 1 OF INTEGER' cannot be converted to 'ARRAY 2 OF INTEGER'"],
  1316. ["PROCEDURE p(a: ARRAY OF INTEGER); BEGIN pa(a) END p",
  1317. "type mismatch for argument 1: 'ARRAY OF INTEGER' cannot be converted to 'ARRAY 2 OF INTEGER'"]
  1318. )
  1319. ),
  1320. "string assignment to open array fails": testWithGrammar(
  1321. grammar.procedureDeclaration,
  1322. pass(),
  1323. fail(["PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "cannot assign to read-only variable"],
  1324. ["PROCEDURE p(VAR s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "string cannot be assigned to open ARRAY OF CHAR"])
  1325. ),
  1326. "scope": testWithGrammar(
  1327. grammar.declarationSequence,
  1328. pass("PROCEDURE p1(a1: INTEGER); END p1; PROCEDURE p2(a1: BOOLEAN); END p2;")
  1329. ),
  1330. "module": testWithGrammar(
  1331. grammar.module,
  1332. pass("MODULE m; END m."),
  1333. fail(["MODULE m; END undeclared.",
  1334. "original module name 'm' expected, got 'undeclared'"],
  1335. ["MODULE m; BEGIN - END m.", "END expected (MODULE)"])
  1336. ),
  1337. "export": testWithGrammar(
  1338. grammar.declarationSequence,
  1339. pass("CONST i* = 1;",
  1340. "TYPE T* = RECORD END;",
  1341. "VAR i*: INTEGER;",
  1342. "PROCEDURE p*; END p;"
  1343. ),
  1344. fail(["TYPE T = RECORD f*: INTEGER END;",
  1345. "field 'f' can be exported only if record 'T' itself is exported too"],
  1346. ["TYPE PT* = POINTER TO RECORD f*: INTEGER END;",
  1347. "cannot export anonymous RECORD field: 'f'"],
  1348. ["VAR p: POINTER TO RECORD f*: INTEGER END;",
  1349. "cannot export anonymous RECORD field: 'f'"],
  1350. ["VAR p*: POINTER TO RECORD r: RECORD f*: INTEGER END END;",
  1351. "field 'f' can be exported only if field 'r' itself is exported too"],
  1352. ["VAR i*: POINTER TO RECORD f*: INTEGER END;",
  1353. "cannot export anonymous RECORD field: 'f'"],
  1354. ["VAR i*: POINTER TO RECORD r*: RECORD f*: INTEGER END END;",
  1355. "cannot export anonymous RECORD field: 'r'"],
  1356. ["PROCEDURE p*; VAR i*: INTEGER; END p;",
  1357. "cannot export from within procedure: variable 'i'"]
  1358. //["TYPE PT = POINTER TO RECORD END; PROCEDURE p*(): PT; RETURN NIL END p;",
  1359. //"exported PROCEDURE 'p' uses non-exported type 'PT'"]
  1360. )
  1361. ),
  1362. "import JS": testWithGrammar(
  1363. grammar.module,
  1364. pass("MODULE m; IMPORT JS; END m.",
  1365. "MODULE m; IMPORT JS; BEGIN JS.alert(\"test\") END m.",
  1366. "MODULE m; IMPORT JS; BEGIN JS.console.info(123) END m.",
  1367. "MODULE m; IMPORT JS; BEGIN JS.do(\"throw new Error()\") END m."
  1368. ),
  1369. fail(["MODULE m; IMPORT JS; BEGIN JS.do(123) END m.",
  1370. "string is expected as an argument of JS predefined procedure 'do', got INTEGER"],
  1371. ["MODULE m; IMPORT JS; BEGIN JS.do(\"a\", \"b\") END m.",
  1372. "1 argument(s) expected, got 2"],
  1373. ["MODULE m; IMPORT JS; VAR s: ARRAY 10 OF CHAR; BEGIN JS.do(s) END m.",
  1374. "string is expected as an argument of JS predefined procedure 'do', got ARRAY 10 OF CHAR"]
  1375. )
  1376. ),
  1377. "JS.var": testWithGrammar(
  1378. grammar.module,
  1379. pass("MODULE m; IMPORT JS; VAR v: JS.var; END m.",
  1380. "MODULE m; IMPORT JS; VAR v: JS.var; BEGIN v := JS.f(); END m.",
  1381. "MODULE m; IMPORT JS; VAR v: JS.var; BEGIN v := JS.f1(); JS.f2(v); END m."
  1382. ),
  1383. fail(["MODULE m; IMPORT JS; VAR v: JS.var; i: INTEGER; BEGIN i := v; END m.",
  1384. "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'JS.var' expression"])
  1385. ),
  1386. "import unknown module": testWithGrammar(
  1387. grammar.module,
  1388. pass(),
  1389. fail(["MODULE m; IMPORT unknown; END m.", "module(s) not found: unknown"],
  1390. ["MODULE m; IMPORT unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2"]
  1391. )
  1392. ),
  1393. "self import is failed": testWithGrammar(
  1394. grammar.module,
  1395. pass(),
  1396. fail(["MODULE test; IMPORT test; END test.", "module 'test' cannot import itself"])
  1397. ),
  1398. "import aliases": testWithGrammar(
  1399. grammar.module,
  1400. pass("MODULE m; IMPORT J := JS; END m.",
  1401. "MODULE m; IMPORT J := JS; BEGIN J.alert(\"test\") END m."),
  1402. fail(["MODULE m; IMPORT u1 := unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2"],
  1403. ["MODULE m; IMPORT a1 := m1, a2 := m1; END m.", "module already imported: 'm1'"],
  1404. ["MODULE m; IMPORT a1 := u1, a1 := u2; END m.", "duplicated alias: 'a1'"],
  1405. ["MODULE m; IMPORT J := JS; BEGIN JS.alert(\"test\") END m.", "undeclared identifier: 'JS'"]
  1406. )
  1407. ),
  1408. "syntax errors": testWithGrammar(
  1409. grammar.module,
  1410. pass(),
  1411. fail(["MODULE m; CONST c = 1 END m.",
  1412. "';' expected"],
  1413. ["MODULE m; TYPE T = RECORD END END m.",
  1414. "';' expected"],
  1415. ["MODULE m; VAR v: INTEGER END m.",
  1416. "';' expected"],
  1417. ["MODULE m; PROCEDURE p(INTEGER) END m.",
  1418. "')' expected"])
  1419. )
  1420. };
  1421. }
  1422. var result = Test.run({
  1423. "common": {
  1424. "oberon": makeSuiteForGrammar(oberon),
  1425. "eberon": makeSuiteForGrammar(eberon)
  1426. },
  1427. "eberon": TestUnitEberon.suite,
  1428. "oberon": TestUnitOberon.suite
  1429. });
  1430. if (typeof process != "undefined")
  1431. process.exit(result ? 0 : -1);