test_unit.js 39 KB

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