test_unit.js 38 KB

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