test_unit.js 33 KB

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