ivprogProcessor.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. import { Store } from './store/store';
  2. import { StoreObject } from './store/storeObject';
  3. import { StoreObjectArray } from './store/storeObjectArray';
  4. import { StoreObjectRef } from './store/storeObjectRef';
  5. import { Modes } from './modes';
  6. import { Context } from './context';
  7. import { Types, toInt } from './../ast/types';
  8. import { Operators } from './../ast/operators';
  9. import { NAMES } from './definedFunctions';
  10. import { canApplyInfixOp, canApplyUnaryOp } from './compatibilityTable';
  11. import * as Commands from './../ast/commands/';
  12. import * as Expressions from './../ast/expressions/';
  13. export class IVProgProcessor {
  14. constructor(ast) {
  15. this.ast = ast;
  16. this.globalStore = new Store();
  17. this.stores = [this.globalStore];
  18. this.context = [Context.BASE];
  19. this.input = null;
  20. this.output = null;
  21. }
  22. registerInput (input) {
  23. this.input = input;
  24. }
  25. registerOutput (output) {
  26. this.output = output;
  27. }
  28. checkContext(context) {
  29. return this.context[this.context.length - 1] === context;
  30. }
  31. ignoreSwitchCases (store) {
  32. if (store.mode === Modes.RETURN) {
  33. return true;
  34. } else if (store.mode === Modes.BREAK) {
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. }
  40. interpretAST () {
  41. this.initGlobal();
  42. const mainFunc = this.findMainFunction();
  43. if(mainFunc === null) {
  44. // TODO: Better error message
  45. throw new Error("Missing main funciton.");
  46. }
  47. return this.runFunction(mainFunc, [], this.globalStore);
  48. }
  49. initGlobal () {
  50. if(!this.checkContext(Context.BASE)) {
  51. throw new Error("!!!CRITICAL: Invalid call to initGlobal outside BASE context!!!");
  52. }
  53. this.ast.global.forEach(decl => {
  54. this.executeCommand(this.globalStore, decl).then(sto => this.globalStore = sto);
  55. });
  56. }
  57. findMainFunction () {
  58. return this.ast.functions.find(v => v.isMain);
  59. }
  60. findFunction (name) {
  61. const val = this.ast.functions.find( v => v.name === name);
  62. if (!!!val) {
  63. // TODO: better error message;
  64. throw new Error(`Function ${name} is not defined.`);
  65. }
  66. return val;
  67. }
  68. runFunction (func, actualParameters, store) {
  69. let funcStore = new Store();
  70. funcStore.extendStore(this.globalStore);
  71. const returnStoreObject = new StoreObject(func.returnType, null);
  72. const funcName = func.isMain ? 'main' : func.name;
  73. const funcNameStoreObject = new StoreObject(Types.STRING, funcName, true);
  74. funcStore.insertStore('$', returnStoreObject);
  75. funcStore.insertStore('$name', funcNameStoreObject);
  76. funcStore = this.associateParameters(func.formalParameters, actualParameters, store, funcStore);
  77. this.context.push(Context.FUNCTION);
  78. this.stores.push(funcStore);
  79. const result = this.executeCommands(funcStore, func.variablesDeclarations).then(sto => {
  80. return this.executeCommands(sto, func.commands)
  81. });
  82. this.stores.pop();
  83. this.context.pop();
  84. return result;
  85. }
  86. associateParameters (formalList, actualList, callerStore, calleeStore) {
  87. if (formalList.length != actualList.length) {
  88. // TODO: Better error message
  89. throw new Error("Numbers of parameters doesn't match");
  90. }
  91. formalList.forEach((v, i) => {
  92. this.evaluateExpression(callerStore, actualList[i])
  93. .then(vl => {
  94. switch (v.dimensions) {
  95. case 1: {
  96. if (vl.lines > 0 && vl.columns === null
  97. && vl.subtype === v.type) {
  98. if(v.byRef && !vl.inStore) {
  99. throw new Error('You must inform a variable as parameter');
  100. }
  101. if(v.byRef) {
  102. const ref = new StoreObjectRef(vl.id, callerStore);
  103. calleeStore.insertStore(v.id, ref);
  104. } else {
  105. calleeStore.insertStore(v.id, vl);
  106. }
  107. } else {
  108. // TODO: Better error message
  109. throw new Error(`Parameter ${v.id} is not compatible with the value given.`);
  110. }
  111. break;
  112. }
  113. case 2: {
  114. if (vl.lines > 0 && vl.columns > 0
  115. && vl.subtype === v.type) {
  116. if(v.byRef && !vl.inStore) {
  117. throw new Error('You must inform a variable as parameter');
  118. }
  119. if(v.byRef) {
  120. const ref = new StoreObjectRef(vl.id, callerStore);
  121. calleeStore.insertStore(v.id, ref);
  122. } else {
  123. calleeStore.insertStore(v.id, vl);
  124. }
  125. } else {
  126. // TODO: Better error message
  127. throw new Error(`Parameter ${v.id} is not compatible with the value given.`);
  128. }
  129. break;
  130. }
  131. case 0: {
  132. if(v.byRef && !vl.inStore) {
  133. throw new Error('You must inform a variable as parameter');
  134. } else if (v.type !== Types.ALL && vl.type !== v.type) {
  135. // TODO: Better error message
  136. throw new Error(`Parameter ${v.id} is not compatible with ${vl.type}.`);
  137. } else {
  138. if(v.byRef) {
  139. const ref = new StoreObjectRef(vl.id, callerStore);
  140. calleeStore.insertStore(v.id, ref);
  141. } else {
  142. calleeStore.insertStore(v.id, vl);
  143. }
  144. }
  145. }
  146. }
  147. });
  148. });
  149. return calleeStore;
  150. }
  151. executeCommands (store, cmds) {
  152. const auxExecCmdFun = (promise, cmd) => promise.then( sto => this.executeCommand(sto, cmd));
  153. let breakLoop = false;
  154. let $result = Promise.resolve(store);
  155. for (let index = 0; index < cmds.length && !breakLoop; index++) {
  156. const cmd = cmds[index];
  157. $result = auxExecCmdFun($result, cmd);
  158. $result.then(sto => {
  159. if(sto.mode === Modes.RETURN) {
  160. breakLoop = true;
  161. } else if (this.checkContext(Context.BREAKABLE &&
  162. sto.mode === Modes.BREAK)) {
  163. breakLoop = true;
  164. }
  165. return sto;
  166. });
  167. }
  168. return $result;
  169. }
  170. executeCommand (store, cmd) {
  171. while (store.mode === Modes.PAUSE) {
  172. continue;
  173. }
  174. if(store.mode === Modes.RETURN) {
  175. return Promise.resolve(store);
  176. } else if(this.checkContext(Context.BREAKABLE) && store.mode === Modes.BREAK) {
  177. return Promise.resolve(store);
  178. }
  179. if (cmd instanceof Commands.Declaration) {
  180. return this.executeDeclaration(store, cmd);
  181. } else if (cmd instanceof Commands.Assign) {
  182. return this.executeAssign(store, cmd);
  183. } else if (cmd instanceof Commands.Break) {
  184. return this.executeBreak(store, cmd);
  185. } else if (cmd instanceof Commands.Return) {
  186. return this.executeReturn(store, cmd);
  187. } else if (cmd instanceof Commands.IfThenElse) {
  188. return this.executeIfThenElse(store, cmd);
  189. } else if (cmd instanceof Commands.While) {
  190. return this.executeWhile(store, cmd);
  191. } else if (cmd instanceof Commands.DoWhile) {
  192. return this.executeDoWhile(store, cmd);
  193. } else if (cmd instanceof Commands.For) {
  194. return this.executeFor(store, cmd);
  195. } else if (cmd instanceof Commands.Switch) {
  196. return this.executeSwitch(store, cmd);
  197. } else if (cmd instanceof Commands.FunctionCall) {
  198. return this.executeFunctionCall(store, cmd);
  199. } else if (cmd instanceof Commands.SysCall) {
  200. return this.executeSysCall(store, cmd);
  201. } else {
  202. throw new Error("!!!CRITICAL A unknown command was found!!!\n" + cmd);
  203. }
  204. }
  205. executeSysCall (store, cmd) {
  206. if (cmd.id === NAMES.WRITE) {
  207. this.runWriteFunction(store)
  208. }
  209. }
  210. runWriteFunction (store) {
  211. const val = store.applyStore('p1');
  212. this.output.appendText(val.val);
  213. return Promise.resolve(store);
  214. }
  215. runReadFunction (store) {
  216. const typeToConvert = store.applyStore('p1').type;
  217. const listenInput = (text) => {
  218. if (typeToConvert === Types.INTEGER) {
  219. const val = toInt(text);
  220. return new StoreObject(Types.INTEGER, val);
  221. } else if (typeToConvert === Types.REAL) {
  222. return new StoreObject(Types.REAL, parseFloat(text));
  223. } else if (typeToConvert === Types.BOOLEAN) {
  224. } else if (typeToConvert === Types.STRING) {
  225. return new StoreObject(Types.STRING, text);
  226. }
  227. }
  228. return Promise.resolve(store).then( sto => {
  229. let received = false;
  230. const notify = (text) => {
  231. received = true;
  232. const result = listenInput(text);
  233. sto.updateStore('p1', result);
  234. }
  235. const obj = {notify: notify.bind(this)}
  236. this.input.registerListener(obj);
  237. this.input.requestInput();
  238. while(!received) {
  239. continue;
  240. }
  241. this.input.removeListener(obj);
  242. return sto;
  243. });
  244. }
  245. executeFunctionCall (store, cmd) {
  246. const func = this.findFunction(cmd.id);
  247. this.runFunction(func, cmd.actualParameters, store);
  248. return Promise.resolve(store);
  249. }
  250. executeSwitch (store, cmd) {
  251. this.context.push(Context.BREAKABLE);
  252. const auxCaseFun = (promise, switchExp, aCase) => {
  253. return promise.then( result => {
  254. const sto = result.sto;
  255. if (this.ignoreSwitchCases(sto)) {
  256. return Promise.resolve(result);
  257. } else if (result.wasTrue || aCase.isDefault) {
  258. const $newSto = this.executeCommands(result.sto,aCase.commands);
  259. return $newSto.then(nSto => {
  260. return Promise.resolve({wasTrue: true, sto: nSto});
  261. });
  262. } else {
  263. const $value = this.evaluateExpression(sto,
  264. new Expressions.InfixApp(Operators.EQ, switchExp, aCase.expression));
  265. return $value.then(vl => {
  266. if (vl.value) {
  267. const $newSto = this.executeCommands(result.sto,aCase.commands);
  268. return $newSto.then(nSto => {
  269. return Promise.resolve({wasTrue: true, sto: nSto});
  270. });
  271. } else {
  272. return Promise.resolve({wasTrue: false, sto: sto});
  273. }
  274. });
  275. }
  276. });
  277. }
  278. try {
  279. let breakLoop = false;
  280. let $result = Promise.resolve({wasTrue: false, sto: store});
  281. for (let index = 0; index < cmd.cases.length && !breakLoop; index++) {
  282. const aCase = cmd.cases[index];
  283. $result = auxCaseFun($result, cmd.expression, aCase);
  284. $result.then( r => breakLoop = this.ignoreSwitchCases(r.sto));
  285. }
  286. return $result.then(r => {
  287. this.context.pop();
  288. if(r.sto.mode === Modes.BREAK) {
  289. r.sto.mode = Modes.RUN;
  290. }
  291. return r.sto;
  292. });
  293. } catch (error) {
  294. return Promise.reject(error);
  295. }
  296. }
  297. executeFor (store, cmd) {
  298. try {
  299. //BEGIN for -> while rewrite
  300. const initCmd = cmd.assignment;
  301. const condition = cmd.condition;
  302. const increment = cmd.increment;
  303. const whileBlock = new Commands.CommandBlock([],
  304. cmd.commands.concat(increment));
  305. const forAsWhile = new Commands.While(condition, whileBlock);
  306. //END for -> while rewrite
  307. const newCmdList = [initCmd,forAsWhile];
  308. return this.executeCommands(store, newCmdList);
  309. } catch (error) {
  310. return Promise.reject(error);
  311. }
  312. }
  313. executeDoWhile (store, cmd) {
  314. try {
  315. this.context.push(Context.BREAKABLE);
  316. const $newStore = this.executeCommands(store, cmd.commands);
  317. return $newStore.then(sto => {
  318. if(sto.mode === Modes.BREAK) {
  319. this.context.pop();
  320. sto.mode = Modes.RUN;
  321. return Promise.resolve(sto);
  322. }
  323. const $value = this.evaluateExpression(sto, cmd.expression);
  324. return $value.then(vl => {
  325. if (vl.type !== Types.BOOLEAN) {
  326. // TODO: Better error message -- Inform line and column from token!!!!
  327. // THIS IF SHOULD BE IN A SEMANTIC ANALYSER
  328. return Promise.reject(new Error(`DoWhile expression must be of type boolean`));
  329. }
  330. if (vl.value) {
  331. this.context.pop();
  332. return this.executeCommand(sto, cmd);
  333. } else {
  334. this.context.pop();
  335. return Promise.resolve(sto);
  336. }
  337. });
  338. });
  339. } catch (error) {
  340. return Promise.reject(error)
  341. }
  342. }
  343. executeWhile (store, cmd) {
  344. try {
  345. this.context.push(Context.BREAKABLE);
  346. const $value = this.evaluateExpression(store, cmd.expression);
  347. return $value.then(vl => {
  348. if(vl.type === Types.BOOLEAN) {
  349. if(vl.value) {
  350. const $newStore = this.executeCommands(store, cmd.commands);
  351. return $newStore.then(sto => {
  352. this.context.pop();
  353. if (sto.mode === Modes.BREAK) {
  354. sto.mode = Modes.RUN;
  355. return Promise.resolve(sto);
  356. }
  357. return this.executeCommand(sto, cmd);
  358. });
  359. } else {
  360. this.context.pop();
  361. return Promise.resolve(store);
  362. }
  363. } else {
  364. // TODO: Better error message -- Inform line and column from token!!!!
  365. // THIS IF SHOULD BE IN A SEMANTIC ANALYSER
  366. return Promise.reject(new Error(`Loop condition must be of type boolean`));
  367. }
  368. });
  369. } catch (error) {
  370. return Promise.reject(error);
  371. }
  372. }
  373. executeIfThenElse (store, cmd) {
  374. try {
  375. const $value = this.evaluateExpression(cmd.condition);
  376. return $value.then(vl => {
  377. if(vl.type === Types.BOOLEAN) {
  378. if(vl.value) {
  379. return this.executeCommands(store, cmd.ifTrue);
  380. } else {
  381. return this.executeCommands(store, cmd.ifFalse);
  382. }
  383. } else {
  384. // TODO: Better error message -- Inform line and column from token!!!!
  385. // THIS IF SHOULD BE IN A SEMANTIC ANALYSER
  386. return Promise.reject(new Error(`If expression must be of type boolean`));
  387. }
  388. });
  389. } catch (error) {
  390. return Promise.reject(error);
  391. }
  392. }
  393. executeReturn (store, cmd) {
  394. try {
  395. const funcType = store.applyStore('$');
  396. const $value = this.evaluateExpression(store, cmd.expression);
  397. const funcName = store.applyStore('$name');
  398. return $value.then(vl => {
  399. if (funcType.type !== vl.type) {
  400. // TODO: Better error message -- Inform line and column from token!!!!
  401. // THIS IF SHOULD BE IN A SEMANTIC ANALYSER
  402. return Promise.reject(new Error(`Function ${funcName.value} must return ${funcType.type} instead of ${vl.type}.`));
  403. } else {
  404. store.updateStore('$', vl);
  405. store.mode = Modes.RETURN;
  406. return Promise.resolve(store);
  407. }
  408. });
  409. } catch (error) {
  410. return Promise.reject(error);
  411. }
  412. }
  413. executeBreak (store, _) {
  414. if(this.checkContext(Context.BREAKABLE)) {
  415. store.mode = Modes.BREAK;
  416. return Promise.resolve(store);
  417. } else {
  418. return Promise.reject(new Error("!!!CRITIAL: Break command outside Loop/Switch scope!!!"));
  419. }
  420. }
  421. executeAssign (store, cmd) {
  422. try {
  423. const $value = this.evaluateExpression(store, cmd.expression);
  424. return $value.then( vl => {
  425. store.updateStore(cmd.id, vl)
  426. return store;
  427. });
  428. } catch (error) {
  429. return Promise.reject(error);
  430. }
  431. }
  432. executeDeclaration (store, cmd) {
  433. try {
  434. const $value = this.evaluateExpression(store, cmd.initial);
  435. if(cmd instanceof Commands.ArrayDeclaration) {
  436. const $lines = this.evaluateExpression(store, cmd.lines);
  437. const $columns = cmd.columns === null ? null: this.evaluateExpression(store, cmd.columns);
  438. return Promise.all([$lines, $columns, $value]).then(values => {
  439. const lineSO = values[0];
  440. if(lineSO.type !== Types.INTEGER) {
  441. // TODO: better error message
  442. //SHOULD NOT BE HERE. IT MUST HAVE A SEMANTIC ANALYSIS
  443. return Promise.reject(new Error("Array dimension must be of type int"));
  444. }
  445. const line = lineSO.value;
  446. const columnSO = values[1];
  447. let column = null
  448. if (columnSO !== null) {
  449. if(columnSO.type !== Types.INTEGER) {
  450. // TODO: better error message
  451. //SHOULD NOT BE HERE. IT MUST HAVE A SEMANTIC ANALYSIS
  452. return Promise.reject(new Error("Array dimension must be of type int"));
  453. }
  454. column = columnSO.value;
  455. }
  456. const value = values[2];
  457. const temp = new StoreObjectArray(cmd.subtype, line, column, null, cmd.isConst);
  458. store.insertStore(cmd.id, temp);
  459. if(value !== null)
  460. return store.updateStore(cmd.id, value);
  461. else
  462. return store;
  463. });
  464. } else {
  465. const temp = new StoreObject(cmd.type, null, cmd.isConst);
  466. store.insertStore(cmd.id, temp);
  467. return $value.then(vl => {
  468. if (vl !== null)
  469. return store.updateStore(cmd.id, vl)
  470. else
  471. return store;
  472. });
  473. }
  474. } catch (e) {
  475. return Promise.reject(e);
  476. }
  477. }
  478. evaluateExpression (store, exp) {
  479. if (exp instanceof Expressions.UnaryApp) {
  480. return this.evaluateUnaryApp(store, exp);
  481. } else if (exp instanceof Expressions.InfixApp) {
  482. return this.evaluateInfixApp(store, exp);
  483. } else if (exp instanceof Expressions.ArrayAccess) {
  484. return this.evaluateArrayAccess(store, exp);
  485. } else if (exp instanceof Expressions.VariableLiteral) {
  486. return this.evaluateVariableLiteral(store, exp);
  487. } else if (exp instanceof Expressions.IntLiteral) {
  488. return this.evaluateLiteral(store, exp);
  489. } else if (exp instanceof Expressions.RealLiteral) {
  490. return this.evaluateLiteral(store, exp);
  491. } else if (exp instanceof Expressions.BoolLiteral) {
  492. return this.evaluateLiteral(store, exp);
  493. } else if (exp instanceof Expressions.StringLiteral) {
  494. return this.evaluateLiteral(store, exp);
  495. } else if (exp instanceof Expressions.ArrayLiteral) {
  496. return this.evaluateArrayLiteral(store, exp);
  497. } else if (exp instanceof Expressions.FunctionCall) {
  498. return this.evaluateFunctionCall(store, exp);
  499. }
  500. return Promise.resolve(null);
  501. }
  502. evaluateFunctionCall (store, exp) {
  503. const func = this.findFunction(exp.id);
  504. if(func.returnType === Types.VOID) {
  505. // TODO: better error message
  506. return Promise.reject(new Error(`Function ${exp.id} cannot be used inside an expression`));
  507. }
  508. const $newStore = this.runFunction(func, exp.actualParameters, store);
  509. return $newStore.then( sto => {
  510. const val = sto.applyStore('$');
  511. if (val.type === Types.ARRAY) {
  512. return Promise.resolve(Object.assign(new StoreObjectArray(null,null,null,null,null), val));
  513. } else {
  514. return Promise.resolve(Object.assign(new StoreObject(null,null), val));
  515. }
  516. });
  517. }
  518. evaluateArrayLiteral (store, exp) {
  519. if(!exp.isVector) {
  520. const $matrix = this.evaluateMatrix(store, exp.value);
  521. return $matrix.then(list => {
  522. const arr = new StoreObjectArray(list[0].subtype, list.length, list[0].lines, list);
  523. if(arr.isValid)
  524. return Promise.resolve(arr);
  525. else
  526. return Promise.reject(new Error(`Invalid array`))
  527. });
  528. } else {
  529. return this.evaluateVector(store, exp.value).then(list => {
  530. const stoArray = new StoreObjectArray(list[0].type, list.length, null, list);
  531. if(stoArray.isValid)
  532. return Promise.resolve(stoArray);
  533. else
  534. return Promise.reject(new Error(`Invalid array`))
  535. });
  536. }
  537. }
  538. evaluateVector (store, exps) {
  539. return Promise.all(exps.map( exp => this.evaluateExpression(store, exp)));
  540. }
  541. evaluateMatrix (store, exps) {
  542. return Promise.all(exps.map( vector => {
  543. const $vector = this.evaluateVector(store, vector.value)
  544. return $vector.then(list => new StoreObjectArray(list[0].type, list.length, null, list))
  545. } ));
  546. }
  547. evaluateLiteral (_, exp) {
  548. return Promise.resolve(new StoreObject(exp.type, exp.value));
  549. }
  550. evaluateVariableLiteral (store, exp) {
  551. try {
  552. const val = store.applyStore(exp.id);
  553. if (val.type === Types.ARRAY) {
  554. return Promise.resolve(Object.assign(new StoreObjectArray(null,null,null,null), val));
  555. } else {
  556. return Promise.resolve(Object.assign(new StoreObject(null,null), val));
  557. }
  558. } catch (error) {
  559. return Promise.reject(error);
  560. }
  561. }
  562. evaluateArrayAccess (store, exp) {
  563. const mustBeArray = store.applyStore(exp.id);
  564. if (mustBeArray.type !== Types.ARRAY) {
  565. // TODO: better error message
  566. return Promise.reject(new Error(`${exp.id} is not of type array`));
  567. }
  568. const $line = this.evaluateExpression(store, exp.line);
  569. const $column = this.evaluateExpression(store, exp.column);
  570. return Promise.all([$line, $column]).then(values => {
  571. const lineSO = values[0];
  572. const columnSO = values[1];
  573. if(lineSO.type !== Types.INTEGER) {
  574. // TODO: better error message
  575. //SHOULD NOT BE HERE. IT MUST HAVE A SEMANTIC ANALYSIS
  576. return Promise.reject(new Error("Array dimension must be of type int"));
  577. }
  578. const line = lineSO.value;
  579. let column = null;
  580. if(columnSO !== null) {
  581. if(columnSO.type !== Types.INTEGER) {
  582. // TODO: better error message
  583. //SHOULD NOT BE HERE. IT MUST HAVE A SEMANTIC ANALYSIS
  584. return Promise.reject(new Error("Array dimension must be of type int"));
  585. }
  586. column = columnSO.value;
  587. }
  588. if (line >= mustBeArray.lines) {
  589. // TODO: better error message
  590. return Promise.reject(new Error(`${exp.id}: index out of bounds: ${lines}`));
  591. }
  592. if (column !== null && mustBeArray.columns === null ){
  593. // TODO: better error message
  594. return Promise.reject(new Error(`${exp.id}: index out of bounds: ${column}`));
  595. }
  596. if(column !== null && column >= mustBeArray.columns) {
  597. // TODO: better error message
  598. return Promise.reject(new Error(`${exp.id}: index out of bounds: ${column}`));
  599. }
  600. if (column !== null) {
  601. return Promise.resolve(mustBeArray.value[line].value[column]);
  602. } else {
  603. return Promise.resolve(mustBeArray.value[line]);
  604. }
  605. });
  606. }
  607. evaluateUnaryApp (store, unaryApp) {
  608. const $left = this.evaluateExpression(store, unaryApp.left);
  609. return $left.then( left => {
  610. if (!canApplyUnaryOp(unaryApp.op, left)) {
  611. // TODO: better urgent error message
  612. return Promise.reject(new Error(`Cannot use this op to ${left.type}`));
  613. }
  614. switch (unaryApp.op) {
  615. case Operators.ADD:
  616. return new StoreObject(left.type, +left.value);
  617. case Operators.SUB:
  618. return new StoreObject(left.type, -left.value);
  619. case Operators.NOT:
  620. return new StoreObject(left.type, !left.value);
  621. default:
  622. return Promise.reject(new Error('!!!Critical Invalid UnaryApp '+ unaryApp.op));
  623. }
  624. });
  625. }
  626. evaluateInfixApp (store, infixApp) {
  627. const $left = this.evaluateExpression(store, infixApp.left);
  628. const $right = this.evaluateExpression(store, infixApp.right);
  629. return Promise.all([$left, $right]).then(values => {
  630. const left = values[0];
  631. const right = values[1];
  632. if (!canApplyInfixOp(infixApp.op, left, right)) {
  633. // TODO: better urgent error message
  634. return Promise.reject(new Error(`Cannot use this op to ${left.type} and ${right.type}`));
  635. }
  636. switch (infixApp.op) {
  637. case Operators.ADD:
  638. return new StoreObject(left.type, left.value + right.value);
  639. case Operators.SUB:
  640. return new StoreObject(left.type, left.value - right.value);
  641. case Operators.MULT:
  642. return new StoreObject(left.type, left.value * right.value);
  643. case Operators.DIV:
  644. return new StoreObject(left.type, left.value / right.value);
  645. case Operators.MOD:
  646. return new StoreObject(left.type, left.value % right.value);
  647. case Operators.GT:
  648. return new StoreObject(Types.BOOLEAN, left.value > right.value);
  649. case Operators.GE:
  650. return new StoreObject(Types.BOOLEAN, left.value >= right.value);
  651. case Operators.LT:
  652. return new StoreObject(Types.BOOLEAN, left.value < right.value);
  653. case Operators.LE:
  654. return new StoreObject(Types.BOOLEAN, left.value <= right.value);
  655. case Operators.EQ:
  656. return new StoreObject(Types.BOOLEAN, left.value === right.value);
  657. case Operators.NEQ:
  658. return new StoreObject(Types.BOOLEAN, left.value !== right.value);
  659. case Operators.AND:
  660. return new StoreObject(Types.BOOLEAN, left.value && right.value);
  661. case Operators.OR:
  662. return new StoreObject(Types.BOOLEAN, left.value || right.value);
  663. default:
  664. return Promise.reject(new Error('!!!Critical Invalid InfixApp '+ infixApp.op));
  665. }
  666. });
  667. }
  668. }