ivprogProcessor.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. import { Store } from './store/store';
  2. import { Modes } from './modes';
  3. import { Context } from './context';
  4. import { Types } from './../typeSystem/types';
  5. import { Operators } from './../ast/operators';
  6. import { LanguageDefinedFunction } from './definedFunctions';
  7. import { resultTypeAfterInfixOp, resultTypeAfterUnaryOp } from './compatibilityTable';
  8. import * as Commands from './../ast/commands/';
  9. import * as Expressions from './../ast/expressions/';
  10. import { ArrayType } from './../typeSystem/array_type';
  11. import { convertToString, toInt } from '../typeSystem/parsers';
  12. import { Config } from '../util/config';
  13. import { ProcessorErrorFactory } from './error/processorErrorFactory';
  14. import { RuntimeError } from './error/runtimeError';
  15. import { Location } from '../memory/location';
  16. import { StoreValue } from './store/value/store_value';
  17. import { StoreValueRef } from './store/value/store_value_ref';
  18. import { ArrayStoreValue } from './store/value/array_store_value';
  19. import { ArrayStoreValueRef } from './store/value/array_store_value_ref';
  20. import { StoreValueAddress } from './store/value/store_value_address';
  21. import { LocalizedStrings } from '../services/localizedStringsService';
  22. export class IVProgProcessor {
  23. static get MAIN_INTERNAL_ID () {
  24. return "$main";
  25. }
  26. constructor (ast) {
  27. this.ast = ast;
  28. this.globalStore = new Store("$global");
  29. this.stores = [this.globalStore];
  30. this.context = [Context.BASE];
  31. this.input = null;
  32. this.forceKill = false;
  33. this.output = null;
  34. this.mode = Modes.RUN;
  35. /**
  36. * Stores the sourceInfo of every function call, command or expression
  37. */
  38. this.function_call_stack = [];
  39. this.instruction_count = 0;
  40. this.function_call_count = 0;
  41. }
  42. registerInput (input) {
  43. if(this.input !== null)
  44. this.input = null;
  45. this.input = input;
  46. }
  47. registerOutput (output) {
  48. if(this.output !== null)
  49. this.output = null;
  50. this.output = output;
  51. }
  52. checkContext(context) {
  53. return this.context[this.context.length - 1] === context;
  54. }
  55. ignoreSwitchCases (store) {
  56. if (store.mode === Modes.RETURN) {
  57. return true;
  58. } else if (store.mode === Modes.BREAK) {
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. prepareState () {
  65. if(this.stores !== null) {
  66. for (let i = 0; i < this.stores.length; i++) {
  67. delete this.stores[i];
  68. }
  69. this.stores = null;
  70. }
  71. if(this.globalStore !== null)
  72. this.globalStore = null;
  73. this.globalStore = new Store("$global");
  74. this.stores = [this.globalStore];
  75. this.context = [Context.BASE];
  76. this.instruction_count = 0;
  77. this.mode = Modes.RUN;
  78. }
  79. interpretAST () {
  80. this.prepareState();
  81. Location.clear();
  82. return this.initGlobal().then( _ => {
  83. const mainFunc = this.findMainFunction();
  84. if(mainFunc === null) {
  85. return Promise.reject(ProcessorErrorFactory.main_missing())
  86. }
  87. return this.runFunction(mainFunc, [], this.globalStore);
  88. });
  89. }
  90. initGlobal () {
  91. if(!this.checkContext(Context.BASE)) {
  92. return Promise.reject(ProcessorErrorFactory.invalid_global_var())
  93. }
  94. return this.executeCommands(this.globalStore, this.ast.global);
  95. }
  96. findMainFunction () {
  97. return this.ast.functions.find(v => v.isMain);
  98. }
  99. findFunction (name) {
  100. if(name.match(/^\$.+$/)) {
  101. if(name === IVProgProcessor.MAIN_INTERNAL_ID) {
  102. return this.findMainFunction();
  103. }
  104. const fun = LanguageDefinedFunction.getFunction(name);
  105. if(!fun) {
  106. throw ProcessorErrorFactory.not_implemented(name);
  107. }
  108. return fun;
  109. } else {
  110. const val = this.ast.functions.find( v => v.name === name);
  111. if (!val) {
  112. throw ProcessorErrorFactory.function_missing(name);
  113. }
  114. return val;
  115. }
  116. }
  117. runFunction (func, actualParameters, store) {
  118. const funcName = func.isMain ? IVProgProcessor.MAIN_INTERNAL_ID : func.name;
  119. const funcStore = new Store(funcName);
  120. funcStore.extendStore(this.globalStore);
  121. return new Promise((resolve, reject) => {
  122. const run_lambda = () => {
  123. const newFuncStore$ = this.associateParameters(func.formalParameters, actualParameters, store, funcStore);
  124. newFuncStore$.then(sto => {
  125. this.context.push(Context.FUNCTION);
  126. this.stores.push(sto);
  127. return this.executeCommands(sto, func.variablesDeclarations)
  128. .then(stoWithVars => this.executeCommands(stoWithVars, func.commands)).then(finalSto => {
  129. this.stores.pop();
  130. this.context.pop();
  131. return finalSto;
  132. });
  133. }).then(resolve)
  134. .catch(reject);
  135. }
  136. run_lambda();
  137. });
  138. }
  139. associateParameters (formal_params, effective_params, caller_store, callee_store) {
  140. const funcName = callee_store.name === IVProgProcessor.MAIN_INTERNAL_ID ?
  141. LanguageDefinedFunction.getMainFunctionName() : callee_store.name;
  142. if (formal_params.length != effective_params.length) {
  143. return Promise.reject(ProcessorErrorFactory.invalid_parameters_size(funcName, formal_params.length, effective_params.length))
  144. }
  145. const promises$ = effective_params.map(actual_param => this.evaluateExpression(caller_store, actual_param));
  146. return Promise.all(promises$).then(values => {
  147. for (let i = 0; i < values.length; i++) {
  148. const sto_value = values[i];
  149. // console.log(callee_store.name);
  150. // console.log(sto_value);
  151. const exp = effective_params[i];
  152. let shouldTypeCast = false;
  153. const formalParameter = formal_params[i];
  154. if(!formalParameter.type.isCompatible(sto_value.type)) {
  155. if (Config.enable_type_casting && !formalParameter.byRef
  156. && Store.canImplicitTypeCast(formalParameter.type, sto_value.type)) {
  157. shouldTypeCast = true;
  158. } else {
  159. return Promise.reject(ProcessorErrorFactory.invalid_parameter_type(funcName, exp.toString()))
  160. }
  161. }
  162. if(formalParameter.byRef && !sto_value.inStore()) {
  163. return Promise.reject(ProcessorErrorFactory.invalid_ref(funcName, exp.toString()))
  164. }
  165. if(formalParameter.byRef) {
  166. const realObj = caller_store.getStoreObject(sto_value.id);
  167. let ref = null;
  168. if(sto_value instanceof ArrayStoreValue) {
  169. // it's a vector or matrix...
  170. const values = sto_value.get();
  171. const array_type = sto_value.type;
  172. const addresses = values.map( v => realObj.getLocAddressOf(v.line, v.column));
  173. const columns = sto_value.isVector() ? 0 : sto_value.columns;
  174. ref = new ArrayStoreValueRef(array_type, values, addresses, sto_value.lines, columns, realObj.id);
  175. } else {
  176. if(sto_value instanceof StoreValueAddress) {
  177. const line = sto_value.line;
  178. const column = sto_value.column;
  179. ref = new StoreValueRef(sto_value.type, sto_value.get(),
  180. realObj.getLocAddressOf(line, column), realObj.id);
  181. ref.setReferenceDimension(realObj.type.dimensions);
  182. } else {
  183. ref = new StoreValueRef(sto_value.type, sto_value.get(), realObj.locAddress, realObj.id);
  184. }
  185. }
  186. callee_store.insertStore(formalParameter.id, ref);
  187. } else {
  188. let realValue = sto_value;
  189. if (shouldTypeCast) {
  190. realValue = Store.doImplicitCasting(formalParameter.type, realValue);
  191. }
  192. callee_store.insertStore(formalParameter.id, realValue);
  193. }
  194. }
  195. return callee_store;
  196. });
  197. }
  198. executeCommands (store, cmds) {
  199. // helper to partially apply a function, in this case executeCommand
  200. const partial = (fun, cmd) => (sto) => fun(sto, cmd);
  201. return cmds.reduce((lastCommand, next) => {
  202. const nextCommand = partial(this.executeCommand.bind(this), next);
  203. return lastCommand.then(nextCommand);
  204. }, Promise.resolve(store));
  205. }
  206. executeCommand (store, cmd) {
  207. this.instruction_count += 1;
  208. return new Promise((resolve, reject) => {
  209. const command_lambda = () => {
  210. if(this.instruction_count >= Config.max_instruction_count) {
  211. return reject(ProcessorErrorFactory.exceed_max_instructions());
  212. } else if(this.forceKill) {
  213. return reject("FORCED_KILL!");
  214. } else if (store.mode === Modes.PAUSE) {
  215. return resolve(this.executeCommand(store, cmd));
  216. } else if(store.mode === Modes.RETURN) {
  217. return resolve(store);
  218. } else if(this.checkContext(Context.BREAKABLE) && store.mode === Modes.BREAK) {
  219. return resolve(store);
  220. } else if (this.mode === Modes.ABORT) {
  221. return reject(LocalizedStrings.getMessage('aborted_execution'));
  222. }
  223. if (cmd instanceof Commands.Declaration) {
  224. return resolve(this.executeDeclaration(store, cmd));
  225. } else if (cmd instanceof Commands.ArrayIndexAssign) {
  226. return resolve(this.executeArrayIndexAssign(store, cmd));
  227. } else if (cmd instanceof Commands.Assign) {
  228. return resolve(this.executeAssign(store, cmd));
  229. } else if (cmd instanceof Commands.Break) {
  230. return resolve(this.executeBreak(store, cmd));
  231. } else if (cmd instanceof Commands.Return) {
  232. return resolve(this.executeReturn(store, cmd));
  233. } else if (cmd instanceof Commands.IfThenElse) {
  234. return resolve(this.executeIfThenElse(store, cmd));
  235. } else if (cmd instanceof Commands.RepeatUntil) {
  236. return resolve(this.executeRepeatUntil(store, cmd));
  237. } else if (cmd instanceof Commands.While) {
  238. return resolve(this.executeWhile(store, cmd));
  239. } else if (cmd instanceof Commands.For) {
  240. return resolve(this.executeFor(store, cmd));
  241. } else if (cmd instanceof Commands.Switch) {
  242. return resolve(this.executeSwitch(store, cmd));
  243. } else if (cmd instanceof Expressions.FunctionCall) {
  244. return resolve(this.executeFunctionCall(store, cmd));
  245. } else if (cmd instanceof Commands.SysCall) {
  246. return resolve(this.executeSysCall(store, cmd));
  247. } else {
  248. return reject(ProcessorErrorFactory.unknown_command(cmd.sourceInfo))
  249. }
  250. };
  251. if(this.instruction_count % Config.suspend_threshold == 0) {
  252. //every 100th command should briefly delay its execution in order to allow the browser to process other things
  253. setTimeout(command_lambda, 5);
  254. } else {
  255. command_lambda();
  256. }
  257. })
  258. }
  259. executeSysCall (store, cmd) {
  260. const func = cmd.langFunc.bind(this);
  261. return func(store, cmd);
  262. }
  263. executeFunctionCall (store, cmd) {
  264. let func = null;
  265. if(cmd.isMainCall) {
  266. func = this.findMainFunction();
  267. } else {
  268. func = this.findFunction(cmd.id);
  269. }
  270. // if(this.function_call_stack.length >= Config.max_call_stack) {
  271. // return Promise.reject(ProcessorErrorFactory.exceeded_recursive_calls(cmd.sourceInfo));
  272. // }
  273. this.function_call_stack.push(cmd.sourceInfo);
  274. return this.runFunction(func, cmd.actualParameters, store)
  275. .then(sto => {
  276. sto.destroy();
  277. if(!Types.VOID.isCompatible(func.returnType) && sto.mode !== Modes.RETURN) {
  278. const funcName = func.name === IVProgProcessor.MAIN_INTERNAL_ID ?
  279. LanguageDefinedFunction.getMainFunctionName() : func.name;
  280. return Promise.reject(ProcessorErrorFactory.function_no_return(funcName));
  281. } else {
  282. this.function_call_stack.pop();
  283. return store;
  284. }
  285. });
  286. }
  287. executeSwitch (store, cmd) {
  288. this.context.push(Context.BREAKABLE);
  289. const caseSequence = cmd.cases.reduce( (prev,next) => {
  290. return prev.then( tuple => {
  291. if(this.ignoreSwitchCases(tuple[1])) {
  292. return Promise.resolve(tuple);
  293. } else if(tuple[0] || next.isDefault) {
  294. return this.executeCommands(tuple[1], next.commands)
  295. .then(nSto => Promise.resolve([true, nSto]));
  296. } else {
  297. const equalityInfixApp = new Expressions.InfixApp(Operators.EQ, cmd.expression, next.expression);
  298. equalityInfixApp.sourceInfo = next.sourceInfo;
  299. return this.evaluateExpression(tuple[1],equalityInfixApp).then(stoObj => stoObj.get())
  300. .then(isEqual => {
  301. if (isEqual) {
  302. return this.executeCommands(tuple[1], next.commands)
  303. .then(nSto => Promise.resolve([true, nSto]));
  304. } else {
  305. return Promise.resolve(tuple);
  306. }
  307. });
  308. }
  309. });
  310. }, Promise.resolve([false, store]));
  311. return caseSequence.then(tuple => {
  312. this.context.pop();
  313. const newStore = tuple[1];
  314. if (newStore.mode === Modes.BREAK) {
  315. newStore.mode = Modes.RUN;
  316. }
  317. return newStore;
  318. });
  319. }
  320. /**
  321. *
  322. * @param {Store} store
  323. * @param {Commands.For} cmd
  324. */
  325. executeFor (store, cmd) {
  326. //BEGIN for -> while rewrite
  327. const initCmd = new Commands.Assign(cmd.for_id.id, cmd.for_from);
  328. initCmd.sourceInfo = cmd.sourceInfo;
  329. const is_forward_exp = new Expressions.InfixApp(Operators.GE, cmd.for_to, cmd.for_from);
  330. return this.evaluateExpression(store, is_forward_exp).then (result => {
  331. const is_forward = result.value;
  332. let condition = null;
  333. if (is_forward) {
  334. condition = new Expressions.InfixApp(Operators.LE, cmd.for_id, cmd.for_to);
  335. } else {
  336. condition = new Expressions.InfixApp(Operators.GE, cmd.for_id, cmd.for_to);
  337. }
  338. condition.sourceInfo = cmd.sourceInfo;
  339. let pass_value = cmd.for_pass;
  340. if(pass_value == null) {
  341. if(is_forward) {
  342. pass_value = new Expressions.IntLiteral(toInt(1));
  343. } else {
  344. pass_value = new Expressions.IntLiteral(toInt(-1));
  345. }
  346. }
  347. const increment = new Commands.Assign(cmd.for_id.id,
  348. new Expressions.InfixApp(Operators.ADD, cmd.for_id, pass_value));
  349. increment.sourceInfo = cmd.sourceInfo;
  350. const whileBlock = new Commands.CommandBlock([],
  351. cmd.commands.concat(increment));
  352. const forAsWhile = new Commands.While(condition, whileBlock);
  353. forAsWhile.sourceInfo = cmd.sourceInfo;
  354. //END for -> while rewrite
  355. const newCmdList = [initCmd,forAsWhile];
  356. return this.executeCommands(store, newCmdList);
  357. }).catch(error => Promise.reject(error));
  358. }
  359. executeRepeatUntil (store, cmd) {
  360. try {
  361. this.context.push(Context.BREAKABLE);
  362. const $newStore = this.executeCommands(store, cmd.commands);
  363. return $newStore.then(sto => {
  364. if(sto.mode === Modes.BREAK) {
  365. this.context.pop();
  366. sto.mode = Modes.RUN;
  367. return sto;
  368. }
  369. const $value = this.evaluateExpression(sto, cmd.expression);
  370. return $value.then(vl => {
  371. if (!vl.type.isCompatible(Types.BOOLEAN)) {
  372. return Promise.reject(ProcessorErrorFactory.loop_condition_type_full(cmd.sourceInfo));
  373. }
  374. if (!vl.get()) {
  375. this.context.pop();
  376. return this.executeCommand(sto, cmd);
  377. } else {
  378. this.context.pop();
  379. return sto;
  380. }
  381. })
  382. })
  383. } catch (error) {
  384. return Promise.reject(error);
  385. }
  386. }
  387. executeWhile (store, cmd) {
  388. try {
  389. this.context.push(Context.BREAKABLE);
  390. const $value = this.evaluateExpression(store, cmd.expression);
  391. return $value.then(vl => {
  392. if(vl.type.isCompatible(Types.BOOLEAN)) {
  393. if(vl.get()) {
  394. const $newStore = this.executeCommands(store, cmd.commands);
  395. return $newStore.then(sto => {
  396. this.context.pop();
  397. if (sto.mode === Modes.BREAK) {
  398. sto.mode = Modes.RUN;
  399. return sto;
  400. }
  401. return this.executeCommand(sto, cmd);
  402. });
  403. } else {
  404. this.context.pop();
  405. return store;
  406. }
  407. } else {
  408. return Promise.reject(ProcessorErrorFactory.loop_condition_type_full(cmd.expression.toString(), cmd.sourceInfo));
  409. }
  410. })
  411. } catch (error) {
  412. return Promise.reject(error);
  413. }
  414. }
  415. executeIfThenElse (store, cmd) {
  416. try {
  417. const $value = this.evaluateExpression(store, cmd.condition);
  418. return $value.then(vl => {
  419. if(vl.type.isCompatible(Types.BOOLEAN)) {
  420. if(vl.get()) {
  421. return this.executeCommands(store, cmd.ifTrue.commands);
  422. } else if( cmd.ifFalse !== null){
  423. if(cmd.ifFalse instanceof Commands.IfThenElse) {
  424. return this.executeCommand(store, cmd.ifFalse);
  425. } else {
  426. return this.executeCommands(store, cmd.ifFalse.commands);
  427. }
  428. } else {
  429. return Promise.resolve(store);
  430. }
  431. } else {
  432. return Promise.reject(ProcessorErrorFactory.if_condition_type_full(cmd.condition.toString(), cmd.sourceInfo));
  433. }
  434. });
  435. } catch (error) {
  436. return Promise.reject(error);
  437. }
  438. }
  439. executeReturn (store, cmd) {
  440. try {
  441. const funcName = store.name === IVProgProcessor.MAIN_INTERNAL_ID ?
  442. LanguageDefinedFunction.getMainFunctionName() : store.name;
  443. // console.log(funcName, store.name === IVProgProcessor.MAIN_INTERNAL_ID);
  444. const func = this.findFunction(store.name);
  445. const funcType = func.returnType;
  446. const $value = this.evaluateExpression(store, cmd.expression);
  447. return $value.then(value => {
  448. let real_value = value;
  449. if(value === null && funcType.isCompatible(Types.VOID)) {
  450. store.mode = Modes.RETURN;
  451. return Promise.resolve(store);
  452. }
  453. if (value === null || !funcType.isCompatible(value.type)) {
  454. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(funcType, value.type)) {
  455. const stringInfo = funcType.stringInfo();
  456. const info = stringInfo[0];
  457. return Promise.reject(ProcessorErrorFactory.invalid_return_type_full(funcName, info.type, info.dim, cmd.sourceInfo));
  458. }
  459. real_value = Store.doImplicitCasting(funcType, value);
  460. } else {
  461. store.insertStore('$', real_value);
  462. store.mode = Modes.RETURN;
  463. return Promise.resolve(store);
  464. }
  465. });
  466. } catch (error) {
  467. return Promise.reject(error);
  468. }
  469. }
  470. executeBreak (store, cmd) {
  471. if(this.checkContext(Context.BREAKABLE)) {
  472. store.mode = Modes.BREAK;
  473. return Promise.resolve(store);
  474. } else {
  475. return Promise.reject(ProcessorErrorFactory.unexpected_break_command_full(cmd.sourceInfo));
  476. }
  477. }
  478. executeAssign (store, cmd) {
  479. try {
  480. const inStore = store.applyStore(cmd.id);
  481. if(inStore.isConst) {
  482. return Promise.reject(ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo))
  483. }
  484. const $value = this.evaluateExpression(store, cmd.expression);
  485. return $value.then( vl => {
  486. let realValue = vl;
  487. if(!inStore.type.isCompatible(realValue.type)) {
  488. if(Config.enable_type_casting && Store.canImplicitTypeCast(inStore.type, vl.type)) {
  489. realValue = Store.doImplicitCasting(inStore.type, realValue);
  490. } else {
  491. const stringInfo = inStore.type.stringInfo()
  492. const info = stringInfo[0]
  493. const exp_type_string_info = vl.type.stringInfo();
  494. const exp_type_info = exp_type_string_info[0];
  495. const exp = cmd.expression.toString();
  496. return Promise.reject(ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp, cmd.sourceInfo));
  497. }
  498. }
  499. if(inStore instanceof ArrayStoreValue) {
  500. const columns = realValue.columns == null ? 0 : realValue.columns;
  501. if(inStore.lines !== realValue.lines || inStore.columns !== columns){
  502. const exp = cmd.expression.toString();
  503. if(inStore.isVector()) {
  504. return Promise.reject(ProcessorErrorFactory.invalid_vector_assignment_full(cmd.id, inStore.lines, exp, realValue.lines, cmd.sourceInfo));
  505. } else {
  506. return Promise.reject(ProcessorErrorFactory.invalid_matrix_assignment_full(cmd.id, inStore.lines, inStore.columns, exp, realValue.lines, realValue.columns, cmd.sourceInfo));
  507. }
  508. }
  509. }
  510. store.updateStore(cmd.id, realValue)
  511. return store;
  512. });
  513. } catch (error) {
  514. return Promise.reject(error);
  515. }
  516. }
  517. executeArrayIndexAssign (store, cmd) {
  518. const mustBeArray = store.applyStore(cmd.id);
  519. let used_dims = 0;
  520. if(mustBeArray.isConst) {
  521. return Promise.reject(ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo))
  522. }
  523. if(!(mustBeArray.type instanceof ArrayType)) {
  524. return Promise.reject(ProcessorErrorFactory.invalid_array_access_full(cmd.id, cmd.sourceInfo));
  525. }
  526. const line$ = this.evaluateExpression(store, cmd.line);
  527. const column$ = this.evaluateExpression(store, cmd.column);
  528. const value$ = this.evaluateExpression(store, cmd.expression);
  529. return Promise.all([line$, column$, value$]).then(([line_sv, column_sv, value]) => {
  530. if(!Types.INTEGER.isCompatible(line_sv.type)) {
  531. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  532. }
  533. const line = line_sv.get().toNumber();
  534. used_dims += 1;
  535. let column = undefined;
  536. if (column_sv != null) {
  537. if(!Types.INTEGER.isCompatible(column_sv.type)) {
  538. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  539. }
  540. column = column_sv.get().toNumber();
  541. used_dims += 1;
  542. }
  543. let actualValue = value;
  544. if (line >= mustBeArray.lines) {
  545. if(mustBeArray.isVector) {
  546. return Promise.reject(ProcessorErrorFactory.vector_line_outbounds_full(cmd.id, line, mustBeArray.lines, cmd.sourceInfo));
  547. } else {
  548. return Promise.reject(ProcessorErrorFactory.matrix_line_outbounds_full(cmd.id, line, mustBeArray.lines, cmd.sourceInfo));
  549. }
  550. } else if (line < 0) {
  551. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo))
  552. }
  553. if (column != null && mustBeArray.columns === 0 ){
  554. return Promise.reject(ProcessorErrorFactory.vector_not_matrix_full(cmd.id, cmd.sourceInfo));
  555. }
  556. if(column != null ) {
  557. if (column >= mustBeArray.columns) {
  558. return Promise.reject(ProcessorErrorFactory.matrix_column_outbounds_full(cmd.id, column,mustBeArray.columns, cmd.sourceInfo));
  559. } else if (column < 0) {
  560. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo))
  561. }
  562. }
  563. if (!mustBeArray.type.canAccept(value.type, used_dims)) {
  564. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(mustBeArray.type.innerType, value.type)) {
  565. const type = mustBeArray.type.innerType;
  566. const stringInfo = type.stringInfo();
  567. const info = stringInfo[0];
  568. const exp_type_string_info = value.type.stringInfo();
  569. const exp_type_info = exp_type_string_info[0];
  570. const exp = cmd.expression.toString();
  571. return Promise.reject(ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp, cmd.sourceInfo));
  572. }
  573. actualValue = Store.doImplicitCasting(mustBeArray.type.innerType, value);
  574. }
  575. const current_value = mustBeArray.getAt(line, column);
  576. if(current_value instanceof ArrayStoreValue) {
  577. if(current_value.lines !== actualValue.lines || current_value.columns !== actualValue.columns){
  578. const exp = cmd.expression.toString();
  579. return Promise.reject(ProcessorErrorFactory.invalid_matrix_index_assign_full(cmd.id, line, current_value.lines, exp, actualValue.lines, cmd.sourceInfo))
  580. }
  581. }
  582. // mustBeArray.setAt(actualValue, line, column);
  583. // store.updateStore(cmd.id, mustBeArray);
  584. return store.updateStoreArray(cmd.id, actualValue, line, column);
  585. });
  586. }
  587. /**
  588. *
  589. * @param {Store} store
  590. * @param {Commands.Declaration} cmd
  591. */
  592. executeDeclaration (store, cmd) {
  593. try {
  594. let $value = Promise.resolve(null);
  595. if(cmd instanceof Commands.ArrayDeclaration) {
  596. return this.executeArrayDeclaration(store, cmd);
  597. } else {
  598. if(cmd.initial !== null) {
  599. $value = this.evaluateExpression(store, cmd.initial);
  600. }
  601. return $value.then(vl => {
  602. let realValue = vl;
  603. let temp = null;
  604. if (vl !== null) {
  605. if(!vl.type.isCompatible(cmd.type)) {
  606. if(Config.enable_type_casting && Store.canImplicitTypeCast(cmd.type, vl.type)) {
  607. realValue = Store.doImplicitCasting(cmd.type, realValue);
  608. } else {
  609. const stringInfo = vl.type.stringInfo();
  610. const info = stringInfo[0];
  611. const exp_type_string_info = vl.type.stringInfo();
  612. const exp_type_info = exp_type_string_info[0];
  613. const exp = cmd.expression.toString();
  614. return Promise.reject(ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp, cmd.sourceInfo));
  615. }
  616. }
  617. temp = new StoreValue(cmd.type, realValue.get(), null, cmd.isConst);
  618. } else {
  619. temp = new StoreValue(cmd.type, null, null, cmd.isConst);
  620. }
  621. store.insertStore(cmd.id, temp);
  622. return store;
  623. });
  624. }
  625. } catch (e) {
  626. return Promise.reject(e);
  627. }
  628. }
  629. /**
  630. *
  631. * @param {Store} store
  632. * @param {Commands.ArrayDeclaration} cmd
  633. */
  634. executeArrayDeclaration (store, cmd) {
  635. const $lines = this.evaluateExpression(store, cmd.lines);
  636. const $columns = cmd.columns === null ? null: this.evaluateExpression(store, cmd.columns);
  637. return Promise.all([$lines, $columns]).then(([line_sv, column_sv]) => {
  638. if(!Types.INTEGER.isCompatible(line_sv.type)) {
  639. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  640. }
  641. const line = line_sv.get().toNumber();
  642. if(line < 0) {
  643. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo));
  644. }
  645. let column = null
  646. if (column_sv !== null) {
  647. if(!Types.INTEGER.isCompatible(column_sv.type)) {
  648. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  649. }
  650. column = column_sv.get().toNumber();
  651. if(column < 0) {
  652. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo));
  653. }
  654. }
  655. let $value = Promise.resolve(null);
  656. if(cmd.initial !== null) {
  657. // array can only be initialized by a literal....
  658. $value = this.evaluateArrayLiteral(store, cmd.initial, cmd.type, line, column);
  659. }
  660. return $value.then(vector_list => {
  661. let temp = null;
  662. if (vector_list !== null) {
  663. temp = new ArrayStoreValue(cmd.type, vector_list, line, column, null, cmd.isConst);
  664. } else {
  665. temp = new ArrayStoreValue(cmd.type, [], line, column, null, cmd.isConst);
  666. }
  667. store.insertStore(cmd.id, temp);
  668. return store;
  669. })
  670. });
  671. }
  672. evaluateExpression (store, exp) {
  673. this.instruction_count += 1;
  674. return new Promise((resolve, reject) => {
  675. const expression_lambda = () => {
  676. if (this.mode === Modes.ABORT) {
  677. return reject(LocalizedStrings.getMessage('aborted_execution'));
  678. }
  679. if(this.instruction_count >= Config.max_instruction_count) {
  680. return reject(new Error("Número de instruções excedeu o limite definido. Verifique se seu código não possui laços infinitos ou muitas chamadas de funções recursivas."))
  681. }
  682. if (exp instanceof Expressions.UnaryApp) {
  683. return resolve(this.evaluateUnaryApp(store, exp));
  684. } else if (exp instanceof Expressions.InfixApp) {
  685. return resolve(this.evaluateInfixApp(store, exp));
  686. } else if (exp instanceof Expressions.ArrayAccess) {
  687. return resolve(this.evaluateArrayAccess(store, exp));
  688. } else if (exp instanceof Expressions.VariableLiteral) {
  689. return resolve(this.evaluateVariableLiteral(store, exp));
  690. } else if (exp instanceof Expressions.IntLiteral) {
  691. return resolve(this.evaluateLiteral(store, exp));
  692. } else if (exp instanceof Expressions.RealLiteral) {
  693. return resolve(this.evaluateLiteral(store, exp));
  694. } else if (exp instanceof Expressions.BoolLiteral) {
  695. return resolve(this.evaluateLiteral(store, exp));
  696. } else if (exp instanceof Expressions.StringLiteral) {
  697. return resolve(this.evaluateLiteral(store, exp));
  698. } else if (exp instanceof Expressions.ArrayLiteral) {
  699. return reject(new Error("Internal Error: The system should not eval an array literal."))
  700. } else if (exp instanceof Expressions.FunctionCall) {
  701. return resolve(this.evaluateFunctionCall(store, exp));
  702. }
  703. return resolve(null);
  704. };
  705. if(this.instruction_count % Config.suspend_threshold == 0) {
  706. //every 100th command should briefly delay its execution in order to allow the browser to process other things
  707. setTimeout(expression_lambda, 5);
  708. } else {
  709. expression_lambda();
  710. }
  711. });
  712. }
  713. evaluateFunctionCall (store, exp) {
  714. if(exp.isMainCall) {
  715. return Promise.reject(ProcessorErrorFactory.void_in_expression_full(LanguageDefinedFunction.getMainFunctionName(), exp.sourceInfo));
  716. }
  717. const func = this.findFunction(exp.id);
  718. if(Types.VOID.isCompatible(func.returnType)) {
  719. return Promise.reject(ProcessorErrorFactory.void_in_expression_full(exp.id, exp.sourceInfo));
  720. }
  721. if(this.function_call_stack.length >= Config.max_call_stack) {
  722. return Promise.reject(ProcessorErrorFactory.exceeded_recursive_calls(exp.sourceInfo));
  723. }
  724. this.function_call_stack.push(exp.sourceInfo);
  725. const $newStore = this.runFunction(func, exp.actualParameters, store);
  726. return $newStore.then( sto => {
  727. if(sto.mode !== Modes.RETURN) {
  728. return Promise.reject(new Error("!!!Internal error: the function that was called did not have a return command or did not set the store mode properly -> "+exp.id));
  729. }
  730. const val = sto.applyStore('$');
  731. sto.destroy();
  732. this.function_call_stack.pop();
  733. return Promise.resolve(val);
  734. });
  735. }
  736. /**
  737. *
  738. * @param {Store} store
  739. * @param {Expressions.ArrayLiteral} exp
  740. * @param {ArrayType} type
  741. */
  742. evaluateArrayLiteral (store, exp, type, lines, columns) {
  743. if(!exp.isVector) {
  744. if(columns == null) {
  745. return Promise.reject(new Error("This should never happen: Vector cannot be initialized by a matrix"));
  746. }
  747. const $matrix = this.evaluateMatrix(store, exp, type, lines, columns);
  748. return Promise.all($matrix).then(vectorList => {
  749. const values = vectorList.reduce((prev, next) => prev.concat(next), []);
  750. return Promise.resolve(values);
  751. });
  752. } else {
  753. if(columns != null) {
  754. return Promise.reject(new Error("This should never happen: Matrix cannot be initialized by a vector"));
  755. }
  756. return this.evaluateVector(store, exp, type, lines).then(list => {
  757. return Promise.resolve(list);
  758. });
  759. }
  760. }
  761. /**
  762. * Evalautes a list of literals and expression composing the vector
  763. * @param {Store} store
  764. * @param {Expressions.ArrayLiteral} exps
  765. * @param {ArrayType} type
  766. * @param {number} n_elements
  767. * @returns {Promise<StoreValue[]>} store object list
  768. */
  769. evaluateVector (store, exps, type, n_elements) {
  770. const values = exps.value;
  771. if(n_elements !== values.length) {
  772. return Promise.reject(ProcessorErrorFactory.invalid_number_elements_vector(n_elements, exps.toString(), values.length, exps.sourceInfo));
  773. }
  774. const actual_values = Promise.all(values.map( exp => this.evaluateExpression(store, exp)));
  775. return actual_values.then( values => {
  776. return values.map((v, index) => {
  777. if(!type.canAccept(v.type, 1)) {
  778. if (!Config.enable_type_casting || !Store.canImplicitTypeCast(type.innerType, v.type)) {
  779. // const stringInfo = v.type.stringInfo();
  780. // const info = stringInfo[0];
  781. const exp_str = values[index].toString();
  782. // TODO - fix error message
  783. return Promise.reject(ProcessorErrorFactory.invalid_array_literal_type_full(exp_str, values[index].sourceInfo));
  784. }
  785. const new_value = Store.doImplicitCasting(type.innerType, v);
  786. return new_value;
  787. }
  788. return v;
  789. });
  790. });
  791. }
  792. /**
  793. * Evaluates a list of array literals composing the matrix
  794. * @param {Store} store
  795. * @param {Expressions.ArrayLiteral} exps
  796. * @param {ArrayType} type
  797. * @returns {Promise<StoreValue[]>[]}
  798. */
  799. evaluateMatrix (store, exps, type, lines, columns) {
  800. const values = exps.value;
  801. if(values.length !== lines) {
  802. return Promise.reject(ProcessorErrorFactory.invalid_number_lines_matrix(lines,exps.toString(),values.length, exps.sourceInfo));
  803. }
  804. return values.map( vector => {
  805. const vec_type = new ArrayType(type.innerType, 1);
  806. return this.evaluateVector(store, vector, vec_type, columns);
  807. });
  808. }
  809. evaluateLiteral (_, exp) {
  810. return Promise.resolve(new StoreValue(exp.type, exp.value));
  811. }
  812. evaluateVariableLiteral (store, exp) {
  813. try {
  814. const val = store.applyStore(exp.id);
  815. return Promise.resolve(val);
  816. } catch (error) {
  817. return Promise.reject(error);
  818. }
  819. }
  820. evaluateArrayAccess (store, exp) {
  821. const mustBeArray = store.getStoreObject(exp.id);
  822. if (!(mustBeArray.type instanceof ArrayType)) {
  823. return Promise.reject(ProcessorErrorFactory.invalid_array_access_full(exp.id, exp.sourceInfo));
  824. }
  825. const $line = this.evaluateExpression(store, exp.line);
  826. const $column = this.evaluateExpression(store, exp.column);
  827. return Promise.all([$line, $column]).then(([line_sv, column_sv]) => {
  828. if(!Types.INTEGER.isCompatible(line_sv.type)) {
  829. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(exp.sourceInfo));
  830. }
  831. const line = line_sv.get().toNumber();
  832. let column = null;
  833. if(column_sv !== null) {
  834. if(!Types.INTEGER.isCompatible(column_sv.type)) {
  835. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(exp.sourceInfo));
  836. }
  837. column = column_sv.get().toNumber();
  838. }
  839. if (line >= mustBeArray.lines) {
  840. if(mustBeArray.isVector) {
  841. return Promise.reject(ProcessorErrorFactory.vector_line_outbounds_full(exp.id, line, mustBeArray.lines, exp.sourceInfo));
  842. } else {
  843. return Promise.reject(ProcessorErrorFactory.matrix_line_outbounds_full(exp.id, line, mustBeArray.lines, exp.sourceInfo));
  844. }
  845. } else if (line < 0) {
  846. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(exp.sourceInfo));
  847. }
  848. if (column !== null && mustBeArray.columns === 0 ){
  849. return Promise.reject(ProcessorErrorFactory.vector_not_matrix_full(exp.id, exp.sourceInfo));
  850. }
  851. if(column !== null ) {
  852. if (column >= mustBeArray.columns) {
  853. return Promise.reject(ProcessorErrorFactory.matrix_column_outbounds_full(exp.id, column,mustBeArray.columns, exp.sourceInfo));
  854. } else if (column < 0) {
  855. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(exp.sourceInfo));
  856. }
  857. }
  858. const result = mustBeArray.getAt(line, column);
  859. const type = mustBeArray.type.innerType;
  860. if(Array.isArray(result)) {
  861. const values = result.map((v, c) => {
  862. return new StoreValueAddress(type, v, line, c, mustBeArray.id, mustBeArray.readOnly);
  863. });
  864. return Promise.resolve(new ArrayStoreValue(new ArrayType(type, 1),
  865. values, mustBeArray.columns, null, mustBeArray.id, mustBeArray.readOnly))
  866. } else {
  867. return Promise.resolve(new StoreValueAddress(type, result, line, column, mustBeArray.id, mustBeArray.readOnly));
  868. }
  869. });
  870. }
  871. evaluateUnaryApp (store, unaryApp) {
  872. const $left = this.evaluateExpression(store, unaryApp.left);
  873. return $left.then( left => {
  874. const resultType = resultTypeAfterUnaryOp(unaryApp.op, left.type);
  875. if (Types.UNDEFINED.isCompatible(resultType)) {
  876. const stringInfo = left.type.stringInfo();
  877. const info = stringInfo[0];
  878. return Promise.reject(ProcessorErrorFactory.invalid_unary_op_full(unaryApp.op, info.type, info.dim, unaryApp.sourceInfo));
  879. }
  880. switch (unaryApp.op.ord) {
  881. case Operators.ADD.ord:
  882. return new StoreValue(resultType, left.get());
  883. case Operators.SUB.ord:
  884. return new StoreValue(resultType, left.get().negated());
  885. case Operators.NOT.ord:
  886. return new StoreValue(resultType, !left.get());
  887. default:
  888. return Promise.reject(new RuntimeError('!!!Critical Invalid UnaryApp '+ unaryApp.op));
  889. }
  890. });
  891. }
  892. evaluateInfixApp (store, infixApp) {
  893. const $left = this.evaluateExpression(store, infixApp.left);
  894. const $right = this.evaluateExpression(store, infixApp.right);
  895. return Promise.all([$left, $right]).then(values => {
  896. let shouldImplicitCast = false;
  897. const left = values[0];
  898. const right = values[1];
  899. let resultType = resultTypeAfterInfixOp(infixApp.op, left.type, right.type);
  900. if (Types.UNDEFINED.isCompatible(resultType)) {
  901. if (Config.enable_type_casting && Store.canImplicitTypeCast(left.type, right.type)) {
  902. shouldImplicitCast = true;
  903. } else {
  904. const stringInfoLeft = left.type.stringInfo();
  905. const infoLeft = stringInfoLeft[0];
  906. const stringInfoRight = right.type.stringInfo();
  907. const infoRight = stringInfoRight[0];
  908. return Promise.reject(ProcessorErrorFactory.invalid_infix_op_full(infixApp.op, infoLeft.type, infoLeft.dim,
  909. infoRight.type,infoRight.dim,infixApp.sourceInfo));
  910. }
  911. }
  912. let result = null;
  913. switch (infixApp.op.ord) {
  914. case Operators.ADD.ord: {
  915. if(Types.STRING.isCompatible(left.type)) {
  916. const rightStr = convertToString(right.get(), right.type);
  917. return new StoreValue(resultType, (left.get() + rightStr));
  918. } else if (Types.STRING.isCompatible(right.type)) {
  919. const leftStr = convertToString(left.get(), left.type);
  920. return new StoreValue(resultType, (leftStr + right.get()));
  921. } else {
  922. return new StoreValue(resultType, (left.get().plus(right.get())));
  923. }
  924. }
  925. case Operators.SUB.ord:
  926. return new StoreValue(resultType, (left.get().minus(right.get())));
  927. case Operators.MULT.ord: {
  928. result = left.get().times(right.get());
  929. return new StoreValue(resultType, result);
  930. }
  931. case Operators.DIV.ord: {
  932. if(right.get() == 0) {
  933. return Promise.reject(ProcessorErrorFactory.divsion_by_zero_full(infixApp.toString(), infixApp.sourceInfo));
  934. }
  935. if (Types.INTEGER.isCompatible(resultType))
  936. result = left.get().divToInt(right.get());
  937. else
  938. result = left.get().div(right.get());
  939. return new StoreValue(resultType, (result));
  940. }
  941. case Operators.MOD.ord: {
  942. let leftValue = left.get();
  943. let rightValue = right.get();
  944. if(shouldImplicitCast) {
  945. resultType = Types.INTEGER;
  946. leftValue = leftValue.trunc();
  947. rightValue = rightValue.trunc();
  948. }
  949. result = leftValue.modulo(rightValue);
  950. return new StoreValue(resultType, (result));
  951. }
  952. case Operators.GT.ord: {
  953. let leftValue = left.get();
  954. let rightValue = right.get();
  955. if (Types.STRING.isCompatible(left.type)) {
  956. result = leftValue.length > rightValue.length;
  957. } else {
  958. if (shouldImplicitCast) {
  959. resultType = Types.BOOLEAN;
  960. leftValue = leftValue.trunc();
  961. rightValue = rightValue.trunc();
  962. }
  963. result = leftValue.gt(rightValue);
  964. }
  965. return new StoreValue(resultType, result);
  966. }
  967. case Operators.GE.ord: {
  968. let leftValue = left.get();
  969. let rightValue = right.get();
  970. if (Types.STRING.isCompatible(left.type)) {
  971. result = leftValue.length >= rightValue.length;
  972. } else {
  973. if (shouldImplicitCast) {
  974. resultType = Types.BOOLEAN;
  975. leftValue = leftValue.trunc();
  976. rightValue = rightValue.trunc();
  977. }
  978. result = leftValue.gte(rightValue);
  979. }
  980. return new StoreValue(resultType, result);
  981. }
  982. case Operators.LT.ord: {
  983. let leftValue = left.get();
  984. let rightValue = right.get();
  985. if (Types.STRING.isCompatible(left.type)) {
  986. result = leftValue.length < rightValue.length;
  987. } else {
  988. if (shouldImplicitCast) {
  989. resultType = Types.BOOLEAN;
  990. leftValue = leftValue.trunc();
  991. rightValue = rightValue.trunc();
  992. }
  993. result = leftValue.lt(rightValue);
  994. }
  995. return new StoreValue(resultType, (result));
  996. }
  997. case Operators.LE.ord: {
  998. let leftValue = left.get();
  999. let rightValue = right.get();
  1000. if (Types.STRING.isCompatible(left.type)) {
  1001. result = leftValue.length <= rightValue.length;
  1002. } else {
  1003. if (shouldImplicitCast) {
  1004. resultType = Types.BOOLEAN;
  1005. leftValue = leftValue.trunc();
  1006. rightValue = rightValue.trunc();
  1007. }
  1008. result = leftValue.lte(rightValue);
  1009. }
  1010. return new StoreValue(resultType, result);
  1011. }
  1012. case Operators.EQ.ord: {
  1013. let leftValue = left.get();
  1014. let rightValue = right.get();
  1015. if (Types.INTEGER.isCompatible(left.type) || Types.REAL.isCompatible(left.type)) {
  1016. if (shouldImplicitCast) {
  1017. resultType = Types.BOOLEAN;
  1018. leftValue = leftValue.trunc();
  1019. rightValue = rightValue.trunc();
  1020. }
  1021. result = leftValue.eq(rightValue);
  1022. } else {
  1023. result = leftValue === rightValue;
  1024. }
  1025. return new StoreValue(resultType, result);
  1026. }
  1027. case Operators.NEQ.ord: {
  1028. let leftValue = left.get();
  1029. let rightValue = right.get();
  1030. if (Types.INTEGER.isCompatible(left.type) || Types.REAL.isCompatible(left.type)) {
  1031. if (shouldImplicitCast) {
  1032. resultType = Types.BOOLEAN;
  1033. leftValue = leftValue.trunc();
  1034. rightValue = rightValue.trunc();
  1035. }
  1036. result = !leftValue.eq(rightValue);
  1037. } else {
  1038. result = leftValue !== rightValue;
  1039. }
  1040. return new StoreValue(resultType, result);
  1041. }
  1042. case Operators.AND.ord:
  1043. return new StoreValue(resultType, (left.get() && right.get()));
  1044. case Operators.OR.ord:
  1045. return new StoreValue(resultType, (left.get() || right.get()));
  1046. default:
  1047. return Promise.reject(new RuntimeError('!!!Critical Invalid InfixApp '+ infixApp.op));
  1048. }
  1049. });
  1050. }
  1051. }