Browse Source

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

Lucas de Souza 4 years ago
parent
commit
9855101099
1 changed files with 21 additions and 21 deletions
  1. 21 21
      js/processor/lib/lang.js

+ 21 - 21
js/processor/lib/lang.js

@@ -1,6 +1,6 @@
 import * as Commands from './../../ast/commands';
 import { Types } from './../../typeSystem/types';
-import { toReal, convertToString } from "./../../typeSystem/parsers";
+import * as Parsers from "./../../typeSystem/parsers";
 import { IVProgParser } from '../../ast/ivprogParser';
 import { RealLiteral, IntLiteral, BoolLiteral } from '../../ast/expressions';
 import { Modes } from '../modes';
@@ -20,7 +20,7 @@ import { StoreValue } from '../store/value/store_value';
  */
 
 export function createIsRealFun () {
-  const isRealFun = (sto, _) => {
+  const isRealFun = async (sto, _) => {
     const str = sto.applyStore("str");
     const parser = IVProgParser.createParser(str.get());
     let result = false;
@@ -35,7 +35,7 @@ export function createIsRealFun () {
     const temp = new StoreValue(Types.BOOLEAN, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   }
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(isRealFun)]);
@@ -46,7 +46,7 @@ export function createIsRealFun () {
 }
 
 export function createIsIntFun () {
-  const isIntFun = (sto, _) => {
+  const isIntFun = async (sto, _) => {
     const str = sto.applyStore("str");
     const parser = IVProgParser.createParser(str.get());
     let result = false;
@@ -61,7 +61,7 @@ export function createIsIntFun () {
     const temp = new StoreValue(Types.BOOLEAN, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   }
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(isIntFun)]);
@@ -87,7 +87,7 @@ export function createIsBoolFun () {
     const temp = new StoreValue(Types.BOOLEAN, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
   }
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(isBoolFun)]);
@@ -98,16 +98,16 @@ export function createIsBoolFun () {
 }
 
 export function createCastRealFun () {
-  const castRealFun = (sto, _) => {
+  const castRealFun = async (sto, _) => {
     const val = sto.applyStore("val");
     let value = val.get();
     switch (val.type.ord) {
       case Types.INTEGER.ord: {
         value = value.toNumber();
-        const temp = new StoreValue(Types.REAL, toReal(value));
+        const temp = new StoreValue(Types.REAL, Parsers.toReal(value));
         sto.insertStore("$", temp);
         sto.mode = Modes.RETURN;
-        return Promise.resolve(sto);
+        return sto;
       }
       case Types.STRING.ord: {
         const parser = IVProgParser.createParser(value);
@@ -117,7 +117,7 @@ export function createCastRealFun () {
             const temp = new StoreValue(Types.REAL, result.value);
             sto.insertStore("$", temp);
             sto.mode = Modes.RETURN;
-            return Promise.resolve(sto);
+            return sto;
           }
         } catch (error) {
           // ignore
@@ -126,7 +126,7 @@ export function createCastRealFun () {
     }
     const typeStringInfoArray = Types.REAL.stringInfo();
     const typeInfo = typeStringInfoArray[0];
-    return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
+    throw ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim);
   }
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(castRealFun)]);
@@ -137,7 +137,7 @@ export function createCastRealFun () {
 }
 
 export function createCastIntFun () {
-  const castIntFun = (sto, _) => {
+  const castIntFun = async (sto, _) => {
     const val = sto.applyStore("val");
     let value = val.get();
     switch (val.type.ord) {
@@ -146,7 +146,7 @@ export function createCastIntFun () {
         const temp = new StoreValue(Types.INTEGER, Math.floor(value));
         sto.insertStore("$", temp);
         sto.mode = Modes.RETURN;
-        return Promise.resolve(sto);
+        return sto;
       }
       case Types.STRING.ord: {
         const parser = IVProgParser.createParser(value);
@@ -156,7 +156,7 @@ export function createCastIntFun () {
             const temp = new StoreValue(Types.INTEGER, result.value);
             sto.insertStore("$", temp);
             sto.mode = Modes.RETURN;
-            return Promise.resolve(sto);
+            return sto;
           }
         } catch (error) { 
           // ignore
@@ -165,7 +165,7 @@ export function createCastIntFun () {
     }
     const typeStringInfoArray = Types.INTEGER.stringInfo();
     const typeInfo = typeStringInfoArray[0];
-    return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
+    throw ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim);
   }
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(castIntFun)]);
@@ -176,7 +176,7 @@ export function createCastIntFun () {
 }
 
 export function createCastBoolFun () {
-  const castBoolFun = (sto, _) => {
+  const castBoolFun = async (sto, _) => {
     const str = sto.applyStore("str");
     const value = str.get(); 
     const parser = IVProgParser.createParser(value);
@@ -186,14 +186,14 @@ export function createCastBoolFun () {
         const temp = new StoreValue(Types.BOOLEAN, val.value);
         sto.insertStore("$", temp);
         sto.mode = Modes.RETURN;
-        return Promise.resolve(sto);
+        return sto;
       }
     } catch (error) { 
       // ignore
     }
     const typeStringInfoArray = Types.BOOLEAN.stringInfo();
     const typeInfo = typeStringInfoArray[0];
-    return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
+    throw ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim);
   }
 
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(castBoolFun)]);
@@ -204,13 +204,13 @@ export function createCastBoolFun () {
 }
 
 export function createCastStringFun () {
-  const castStringFun = function (store, _) {
+  const castStringFun = async function (store, _) {
     const val = store.applyStore('str');
-    const result = convertToString(val.get(), val.type);
+    const result = Parsers.convertToString(val.get(), val.type);
     const temp = new StoreValue(Types.STRING, result);
     store.insertStore("$", temp);
     store.mode = Modes.RETURN;
-    return Promise.resolve(store);
+    return store;
   }
   const block = new Commands.CommandBlock([], [new Commands.SysCall(castStringFun)]);
   const func = new Commands.Function('$castString', Types.STRING,