2
0

test_unit.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. ,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. "LEN": function(){
  301. var test = setup(Grammar.procedureDeclaration);
  302. test.parse("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a) END p");
  303. test.parse("PROCEDURE p(VAR a: ARRAY OF BOOLEAN): INTEGER; RETURN LEN(a) END p");
  304. test.parse("PROCEDURE p(): INTEGER; RETURN LEN(\"abc\") END p");
  305. test.expectError("PROCEDURE p(a: ARRAY OF INTEGER): INTEGER; RETURN LEN(a[0]) END p",
  306. "type mismatch for argument 1: 'INTEGER' cannot be converted to 'ARRAY OF any type'");
  307. },
  308. "ODD": function(){
  309. var test = setup(Grammar.statement);
  310. test.parse("ODD(1)");
  311. test.parse("ASSERT(ODD(123))");
  312. test.expectError("ODD(1.2)", "type mismatch for argument 1: 'REAL' cannot be converted to 'INTEGER'");
  313. test.expectError("ODD(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'");
  314. },
  315. "ORD": function(){
  316. var test = setupWithContext(Grammar.statement, "VAR ch: CHAR;");
  317. test.parse("ORD(ch)");
  318. test.parse("ORD(TRUE)");
  319. test.parse("ORD({1})");
  320. test.parse("ORD(\"a\")");
  321. test.parse("ASSERT(ORD(22X) = 022H)");
  322. test.expectError("ORD(1.2)", "type mismatch for argument 1: 'REAL' cannot be converted to 'CHAR or BOOLEAN or SET'");
  323. test.expectError("ORD(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'CHAR or BOOLEAN or SET'");
  324. },
  325. "assignment statement": function(){
  326. var test = setupWithContext(
  327. Grammar.statement
  328. , "CONST c = 15;"
  329. + "VAR ch: CHAR; i, n: INTEGER; b: BOOLEAN;"
  330. + "proc1: PROCEDURE; proc2: PROCEDURE(): INTEGER;"
  331. + "a: ARRAY 5 OF INTEGER;"
  332. + "PROCEDURE p(): INTEGER; RETURN 1 END p;"
  333. + "PROCEDURE noResult(); END noResult;");
  334. test.parse("i := 0");
  335. test.parse("i := n");
  336. test.parse("i := c");
  337. test.parse("b := TRUE");
  338. test.parse("ch := \"A\"");
  339. test.parse("i := p()");
  340. test.parse("proc1 := proc1");
  341. test.parse("proc2 := NIL");
  342. test.parse("a[1] := 2");
  343. test.expectError("i := b", "type mismatch: 'i' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression");
  344. test.expectError("c := i", "cannot assign to constant");
  345. test.expectError("ch := \"AB\""
  346. , "type mismatch: 'ch' is 'CHAR' and cannot be assigned to 'multi-character string' expression");
  347. test.expectError("i := .1", "expression expected");
  348. test.expectError("proc1 := proc2"
  349. , "type mismatch: 'proc1' is 'PROCEDURE' and cannot be assigned to 'PROCEDURE(): INTEGER' expression");
  350. test.expectError("i := noResult()", "procedure returning no result cannot be used in an expression");
  351. },
  352. "array expression": function(){
  353. var test = setup(Grammar.procedureBody);
  354. test.parse("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1 END");
  355. test.parse("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := 1; a[1] := a[0] END");
  356. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0] := TRUE END"
  357. , "type mismatch: 'a[0]' is 'INTEGER' and cannot be assigned to 'BOOLEAN' expression");
  358. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[TRUE] := 1 END"
  359. , "'INTEGER' expression expected, got 'BOOLEAN'");
  360. test.expectError("VAR i: INTEGER; BEGIN i[0] := 1 END"
  361. , "ARRAY expected, got 'INTEGER'");
  362. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[0][0] := 1 END"
  363. , "ARRAY expected, got 'INTEGER'");
  364. test.expectError("VAR a: ARRAY 10 OF BOOLEAN; BEGIN a[0,0] := TRUE END"
  365. , "ARRAY expected, got 'BOOLEAN'");
  366. test.expectError("VAR a: ARRAY 10, 20 OF BOOLEAN; BEGIN a[0] := TRUE END"
  367. , "type mismatch: 'a[0]' is 'ARRAY OF BOOLEAN' and cannot be assigned to 'BOOLEAN' expression");
  368. test.expectError("VAR a: ARRAY 10 OF INTEGER; BEGIN a[10] := 0 END"
  369. , "index out of bounds: maximum possible index is 9, got 10");
  370. test.expectError("CONST c1 = 5; VAR a: ARRAY 10 OF INTEGER; BEGIN a[10 + c1] := 0 END"
  371. , "index out of bounds: maximum possible index is 9, got 15");
  372. },
  373. "multi-dimensional array expression": function(){
  374. var test = setup(Grammar.procedureBody);
  375. test.parse("VAR a: ARRAY 10 OF ARRAY 5 OF INTEGER; BEGIN a[0][0] := 1 END");
  376. test.parse("VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0][0] := TRUE END");
  377. test.parse("VAR a: ARRAY 10, 5 OF BOOLEAN; BEGIN a[0, 0] := TRUE END");
  378. },
  379. "INTEGER number": function(){
  380. var test = setup(Grammar.expression);
  381. test.parse("0");
  382. test.parse("123");
  383. test.parse("1H");
  384. test.parse("1FH");
  385. test.parse("0FFH");
  386. test.parse("0H");
  387. test.expectError("FFH", "undeclared identifier: 'FFH'");
  388. test.expectError("FF", "undeclared identifier: 'FF'");
  389. test.expectError("1HH", "not parsed");
  390. test.expectError("1H0", "not parsed");
  391. test.expectError("1 23", "not parsed");
  392. test.expectError("1F FH", "not parsed");
  393. },
  394. "SET statement": function(){
  395. var test = setupWithContext(Grammar.statement, "VAR s: SET;");
  396. test.parse("s := {}");
  397. test.parse("s := {0}");
  398. test.parse("s := {0, 1}");
  399. test.parse("s := {1 + 2, 5..10}");
  400. //test.expectError("s := {32}", "0..31");
  401. },
  402. "REAL number": function(){
  403. var test = setup(Grammar.expression);
  404. test.parse("1.2345");
  405. test.parse("1.");
  406. test.parse("1.2345E6");
  407. test.parse("1.2345E+6");
  408. test.parse("1.2345E-12");
  409. test.expectError("1. 2345E-12", "not parsed");
  410. test.expectError("1.23 45E-12", "not parsed");
  411. test.expectError("1.2345 E-12", "not parsed");
  412. test.expectError("1.2345E-1 2", "not parsed");
  413. },
  414. "LONGREAL number": function(){
  415. var test = setup(Grammar.expression);
  416. test.parse("1.2345D6");
  417. test.parse("1.2345D+6");
  418. test.parse("1.2345D-6");
  419. },
  420. "IF statement": function(){
  421. var test = setupWithContext(
  422. Grammar.statement
  423. , "VAR b1: BOOLEAN; i1: INTEGER;");
  424. test.parse("IF b1 THEN i1 := 0 END");
  425. test.parse("IF FALSE THEN i1 := 0 ELSE i1 := 1 END");
  426. test.parse("IF TRUE THEN i1 := 0 ELSIF FALSE THEN i1 := 1 ELSE i1 := 2 END");
  427. test.expectError("IF i1 THEN i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'");
  428. test.expectError("IF b1 THEN i1 := 0 ELSIF i1 THEN i1 := 2 END"
  429. , "'BOOLEAN' expression expected, got 'INTEGER'");
  430. },
  431. "CASE statement": function(){
  432. var test = setupWithContext(
  433. Grammar.statement
  434. , "CONST ci = 15; cc = \"A\"; VAR c1: CHAR; b1: BOOLEAN; i1, i2: INTEGER;");
  435. test.parse("CASE i1 OF END");
  436. test.parse("CASE i1 OF 0: b1 := TRUE END");
  437. test.parse("CASE c1 OF \"A\": b1 := TRUE END");
  438. test.parse("CASE i1 OF 0: b1 := TRUE | 1: b1 := FALSE END");
  439. test.parse("CASE i1 OF 0, 1: b1 := TRUE END");
  440. test.parse("CASE c1 OF \"A\", \"B\": b1 := TRUE END");
  441. test.parse("CASE i1 OF 0..2: b1 := TRUE END");
  442. test.parse("CASE i1 OF ci..2: b1 := TRUE END");
  443. test.parse("CASE c1 OF cc..\"Z\": b1 := TRUE END");
  444. test.parse("CASE i1 OF 1, 2, 3: b1 := TRUE | 4..10: b1 := FALSE | 11: c1 := \"A\" END");
  445. test.parse("CASE i1 OF 1, 2, 5..9: b1 := TRUE END");
  446. test.expectError("CASE i1 OF undefined: b1 := TRUE END"
  447. , "undeclared identifier: 'undefined'");
  448. test.expectError("CASE i1 OF i2: b1 := TRUE END"
  449. , "'i2' is not a constant");
  450. test.expectError("CASE b1 OF END", "'INTEGER' or 'CHAR' expected as CASE expression");
  451. test.expectError("CASE i1 OF \"A\": b1 := TRUE END"
  452. , "label must be 'INTEGER' (the same as case expression), got 'CHAR'");
  453. test.expectError("CASE c1 OF \"A\", 1: b1 := TRUE END"
  454. , "label must be 'CHAR' (the same as case expression), got 'INTEGER'");
  455. test.expectError("CASE c1 OF \"A\"..1: b1 := TRUE END"
  456. , "label must be 'CHAR' (the same as case expression), got 'INTEGER'");
  457. },
  458. "WHILE statement": function(){
  459. var test = setupWithContext(
  460. Grammar.statement
  461. , "VAR b1: BOOLEAN; i1: INTEGER;");
  462. test.parse("WHILE TRUE DO i1 := 0 END");
  463. test.parse("WHILE b1 DO i1 := 0 ELSIF FALSE DO i1 := 1 END");
  464. test.expectError("WHILE i1 DO i1 := 0 END", "'BOOLEAN' expression expected, got 'INTEGER'");
  465. test.expectError("WHILE b1 DO i1 := 0 ELSIF i1 DO i1 := 1 END", "'BOOLEAN' expression expected, got 'INTEGER'");
  466. },
  467. "REPEAT statement": function(){
  468. var test = setupWithContext(
  469. Grammar.statement
  470. , "VAR b1: BOOLEAN; i1: INTEGER;");
  471. test.parse("REPEAT i1 := 0 UNTIL TRUE");
  472. test.parse("REPEAT i1 := 0 UNTIL b1");
  473. test.expectError("REPEAT i1 := 0 UNTIL i1", "'BOOLEAN' expression expected, got 'INTEGER'");
  474. },
  475. "FOR statement": function(){
  476. var test = setupWithContext(
  477. Grammar.statement
  478. , "CONST c = 15; VAR b: BOOLEAN; i, n: INTEGER;");
  479. test.parse("FOR i := 0 TO 10 DO n := 1 END");
  480. test.parse("FOR i := 0 TO 10 BY 5 DO b := TRUE END");
  481. test.parse("FOR i := 0 TO n DO b := TRUE END");
  482. test.parse("FOR i := 0 TO n BY c DO n := 1; b := FALSE END");
  483. test.expectError("FOR undefined := 0 TO 10 DO n := 1 END"
  484. , "undeclared identifier: 'undefined'");
  485. test.expectError("FOR b := TRUE TO 10 DO n := 1 END"
  486. , "'b' is a 'BOOLEAN' variable, 'FOR' control variable must be 'INTEGER'");
  487. test.expectError("FOR c := 0 TO 10 DO END", "'c' is not a variable");
  488. test.expectError("FOR i := TRUE TO 10 DO n := 1 END"
  489. , "'INTEGER' expression expected to assign 'i', got 'BOOLEAN'");
  490. test.expectError("FOR i := 0 TO TRUE DO END"
  491. , "'INTEGER' expression expected as 'TO' parameter, got 'BOOLEAN'");
  492. test.expectError("FOR i := 0 TO 10 BY n DO END"
  493. , "constant expression expected as 'BY' parameter");
  494. test.expectError("FOR i := 0 TO 10 BY TRUE DO END"
  495. , "'INTEGER' expression expected as 'BY' parameter, got 'BOOLEAN'");
  496. },
  497. "logical operators": function(){
  498. var test = setupWithContext(
  499. Grammar.statement, "VAR b1, b2: BOOLEAN; i1: INTEGER;");
  500. test.parse("b1 := b1 OR b2");
  501. test.parse("b1 := b1 & b2");
  502. test.parse("b1 := ~b2");
  503. test.expectError("b1 := i1 OR b2", "BOOLEAN expected as operand of 'OR', got 'INTEGER'");
  504. test.expectError("b1 := b1 OR i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'");
  505. test.expectError("b1 := i1 & b2", "BOOLEAN expected as operand of '&', got 'INTEGER'");
  506. test.expectError("b1 := b1 & i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'");
  507. test.expectError("b1 := ~i1", "type mismatch: expected 'BOOLEAN', got 'INTEGER'");
  508. },
  509. "arithmetic operators": function(){
  510. var test = setupWithContext(
  511. Grammar.statement, "VAR b1: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL;");
  512. test.parse("i1 := i1 + i2");
  513. test.parse("i1 := i1 - i2");
  514. test.parse("i1 := i1 * i2");
  515. test.parse("i1 := i1 DIV i2");
  516. test.parse("i1 := i1 MOD i2");
  517. test.parse("r1 := r1 + r2");
  518. test.parse("r1 := r1 - r2");
  519. test.parse("r1 := r1 * r2");
  520. test.parse("r1 := r1 / r2");
  521. },
  522. "relations are BOOLEAN": function(){
  523. var test = setupWithContext(
  524. Grammar.statement
  525. , "TYPE Base = RECORD END; Derived = RECORD (Base) END;"
  526. + "VAR pBase: POINTER TO Base; proc1, proc2: PROCEDURE;"
  527. + "set1, set2: SET;"
  528. + "b: BOOLEAN; i1, i2: INTEGER; r1, r2: REAL; c1, c2: CHAR; ca1, ca2: ARRAY 10 OF CHAR;");
  529. test.parse("b := pBase IS Derived");
  530. test.parse("b := pBase = pBase");
  531. test.parse("b := proc1 # proc2");
  532. test.parse("b := set1 <= set2");
  533. test.parse("b := i1 IN set2");
  534. test.parse("b := i1 < i2");
  535. test.parse("b := c1 > c2");
  536. test.parse("b := ca1 <= ca2");
  537. test.parse("b := r1 >= r2");
  538. },
  539. "SET relations": function(){
  540. var test = setupWithContext(
  541. Grammar.expression
  542. , "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;");
  543. test.parse("set1 <= set2");
  544. test.parse("set1 >= set2");
  545. test.parse("set1 = set2");
  546. test.parse("set1 # set2");
  547. test.parse("i IN set1");
  548. test.expectError("set1 <= i", "type mismatch: expected 'SET', got 'INTEGER'");
  549. test.expectError("b IN set1", "'INTEGER' expected as an element of SET, got 'BOOLEAN'");
  550. test.expectError("i IN b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  551. },
  552. "SET operators": function(){
  553. var test = setupWithContext(
  554. Grammar.expression
  555. , "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;");
  556. test.parse("set1 + set2");
  557. test.parse("set1 - set2");
  558. test.parse("set1 * set2");
  559. test.parse("set1 / set2");
  560. test.parse("-set1");
  561. test.expectError("set1 + i", "type mismatch: expected 'SET', got 'INTEGER'");
  562. test.expectError("set1 - b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  563. test.expectError("set1 * b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  564. test.expectError("set1 / b", "type mismatch: expected 'SET', got 'BOOLEAN'");
  565. },
  566. "SET functions": function(){
  567. var test = setupWithContext(
  568. Grammar.statement
  569. , "VAR set1, set2: SET; b: BOOLEAN; i: INTEGER;");
  570. test.parse("INCL(set1, i)");
  571. test.parse("EXCL(set1, i)");
  572. test.expectError("INCL({}, i)", "expression cannot be used as VAR parameter");
  573. },
  574. "procedure body": function(){
  575. var test = setup(Grammar.procedureBody);
  576. test.parse("END");
  577. test.parse("VAR END");
  578. test.parse("VAR i: INTEGER; END");
  579. test.parse("VAR a: ARRAY 10 OF INTEGER; END");
  580. test.expectError("VAR i: INTEGER;", "not parsed");
  581. test.parse("VAR i: INTEGER; BEGIN i := 1 END");
  582. test.parse("VAR b: BOOLEAN; BEGIN b := TRUE END");
  583. test.expectError("VAR i: INTEGER; BEGIN j := 1 END", "undeclared identifier: 'j'");
  584. test.expectError("VAR i: INTEGER; BEGIN i.field := 1 END",
  585. "cannot designate 'INTEGER'");
  586. test.expectError("VAR i: INTEGER; BEGIN i := j END", "undeclared identifier: 'j'");
  587. test.parse("VAR i, j: INTEGER; BEGIN i := 1; j := 2; i := 1 + i + j - 2 END");
  588. test.expectError("TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v := 1 END"
  589. , "type mismatch: 'v' is 'T' and cannot be assigned to 'INTEGER' expression");
  590. test.expectError("TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.unknown := 1 END"
  591. , "Type 'T' has no 'unknown' field");
  592. test.parse("TYPE T = RECORD field: INTEGER END; VAR v: T; BEGIN v.field := 1 END");
  593. 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");
  594. test.parse("TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field2: INTEGER END; VAR v: T2; BEGIN v.field2 := v.field1 END");
  595. test.expectError("TYPE T1 = RECORD field1: INTEGER END; T2 = RECORD (T1) field1: INTEGER END; END"
  596. , "base record already has field: 'field1'");
  597. },
  598. "procedure heading": function(){
  599. function makeContext(cx){return new Context.ProcDecl(new Context.Context(cx));}
  600. var test = setup(Grammar.procedureHeading, makeContext);
  601. test.parse("PROCEDURE p");
  602. test.parse("PROCEDURE p(a1: INTEGER)");
  603. test.parse("PROCEDURE p(a1, a2: INTEGER; b1: BOOLEAN)");
  604. test.expectError("PROCEDURE p(a1: INTEGER; a1: BOOLEAN)", "'a1' already declared");
  605. test.expectError("PROCEDURE p(p: INTEGER)", "argument 'p' has the same name as procedure");
  606. },
  607. procedure: function(){
  608. var test = setupWithContext(Grammar.procedureDeclaration
  609. , "TYPE ProcType = PROCEDURE(): ProcType;");
  610. test.parse("PROCEDURE p; END p");
  611. test.expectError("PROCEDURE p; END", "not parsed");
  612. test.expectError("PROCEDURE p1; END p2"
  613. , "mismatched procedure names: 'p1' at the begining and 'p2' at the end");
  614. test.parse("PROCEDURE p; VAR i: INTEGER; BEGIN i := i + 1 END p");
  615. test.parse("PROCEDURE p(a: INTEGER); BEGIN a := a + 1 END p");
  616. test.expectError("PROCEDURE p(a: INTEGER); VAR a: INTEGER END p", "'a' already declared");
  617. test.parse("PROCEDURE p; BEGIN p() END p");
  618. test.expectError("PROCEDURE p(a: INTEGER); BEGIN p() END p", "1 argument(s) expected, got 0");
  619. test.expectError("PROCEDURE p(a: INTEGER); BEGIN p(1, 2) END p", "1 argument(s) expected, got 2");
  620. test.parse("PROCEDURE p(a: INTEGER); BEGIN p(a) END p");
  621. test.parse("PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(a, b) END p");
  622. test.expectError("PROCEDURE p(a: INTEGER; b: BOOLEAN); BEGIN p(b, a) END p"
  623. , "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'");
  624. test.expectError("PROCEDURE p; BEGIN p1() END p", "undeclared identifier: 'p1'");
  625. test.parse("PROCEDURE p(): ProcType; RETURN p END p");
  626. },
  627. "procedure RETURN": function(){
  628. var test = setupWithContext(Grammar.procedureDeclaration
  629. , "VAR i: INTEGER; PROCEDURE int(): INTEGER; RETURN 1 END int;");
  630. test.parse("PROCEDURE p(): BOOLEAN; RETURN TRUE END p");
  631. test.parse("PROCEDURE p(): BOOLEAN; RETURN int() = 1 END p");
  632. test.expectError("PROCEDURE p; RETURN TRUE END p"
  633. , "unexpected RETURN in PROCEDURE declared with no result type");
  634. test.expectError("PROCEDURE p(): BOOLEAN; END p", "RETURN expected at the end of PROCEDURE declared with 'BOOLEAN' result type");
  635. test.expectError("PROCEDURE p(): undeclared; END p", "undeclared identifier: 'undeclared'");
  636. test.expectError("PROCEDURE p(): i; END p", "type name expected");
  637. test.expectError("PROCEDURE p(): INTEGER; RETURN TRUE END p"
  638. , "RETURN 'INTEGER' expected, got 'BOOLEAN'");
  639. },
  640. "pass VAR argument as VAR parameter": function(){
  641. var test = setupWithContext(Grammar.procedureDeclaration,
  642. "PROCEDURE p1(VAR i: INTEGER); END p1;"
  643. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"
  644. );
  645. test.parse("PROCEDURE p(VAR i1: INTEGER); BEGIN p1(i1) END p");
  646. test.expectError("PROCEDURE p(VAR b: BOOLEAN); BEGIN p2(~b) END p", "expression cannot be used as VAR parameter");
  647. },
  648. "VAR parameter": function(){
  649. var test = setupWithContext(Grammar.statement
  650. , "CONST c = 123;"
  651. + "VAR i1: INTEGER; b1: BOOLEAN; a1: ARRAY 5 OF INTEGER;"
  652. + "r1: RECORD f1: INTEGER END;"
  653. + "PROCEDURE p1(VAR i: INTEGER); END p1;"
  654. + "PROCEDURE p2(VAR b: BOOLEAN); END p2;"
  655. );
  656. test.parse("p1(i1)");
  657. test.parse("p1(a1[0])");
  658. test.parse("p1(r1.f1)");
  659. test.expectError("p1(c)", "constant cannot be used as VAR parameter");
  660. test.expectError("p1(123)", "expression cannot be used as VAR parameter");
  661. test.expectError("p2(TRUE)", "expression cannot be used as VAR parameter");
  662. test.expectError("p1(i1 + i1)", "expression cannot be used as VAR parameter");
  663. test.expectError("p1(i1 * i1)", "expression cannot be used as VAR parameter");
  664. test.expectError("p1(+i1)", "expression cannot be used as VAR parameter");
  665. test.expectError("p1(-i1)", "expression cannot be used as VAR parameter");
  666. test.expectError("p2(~b1)", "expression cannot be used as VAR parameter");
  667. },
  668. "ARRAY parameter": function(){
  669. var test = setupWithContext(Grammar.procedureDeclaration
  670. , "TYPE T = RECORD i: INTEGER; p: POINTER TO T END;"
  671. + "PROCEDURE p1(i: INTEGER); END p1;"
  672. + "PROCEDURE varInteger(VAR i: INTEGER); END varInteger;"
  673. + "PROCEDURE p2(a: ARRAY OF INTEGER); END p2;"
  674. + "PROCEDURE p3(VAR a: ARRAY OF INTEGER); END p3;"
  675. );
  676. test.parse("PROCEDURE p(a: ARRAY OF INTEGER); END p");
  677. test.parse("PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); END p");
  678. test.parse("PROCEDURE p(a: ARRAY OF ARRAY OF INTEGER); BEGIN p1(a[0][0]) END p");
  679. test.parse("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p2(a) END p");
  680. test.parse("PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].p.i) END p");
  681. test.expectError("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN a[0] := 0 END p",
  682. "cannot assign to read-only variable");
  683. test.expectError("PROCEDURE p(a: ARRAY OF INTEGER); BEGIN p3(a) END p",
  684. "read-only variable cannot be used as VAR parameter");
  685. test.expectError("PROCEDURE p(a: ARRAY OF T); BEGIN a[0].i := 0 END p",
  686. "cannot assign to read-only variable");
  687. test.expectError("PROCEDURE p(a: ARRAY OF T); BEGIN varInteger(a[0].i) END p",
  688. "read-only variable cannot be used as VAR parameter");
  689. },
  690. "procedure call": function(){
  691. var test = setupWithContext(Grammar.statement
  692. , "TYPE ProcType = PROCEDURE;"
  693. + "VAR notProcedure: INTEGER;"
  694. + "PROCEDURE p; END p;"
  695. + "PROCEDURE p1(i: INTEGER); END p1;"
  696. + "PROCEDURE p2(i: INTEGER; b: BOOLEAN); END p2;"
  697. + "PROCEDURE p3(): ProcType; RETURN p END p3;"
  698. );
  699. test.parse("p");
  700. test.parse("p()");
  701. test.parse("p1(1)");
  702. test.parse("p1(1 + 2)");
  703. test.parse("p2(1, TRUE)");
  704. test.expectError("notProcedure", "PROCEDURE expected, got 'INTEGER'");
  705. test.expectError("p2(TRUE, 1)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'");
  706. test.expectError("p2(1, 1)", "type mismatch for argument 2: 'INTEGER' cannot be converted to 'BOOLEAN'");
  707. test.expectError("p3()()", "not parsed");
  708. },
  709. "procedure assignment": function(){
  710. var test = setupWithContext(
  711. Grammar.statement
  712. , "TYPE ProcType1 = PROCEDURE(): ProcType1;"
  713. + "ProcType2 = PROCEDURE(): ProcType2;"
  714. + "ProcType3 = PROCEDURE(p: ProcType3): ProcType3;"
  715. + "ProcType4 = PROCEDURE(p: ProcType4): ProcType4;"
  716. + "ProcType4VAR = PROCEDURE(VAR p: ProcType4VAR): ProcType4VAR;"
  717. + "ProcType5 = PROCEDURE(p: ProcType3): ProcType4;"
  718. + "ProcType6 = PROCEDURE(p: INTEGER);"
  719. + "ProcType7 = PROCEDURE(VAR p: INTEGER);"
  720. + "VAR v1: ProcType1; v2: ProcType2;"
  721. + "v3: PROCEDURE(i: INTEGER): ProcType1; v4: PROCEDURE(b: BOOLEAN): ProcType1;"
  722. + "v5: PROCEDURE(p: ProcType1); v6: PROCEDURE(p: ProcType2);"
  723. + "v7: ProcType3; v8: ProcType4; v8VAR: ProcType4VAR; v9: ProcType5; v10: ProcType6; v11: ProcType7;"
  724. + "PROCEDURE p1(): ProcType1; RETURN p1 END p1;"
  725. );
  726. test.parse("v1 := v2");
  727. test.parse("v5 := v6");
  728. test.parse("v7 := v8");
  729. test.parse("v7 := v9");
  730. test.parse("v8 := v9");
  731. test.parse("v1 := p1");
  732. test.expectError("p1 := v1", "cannot assign to procedure" );
  733. test.expectError(
  734. "v3 := v1"
  735. , "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'ProcType1' expression");
  736. test.expectError(
  737. "v3 := v4"
  738. , "type mismatch: 'v3' is 'PROCEDURE(INTEGER): ProcType1' and cannot be assigned to 'PROCEDURE(BOOLEAN): ProcType1' expression");
  739. test.expectError(
  740. "v10 := NEW"
  741. , "type mismatch: 'v10' is 'ProcType6' and cannot be assigned to 'predefined procedure NEW' expression");
  742. test.expectError("v10 := v11", "type mismatch: 'v10' is 'ProcType6' and cannot be assigned to 'ProcType7' expression" );
  743. test.expectError("v8 := v8VAR", "type mismatch: 'v8' is 'ProcType4' and cannot be assigned to 'ProcType4VAR' expression" );
  744. },
  745. "string assignment": function(){
  746. var test = setupWithContext(
  747. Grammar.statement
  748. , "VAR a1: ARRAY 3 OF CHAR;"
  749. + "ch1: CHAR;"
  750. + "intArray: ARRAY 10 OF INTEGER;"
  751. );
  752. test.parse("a1 := \"abc\"");
  753. test.parse("a1 := \"ab\"");
  754. test.parse("a1 := \"a\"");
  755. test.parse("a1 := 22X");
  756. test.parse("ch1 := \"A\"");
  757. test.parse("ch1 := 22X");
  758. test.expectError("a1 := \"abcd\"", "3-character ARRAY is too small for 4-character string");
  759. test.expectError("intArray := \"abcd\""
  760. , "type mismatch: 'intArray' is 'ARRAY OF INTEGER' and cannot be assigned to 'multi-character string' expression");
  761. },
  762. "array assignment": function(){
  763. var test = setupWithContext(
  764. Grammar.statement
  765. , "VAR charArray: ARRAY 3 OF CHAR;"
  766. + "intArray: ARRAY 10 OF INTEGER;"
  767. + "intArray2: ARRAY 10 OF INTEGER;"
  768. + "intArray3: ARRAY 5 OF INTEGER;"
  769. );
  770. test.parse("intArray := intArray2");
  771. test.expectError("intArray := charArray"
  772. , "type mismatch: 'intArray' is 'ARRAY OF INTEGER' and cannot be assigned to 'ARRAY OF CHAR' expression");
  773. test.expectError("intArray2 := intArray3"
  774. , "array size mismatch: 'intArray2' has size 10 and cannot be assigned to the array with size 5");
  775. test.expectError("intArray3 := charArray"
  776. , "type mismatch: 'intArray3' is 'ARRAY OF INTEGER' and cannot be assigned to 'ARRAY OF CHAR' expression");
  777. },
  778. "record assignment": function(){
  779. var test = setupWithContext(
  780. Grammar.statement
  781. , "TYPE Base1 = RECORD END;"
  782. + "T1 = RECORD (Base1) END;"
  783. + "T2 = RECORD END;"
  784. + "VAR b1: Base1; r1: T1; r2: T2;"
  785. );
  786. test.parse("r1 := r1");
  787. test.parse("b1 := r1");
  788. test.expectError("r1 := r2", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'T2' expression");
  789. test.expectError("r1 := b1", "type mismatch: 'r1' is 'T1' and cannot be assigned to 'Base1' expression");
  790. },
  791. "open array assignment fails": function(){
  792. var test = setup(Grammar.procedureDeclaration);
  793. test.expectError("PROCEDURE p(s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p"
  794. , "cannot assign to read-only variable");
  795. test.expectError("PROCEDURE p(VAR s1, s2: ARRAY OF CHAR); BEGIN s1 := s2 END p"
  796. , "'s1' is open 'ARRAY OF CHAR' and cannot be assigned");
  797. test.expectError("PROCEDURE p(s1: ARRAY OF CHAR); VAR s2: ARRAY 10 OF CHAR; BEGIN s2 := s1 END p"
  798. , "'s2' cannot be assigned to open 'ARRAY OF CHAR'");
  799. },
  800. "string assignment to open array fails": function(){
  801. var test = setup(Grammar.procedureDeclaration);
  802. test.expectError("PROCEDURE p(s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "cannot assign to read-only variable");
  803. test.expectError("PROCEDURE p(VAR s: ARRAY OF CHAR); BEGIN s := \"abc\" END p", "string cannot be assigned to open ARRAY OF CHAR");
  804. },
  805. "string argument": function(){
  806. var test = setupWithContext(
  807. Grammar.statement
  808. , "PROCEDURE p1(s: ARRAY OF CHAR); END p1;"
  809. + "PROCEDURE p2(VAR s: ARRAY OF CHAR); END p2;"
  810. + "PROCEDURE p3(i: INTEGER); END p3;"
  811. + "PROCEDURE p4(a: ARRAY OF INTEGER); END p4;"
  812. );
  813. test.parse("p1(\"abc\")");
  814. test.expectError("p2(\"abc\")", "expression cannot be used as VAR parameter");
  815. test.expectError("p3(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'INTEGER'");
  816. test.expectError("p4(\"abc\")", "type mismatch for argument 1: 'multi-character string' cannot be converted to 'ARRAY OF INTEGER'");
  817. },
  818. "scope": function(){
  819. var test = setup(Grammar.declarationSequence);
  820. test.parse("PROCEDURE p1(a1: INTEGER); END p1; PROCEDURE p2(a1: BOOLEAN); END p2;");
  821. },
  822. module: function(){
  823. var test = setup(Grammar.module);
  824. test.parse("MODULE m; END m.");
  825. test.expectError("MODULE m; END undeclared.",
  826. "original module name 'm' expected, got 'undeclared'");
  827. },
  828. assert: function(){
  829. var test = setup(Grammar.statement);
  830. test.parse("ASSERT(TRUE)");
  831. test.parse("ASSERT(TRUE, 123)");
  832. test.expectError("ASSERT()", "1 or 2 arguments expected, got 0");
  833. test.expectError("ASSERT(123, TRUE)", "type mismatch for argument 1: 'INTEGER' cannot be converted to 'BOOLEAN'");
  834. },
  835. IMPORT: function(){
  836. var test = setup(Grammar.module);
  837. test.parse("MODULE m; IMPORT JS; END m.");
  838. test.parse("MODULE m; IMPORT JS; BEGIN JS.alert(\"test\") END m.");
  839. test.parse("MODULE m; IMPORT JS; BEGIN JS.console.info(123) END m.");
  840. }};
  841. Test.run(testSuite);