test_unit.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. "use strict";
  2. var assert = require("assert.js").ok;
  3. var Context = require("context.js");
  4. var Errors = require("errors.js");
  5. var Grammar = require("grammar.js");
  6. var oc = require("oc.js");
  7. var Class = require("rtl.js").Class;
  8. var Stream = require("stream.js").Stream;
  9. var Test = require("test.js");
  10. var TestError = Test.TestError;
  11. function parseInContext(grammar, s, context){
  12. var stream = new Stream(s);
  13. if (!grammar(stream, context) || !stream.eof())
  14. throw new Errors.Error("not parsed");
  15. }
  16. function parseUsingGrammar(grammar, s, cxFactory, handlerError){
  17. var baseContext = new Context.Context();
  18. var context = cxFactory ? cxFactory(baseContext) : baseContext;
  19. try {
  20. parseInContext(grammar, s, context);
  21. }
  22. catch (x){
  23. if (!(x instanceof Errors.Error))
  24. throw x;//console.log(x.stack);
  25. if (handlerError)
  26. handlerError(x);
  27. //else
  28. // throw x;
  29. // console.log(s + ": " + x);
  30. return false;
  31. }
  32. return true;
  33. }
  34. function setup(parser, contextFactory){
  35. function parseImpl(s, handleError){
  36. return parseUsingGrammar(parser, s, contextFactory, handleError);
  37. }
  38. return {
  39. parse: function(s){
  40. function handleError(e){throw new TestError(s + "\n\t" + e);}
  41. if (!parseImpl(s, handleError))
  42. throw new TestError(s + ": not parsed");
  43. },
  44. expectError: function(s, error){
  45. function handleError(actualError){
  46. var sErr = actualError.toString();
  47. if (sErr != error)
  48. throw new TestError(s + "\n\texpected error: " + error + "\n\tgot: " + sErr );
  49. }
  50. if (parseImpl(s, handleError))
  51. throw new TestError(s + ": should not be parsed, expect error: " + error);
  52. }
  53. };
  54. }
  55. function setupWithContext(grammar, source){
  56. function makeContext(){
  57. var context = new Context.Context();
  58. try {
  59. parseInContext(Grammar.declarationSequence, source, context);
  60. }
  61. catch (x) {
  62. if (x instanceof Errors.Error)
  63. throw new TestError("setup error: " + x + "\n" + source);
  64. throw x;
  65. }
  66. return context;
  67. }
  68. return setup(grammar, makeContext);
  69. }
  70. function context(grammar, source){
  71. return {grammar: grammar, source: source};
  72. }
  73. function pass(/*...*/){return Array.prototype.slice.call(arguments);}
  74. function fail(/*...*/){return Array.prototype.slice.call(arguments);}
  75. function testWithContext(context, pass, fail){
  76. return function(){
  77. var test = setupWithContext(context.grammar, context.source);
  78. var i;
  79. for(i = 0; i < pass.length; ++i)
  80. test.parse(pass[i]);
  81. for(i = 0; i < fail.length; ++i){
  82. var f = fail[i];
  83. test.expectError(f[0], f[1]);
  84. }
  85. };
  86. }
  87. var testSuite = {
  88. comment: function(){
  89. var test = setup(Grammar.expression);
  90. test.parse("(**)123");
  91. test.parse("(*abc*)123");
  92. test.parse("(*abc*)(*def*)123");
  93. test.parse("(*a(*b*)c*)123");
  94. test.expectError("(*123", "comment was not closed");
  95. },
  96. "spaces are required to separate keywords and integers": function(){
  97. var test = setup(Grammar.typeDeclaration);
  98. test.expectError("T = ARRAY10OFARRAY5OFINTEGER", "not parsed");
  99. test.expectError("T = ARRAY10 OF ARRAY 5 OF INTEGER", "not parsed");
  100. test.expectError("T = ARRAY 10OF ARRAY 5 OF INTEGER", "not parsed");
  101. test.expectError("T = ARRAY 10 OFARRAY 5 OF INTEGER", "not parsed");
  102. test.expectError("T = ARRAY 10 OF ARRAY5 OF INTEGER", "undeclared type: 'ARRAY5'");
  103. test.expectError("T = ARRAY 10 OF ARRAY 5OF INTEGER", "not parsed");
  104. test.expectError("T = ARRAY 10 OF ARRAY 5 OFINTEGER", "not parsed");
  105. },
  106. expression: function(){
  107. var test = setupWithContext(
  108. Grammar.expression
  109. , "TYPE ProcType = PROCEDURE(): INTEGER;"
  110. + "PROCEDURE p1(): INTEGER; RETURN 1 END p1;"
  111. + "PROCEDURE p2(): ProcType; RETURN p1 END p2;"
  112. + "PROCEDURE noResult(); END noResult;");
  113. test.expectError("", "not parsed");
  114. test.parse("123");
  115. test.expectError("12a", "not parsed");
  116. test.parse("1+2");
  117. test.parse("1 + 2");
  118. test.parse("1 + 2 + 3");
  119. test.parse("-1");
  120. test.parse("+1");
  121. test.parse("p1() + p1()");
  122. test.parse("p2()");
  123. test.expectError("p2()()", "not parsed");
  124. test.expectError("noResult()", "procedure returning no result cannot be used in an expression");
  125. },
  126. "string expression": function(){
  127. var test = setup(Grammar.expression);
  128. test.parse("\"\"");
  129. test.parse("\"a\"");
  130. test.parse("\"abc\"");
  131. test.parse("0FFX");
  132. test.parse("0AX");
  133. test.parse("22X");
  134. test.parse("0X");
  135. test.expectError("\"", "unexpected end of string");
  136. test.expectError("FFX", "undeclared identifier: 'FFX'");
  137. //assert(!parse("1 + \"a\""));
  138. //assert(parse("\"a\" + \"b\""));
  139. },
  140. "parentheses": function(){
  141. var test = setup(Grammar.expression);
  142. test.parse("(1)");
  143. test.parse("(1 + 2)");
  144. test.parse("(1 + 2) * 3");
  145. test.parse("3 * (1 + 2)");
  146. test.expectError("(1 + 2", "no matched ')'");
  147. },
  148. identifier: function(){
  149. var IdentDeclarationContext = Class.extend({
  150. init: function(){this.__ident = undefined;},
  151. setIdent: function(id){this.__ident = id;},
  152. ident: function() {return this.__ident;},
  153. getResult: function() {return this.__ident;}
  154. });
  155. function makeContext() {return new IdentDeclarationContext();}
  156. var parse = function(s) { return parseUsingGrammar(Grammar.ident, s, makeContext); };
  157. assert(!parse(""));
  158. assert(parse("i"));
  159. assert(!parse("1"));
  160. assert(parse("abc1"));
  161. },
  162. "variable declaration": function(){
  163. var parse = function(s) { return parseUsingGrammar(Grammar.variableDeclaration, s); };
  164. assert(parse("i: INTEGER"));
  165. assert(parse("i, j: INTEGER"));
  166. assert(!parse("i: T"));
  167. },
  168. "procedure VAR section": function(){
  169. var parse = function(s) { return parseUsingGrammar(Grammar.declarationSequence, s); };
  170. assert(parse("VAR"));
  171. assert(parse("VAR i: INTEGER;"));
  172. assert(parse("VAR i, j: INTEGER;"));
  173. assert(parse("VAR i, j: INTEGER; b: BOOLEAN;"));
  174. },
  175. "const declaration": function(){
  176. var test = setupWithContext(
  177. Grammar.declarationSequence
  178. , "CONST ci = 1; VAR v1: INTEGER;");
  179. test.parse("CONST i = 10;");
  180. test.parse("CONST i = 1 + 2;");
  181. test.parse("CONST i = ci + 2;");
  182. test.parse("CONST i = ci * 2;");
  183. test.parse("CONST i = ORD({0..5});");
  184. test.parse("CONST i = ORD({0..5} <= {0..8});");
  185. test.parse("CONST b = TRUE;");
  186. test.parse("CONST b = {0..5} <= {0..8};");
  187. test.parse("CONST c = \"a\";");
  188. test.parse("CONST s = \"abc\";");
  189. test.parse("CONST s0 = \"\";");
  190. test.parse("CONST set = {};");
  191. test.parse("CONST set = {1 + 2};");
  192. test.parse("CONST set = {0..32 - 1};");
  193. test.parse("CONST set = {ci};");
  194. test.parse("CONST i1 = 1; b1 = TRUE;");
  195. test.parse("CONST i1 = 1; i2 = i1 + 1;");
  196. test.parse("CONST i1 = 1; i2 = i1 + 1; i3 = i2 + 2;");
  197. test.expectError("CONST i1 = v1;", "constant expression expected");
  198. test.expectError("CONST i1 = v1 * 2;", "constant expression expected");
  199. test.expectError("CONST i1 = v1 - 10;", "constant expression expected");
  200. test.expectError("CONST i1 = 10 - v1;", "constant expression expected");
  201. test.expectError("CONST s = {v1};", "constant expression expected");
  202. test.expectError("CONST s = {1, v1};", "constant expression expected");
  203. test.expectError("CONST s = {1..v1};", "constant expression expected");
  204. test.expectError("CONST s = {10 - v1..15};", "constant expression expected");
  205. },
  206. "record declaration": function(){
  207. var parse = function(s) { return parseUsingGrammar(Grammar.typeDeclaration, s); };
  208. assert(parse("t = RECORD END"));
  209. assert(parse("t = RECORD i: INTEGER END"));
  210. assert(parse("t = RECORD i, j: INTEGER END"));
  211. assert(!parse("t = RECORD i, j, i: INTEGER END"));
  212. assert(parse("t = RECORD i, j: INTEGER; b: BOOLEAN END"));
  213. },
  214. "array declaration": function(){
  215. var test = setupWithContext(
  216. Grammar.typeDeclaration
  217. , "CONST c1 = 5; VAR v1: INTEGER;");
  218. test.parse("T = ARRAY 10 OF INTEGER");
  219. test.parse("T = ARRAY 10 OF BOOLEAN");
  220. test.expectError("T = ARRAY 0 OF INTEGER", "array size must be greater than 0, got 0");
  221. test.expectError("T = ARRAY TRUE OF INTEGER"
  222. , "'INTEGER' constant expression expected, got 'BOOLEAN'");
  223. test.parse("T = ARRAY 1 + 2 OF INTEGER");
  224. test.parse("T = ARRAY c1 OF INTEGER");
  225. test.parse("T = ARRAY ORD({0..5} <= {0..8}) OF INTEGER");
  226. test.expectError("T = ARRAY v1 OF INTEGER", "constant expression expected as ARRAY size");
  227. test.expectError("T = ARRAY c1 - 10 OF INTEGER", "array size must be greater than 0, got -5");
  228. test.expectError("T = ARRAY ORD({0..5} >= {0..8}) OF INTEGER", "array size must be greater than 0, got 0");
  229. },
  230. "multi-dimensional array declaration": function(){
  231. var test = setup(Grammar.typeDeclaration);
  232. test.parse("T = ARRAY 10 OF ARRAY 5 OF INTEGER");
  233. test.parse("T = ARRAY 10, 5 OF INTEGER");
  234. },
  235. "PROCEDURE type declaration": function(){
  236. var test = setup(Grammar.typeDeclaration);
  237. test.parse("T = PROCEDURE");
  238. test.parse("T = PROCEDURE()");
  239. test.parse("T = PROCEDURE(a: INTEGER)");
  240. test.parse("T = PROCEDURE(a: INTEGER; b: BOOLEAN)");
  241. test.parse("T = PROCEDURE(): T");
  242. },
  243. "POINTER declaration": function(){
  244. var test = setup(Grammar.typeDeclaration);
  245. test.parse("T = POINTER TO RECORD END");
  246. test.parse("T = POINTER TO NotDeclaredYet");
  247. test.parse("T = POINTER TO RECORD p: POINTER TO T END");
  248. test.expectError("T = POINTER TO INTEGER"
  249. , "RECORD is expected as a POINTER base type, got 'INTEGER'");
  250. test.expectError("T = POINTER TO POINTER TO RECORD END"
  251. , "RECORD is expected as a POINTER base type, got 'POINTER TO anonymous RECORD'");
  252. },
  253. "POINTER dereference": function(){
  254. var test = setupWithContext(
  255. Grammar.statement
  256. , "VAR p: POINTER TO RECORD field: INTEGER END; i: INTEGER; r: RECORD END;");
  257. test.parse("p^.field := 1");
  258. test.parse("p.field := 0");
  259. test.expectError("i^", "POINTER TO type expected, got 'INTEGER'");
  260. test.expectError("r^", "POINTER TO type expected, got 'anonymous RECORD'");
  261. },
  262. "POINTER assignment": function(){
  263. var test = setupWithContext(
  264. Grammar.statement
  265. , "TYPE Base = RECORD END; Derived = RECORD (Base) END;"
  266. + "VAR p1, p2: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived;");
  267. test.parse("p1 := NIL");
  268. test.parse("p1 := p2");
  269. test.parse("pBase := pDerived");
  270. test.expectError("p1 := pBase"
  271. , "type mismatch: 'p1' is 'POINTER TO anonymous RECORD' and cannot be assigned to 'POINTER TO Base' expression");
  272. test.expectError("pDerived := pBase"
  273. , "type mismatch: 'pDerived' is 'POINTER TO Derived' and cannot be assigned to 'POINTER TO Base' expression");
  274. test.expectError("NIL := p1", "not parsed");
  275. },
  276. "POINTER cast": function(){
  277. var test = setupWithContext(
  278. Grammar.expression
  279. , "TYPE Base = RECORD END; Derived = RECORD (Base) END; PDerived = POINTER TO Derived;"
  280. + "VAR p1, p2: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived; i: INTEGER;");
  281. test.parse("pBase(Derived)");
  282. test.expectError("pDerived(Derived)"
  283. , "invalid type cast: 'Derived' is not an extension of 'Derived'");
  284. test.expectError("p1(Base)"
  285. , "invalid type cast: 'Base' is not an extension of 'anonymous RECORD'");
  286. test.expectError("p1(INTEGER)"
  287. , "invalid type cast: RECORD type expected as an argument of type guard, got 'INTEGER'");
  288. test.expectError("p1(PDerived)"
  289. , "invalid type cast: RECORD type expected as an argument of type guard, got 'PDerived'");
  290. test.expectError("i(Derived)"
  291. , "invalid type cast: 'Derived' is not an extension of 'INTEGER'");
  292. },
  293. "POINTER relations": testWithContext(
  294. context(Grammar.expression,
  295. "TYPE B = RECORD END; D = RECORD(B) END;"
  296. + "VAR p1, p2: POINTER TO RECORD END; pb: POINTER TO B; pd: POINTER TO D;"),
  297. pass("p1 = p2",
  298. "p1 # p2",
  299. "pb = pd",
  300. "pd # pb"
  301. ),
  302. fail(["p1 < p2", "operator '<' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  303. ["p1 <= p2", "operator '<=' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  304. ["p1 > p2", "operator '>' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  305. ["p1 >= p2", "operator '>=' type mismatch: numeric type or CHAR or character array expected, got 'POINTER TO anonymous RECORD'"],
  306. ["p1 = pb", "type mismatch: expected 'POINTER TO anonymous RECORD', got 'POINTER TO B'"]
  307. )
  308. ),
  309. "IS expression": function(){
  310. var test = setupWithContext(
  311. Grammar.expression
  312. , "TYPE Base = RECORD END; Derived = RECORD (Base) END; PDerived = POINTER TO Derived;"
  313. + "VAR p: POINTER TO RECORD END; pBase: POINTER TO Base; pDerived: POINTER TO Derived; vDerived: Derived; i: INTEGER;");
  314. test.parse("pBase IS Derived");
  315. test.expectError("pBase IS pDerived", "RECORD type expected after 'IS'");
  316. test.expectError("pBase IS TRUE", "RECORD type expected after 'IS'");
  317. test.expectError("pBase IS vDerived", "type name expected");
  318. test.expectError("Derived IS Derived", "POINTER to type expected before 'IS'");
  319. test.expectError("i IS Derived", "POINTER to type expected before 'IS'");
  320. test.expectError("p IS Derived"
  321. , "invalid type test: 'Derived' is not an extension of 'anonymous RECORD'");
  322. test.expectError("pDerived IS Derived"
  323. , "invalid type test: 'Derived' is not an extension of 'Derived'");
  324. test.expectError("pDerived IS Base"
  325. , "invalid type test: 'Base' is not an extension of 'Derived'");
  326. test.expectError("pDerived IS INTEGER", "RECORD type expected after 'IS'");
  327. },
  328. "NEW": function(){
  329. var test = setupWithContext(
  330. Grammar.statement
  331. , "TYPE P = POINTER TO RECORD END;"
  332. + "VAR p: P; i: INTEGER;"
  333. + "PROCEDURE proc(): P; RETURN NIL END proc;"
  334. );
  335. test.parse("NEW(p)");
  336. test.expectError("NEW.NEW(p)", "cannot designate 'predefined procedure NEW'");
  337. test.expectError("NEW(i)", "POINTER variable expected, got 'INTEGER'");
  338. test.expectError("NEW()", "1 argument(s) expected, got 0");
  339. test.expectError("NEW(p, p)", "1 argument(s) expected, got 2");
  340. test.expectError("NEW(proc())", "expression cannot be used as VAR parameter");
  341. },
  342. "NEW for read only array element fails": function(){
  343. var test = setupWithContext(Grammar.procedureDeclaration
  344. , "TYPE P = POINTER TO RECORD END;");
  345. test.expectError("PROCEDURE readOnlyPointers(a: ARRAY OF P); BEGIN NEW(a[0]) END readOnlyPointers",
  346. "read-only variable cannot be used as VAR parameter");
  347. },
  348. "LEN": function(){
  349. var test = setup(Grammar.procedureDeclaration);
  350. test.parse("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a) END p");
  351. test.parse("PROCEDURE p(VAR a: ARRAY OF BOOLEAN): INTEGER; RETURN LEN(a) END p");
  352. test.parse("PROCEDURE p(): INTEGER; RETURN LEN(\"abc\") END p");
  353. test.expectError("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a[0]) END p",
  354. "type mismatch for argument 1: 'INTEGER' cannot be converted to 'ARRAY OF any type'");
  355. },
  356. "ABS": testWithContext(
  357. context(Grammar.statement,
  358. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  359. pass("i := ABS(i)",
  360. "r := ABS(r)"),
  361. fail(["i := ABS(r)", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'REAL' expression"],
  362. ["i := ABS(c)", "type mismatch: expected numeric type, got 'CHAR'"],
  363. ["i := ABS(i, i)", "1 argument(s) expected, got 2"]
  364. )
  365. ),
  366. "FLOOR": testWithContext(
  367. context(Grammar.statement, "VAR i: INTEGER; r: REAL;"),
  368. pass("i := FLOOR(r)"),
  369. fail(["i := FLOOR(i)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'REAL'"],
  370. ["i := FLOOR(r, r)", "1 argument(s) expected, got 2"]
  371. )
  372. ),
  373. "FLT": testWithContext(
  374. context(Grammar.statement, "VAR i: INTEGER; r: REAL;"),
  375. pass("r := FLT(i)"),
  376. fail(["r := FLT(r)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  377. ["i := FLT(i, i)", "1 argument(s) expected, got 2"]
  378. )
  379. ),
  380. "LONG": testWithContext(
  381. context(Grammar.statement, "VAR i: INTEGER; r: REAL; lr: LONGREAL;"),
  382. pass("lr := LONG(r)"),
  383. fail(["lr := LONG(i)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'REAL'"],
  384. ["lr := LONG(r, r)", "1 argument(s) expected, got 2"]
  385. )
  386. ),
  387. "SHORT": testWithContext(
  388. context(Grammar.statement, "VAR i: INTEGER; r: REAL; lr: LONGREAL;"),
  389. pass("r := SHORT(lr)"),
  390. fail(["r := SHORT(i)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'REAL'"],
  391. ["r := SHORT(lr, lr)", "1 argument(s) expected, got 2"]
  392. )
  393. ),
  394. "LSL": testWithContext(
  395. context(Grammar.statement,
  396. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  397. pass("i := LSL(i, i)"),
  398. fail(["i := LSL(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  399. ["i := LSL(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  400. ["r := LSL(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  401. ["i := LSL(i)", "2 argument(s) expected, got 1"]
  402. )
  403. ),
  404. "ASR": testWithContext(
  405. context(Grammar.statement,
  406. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  407. pass("i := ASR(i, i)"),
  408. fail(["i := ASR(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  409. ["i := ASR(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  410. ["r := ASR(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  411. ["i := ASR(i)", "2 argument(s) expected, got 1"]
  412. )
  413. ),
  414. "ROR": testWithContext(
  415. context(Grammar.statement,
  416. "VAR i: INTEGER; r: REAL; c: CHAR;"),
  417. pass("i := ROR(i, i)"),
  418. fail(["i := ROR(i, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  419. ["i := ROR(r, i)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  420. ["r := ROR(i, i)", "type mismatch: 'r' is 'REAL' and cannot be assigned to 'INTEGER' expression"],
  421. ["i := ROR(i)", "2 argument(s) expected, got 1"]
  422. )
  423. ),
  424. "ODD": testWithContext(
  425. context(Grammar.statement, "VAR b: BOOLEAN;"),
  426. pass("b := ODD(1)",
  427. "b := ODD(123)"
  428. ),
  429. fail(["b := ODD(1.2)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'"],
  430. ["b := ODD(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
  431. )
  432. ),
  433. "ORD": testWithContext(
  434. context(Grammar.statement, "VAR ch: CHAR; i: INTEGER; b: BOOLEAN;"),
  435. pass("i := ORD(ch)",
  436. "i := ORD(TRUE)",
  437. "i := ORD({1})",
  438. "i := ORD(\"a\")",
  439. "b := ORD(22X) = 022H"),
  440. fail(["i := ORD(1.2)", "type mismatch for argument 1: 'REAL' cannot be converted to 'CHAR or BOOLEAN or SET'"],
  441. ["i := ORD(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'CHAR or BOOLEAN or SET'"]
  442. )
  443. ),
  444. "CHR": testWithContext(
  445. context(Grammar.statement, "VAR i: INTEGER; ch: CHAR;"),
  446. pass("ch := CHR(i)"),
  447. fail(["ch := CHR(ch)", "type mismatch for argument 1: 'CHAR' cannot be converted to 'INTEGER'"])
  448. ),
  449. "INC": testWithContext(
  450. context(Grammar.statement, "VAR i: INTEGER;"),
  451. pass("INC(i)",
  452. "INC(i, 3)"),
  453. fail(["INC(i + i)", "expression cannot be used as VAR parameter"],
  454. ["INC(i, i)", "constant expected as second argument of INC"],
  455. ["INC()", "at least 1 argument expected, got 0"],
  456. ["INC(i, 1, 2)", "at most 2 arguments expected, got 3"]
  457. )
  458. ),
  459. "DEC": testWithContext(
  460. context(Grammar.statement, "VAR i: INTEGER;"),
  461. pass("DEC(i)",
  462. "DEC(i, 3)"),
  463. fail(["DEC(i + i)", "expression cannot be used as VAR parameter"],
  464. ["DEC(i, i)", "constant expected as second argument of DEC"],
  465. ["DEC()", "at least 1 argument expected, got 0"],
  466. ["DEC(i, 1, 2)", "at most 2 arguments expected, got 3"]
  467. )
  468. ),
  469. "COPY": testWithContext(
  470. context(Grammar.statement, "VAR ac3: ARRAY 3 OF CHAR; ac4: ARRAY 4 OF CHAR;"),
  471. pass("COPY(\"abc\", ac3)",
  472. "COPY(ac3, ac3)"
  473. ),
  474. fail(["COPY(ac3, \"abc\")", "expression cannot be used as VAR parameter"],
  475. ["COPY(\"abcd\", ac3)", "3-character ARRAY is too small for 4-character string"],
  476. ["COPY(ac3, ac4)", "array size mismatch: 'ac4' has size 4 and cannot be copied to the array with size 3"]
  477. )
  478. ),
  479. "PACK": testWithContext(
  480. context(Grammar.statement, "VAR r: REAL; i: INTEGER;"),
  481. pass("PACK(r, i)",
  482. "PACK(r, 3)"),
  483. fail(["PACK(r, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"])
  484. ),
  485. "UNPACK": testWithContext(
  486. context(Grammar.statement, "VAR r: REAL; i: INTEGER;"),
  487. pass("UNPACK(r, i)"),
  488. fail(["UNPACK(r, r)", "type mismatch for argument 2: 'REAL' cannot be converted to 'INTEGER'"],
  489. ["UNPACK(r, 3)", "expression cannot be used as VAR parameter"],
  490. ["UNPACK(123.456, i)", "expression cannot be used as VAR parameter"]
  491. )
  492. ),
  493. "assignment statement": function(){
  494. var test = setupWithContext(
  495. Grammar.statement
  496. , "CONST c = 15;"
  497. + "VAR ch: CHAR; i, n: INTEGER; b: BOOLEAN;"
  498. + "proc1: PROCEDURE; proc2: PROCEDURE(): INTEGER;"
  499. + "a: ARRAY 5 OF INTEGER;"
  500. + "PROCEDURE p(): INTEGER; RETURN 1 END p;"
  501. + "PROCEDURE noResult(); END noResult;");
  502. test.parse("i := 0");
  503. test.parse("i := n");
  504. test.parse("i := c");
  505. test.parse("b := TRUE");
  506. test.parse("ch := \"A\"");
  507. test.parse("i := p()");
  508. test.parse("proc1 := proc1");
  509. test.parse("proc2 := NIL");
  510. test.parse("a[1] := 2");
  511. test.expectError("i := b", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression");
  512. test.expectError("c := i", "cannot assign to constant");
  513. test.expectError("ch := \"AB\""
  514. , "type mismatch: 'ch' is 'CHAR' and cannot be assigned to 'multi-character string' expression");
  515. test.expectError("i := .1", "expression expected");
  516. test.expectError("proc1 := proc2"
  517. , "type mismatch: 'proc1' is 'PROCEDURE' and cannot be assigned to 'PROCEDURE(): INTEGER' expression");
  518. test.expectError("i := noResult()", "procedure returning no result cannot be used in an expression");
  519. },
  520. "array expression": function(){
  521. var test = setup(Grammar.procedureBody);
  522. test.parse("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1 END");
  523. test.parse("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1; a[1] := a[0] END");
  524. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := TRUE END"
  525. , "type mismatch: 'a[0]' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression");
  526. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[TRUE] := 1 END"
  527. , "'INTEGER' expression expected, got 'BOOLEAN'");
  528. test.expectError("VAR i: INTEGER; BEGIN i[0] := 1 END"
  529. , "ARRAY expected, got 'INTEGER'");
  530. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0][0] := 1 END"
  531. , "ARRAY expected, got 'INTEGER'");
  532. test.expectError("VAR a: ARRAY 10 OF BOOLEAN; BEGIN a[0,0] := TRUE END"
  533. , "ARRAY expected, got 'BOOLEAN'");
  534. test.expectError("VAR a: ARRAY 10, 20 OF BOOLEAN; BEGIN a[0] := TRUE END"
  535. , "type mismatch: 'a[0]' is 'ARRAY OF BOOLEAN' and cannot be assigned to 'BOOLEAN' expression");
  536. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[10] := 0 END"
  537. , "index out of bounds: maximum possible index is 9, got 10");
  538. test.expectError("CONST c1 = 5; VAR a: ARRAY 10 OF INTEGER; BEGIN a[10 + c1] := 0 END"
  539. , "index out of bounds: maximum possible index is 9, got 15");
  540. },
  541. "multi-dimensional array expression": function(){
  542. var test = setup(Grammar.procedureBody);
  543. test.parse("VAR a: ARRAY 10 OF ARRAY 5 OF INTEGER; BEGIN a[0][0] := 1 END");
  544. test.parse("VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0][0] := TRUE END");
  545. test.parse("VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0, 0] := TRUE END");
  546. },
  547. "INTEGER number": function(){
  548. var test = setup(Grammar.expression);
  549. test.parse("0");
  550. test.parse("123");
  551. test.parse("1H");
  552. test.parse("1FH");
  553. test.parse("0FFH");
  554. test.parse("0H");
  555. test.expectError("FFH", "undeclared identifier: 'FFH'");
  556. test.expectError("FF", "undeclared identifier: 'FF'");
  557. test.expectError("1HH", "not parsed");
  558. test.expectError("1H0", "not parsed");
  559. test.expectError("1 23", "not parsed");
  560. test.expectError("1F FH", "not parsed");
  561. },
  562. "SET statement": function(){
  563. var test = setupWithContext(Grammar.statement, "VAR s: SET;");
  564. test.parse("s := {}");
  565. test.parse("s := {0}");
  566. test.parse("s := {0, 1}");
  567. test.parse("s := {1 + 2, 5..10}");
  568. //test.expectError("s := {32}", "0..31");
  569. },
  570. "REAL number": function(){
  571. var test = setup(Grammar.expression);
  572. test.parse("1.2345");
  573. test.parse("1.");
  574. test.parse("1.2345E6");
  575. test.parse("1.2345E+6");
  576. test.parse("1.2345E-12");
  577. test.expectError("1. 2345E-12", "not parsed");
  578. test.expectError("1.23 45E-12", "not parsed");
  579. test.expectError("1.2345 E-12", "not parsed");
  580. test.expectError("1.2345E-1 2", "not parsed");
  581. },
  582. "LONGREAL number": function(){
  583. var test = setup(Grammar.expression);
  584. test.parse("1.2345D6");
  585. test.parse("1.2345D+6");
  586. test.parse("1.2345D-6");
  587. },
  588. "IF statement": function(){
  589. var test = setupWithContext(
  590. Grammar.statement
  591. , "VAR b1: BOOLEAN; i1: INTEGER;");
  592. test.parse("IF b1 THEN i1 := 0 END");
  593. test.parse("IF FALSE THEN i1 := 0 ELSE i1 := 1 END");
  594. test.parse("IF TRUE THEN i1 := 0 ELSIF FALSE THEN i1 := 1 ELSE i1 := 2 END");
  595. test.expectError("IF i1 THEN i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'");
  596. test.expectError("IF b1 THEN i1 := 0 ELSIF i1 THEN i1 := 2 END"
  597. , "'BOOLEAN' expression expected, got 'INTEGER'");
  598. },
  599. "CASE statement": function(){
  600. var test = setupWithContext(
  601. Grammar.statement
  602. , "CONST ci = 15; cc = \"A\"; VAR c1: CHAR; b1: BOOLEAN; i1, i2: INTEGER;");
  603. test.parse("CASE i1 OF END");
  604. test.parse("CASE i1 OF 0: b1 := TRUE END");
  605. test.parse("CASE c1 OF \"A\": b1 := TRUE END");
  606. test.parse("CASE i1 OF 0: b1 := TRUE | 1: b1 := FALSE END");
  607. test.parse("CASE i1 OF 0, 1: b1 := TRUE END");
  608. test.parse("CASE c1 OF \"A\", \"B\": b1 := TRUE END");
  609. test.parse("CASE i1 OF 0..2: b1 := TRUE END");
  610. test.parse("CASE i1 OF ci..2: b1 := TRUE END");
  611. test.parse("CASE c1 OF cc..\"Z\": b1 := TRUE END");
  612. test.parse("CASE i1 OF 1, 2, 3: b1 := TRUE | 4..10: b1 := FALSE | 11: c1 := \"A\" END");
  613. test.parse("CASE i1 OF 1, 2, 5..9: b1 := TRUE END");
  614. test.expectError("CASE i1 OF undefined: b1 := TRUE END"
  615. , "undeclared identifier: 'undefined'");
  616. test.expectError("CASE i1 OF i2: b1 := TRUE END"
  617. , "'i2' is not a constant");
  618. test.expectError("CASE b1 OF END", "'INTEGER' or 'CHAR' expected as CASE expression");
  619. test.expectError("CASE i1 OF \"A\": b1 := TRUE END"
  620. , "label must be 'INTEGER' (the same as case expression), got 'CHAR'");
  621. test.expectError("CASE c1 OF \"A\", 1: b1 := TRUE END"
  622. , "label must be 'CHAR' (the same as case expression), got 'INTEGER'");
  623. test.expectError("CASE c1 OF \"A\"..1: b1 := TRUE END"
  624. , "label must be 'CHAR' (the same as case expression), got 'INTEGER'");
  625. },
  626. "WHILE statement": function(){
  627. var test = setupWithContext(
  628. Grammar.statement
  629. , "VAR b1: BOOLEAN; i1: INTEGER;");
  630. test.parse("WHILE TRUE DO i1 := 0 END");
  631. test.parse("WHILE b1 DO i1 := 0 ELSIF FALSE DO i1 := 1 END");
  632. test.expectError("WHILE i1 DO i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'");
  633. test.expectError("WHILE b1 DO i1 := 0 ELSIF i1 DO i1 := 1 END", "'BOOLEAN' expression expected, got 'INTEGER'");
  634. },
  635. "REPEAT statement": function(){
  636. var test = setupWithContext(
  637. Grammar.statement
  638. , "VAR b1: BOOLEAN; i1: INTEGER;");
  639. test.parse("REPEAT i1 := 0 UNTIL TRUE");
  640. test.parse("REPEAT i1 := 0 UNTIL b1");
  641. test.expectError("REPEAT i1 := 0 UNTIL i1", "'BOOLEAN' expression expected, got 'INTEGER'");
  642. },
  643. "FOR statement": function(){
  644. var test = setupWithContext(
  645. Grammar.statement
  646. , "CONST c = 15; VAR b: BOOLEAN; i, n: INTEGER;");
  647. test.parse("FOR i := 0 TO 10 DO n := 1 END");
  648. test.parse("FOR i := 0 TO 10 BY 5 DO b := TRUE END");
  649. test.parse("FOR i := 0 TO n DO b := TRUE END");
  650. test.parse("FOR i := 0 TO n BY c DO n := 1; b := FALSE END");
  651. test.expectError("FOR undefined := 0 TO 10 DO n := 1 END"
  652. , "undeclared identifier: 'undefined'");
  653. test.expectError("FOR b := TRUE TO 10 DO n := 1 END"
  654. , "'b' is a 'BOOLEAN' variable, 'FOR' control variable must be 'INTEGER'");
  655. test.expectError("FOR c := 0 TO 10 DO END", "'c' is not a variable");
  656. test.expectError("FOR i := TRUE TO 10 DO n := 1 END"
  657. , "'INTEGER' expression expected to assign 'i', got 'BOOLEAN'");
  658. test.expectError("FOR i := 0 TO TRUE DO END"
  659. , "'INTEGER' expression expected as 'TO' parameter, got 'BOOLEAN'");
  660. test.expectError("FOR i := 0 TO 10 BY n DO END"
  661. , "constant expression expected as 'BY' parameter");
  662. test.expectError("FOR i := 0 TO 10 BY TRUE DO END"
  663. , "'INTEGER' expression expected as 'BY' parameter, got 'BOOLEAN'");
  664. test.expectError("FOR i := 0 TO 10 DO - END"
  665. , "END expected (FOR)");
  666. },
  667. "logical operators": function(){
  668. var test = setupWithContext(
  669. Grammar.statement, "VAR b1, b2: BOOLEAN; i1: INTEGER;");
  670. test.parse("b1 := b1 OR b2");
  671. test.parse("b1 := b1 & b2");
  672. test.parse("b1 := ~b2");
  673. test.expectError("b1 := i1 OR b2", "BOOLEAN expected as operand of 'OR', got 'INTEGER'");
  674. test.expectError("b1 := b1 OR i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'");
  675. test.expectError("b1 := i1 & b2", "BOOLEAN expected as operand of '&', got 'INTEGER'");
  676. test.expectError("b1 := b1 & i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'");
  677. test.expectError("b1 := ~i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'");
  678. },
  679. "arithmetic operators": function(){
  680. var test = setupWithContext(
  681. Grammar.statement,
  682. "VAR b1: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL; c1: CHAR; s1: SET;" +
  683. "p1: PROCEDURE; ptr1: POINTER TO RECORD END;");
  684. test.parse("i1 := i1 + i2");
  685. test.parse("i1 := i1 - i2");
  686. test.parse("i1 := i1 * i2");
  687. test.parse("i1 := i1 DIV i2");
  688. test.parse("i1 := i1 MOD i2");
  689. test.expectError("i1 := i1 / i2", "operator DIV expected for integer division");
  690. test.expectError("r1 := r1 DIV r1", "operator 'DIV' type mismatch: INTEGER expected, got 'REAL'");
  691. test.parse("r1 := r1 + r2");
  692. test.parse("r1 := r1 - r2");
  693. test.parse("r1 := r1 * r2");
  694. test.parse("r1 := r1 / r2");
  695. test.expectError("b1 := b1 + b1", "operator '+' type mismatch: numeric type expected, got 'BOOLEAN'");
  696. test.expectError("c1 := c1 - c1", "operator '-' type mismatch: numeric type expected, got 'CHAR'");
  697. test.expectError("p1 := p1 * p1", "operator '*' type mismatch: numeric type expected, got 'PROCEDURE'");
  698. test.expectError("ptr1 := ptr1 / ptr1", "operator '/' type mismatch: numeric type expected, got 'POINTER TO anonymous RECORD'");
  699. test.expectError("s1 := +s1", "operator '+' type mismatch: numeric type expected, got 'SET'");
  700. test.expectError("b1 := -b1", "operator '-' type mismatch: numeric type expected, got 'BOOLEAN'");
  701. test.expectError("s1 := +b1", "operator '+' type mismatch: numeric type expected, got 'BOOLEAN'");
  702. },
  703. "relations are BOOLEAN": function(){
  704. var test = setupWithContext(
  705. Grammar.statement
  706. , "TYPE Base = RECORD END; Derived = RECORD (Base) END;"
  707. + "VAR pBase: POINTER TO Base; proc1, proc2: PROCEDURE;"
  708. + "set1, set2: SET;"
  709. + "b: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL; c1, c2: CHAR; ca1, ca2: ARRAY 10 OF CHAR;");
  710. test.parse("b := pBase IS Derived");
  711. test.parse("b := pBase = pBase");
  712. test.parse("b := proc1 # proc2");
  713. test.parse("b := set1 <= set2");
  714. test.parse("b := i1 IN set2");
  715. test.parse("b := i1 < i2");
  716. test.parse("IF i1 > i2 THEN END");
  717. test.parse("b := c1 > c2");
  718. test.parse("b := ca1 <= ca2");
  719. test.parse("b := r1 >= r2");
  720. },
  721. "SET relations": function(){
  722. var test = setupWithContext(
  723. Grammar.expression
  724. , "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;");
  725. test.parse("set1 <= set2");
  726. test.parse("set1 >= set2");
  727. test.parse("set1 = set2");
  728. test.parse("set1 # set2");
  729. test.parse("i IN set1");
  730. test.expectError("set1 <= i", "type mismatch: expected 'SET', got 'INTEGER'");
  731. test.expectError("b IN set1", "'INTEGER' expected as an element of SET, got 'BOOLEAN'");
  732. test.expectError("i IN b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  733. },
  734. "SET operators": function(){
  735. var test = setupWithContext(
  736. Grammar.expression
  737. , "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;");
  738. test.parse("set1 + set2");
  739. test.parse("set1 - set2");
  740. test.parse("set1 * set2");
  741. test.parse("set1 / set2");
  742. test.parse("-set1");
  743. test.expectError("set1 + i", "type mismatch: expected 'SET', got 'INTEGER'");
  744. test.expectError("set1 - b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  745. test.expectError("set1 * b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  746. test.expectError("set1 / b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  747. },
  748. "SET functions": testWithContext(
  749. context(Grammar.statement,
  750. "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;"),
  751. pass("INCL(set1, 0)",
  752. "EXCL(set1, 3)"),
  753. fail(["INCL({}, i)", "expression cannot be used as VAR parameter"],
  754. ["INCL(set1, i)", "constant (0..31) expected as second argument of INCL"],
  755. ["EXCL(set1, i)", "constant (0..31) expected as second argument of EXCL"],
  756. ["INCL(set1, 32)", "constant (0..31) expected as second argument of INCL"],
  757. ["EXCL(set1, -1)", "constant (0..31) expected as second argument of EXCL"]
  758. )
  759. ),
  760. "procedure body": function(){
  761. var test = setup(Grammar.procedureBody);
  762. test.parse("END");
  763. test.parse("VAR END");
  764. test.parse("VAR i: INTEGER; END");
  765. test.parse("VAR a: ARRAY 10 OF INTEGER; END");
  766. test.expectError("VAR i: INTEGER;", "END expected (PROCEDURE)");
  767. test.expectError("VAR i: INTEGER; i := 1; END", "END expected (PROCEDURE)");
  768. test.parse("VAR i: INTEGER; BEGIN i := 1 END");
  769. test.parse("VAR b: BOOLEAN; BEGIN b := TRUE END");
  770. test.expectError("VAR i: INTEGER; BEGIN j := 1 END", "undeclared identifier: 'j'");
  771. test.expectError("VAR i: INTEGER; BEGIN i.field := 1 END",
  772. "cannot designate 'INTEGER'");
  773. test.expectError("VAR i: INTEGER; BEGIN i := j END", "undeclared identifier: 'j'");
  774. test.parse("VAR i, j: INTEGER; BEGIN i := 1; j := 2; i := 1 + i + j - 2 END");
  775. test.expectError("TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v := 1 END"
  776. , "type mismatch: 'v' is 'T' and cannot be assigned to 'INTEGER' expression");
  777. test.expectError("TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.unknown := 1 END"
  778. , "Type 'T' has no 'unknown' field");
  779. test.parse("TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.field := 1 END");
  780. test.parse("TYPE T1 = RECORD field: INTEGER END; T2 = RECORD field: T1 END; VAR v1: T1; v2: T2; BEGIN v1.field := v2.field.field END");
  781. test.parse("TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field2: INTEGER END; VAR v: T2; BEGIN v.field2 := v.field1 END");
  782. test.expectError("TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field1: INTEGER END; END"
  783. , "base record already has field: 'field1'");
  784. },
  785. "procedure heading": function(){
  786. function makeContext(cx){return new Context.ProcDecl(new Context.Context(cx));}
  787. var test = setup(Grammar.procedureHeading, makeContext);
  788. test.parse("PROCEDURE p");
  789. test.parse("PROCEDURE p(a1: INTEGER)");
  790. test.parse("PROCEDURE p(a1, a2: INTEGER; b1: BOOLEAN)");
  791. test.expectError("PROCEDURE p(a1: INTEGER; a1: BOOLEAN)", "'a1' already declared");
  792. test.expectError("PROCEDURE p(p: INTEGER)", "argument 'p' has the same name as procedure");
  793. },
  794. procedure: function(){
  795. var test = setupWithContext(Grammar.procedureDeclaration
  796. , "TYPE ProcType = PROCEDURE(): ProcType;");
  797. test.parse("PROCEDURE p; END p");
  798. test.expectError("PROCEDURE p; END", "not parsed");
  799. test.expectError("PROCEDURE p1; END p2"
  800. , "mismatched procedure names: 'p1' at the begining and 'p2' at the end");
  801. test.parse("PROCEDURE p; VAR i: INTEGER; BEGIN i := i + 1 END p");
  802. test.parse("PROCEDURE p(a: INTEGER); BEGIN a := a + 1 END p");
  803. test.expectError("PROCEDURE p(a: INTEGER); VAR a: INTEGER END p", "'a' already declared");
  804. test.parse("PROCEDURE p; BEGIN p() END p");
  805. test.expectError("PROCEDURE p(a: INTEGER); BEGIN p() END p", "1 argument(s) expected, got 0");
  806. test.expectError("PROCEDURE p(a: INTEGER); BEGIN p(1, 2) END p", "1 argument(s) expected, got 2");
  807. test.parse("PROCEDURE p(a: INTEGER); BEGIN p(a) END p");
  808. test.parse("PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(a, b) END p");
  809. test.expectError("PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(b, a) END p"
  810. , "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'");
  811. test.expectError("PROCEDURE p; BEGIN p1() END p", "undeclared identifier: 'p1'");
  812. test.parse("PROCEDURE p(): ProcType; RETURN p END p");
  813. },
  814. "procedure RETURN": testWithContext(
  815. context(Grammar.procedureDeclaration,
  816. "VAR i: INTEGER; PROCEDURE int(): INTEGER; RETURN 1 END int;"),
  817. pass("PROCEDURE p(): BOOLEAN; RETURN TRUE END p",
  818. "PROCEDURE p(): BOOLEAN; RETURN int() = 1 END p",
  819. "PROCEDURE p; BEGIN END p" ,
  820. "PROCEDURE p(): INTEGER; BEGIN RETURN 0 END p"),
  821. fail(["PROCEDURE p; RETURN TRUE END p", "unexpected RETURN in PROCEDURE declared with no result type"],
  822. ["PROCEDURE p(): BOOLEAN; END p", "RETURN expected at the end of PROCEDURE declared with 'BOOLEAN' result type"],
  823. ["PROCEDURE p(): undeclared; END p", "undeclared identifier: 'undeclared'"],
  824. ["PROCEDURE p(): i; END p", "type name expected"],
  825. ["PROCEDURE p(): INTEGER; RETURN TRUE END p", "RETURN 'INTEGER' expected, got 'BOOLEAN'"]
  826. )
  827. ),
  828. "PROCEDURE relations": testWithContext(
  829. context(Grammar.expression,
  830. "VAR p1: PROCEDURE; p2: PROCEDURE;"),
  831. pass("p1 = p2",
  832. "p1 # p2",
  833. "p1 = NIL",
  834. "NIL # p1"
  835. ),
  836. fail()
  837. ),
  838. "pass VAR argument as VAR parameter": function(){
  839. var test = setupWithContext(Grammar.procedureDeclaration,
  840. "PROCEDURE p1(VAR i: INTEGER); END p1;"
  841. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"
  842. );
  843. test.parse("PROCEDURE p(VAR i1: INTEGER); BEGIN p1(i1) END p");
  844. test.expectError("PROCEDURE p(VAR b: BOOLEAN); BEGIN p2(~b) END p", "expression cannot be used as VAR parameter");
  845. },
  846. "VAR parameter": function(){
  847. var test = setupWithContext(Grammar.statement
  848. , "CONST c = 123;"
  849. + "VAR i1: INTEGER; b1: BOOLEAN; a1: ARRAY 5 OF INTEGER;"
  850. + "r1: RECORD f1: INTEGER END;"
  851. + "PROCEDURE p1(VAR i: INTEGER); END p1;"
  852. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"
  853. );
  854. test.parse("p1(i1)");
  855. test.parse("p1(a1[0])");
  856. test.parse("p1(r1.f1)");
  857. test.expectError("p1(c)", "constant cannot be used as VAR parameter");
  858. test.expectError("p1(123)", "expression cannot be used as VAR parameter");
  859. test.expectError("p2(TRUE)", "expression cannot be used as VAR parameter");
  860. test.expectError("p1(i1 + i1)", "expression cannot be used as VAR parameter");
  861. test.expectError("p1(i1 * i1)", "expression cannot be used as VAR parameter");
  862. test.expectError("p1(+i1)", "expression cannot be used as VAR parameter");
  863. test.expectError("p1(-i1)", "expression cannot be used as VAR parameter");
  864. test.expectError("p2(~b1)", "expression cannot be used as VAR parameter");
  865. },
  866. "ARRAY parameter": function(){
  867. var test = setupWithContext(Grammar.procedureDeclaration
  868. , "TYPE T = RECORD i: INTEGER; p: POINTER TO T END;"
  869. + "PROCEDURE p1(i: INTEGER); END p1;"
  870. + "PROCEDURE varInteger(VAR i: INTEGER); END varInteger;"
  871. + "PROCEDURE p2(a: ARRAY OF INTEGER); END p2;"
  872. + "PROCEDURE p3(VAR a: ARRAY OF INTEGER); END p3;"
  873. );
  874. test.parse("PROCEDURE p(a: ARRAY OF INTEGER); END p");
  875. test.parse("PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); END p");
  876. test.parse("PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN p1(a[0][0]) END p");
  877. test.parse("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p2(a) END p");
  878. test.parse("PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].p.i) END p");
  879. test.expectError("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN a[0] := 0 END p",
  880. "cannot assign to read-only variable");
  881. test.expectError("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p3(a) END p",
  882. "read-only variable cannot be used as VAR parameter");
  883. test.expectError("PROCEDURE p(a: ARRAY OF T); BEGIN a[0].i := 0 END p",
  884. "cannot assign to read-only variable");
  885. test.expectError("PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].i) END p",
  886. "read-only variable cannot be used as VAR parameter");
  887. },
  888. "procedure call": testWithContext(
  889. context(Grammar.statement,
  890. "TYPE ProcType = PROCEDURE;" +
  891. "VAR notProcedure: INTEGER;" +
  892. "PROCEDURE p; END p;" +
  893. "PROCEDURE p1(i: INTEGER); END p1;" +
  894. "PROCEDURE p2(i: INTEGER; b: BOOLEAN); END p2;" +
  895. "PROCEDURE p3(): ProcType; RETURN p END p3;"),
  896. pass("p",
  897. "p()",
  898. "p1(1)",
  899. "p1(1 + 2)",
  900. "p2(1, TRUE)"),
  901. fail(["notProcedure", "PROCEDURE expected, got 'INTEGER'"],
  902. ["p2(TRUE, 1)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
  903. ["p2(1, 1)", "type mismatch for argument 2: 'INTEGER' cannot be converted to 'BOOLEAN'"],
  904. ["p()()", "not parsed"],
  905. ["p3", "procedure returning a result cannot be used as a statement"],
  906. ["p3()", "procedure returning a result cannot be used as a statement"]
  907. )
  908. ),
  909. "procedure assignment": function(){
  910. var test = setupWithContext(
  911. Grammar.statement
  912. , "TYPE ProcType1 = PROCEDURE(): ProcType1;"
  913. + "ProcType2 = PROCEDURE(): ProcType2;"
  914. + "ProcType3 = PROCEDURE(p: ProcType3): ProcType3;"
  915. + "ProcType4 = PROCEDURE(p: ProcType4): ProcType4;"
  916. + "ProcType4VAR = PROCEDURE(VAR p: ProcType4VAR): ProcType4VAR;"
  917. + "ProcType5 = PROCEDURE(p: ProcType3): ProcType4;"
  918. + "ProcType6 = PROCEDURE(p: INTEGER);"
  919. + "ProcType7 = PROCEDURE(VAR p: INTEGER);"
  920. + "VAR v1: ProcType1; v2: ProcType2;"
  921. + "v3: PROCEDURE(i: INTEGER): ProcType1; v4: PROCEDURE(b: BOOLEAN): ProcType1;"
  922. + "v5: PROCEDURE(p: ProcType1); v6: PROCEDURE(p: ProcType2);"
  923. + "v7: ProcType3; v8: ProcType4; v8VAR: ProcType4VAR; v9: ProcType5; v10: ProcType6; v11: ProcType7;"
  924. + "PROCEDURE p1(): ProcType1; RETURN p1 END p1;"
  925. );
  926. test.parse("v1 := v2");
  927. test.parse("v5 := v6");
  928. test.parse("v7 := v8");
  929. test.parse("v7 := v9");
  930. test.parse("v8 := v9");
  931. test.parse("v1 := p1");
  932. test.expectError("p1 := v1", "cannot assign to procedure" );
  933. test.expectError(
  934. "v3 := v1"
  935. , "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'ProcType1' expression");
  936. test.expectError(
  937. "v3 := v4"
  938. , "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'PROCEDURE(BOOLEAN): ProcType1' expression");
  939. test.expectError(
  940. "v10 := NEW"
  941. , "type mismatch: 'v10' is 'ProcType6' and cannot be assigned to 'predefined procedure NEW' expression");
  942. test.expectError("v10 := v11", "type mismatch: 'v10' is 'ProcType6' and cannot be assigned to 'ProcType7' expression" );
  943. test.expectError("v8 := v8VAR", "type mismatch: 'v8' is 'ProcType4' and cannot be assigned to 'ProcType4VAR' expression" );
  944. },
  945. "string assignment": function(){
  946. var test = setupWithContext(
  947. Grammar.statement
  948. , "VAR a1: ARRAY 3 OF CHAR;"
  949. + "ch1: CHAR;"
  950. + "intArray: ARRAY 10 OF INTEGER;"
  951. );
  952. test.parse("a1 := \"abc\"");
  953. test.parse("a1 := \"ab\"");
  954. test.parse("a1 := \"a\"");
  955. test.parse("a1 := 22X");
  956. test.parse("ch1 := \"A\"");
  957. test.parse("ch1 := 22X");
  958. test.expectError("a1 := \"abcd\"", "3-character ARRAY is too small for 4-character string");
  959. test.expectError("intArray := \"abcd\""
  960. , "type mismatch: 'intArray' is 'ARRAY OF INTEGER' and cannot be assigned to 'multi-character string' expression");
  961. },
  962. "string relations": testWithContext(
  963. context(Grammar.expression,
  964. "VAR ch: CHAR;"),
  965. pass("ch = \"a\"",
  966. "\"a\" = ch",
  967. "ch # \"a\"",
  968. "\"a\" # ch"
  969. ),
  970. fail(["ch = \"ab\"", "type mismatch: expected 'CHAR', got 'multi-character string'"])),
  971. "array assignment": function(){
  972. var test = setupWithContext(
  973. Grammar.statement
  974. , "VAR charArray: ARRAY 3 OF CHAR;"
  975. + "intArray: ARRAY 10 OF INTEGER;"
  976. + "intArray2: ARRAY 10 OF INTEGER;"
  977. + "intArray3: ARRAY 5 OF INTEGER;"
  978. );
  979. test.parse("intArray := intArray2");
  980. test.expectError("intArray := charArray"
  981. , "type mismatch: 'intArray' is 'ARRAY OF INTEGER' and cannot be assigned to 'ARRAY OF CHAR' expression");
  982. test.expectError("intArray2 := intArray3"
  983. , "array size mismatch: 'intArray2' has size 10 and cannot be copied to the array with size 5");
  984. test.expectError("intArray3 := charArray"
  985. , "type mismatch: 'intArray3' is 'ARRAY OF INTEGER' and cannot be assigned to 'ARRAY OF CHAR' expression");
  986. },
  987. "record assignment": function(){
  988. var test = setupWithContext(
  989. Grammar.statement
  990. , "TYPE Base1 = RECORD END;"
  991. + "T1 = RECORD (Base1) END;"
  992. + "T2 = RECORD END;"
  993. + "VAR b1: Base1; r1: T1; r2: T2;"
  994. );
  995. test.parse("r1 := r1");
  996. test.parse("b1 := r1");
  997. test.expectError("r1 := r2", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'T2' expression");
  998. test.expectError("r1 := b1", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'Base1' expression");
  999. },
  1000. "open Array assignment fails": function(){
  1001. var test = setup(Grammar.procedureDeclaration);
  1002. test.expectError("PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p"
  1003. , "cannot assign to read-only variable");
  1004. test.expectError("PROCEDURE p(VAR s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p"
  1005. , "'s1' is open 'ARRAY OF CHAR' and cannot be assigned");
  1006. test.expectError("PROCEDURE p(s1: ARRAY OF CHAR); VAR s2: ARRAY 10 OF CHAR; BEGIN s2 := s1 END p"
  1007. , "'s2' cannot be assigned to open 'ARRAY OF CHAR'");
  1008. },
  1009. "string assignment to open array fails": function(){
  1010. var test = setup(Grammar.procedureDeclaration);
  1011. test.expectError("PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "cannot assign to read-only variable");
  1012. test.expectError("PROCEDURE p(VAR s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "string cannot be assigned to open ARRAY OF CHAR");
  1013. },
  1014. "string argument": function(){
  1015. var test = setupWithContext(
  1016. Grammar.statement
  1017. , "PROCEDURE p1(s: ARRAY OF CHAR); END p1;"
  1018. + "PROCEDURE p2(VAR s: ARRAY OF CHAR); END p2;"
  1019. + "PROCEDURE p3(i: INTEGER); END p3;"
  1020. + "PROCEDURE p4(a: ARRAY OF INTEGER); END p4;"
  1021. );
  1022. test.parse("p1(\"abc\")");
  1023. test.expectError("p2(\"abc\")", "expression cannot be used as VAR parameter");
  1024. test.expectError("p3(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'INTEGER'");
  1025. test.expectError("p4(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'ARRAY OF INTEGER'");
  1026. },
  1027. "scope": function(){
  1028. var test = setup(Grammar.declarationSequence);
  1029. test.parse("PROCEDURE p1(a1: INTEGER); END p1; PROCEDURE p2(a1: BOOLEAN); END p2;");
  1030. },
  1031. module: function(){
  1032. var test = setup(Grammar.module);
  1033. test.parse("MODULE m; END m.");
  1034. test.expectError("MODULE m; END undeclared.",
  1035. "original module name 'm' expected, got 'undeclared'");
  1036. test.expectError("MODULE m; BEGIN - END m.", "END expected (MODULE)");
  1037. },
  1038. assert: function(){
  1039. var test = setup(Grammar.statement);
  1040. test.parse("ASSERT(TRUE)");
  1041. test.parse("ASSERT(TRUE, 123)");
  1042. test.expectError("ASSERT()", "at least 1 argument expected, got 0");
  1043. test.expectError("ASSERT(123, TRUE)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'BOOLEAN'");
  1044. },
  1045. IMPORT: function(){
  1046. var test = setup(Grammar.module);
  1047. test.parse("MODULE m; IMPORT JS; END m.");
  1048. test.parse("MODULE m; IMPORT JS; BEGIN JS.alert(\"test\") END m.");
  1049. test.parse("MODULE m; IMPORT JS; BEGIN JS.console.info(123) END m.");
  1050. test.expectError("MODULE m; IMPORT unknown; END m.", "module(s) not found: unknown");
  1051. test.expectError("MODULE m; IMPORT unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2");
  1052. test.expectError("MODULE m; IMPORT u1 := unknown1, unknown2; END m.", "module(s) not found: unknown1, unknown2");
  1053. }};
  1054. Test.run(testSuite);