浏览代码

Fix Lib scope function being parsed without Lib ID

-Implement Decimal as numeric type
-Implement Bluebird as Promise engine
Lucas de Souza 6 年之前
父节点
当前提交
4f0ea94318

+ 7 - 1
js/processor/definedFunctions.js

@@ -96,7 +96,13 @@ export const LanguageDefinedFunction = Object.freeze({
       }
       return lib + "." + internalName;
     }
-    return valueToKey(localName, LanguageService.getCurrentLangFuncs());
+    const funcName = valueToKey(localName, LanguageService.getCurrentLangFuncs());
+    if(funcName !== null) {
+      if(funcsObject[funcName]) {
+        return funcName;
+      }
+    }
+    return null;
   },
   getFunction: (internalName) => {
     if (internalName.indexOf(".") !== -1) {

+ 11 - 6
js/processor/ivprogProcessor.js

@@ -1,3 +1,4 @@
+import * as Promise from 'bluebird'
 import { Store } from './store/store';
 import { StoreObject } from './store/storeObject';
 import { StoreObjectArray } from './store/storeObjectArray';
@@ -15,7 +16,7 @@ import { StoreObjectArrayAddressRef } from './store/storeObjectArrayAddressRef';
 import { CompoundType } from './../typeSystem/compoundType';
 import { convertToString } from '../typeSystem/parsers';
 
-let loopTimeoutMs = 5000
+let loopTimeoutMs = 10000
 
 export class IVProgProcessor {
 
@@ -221,7 +222,7 @@ export class IVProgProcessor {
   }
 
   executeFunctionCall (store, cmd) {
-    return new Promise((resolve, reject) => {
+    return new Promise.Promise((resolve, reject) => {
       const func = this.findFunction(cmd.id);
       this.runFunction(func, cmd.actualParameters, store)
         .then(sto => {
@@ -233,6 +234,7 @@ export class IVProgProcessor {
           }
         })
         .catch(err => reject(err));
+      return null;
     }); 
   }
 
@@ -304,7 +306,7 @@ export class IVProgProcessor {
 
   executeDoWhile (store, cmd) {
     const outerRef = this;
-    return new Promise((resolve, reject) => {
+    return new Promise.Promise((resolve, reject) => {
       try {
         outerRef.loopTimers.push(Date.now());
         outerRef.context.push(Context.BREAKABLE);
@@ -344,12 +346,13 @@ export class IVProgProcessor {
       } catch (error) {
         reject(error)
       }
+      return null;
     });
   }
 
   executeWhile (store, cmd) {
     const outerRef = this;
-    return new Promise((resolve, reject) => {
+    return new Promise.Promise((resolve, reject) => {
       try {
         outerRef.loopTimers.push(Date.now());
         outerRef.context.push(Context.BREAKABLE);
@@ -386,9 +389,11 @@ export class IVProgProcessor {
             reject(new Error(`Loop condition must be of type boolean`));
           }
         }).catch(err => reject(err));
+        
       } catch (error) {
         reject(error);
       }
+      return null;
     });
   }
 
@@ -469,7 +474,7 @@ export class IVProgProcessor {
   }
 
   executeArrayIndexAssign (store, cmd) {
-    return new Promise((resolve, reject) => {
+    return new Promise.Promise((resolve, reject) => {
       const mustBeArray = store.applyStore(cmd.id);
       if(!(mustBeArray.type instanceof CompoundType)) {
         reject(new Error(cmd.id + " is not a vector/matrix"));
@@ -804,7 +809,7 @@ export class IVProgProcessor {
         case Operators.DIV.ord: {
           result = left.value / right.value;
           if (Types.INTEGER.isCompatible(resultType))
-            result = left.value.idiv(right.value);
+            result = left.value.divToInt(right.value);
           else
             result = left.value.div(right.value);
           return new StoreObject(resultType, result);

+ 1 - 0
js/processor/lib/arrays.js

@@ -1,3 +1,4 @@
+import * as Promise from 'bluebird';
 import { StoreObject } from '../store/storeObject';
 import * as Commands from './../../ast/commands';
 import { Types } from './../../typeSystem/types';

+ 1 - 0
js/processor/lib/lang.js

@@ -1,3 +1,4 @@
+import * as Promise from 'bluebird';
 import { StoreObject } from '../store/storeObject';
 import * as Commands from './../../ast/commands';
 import { Types } from './../../typeSystem/types';

+ 16 - 11
js/processor/lib/math.js

@@ -1,8 +1,9 @@
+import * as Promise from 'bluebird';
 import { StoreObject } from '../store/storeObject';
 import * as Commands from './../../ast/commands';
 import { Types } from './../../typeSystem/types';
 import { toReal } from "./../../typeSystem/parsers";
-import { BigNumber } from 'bignumber.js';
+import { Decimal } from 'decimal.js';
 import { MultiType } from '../../typeSystem/multiType';
 import { CompoundType } from '../../typeSystem/compoundType';
 import { Modes } from '../modes';
@@ -21,10 +22,14 @@ import { Modes } from '../modes';
  * min
  */
 
+function convertToRadians (degrees) {
+  return degrees.times(Decimal.acos(-1)).div(180);
+}
+
 export function createSinFun () {
    const sinFun = (sto, _) => {
      const x = sto.applyStore('x');
-     const result = toReal(Math.sin(x.number));
+     const result = Decimal.sin(convertToRadians(x.value));
      const temp = new StoreObject(Types.REAL, result);
      sto.mode = Modes.RETURN;
      return Promise.resolve(sto.updateStore('$', temp));
@@ -32,7 +37,7 @@ export function createSinFun () {
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(sinFun)]);
   const func = new Commands.Function('$sin', Types.REAL,
-    [new Commands.FormalParameter(new MultiType([[Types.INTEGER, Types.REAL]]), 'x', false)],
+    [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
     block);
   return func;
 }
@@ -40,7 +45,7 @@ export function createSinFun () {
 export function createCosFun () {
   const cosFun = (sto, _) => {
     const x = sto.applyStore('x');
-    const result = toReal(Math.cos(x.number));
+    const result = Decimal.cos(convertToRadians(x.value));
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -56,7 +61,7 @@ export function createCosFun () {
 export function createTanFun () {
   const tanFun = (sto, _) => {
     const x = sto.applyStore('x');
-    const result = toReal(Math.tan(x.number));
+    const result = Decimal.tan(convertToRadians(x.value));
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -87,9 +92,9 @@ export function createSqrtFun () {
 
 export function createPowFun () {
   const powFun = (sto, _) => {
-    const x = sto.applyStore('x');f
+    const x = sto.applyStore('x');
     const y = sto.applyStore('y');
-    const result = toReal(Math.pow(x.number, y.number));
+    const result = x.value.pow(y.value);
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -109,7 +114,7 @@ export function createLogFun () {
     if (x.value.isNegative()) {
       return Promise.reject("the value passed to log function cannot be negative");
     }
-    const result = toReal(Math.log10(x.number));
+    const result = Decimal.log10(x.value);
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -173,8 +178,8 @@ export function createInvertFun () {
 export function createMaxFun () {
   const maxFun = (sto, _) => {
     const x = sto.applyStore('x');
-    const numbers = x.value.map(stoObj => stoObj.number);
-    const result = BigNumber.max(numbers);
+    const numbers = x.value.map(stoObj => stoObj.value.toNumber());
+    const result = Decimal.max(numbers);
     const temp = new StoreObject(x.type.innerType, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -191,7 +196,7 @@ export function createMinFun () {
   const minFun = (sto, _) => {
     const x = sto.applyStore('x');
     const numbers = x.value.map(stoObj => stoObj.value.toNumber());
-    const result = BigNumber.min(numbers);
+    const result = Decimal.min(numbers);
     const temp = new StoreObject(x.type.innerType, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));

+ 1 - 0
js/processor/lib/strings.js

@@ -1,3 +1,4 @@
+import * as Promise from 'bluebird';
 import { StoreObject } from '../store/storeObject';
 import * as Commands from './../../ast/commands';
 import { Types } from './../../typeSystem/types';

+ 2 - 2
js/processor/store/storeObject.js

@@ -1,4 +1,4 @@
-import { BigNumber } from 'bignumber.js'
+import Decimal from 'decimal.js';
 
 export class StoreObject {
 
@@ -30,7 +30,7 @@ export class StoreObject {
   }
   
   get number () {
-    if (this._value instanceof BigNumber) {
+    if (this._value instanceof Decimal) {
       return this._value.toNumber();
     } else {
       return null;

+ 2 - 2
js/processor/store/storeObjectArrayAddressRef.js

@@ -1,5 +1,5 @@
 import { StoreObject } from './storeObject';
-import { BigNumber } from "bignumber.js";
+import Decimal from 'decimal.js';
 
 export class StoreObjectArrayAddressRef extends StoreObject {
 
@@ -21,7 +21,7 @@ export class StoreObjectArrayAddressRef extends StoreObject {
   }
 
   get number () {
-    if (this.value instanceof BigNumber) {
+    if (this.value instanceof Decimal) {
       return this.value.toNumber();
     } else {
       return null;

+ 2 - 2
js/processor/store/storeObjectRef.js

@@ -1,5 +1,5 @@
 import { StoreObject } from './storeObject';
-import { BigNumber } from "bignumber.js";
+import Decimal from 'decimal.js';
 
 export class StoreObjectRef extends StoreObject {
 
@@ -22,7 +22,7 @@ export class StoreObjectRef extends StoreObject {
   }
 
   get number () {
-    if (this.value instanceof BigNumber) {
+    if (this.value instanceof Decimal) {
       return this.value.toNumber();
     } else {
       return null;

+ 3 - 3
js/typeSystem/parsers.js

@@ -1,9 +1,9 @@
 import { LanguageService } from "../services/languageService";
 import { Types } from "./types";
-import { BigNumber } from 'bignumber.js'
+import Decimal from "decimal.js";
 
 export function toInt (str) {
-  return new BigNumber(str);
+  return new Decimal(str);
 }
 
 export function toString (str) {
@@ -20,7 +20,7 @@ export function toString (str) {
 }
 
 export function toReal (value) {
-  return new BigNumber(value);
+  return new Decimal(value);
 }
 
 export function toBool (str) {

+ 10 - 6
package-lock.json

@@ -1419,10 +1419,9 @@
       "dev": true
     },
     "bluebird": {
-      "version": "3.5.1",
-      "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
-      "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==",
-      "dev": true
+      "version": "3.5.2",
+      "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.2.tgz",
+      "integrity": "sha512-dhHTWMI7kMx5whMQntl7Vr9C6BvV10lFXDAasnqnrMYhXVCzzk6IO9Fo2L75jXHT07WrOngL1WDXOp+yYS91Yg=="
     },
     "bn.js": {
       "version": "4.11.8",
@@ -2147,6 +2146,11 @@
       "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
       "dev": true
     },
+    "decimal.js": {
+      "version": "10.0.1",
+      "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.0.1.tgz",
+      "integrity": "sha512-vklWB5C4Cj423xnaOtsUmAv0/7GqlXIgDv2ZKDyR64OV3OSzGHNx2mk4p/1EKnB5s70k73cIOOEcG9YzF0q4Lw=="
+    },
     "decode-uri-component": {
       "version": "0.2.0",
       "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz",
@@ -4504,8 +4508,8 @@
       }
     },
     "line-i18n": {
-      "version": "git+https://git.lcalion.com/lucascalion/line-i18n.git#d62cb2d82d27761758fe59376d22f4f5b191b51a",
-      "from": "git+https://git.lcalion.com/lucascalion/line-i18n.git"
+      "version": "git+https://git.lcalion.com/LInE/line-i18n.git#3d9c23059ba04f63fde6f2263798f66cdc29c889",
+      "from": "git+https://git.lcalion.com/LInE/line-i18n.git"
     },
     "load-json-file": {
       "version": "1.1.0",

+ 3 - 1
package.json

@@ -47,7 +47,9 @@
   "dependencies": {
     "antlr4": "^4.7.1",
     "bignumber.js": "^7.2.1",
+    "bluebird": "^3.5.2",
+    "decimal.js": "^10.0.1",
     "jquery": "^3.3.1",
-    "line-i18n": "git+https://git.lcalion.com/lucascalion/line-i18n.git"
+    "line-i18n": "git+https://git.lcalion.com/LInE/line-i18n.git"
   }
 }