ivprogProcessor.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 expression_tuple = []; //(for conditional, is for increasing)
  330. if(cmd.for_pass == null) {
  331. expression_tuple.push(Promise.resolve(null));
  332. expression_tuple.push(this.evaluateExpression(store, new Expressions.InfixApp(Operators.GE, cmd.for_to, cmd.for_from)));
  333. } else {
  334. expression_tuple.push(this.evaluateExpression(store, new Expressions.InfixApp(Operators.GE, cmd.for_pass, new Expressions.IntLiteral(toInt(0)))));
  335. expression_tuple.push(Promise.resolve(null));
  336. }
  337. return Promise.all(expression_tuple).then (results => {
  338. // console.log(results);
  339. let is_forward = true;
  340. let is_end_gt_init = undefined;
  341. let condition = null;
  342. let pass_value = cmd.for_pass;
  343. if (results[0] == null) {
  344. // pass is null, we need to deduce a value for it
  345. is_end_gt_init = results[1].value;
  346. } else {
  347. is_forward = results[0].value
  348. }
  349. if(is_end_gt_init == null) {
  350. // console.log("pass is not null and is forward? ", is_forward);
  351. if(is_forward) {
  352. condition = new Expressions.InfixApp(Operators.LE, cmd.for_id, cmd.for_to);
  353. } else {
  354. condition = new Expressions.InfixApp(Operators.GE, cmd.for_id, cmd.for_to);
  355. }
  356. // console.log("Cond", condition);
  357. } else if(is_end_gt_init) {
  358. pass_value = new Expressions.IntLiteral(toInt(1));
  359. condition = new Expressions.InfixApp(Operators.LE, cmd.for_id, cmd.for_to);
  360. } else {
  361. pass_value = new Expressions.IntLiteral(toInt(-1));
  362. condition = new Expressions.InfixApp(Operators.GE, cmd.for_id, cmd.for_to);
  363. }
  364. condition.sourceInfo = cmd.sourceInfo;
  365. const increment = new Commands.Assign(cmd.for_id.id,
  366. new Expressions.InfixApp(Operators.ADD, cmd.for_id, pass_value));
  367. increment.sourceInfo = cmd.sourceInfo;
  368. const whileBlock = new Commands.CommandBlock([],
  369. cmd.commands.concat(increment));
  370. const forAsWhile = new Commands.While(condition, whileBlock);
  371. forAsWhile.sourceInfo = cmd.sourceInfo;
  372. //END for -> while rewrite
  373. const newCmdList = [initCmd,forAsWhile];
  374. return this.executeCommands(store, newCmdList);
  375. }).catch(error => Promise.reject(error));
  376. }
  377. executeRepeatUntil (store, cmd) {
  378. try {
  379. this.context.push(Context.BREAKABLE);
  380. const $newStore = this.executeCommands(store, cmd.commands);
  381. return $newStore.then(sto => {
  382. if(sto.mode === Modes.BREAK) {
  383. this.context.pop();
  384. sto.mode = Modes.RUN;
  385. return sto;
  386. }
  387. const $value = this.evaluateExpression(sto, cmd.expression);
  388. return $value.then(vl => {
  389. if (!vl.type.isCompatible(Types.BOOLEAN)) {
  390. return Promise.reject(ProcessorErrorFactory.loop_condition_type_full(cmd.sourceInfo));
  391. }
  392. if (!vl.get()) {
  393. this.context.pop();
  394. return this.executeCommand(sto, cmd);
  395. } else {
  396. this.context.pop();
  397. return sto;
  398. }
  399. })
  400. })
  401. } catch (error) {
  402. return Promise.reject(error);
  403. }
  404. }
  405. executeWhile (store, cmd) {
  406. try {
  407. this.context.push(Context.BREAKABLE);
  408. const $value = this.evaluateExpression(store, cmd.expression);
  409. return $value.then(vl => {
  410. if(vl.type.isCompatible(Types.BOOLEAN)) {
  411. if(vl.get()) {
  412. const $newStore = this.executeCommands(store, cmd.commands);
  413. return $newStore.then(sto => {
  414. this.context.pop();
  415. if (sto.mode === Modes.BREAK) {
  416. sto.mode = Modes.RUN;
  417. return sto;
  418. }
  419. return this.executeCommand(sto, cmd);
  420. });
  421. } else {
  422. this.context.pop();
  423. return store;
  424. }
  425. } else {
  426. return Promise.reject(ProcessorErrorFactory.loop_condition_type_full(cmd.expression.toString(), cmd.sourceInfo));
  427. }
  428. })
  429. } catch (error) {
  430. return Promise.reject(error);
  431. }
  432. }
  433. executeIfThenElse (store, cmd) {
  434. try {
  435. const $value = this.evaluateExpression(store, cmd.condition);
  436. return $value.then(vl => {
  437. if(vl.type.isCompatible(Types.BOOLEAN)) {
  438. if(vl.get()) {
  439. return this.executeCommands(store, cmd.ifTrue.commands);
  440. } else if( cmd.ifFalse !== null){
  441. if(cmd.ifFalse instanceof Commands.IfThenElse) {
  442. return this.executeCommand(store, cmd.ifFalse);
  443. } else {
  444. return this.executeCommands(store, cmd.ifFalse.commands);
  445. }
  446. } else {
  447. return Promise.resolve(store);
  448. }
  449. } else {
  450. return Promise.reject(ProcessorErrorFactory.if_condition_type_full(cmd.condition.toString(), cmd.sourceInfo));
  451. }
  452. });
  453. } catch (error) {
  454. return Promise.reject(error);
  455. }
  456. }
  457. executeReturn (store, cmd) {
  458. try {
  459. const funcName = store.name === IVProgProcessor.MAIN_INTERNAL_ID ?
  460. LanguageDefinedFunction.getMainFunctionName() : store.name;
  461. // console.log(funcName, store.name === IVProgProcessor.MAIN_INTERNAL_ID);
  462. const func = this.findFunction(store.name);
  463. const funcType = func.returnType;
  464. const $value = this.evaluateExpression(store, cmd.expression);
  465. return $value.then(value => {
  466. let real_value = value;
  467. if(value === null && funcType.isCompatible(Types.VOID)) {
  468. store.mode = Modes.RETURN;
  469. return Promise.resolve(store);
  470. }
  471. if (value === null || !funcType.isCompatible(value.type)) {
  472. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(funcType, value.type)) {
  473. const stringInfo = funcType.stringInfo();
  474. const info = stringInfo[0];
  475. return Promise.reject(ProcessorErrorFactory.invalid_return_type_full(funcName, info.type, info.dim, cmd.sourceInfo));
  476. }
  477. real_value = Store.doImplicitCasting(funcType, value);
  478. } else {
  479. store.insertStore('$', real_value);
  480. store.mode = Modes.RETURN;
  481. return Promise.resolve(store);
  482. }
  483. });
  484. } catch (error) {
  485. return Promise.reject(error);
  486. }
  487. }
  488. executeBreak (store, cmd) {
  489. if(this.checkContext(Context.BREAKABLE)) {
  490. store.mode = Modes.BREAK;
  491. return Promise.resolve(store);
  492. } else {
  493. return Promise.reject(ProcessorErrorFactory.unexpected_break_command_full(cmd.sourceInfo));
  494. }
  495. }
  496. executeAssign (store, cmd) {
  497. try {
  498. const inStore = store.applyStore(cmd.id);
  499. if(inStore.isConst) {
  500. return Promise.reject(ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo))
  501. }
  502. const $value = this.evaluateExpression(store, cmd.expression);
  503. return $value.then( vl => {
  504. let realValue = vl;
  505. if(!inStore.type.isCompatible(realValue.type)) {
  506. if(Config.enable_type_casting && Store.canImplicitTypeCast(inStore.type, vl.type)) {
  507. realValue = Store.doImplicitCasting(inStore.type, realValue);
  508. } else {
  509. const stringInfo = inStore.type.stringInfo()
  510. const info = stringInfo[0]
  511. const exp_type_string_info = vl.type.stringInfo();
  512. const exp_type_info = exp_type_string_info[0];
  513. const exp = cmd.expression.toString();
  514. return Promise.reject(ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp, cmd.sourceInfo));
  515. }
  516. }
  517. if(inStore instanceof ArrayStoreValue) {
  518. const columns = realValue.columns == null ? 0 : realValue.columns;
  519. if(inStore.lines !== realValue.lines || inStore.columns !== columns){
  520. const exp = cmd.expression.toString();
  521. if(inStore.isVector()) {
  522. return Promise.reject(ProcessorErrorFactory.invalid_vector_assignment_full(cmd.id, inStore.lines, exp, realValue.lines, cmd.sourceInfo));
  523. } else {
  524. return Promise.reject(ProcessorErrorFactory.invalid_matrix_assignment_full(cmd.id, inStore.lines, inStore.columns, exp, realValue.lines, realValue.columns, cmd.sourceInfo));
  525. }
  526. }
  527. }
  528. store.updateStore(cmd.id, realValue)
  529. return store;
  530. });
  531. } catch (error) {
  532. return Promise.reject(error);
  533. }
  534. }
  535. executeArrayIndexAssign (store, cmd) {
  536. const mustBeArray = store.applyStore(cmd.id);
  537. let used_dims = 0;
  538. if(mustBeArray.isConst) {
  539. return Promise.reject(ProcessorErrorFactory.invalid_const_assignment_full(cmd.id, cmd.sourceInfo))
  540. }
  541. if(!(mustBeArray.type instanceof ArrayType)) {
  542. return Promise.reject(ProcessorErrorFactory.invalid_array_access_full(cmd.id, cmd.sourceInfo));
  543. }
  544. const line$ = this.evaluateExpression(store, cmd.line);
  545. const column$ = this.evaluateExpression(store, cmd.column);
  546. const value$ = this.evaluateExpression(store, cmd.expression);
  547. return Promise.all([line$, column$, value$]).then(([line_sv, column_sv, value]) => {
  548. if(!Types.INTEGER.isCompatible(line_sv.type)) {
  549. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  550. }
  551. const line = line_sv.get().toNumber();
  552. used_dims += 1;
  553. let column = undefined;
  554. if (column_sv != null) {
  555. if(!Types.INTEGER.isCompatible(column_sv.type)) {
  556. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  557. }
  558. column = column_sv.get().toNumber();
  559. used_dims += 1;
  560. }
  561. let actualValue = value;
  562. if (line >= mustBeArray.lines) {
  563. if(mustBeArray.isVector) {
  564. return Promise.reject(ProcessorErrorFactory.vector_line_outbounds_full(cmd.id, line, mustBeArray.lines, cmd.sourceInfo));
  565. } else {
  566. return Promise.reject(ProcessorErrorFactory.matrix_line_outbounds_full(cmd.id, line, mustBeArray.lines, cmd.sourceInfo));
  567. }
  568. } else if (line < 0) {
  569. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo))
  570. }
  571. if (column != null && mustBeArray.columns === 0 ){
  572. return Promise.reject(ProcessorErrorFactory.vector_not_matrix_full(cmd.id, cmd.sourceInfo));
  573. }
  574. if(column != null ) {
  575. if (column >= mustBeArray.columns) {
  576. return Promise.reject(ProcessorErrorFactory.matrix_column_outbounds_full(cmd.id, column,mustBeArray.columns, cmd.sourceInfo));
  577. } else if (column < 0) {
  578. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo))
  579. }
  580. }
  581. if (!mustBeArray.type.canAccept(value.type, used_dims)) {
  582. if(!Config.enable_type_casting || !Store.canImplicitTypeCast(mustBeArray.type.innerType, value.type)) {
  583. const type = mustBeArray.type.innerType;
  584. const stringInfo = type.stringInfo();
  585. const info = stringInfo[0];
  586. const exp_type_string_info = value.type.stringInfo();
  587. const exp_type_info = exp_type_string_info[0];
  588. const exp = cmd.expression.toString();
  589. return Promise.reject(ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp, cmd.sourceInfo));
  590. }
  591. actualValue = Store.doImplicitCasting(mustBeArray.type.innerType, value);
  592. }
  593. const current_value = mustBeArray.getAt(line, column);
  594. if(current_value instanceof ArrayStoreValue) {
  595. if(current_value.lines !== actualValue.lines || current_value.columns !== actualValue.columns){
  596. const exp = cmd.expression.toString();
  597. return Promise.reject(ProcessorErrorFactory.invalid_matrix_index_assign_full(cmd.id, line, current_value.lines, exp, actualValue.lines, cmd.sourceInfo))
  598. }
  599. }
  600. // mustBeArray.setAt(actualValue, line, column);
  601. // store.updateStore(cmd.id, mustBeArray);
  602. return store.updateStoreArray(cmd.id, actualValue, line, column);
  603. });
  604. }
  605. /**
  606. *
  607. * @param {Store} store
  608. * @param {Commands.Declaration} cmd
  609. */
  610. executeDeclaration (store, cmd) {
  611. try {
  612. let $value = Promise.resolve(null);
  613. if(cmd instanceof Commands.ArrayDeclaration) {
  614. return this.executeArrayDeclaration(store, cmd);
  615. } else {
  616. if(cmd.initial !== null) {
  617. $value = this.evaluateExpression(store, cmd.initial);
  618. }
  619. return $value.then(vl => {
  620. let realValue = vl;
  621. let temp = null;
  622. if (vl !== null) {
  623. if(!vl.type.isCompatible(cmd.type)) {
  624. if(Config.enable_type_casting && Store.canImplicitTypeCast(cmd.type, vl.type)) {
  625. realValue = Store.doImplicitCasting(cmd.type, realValue);
  626. } else {
  627. const stringInfo = vl.type.stringInfo();
  628. const info = stringInfo[0];
  629. const exp_type_string_info = vl.type.stringInfo();
  630. const exp_type_info = exp_type_string_info[0];
  631. const exp = cmd.expression.toString();
  632. return Promise.reject(ProcessorErrorFactory.incompatible_types_full(info.type, info.dim, exp_type_info.type, exp_type_info.dim, exp, cmd.sourceInfo));
  633. }
  634. }
  635. temp = new StoreValue(cmd.type, realValue.get(), null, cmd.isConst);
  636. } else {
  637. temp = new StoreValue(cmd.type, null, null, cmd.isConst);
  638. }
  639. store.insertStore(cmd.id, temp);
  640. return store;
  641. });
  642. }
  643. } catch (e) {
  644. return Promise.reject(e);
  645. }
  646. }
  647. /**
  648. *
  649. * @param {Store} store
  650. * @param {Commands.ArrayDeclaration} cmd
  651. */
  652. executeArrayDeclaration (store, cmd) {
  653. const $lines = this.evaluateExpression(store, cmd.lines);
  654. const $columns = cmd.columns === null ? null: this.evaluateExpression(store, cmd.columns);
  655. return Promise.all([$lines, $columns]).then(([line_sv, column_sv]) => {
  656. if(!Types.INTEGER.isCompatible(line_sv.type)) {
  657. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  658. }
  659. const line = line_sv.get().toNumber();
  660. if(line < 0) {
  661. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo));
  662. }
  663. let column = null
  664. if (column_sv !== null) {
  665. if(!Types.INTEGER.isCompatible(column_sv.type)) {
  666. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(cmd.sourceInfo));
  667. }
  668. column = column_sv.get().toNumber();
  669. if(column < 0) {
  670. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(cmd.sourceInfo));
  671. }
  672. }
  673. let $value = Promise.resolve(null);
  674. if(cmd.initial !== null) {
  675. // array can only be initialized by a literal....
  676. $value = this.evaluateArrayLiteral(store, cmd.initial, cmd.type, line, column);
  677. }
  678. return $value.then(vector_list => {
  679. let temp = null;
  680. if (vector_list !== null) {
  681. temp = new ArrayStoreValue(cmd.type, vector_list, line, column, null, cmd.isConst);
  682. } else {
  683. temp = new ArrayStoreValue(cmd.type, [], line, column, null, cmd.isConst);
  684. }
  685. store.insertStore(cmd.id, temp);
  686. return store;
  687. })
  688. });
  689. }
  690. evaluateExpression (store, exp) {
  691. this.instruction_count += 1;
  692. return new Promise((resolve, reject) => {
  693. const expression_lambda = () => {
  694. if (this.mode === Modes.ABORT) {
  695. return reject(LocalizedStrings.getMessage('aborted_execution'));
  696. }
  697. if(this.instruction_count >= Config.max_instruction_count) {
  698. 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."))
  699. }
  700. if (exp instanceof Expressions.UnaryApp) {
  701. return resolve(this.evaluateUnaryApp(store, exp));
  702. } else if (exp instanceof Expressions.InfixApp) {
  703. return resolve(this.evaluateInfixApp(store, exp));
  704. } else if (exp instanceof Expressions.ArrayAccess) {
  705. return resolve(this.evaluateArrayAccess(store, exp));
  706. } else if (exp instanceof Expressions.VariableLiteral) {
  707. return resolve(this.evaluateVariableLiteral(store, exp));
  708. } else if (exp instanceof Expressions.IntLiteral) {
  709. return resolve(this.evaluateLiteral(store, exp));
  710. } else if (exp instanceof Expressions.RealLiteral) {
  711. return resolve(this.evaluateLiteral(store, exp));
  712. } else if (exp instanceof Expressions.BoolLiteral) {
  713. return resolve(this.evaluateLiteral(store, exp));
  714. } else if (exp instanceof Expressions.StringLiteral) {
  715. return resolve(this.evaluateLiteral(store, exp));
  716. } else if (exp instanceof Expressions.ArrayLiteral) {
  717. return reject(new Error("Internal Error: The system should not eval an array literal."))
  718. } else if (exp instanceof Expressions.FunctionCall) {
  719. return resolve(this.evaluateFunctionCall(store, exp));
  720. }
  721. return resolve(null);
  722. };
  723. if(this.instruction_count % Config.suspend_threshold == 0) {
  724. //every 100th command should briefly delay its execution in order to allow the browser to process other things
  725. setTimeout(expression_lambda, 5);
  726. } else {
  727. expression_lambda();
  728. }
  729. });
  730. }
  731. evaluateFunctionCall (store, exp) {
  732. if(exp.isMainCall) {
  733. return Promise.reject(ProcessorErrorFactory.void_in_expression_full(LanguageDefinedFunction.getMainFunctionName(), exp.sourceInfo));
  734. }
  735. const func = this.findFunction(exp.id);
  736. if(Types.VOID.isCompatible(func.returnType)) {
  737. return Promise.reject(ProcessorErrorFactory.void_in_expression_full(exp.id, exp.sourceInfo));
  738. }
  739. if(this.function_call_stack.length >= Config.max_call_stack) {
  740. return Promise.reject(ProcessorErrorFactory.exceeded_recursive_calls(exp.sourceInfo));
  741. }
  742. this.function_call_stack.push(exp.sourceInfo);
  743. const $newStore = this.runFunction(func, exp.actualParameters, store);
  744. return $newStore.then( sto => {
  745. if(sto.mode !== Modes.RETURN) {
  746. 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));
  747. }
  748. const val = sto.applyStore('$');
  749. sto.destroy();
  750. this.function_call_stack.pop();
  751. return Promise.resolve(val);
  752. });
  753. }
  754. /**
  755. *
  756. * @param {Store} store
  757. * @param {Expressions.ArrayLiteral} exp
  758. * @param {ArrayType} type
  759. */
  760. evaluateArrayLiteral (store, exp, type, lines, columns) {
  761. if(!exp.isVector) {
  762. if(columns == null) {
  763. return Promise.reject(new Error("This should never happen: Vector cannot be initialized by a matrix"));
  764. }
  765. const $matrix = this.evaluateMatrix(store, exp, type, lines, columns);
  766. return Promise.all($matrix).then(vectorList => {
  767. const values = vectorList.reduce((prev, next) => prev.concat(next), []);
  768. return Promise.resolve(values);
  769. });
  770. } else {
  771. if(columns != null) {
  772. return Promise.reject(new Error("This should never happen: Matrix cannot be initialized by a vector"));
  773. }
  774. return this.evaluateVector(store, exp, type, lines).then(list => {
  775. return Promise.resolve(list);
  776. });
  777. }
  778. }
  779. /**
  780. * Evalautes a list of literals and expression composing the vector
  781. * @param {Store} store
  782. * @param {Expressions.ArrayLiteral} exps
  783. * @param {ArrayType} type
  784. * @param {number} n_elements
  785. * @returns {Promise<StoreValue[]>} store object list
  786. */
  787. evaluateVector (store, exps, type, n_elements) {
  788. const values = exps.value;
  789. if(n_elements !== values.length) {
  790. return Promise.reject(ProcessorErrorFactory.invalid_number_elements_vector(n_elements, exps.toString(), values.length, exps.sourceInfo));
  791. }
  792. const actual_values = Promise.all(values.map( exp => this.evaluateExpression(store, exp)));
  793. return actual_values.then( values => {
  794. return values.map((v, index) => {
  795. if(!type.canAccept(v.type, 1)) {
  796. if (!Config.enable_type_casting || !Store.canImplicitTypeCast(type.innerType, v.type)) {
  797. // const stringInfo = v.type.stringInfo();
  798. // const info = stringInfo[0];
  799. const exp_str = values[index].toString();
  800. // TODO - fix error message
  801. return Promise.reject(ProcessorErrorFactory.invalid_array_literal_type_full(exp_str, values[index].sourceInfo));
  802. }
  803. const new_value = Store.doImplicitCasting(type.innerType, v);
  804. return new_value;
  805. }
  806. return v;
  807. });
  808. });
  809. }
  810. /**
  811. * Evaluates a list of array literals composing the matrix
  812. * @param {Store} store
  813. * @param {Expressions.ArrayLiteral} exps
  814. * @param {ArrayType} type
  815. * @returns {Promise<StoreValue[]>[]}
  816. */
  817. evaluateMatrix (store, exps, type, lines, columns) {
  818. const values = exps.value;
  819. if(values.length !== lines) {
  820. return Promise.reject(ProcessorErrorFactory.invalid_number_lines_matrix(lines,exps.toString(),values.length, exps.sourceInfo));
  821. }
  822. return values.map( vector => {
  823. const vec_type = new ArrayType(type.innerType, 1);
  824. return this.evaluateVector(store, vector, vec_type, columns);
  825. });
  826. }
  827. evaluateLiteral (_, exp) {
  828. return Promise.resolve(new StoreValue(exp.type, exp.value));
  829. }
  830. evaluateVariableLiteral (store, exp) {
  831. try {
  832. const val = store.applyStore(exp.id);
  833. return Promise.resolve(val);
  834. } catch (error) {
  835. return Promise.reject(error);
  836. }
  837. }
  838. evaluateArrayAccess (store, exp) {
  839. const mustBeArray = store.getStoreObject(exp.id);
  840. if (!(mustBeArray.type instanceof ArrayType)) {
  841. return Promise.reject(ProcessorErrorFactory.invalid_array_access_full(exp.id, exp.sourceInfo));
  842. }
  843. const $line = this.evaluateExpression(store, exp.line);
  844. const $column = this.evaluateExpression(store, exp.column);
  845. return Promise.all([$line, $column]).then(([line_sv, column_sv]) => {
  846. if(!Types.INTEGER.isCompatible(line_sv.type)) {
  847. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(exp.sourceInfo));
  848. }
  849. const line = line_sv.get().toNumber();
  850. let column = null;
  851. if(column_sv !== null) {
  852. if(!Types.INTEGER.isCompatible(column_sv.type)) {
  853. return Promise.reject(ProcessorErrorFactory.array_dimension_not_int_full(exp.sourceInfo));
  854. }
  855. column = column_sv.get().toNumber();
  856. }
  857. if (line >= mustBeArray.lines) {
  858. if(mustBeArray.isVector) {
  859. return Promise.reject(ProcessorErrorFactory.vector_line_outbounds_full(exp.id, line, mustBeArray.lines, exp.sourceInfo));
  860. } else {
  861. return Promise.reject(ProcessorErrorFactory.matrix_line_outbounds_full(exp.id, line, mustBeArray.lines, exp.sourceInfo));
  862. }
  863. } else if (line < 0) {
  864. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(exp.sourceInfo));
  865. }
  866. if (column !== null && mustBeArray.columns === 0 ){
  867. return Promise.reject(ProcessorErrorFactory.vector_not_matrix_full(exp.id, exp.sourceInfo));
  868. }
  869. if(column !== null ) {
  870. if (column >= mustBeArray.columns) {
  871. return Promise.reject(ProcessorErrorFactory.matrix_column_outbounds_full(exp.id, column,mustBeArray.columns, exp.sourceInfo));
  872. } else if (column < 0) {
  873. return Promise.reject(ProcessorErrorFactory.array_dimension_not_positive_full(exp.sourceInfo));
  874. }
  875. }
  876. const result = mustBeArray.getAt(line, column);
  877. const type = mustBeArray.type.innerType;
  878. if(Array.isArray(result)) {
  879. const values = result.map((v, c) => {
  880. return new StoreValueAddress(type, v, line, c, mustBeArray.id, mustBeArray.readOnly);
  881. });
  882. return Promise.resolve(new ArrayStoreValue(new ArrayType(type, 1),
  883. values, mustBeArray.columns, null, mustBeArray.id, mustBeArray.readOnly))
  884. } else {
  885. return Promise.resolve(new StoreValueAddress(type, result, line, column, mustBeArray.id, mustBeArray.readOnly));
  886. }
  887. });
  888. }
  889. evaluateUnaryApp (store, unaryApp) {
  890. const $left = this.evaluateExpression(store, unaryApp.left);
  891. return $left.then( left => {
  892. const resultType = resultTypeAfterUnaryOp(unaryApp.op, left.type);
  893. if (Types.UNDEFINED.isCompatible(resultType)) {
  894. const stringInfo = left.type.stringInfo();
  895. const info = stringInfo[0];
  896. return Promise.reject(ProcessorErrorFactory.invalid_unary_op_full(unaryApp.op, info.type, info.dim, unaryApp.sourceInfo));
  897. }
  898. switch (unaryApp.op.ord) {
  899. case Operators.ADD.ord:
  900. return new StoreValue(resultType, left.get());
  901. case Operators.SUB.ord:
  902. return new StoreValue(resultType, left.get().negated());
  903. case Operators.NOT.ord:
  904. return new StoreValue(resultType, !left.get());
  905. default:
  906. return Promise.reject(new RuntimeError('!!!Critical Invalid UnaryApp '+ unaryApp.op));
  907. }
  908. });
  909. }
  910. evaluateInfixApp (store, infixApp) {
  911. const $left = this.evaluateExpression(store, infixApp.left);
  912. const $right = this.evaluateExpression(store, infixApp.right);
  913. return Promise.all([$left, $right]).then(values => {
  914. let shouldImplicitCast = false;
  915. const left = values[0];
  916. const right = values[1];
  917. let resultType = resultTypeAfterInfixOp(infixApp.op, left.type, right.type);
  918. if (Types.UNDEFINED.isCompatible(resultType)) {
  919. if (Config.enable_type_casting && Store.canImplicitTypeCast(left.type, right.type)) {
  920. shouldImplicitCast = true;
  921. } else {
  922. const stringInfoLeft = left.type.stringInfo();
  923. const infoLeft = stringInfoLeft[0];
  924. const stringInfoRight = right.type.stringInfo();
  925. const infoRight = stringInfoRight[0];
  926. return Promise.reject(ProcessorErrorFactory.invalid_infix_op_full(infixApp.op, infoLeft.type, infoLeft.dim,
  927. infoRight.type,infoRight.dim,infixApp.sourceInfo));
  928. }
  929. }
  930. let result = null;
  931. switch (infixApp.op.ord) {
  932. case Operators.ADD.ord: {
  933. if(Types.STRING.isCompatible(left.type)) {
  934. const rightStr = convertToString(right.get(), right.type);
  935. return new StoreValue(resultType, (left.get() + rightStr));
  936. } else if (Types.STRING.isCompatible(right.type)) {
  937. const leftStr = convertToString(left.get(), left.type);
  938. return new StoreValue(resultType, (leftStr + right.get()));
  939. } else {
  940. return new StoreValue(resultType, (left.get().plus(right.get())));
  941. }
  942. }
  943. case Operators.SUB.ord:
  944. return new StoreValue(resultType, (left.get().minus(right.get())));
  945. case Operators.MULT.ord: {
  946. result = left.get().times(right.get());
  947. return new StoreValue(resultType, result);
  948. }
  949. case Operators.DIV.ord: {
  950. if(right.get() == 0) {
  951. return Promise.reject(ProcessorErrorFactory.divsion_by_zero_full(infixApp.toString(), infixApp.sourceInfo));
  952. }
  953. if (Types.INTEGER.isCompatible(resultType))
  954. result = left.get().divToInt(right.get());
  955. else
  956. result = left.get().div(right.get());
  957. return new StoreValue(resultType, (result));
  958. }
  959. case Operators.MOD.ord: {
  960. let leftValue = left.get();
  961. let rightValue = right.get();
  962. if(shouldImplicitCast) {
  963. resultType = Types.INTEGER;
  964. leftValue = leftValue.trunc();
  965. rightValue = rightValue.trunc();
  966. }
  967. result = leftValue.modulo(rightValue);
  968. return new StoreValue(resultType, (result));
  969. }
  970. case Operators.GT.ord: {
  971. let leftValue = left.get();
  972. let rightValue = right.get();
  973. if (Types.STRING.isCompatible(left.type)) {
  974. result = leftValue.length > rightValue.length;
  975. } else {
  976. if (shouldImplicitCast) {
  977. resultType = Types.BOOLEAN;
  978. leftValue = leftValue.trunc();
  979. rightValue = rightValue.trunc();
  980. }
  981. result = leftValue.gt(rightValue);
  982. }
  983. return new StoreValue(resultType, result);
  984. }
  985. case Operators.GE.ord: {
  986. let leftValue = left.get();
  987. let rightValue = right.get();
  988. if (Types.STRING.isCompatible(left.type)) {
  989. result = leftValue.length >= rightValue.length;
  990. } else {
  991. if (shouldImplicitCast) {
  992. resultType = Types.BOOLEAN;
  993. leftValue = leftValue.trunc();
  994. rightValue = rightValue.trunc();
  995. }
  996. result = leftValue.gte(rightValue);
  997. }
  998. return new StoreValue(resultType, result);
  999. }
  1000. case Operators.LT.ord: {
  1001. let leftValue = left.get();
  1002. let rightValue = right.get();
  1003. if (Types.STRING.isCompatible(left.type)) {
  1004. result = leftValue.length < rightValue.length;
  1005. } else {
  1006. if (shouldImplicitCast) {
  1007. resultType = Types.BOOLEAN;
  1008. leftValue = leftValue.trunc();
  1009. rightValue = rightValue.trunc();
  1010. }
  1011. result = leftValue.lt(rightValue);
  1012. }
  1013. return new StoreValue(resultType, (result));
  1014. }
  1015. case Operators.LE.ord: {
  1016. let leftValue = left.get();
  1017. let rightValue = right.get();
  1018. if (Types.STRING.isCompatible(left.type)) {
  1019. result = leftValue.length <= rightValue.length;
  1020. } else {
  1021. if (shouldImplicitCast) {
  1022. resultType = Types.BOOLEAN;
  1023. leftValue = leftValue.trunc();
  1024. rightValue = rightValue.trunc();
  1025. }
  1026. result = leftValue.lte(rightValue);
  1027. }
  1028. return new StoreValue(resultType, result);
  1029. }
  1030. case Operators.EQ.ord: {
  1031. let leftValue = left.get();
  1032. let rightValue = right.get();
  1033. if (Types.INTEGER.isCompatible(left.type) || Types.REAL.isCompatible(left.type)) {
  1034. if (shouldImplicitCast) {
  1035. resultType = Types.BOOLEAN;
  1036. leftValue = leftValue.trunc();
  1037. rightValue = rightValue.trunc();
  1038. }
  1039. result = leftValue.eq(rightValue);
  1040. } else {
  1041. result = leftValue === rightValue;
  1042. }
  1043. return new StoreValue(resultType, result);
  1044. }
  1045. case Operators.NEQ.ord: {
  1046. let leftValue = left.get();
  1047. let rightValue = right.get();
  1048. if (Types.INTEGER.isCompatible(left.type) || Types.REAL.isCompatible(left.type)) {
  1049. if (shouldImplicitCast) {
  1050. resultType = Types.BOOLEAN;
  1051. leftValue = leftValue.trunc();
  1052. rightValue = rightValue.trunc();
  1053. }
  1054. result = !leftValue.eq(rightValue);
  1055. } else {
  1056. result = leftValue !== rightValue;
  1057. }
  1058. return new StoreValue(resultType, result);
  1059. }
  1060. case Operators.AND.ord:
  1061. return new StoreValue(resultType, (left.get() && right.get()));
  1062. case Operators.OR.ord:
  1063. return new StoreValue(resultType, (left.get() || right.get()));
  1064. default:
  1065. return Promise.reject(new RuntimeError('!!!Critical Invalid InfixApp '+ infixApp.op));
  1066. }
  1067. });
  1068. }
  1069. }