Sfoglia il codice sorgente

Rework lib/math.js functions using async/await pattern

Lucas de Souza 5 anni fa
parent
commit
c463fba5f6
1 ha cambiato i file con 25 aggiunte e 24 eliminazioni
  1. 25 24
      js/processor/lib/math.js

+ 25 - 24
js/processor/lib/math.js

@@ -1,6 +1,6 @@
 import * as Commands from './../../ast/commands';
 import { Types } from './../../typeSystem/types';
-import { toReal } from "./../../typeSystem/parsers";
+import * as Parsers from "./../../typeSystem/parsers";
 import { Decimal } from 'decimal.js';
 import { MultiType } from '../../typeSystem/multiType';
 import { ArrayType } from '../../typeSystem/array_type';
@@ -27,7 +27,7 @@ function convertToRadians (degrees) {
 }
 
 export function createSinFun () {
-   const sinFun = (sto, _) => {
+   const sinFun = async (sto, _) => {
      const x = sto.applyStore('x');
      const angle = x.get().mod(360);
      let result = null;
@@ -43,7 +43,7 @@ export function createSinFun () {
      const temp = new StoreValue(Types.REAL, result);
      sto.insertStore("$", temp);
      sto.mode = Modes.RETURN;
-     return Promise.resolve(sto);
+     return sto;
    };
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(sinFun)]);
@@ -54,7 +54,7 @@ export function createSinFun () {
 }
 
 export function createCosFun () {
-  const cosFun = (sto, _) => {
+  const cosFun = async (sto, _) => {
     const x = sto.applyStore('x');
     const angle = x.get().mod(360);
     let result = null;
@@ -69,7 +69,7 @@ export function createCosFun () {
     const temp = new StoreValue(Types.REAL, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(cosFun)]);
@@ -80,17 +80,18 @@ export function createCosFun () {
 }
 
 export function createTanFun () {
-  const tanFun = function (sto, _) {
+  const tanFun = async function (sto, _) {
     const x = sto.applyStore('x');
     const angle = x.get().mod(360);
     if(angle.eq(90) || angle.eq(270)) {
-      return Promise.reject(ProcessorErrorFactory.undefined_tanget_value(x.get().toNumber(), this.function_call_stack.pop()));
+      throw ProcessorErrorFactory.undefined_tanget_value(x.get().toNumber(),
+        this.function_call_stack.pop());
     }
     const result = Decimal.tan(convertToRadians(angle));
     const temp = new StoreValue(Types.REAL, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(tanFun)]);
@@ -101,7 +102,7 @@ export function createTanFun () {
 }
 
 export function createSqrtFun () {
-  const sqrtFun = function (sto, _) {
+  const sqrtFun = async function (sto, _) {
     const x = sto.applyStore('x');
     if(x.get().isNeg()) {
       return Promise.reject(ProcessorErrorFactory.negative_sqrt_value(this.function_call_stack.pop()));
@@ -110,7 +111,7 @@ export function createSqrtFun () {
     const temp = new StoreValue(Types.REAL, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(sqrtFun)]);
@@ -121,14 +122,14 @@ export function createSqrtFun () {
 }
 
 export function createPowFun () {
-  const powFun = (sto, _) => {
+  const powFun = async (sto, _) => {
     const x = sto.applyStore('x');
     const y = sto.applyStore('y');
     const result = x.get().pow(y.get());
     const temp = new StoreValue(Types.REAL, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(powFun)]);
@@ -140,7 +141,7 @@ export function createPowFun () {
 }
 
 export function createLogFun () {
-  const logFun = function (sto, _) {
+  const logFun = async function (sto, _) {
     const x = sto.applyStore('x');
     if (x.get().isNegative()) {
       return Promise.reject(ProcessorErrorFactory.negative_log_value(this.function_call_stack.pop()));
@@ -149,7 +150,7 @@ export function createLogFun () {
     const temp = new StoreValue(Types.REAL, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(logFun)]);
@@ -160,13 +161,13 @@ export function createLogFun () {
 }
 
 export function createAbsFun () {
-  const absFun = (sto, _) => {
+  const absFun = async (sto, _) => {
     const x = sto.applyStore('x');
     const result = x.get().abs();
     const temp = new StoreValue(x.type, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(absFun)]);
@@ -177,13 +178,13 @@ export function createAbsFun () {
 }
 
 export function createNegateFun () {
-  const negateFun = (sto, _) => {
+  const negateFun = async (sto, _) => {
     const x = sto.applyStore('x');
     const result = x.get().negated();
     const temp = new StoreValue(x.type, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(negateFun)]);
@@ -196,11 +197,11 @@ export function createNegateFun () {
 export function createInvertFun () {
   const invertFun = (sto, _) => {
     const x = sto.applyStore('x');
-    const result = toReal(1).dividedBy(x.get());
+    const result = Parsers.toReal(1).dividedBy(x.get());
     const temp = new StoreValue(Types.REAL, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
 
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(invertFun)]);
@@ -211,14 +212,14 @@ export function createInvertFun () {
 }
 
 export function createMaxFun () {
-  const maxFun = (sto, _) => {
+  const maxFun = async (sto, _) => {
     const x = sto.applyStore('x');
     const numbers = x.get().map(sto_addrs => sto_addrs.get());
     const result = Decimal.max(...numbers);
     const temp = new StoreValue(x.type.innerType, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
  const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(maxFun)]);
@@ -229,14 +230,14 @@ export function createMaxFun () {
 }
 
 export function createMinFun () {
-  const minFun = (sto, _) => {
+  const minFun = async (sto, _) => {
     const x = sto.applyStore('x');
     const numbers = x.get().map(sto_addrs => sto_addrs.get());
     const result = Decimal.min(...numbers);
     const temp = new StoreValue(x.type.innerType, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   };
  const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
  const block = new Commands.CommandBlock([],  [new Commands.SysCall(minFun)]);