Browse Source

fix: Fix a bug in lib/lang.js int/float convertion functions

Change castIntFun to use toInt type parser when creating StoreValue
Change castRealtFun to use toReal type parser when creating StoreValue
Lucas de Souza 2 years ago
parent
commit
482ddc41fc
1 changed files with 149 additions and 67 deletions
  1. 149 67
      js/processor/lib/lang.js

+ 149 - 67
js/processor/lib/lang.js

@@ -1,15 +1,15 @@
-import * as Commands from './../../ast/commands';
-import { Types } from './../../typeSystem/types';
+import * as Commands from "./../../ast/commands";
+import { Types } from "./../../typeSystem/types";
 import * as Parsers from "./../../typeSystem/parsers";
-import { IVProgParser } from '../../ast/ivprogParser';
-import { RealLiteral, IntLiteral, BoolLiteral } from '../../ast/expressions';
-import { Modes } from '../modes';
-import { MultiType } from '../../typeSystem/multiType';
-import { ProcessorErrorFactory } from '../error/processorErrorFactory';
-import { StoreValue } from '../store/value/store_value';
+import { IVProgParser } from "../../ast/ivprogParser";
+import { RealLiteral, IntLiteral, BoolLiteral } from "../../ast/expressions";
+import { Modes } from "../modes";
+import { MultiType } from "../../typeSystem/multiType";
+import { ProcessorErrorFactory } from "../error/processorErrorFactory";
+import { StoreValue } from "../store/value/store_value";
 
 /**
- * 
+ *
  * is_real
  * is_int
  * is_bool
@@ -32,17 +32,23 @@ export function createIsRealFun () {
       }
     } catch (error) {
       // ignore
-     }
+    }
     const temp = new StoreValue(Types.BOOLEAN, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
     return sto;
-  }
+  };
 
-  const block = new Commands.CommandBlock([],  [new Commands.SysCall(isRealFun)]);
-  const func = new Commands.Function('$isReal', Types.BOOLEAN,
-    [new Commands.FormalParameter(Types.STRING, 'str', false)],
-    block);
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(isRealFun)]
+  );
+  const func = new Commands.Function(
+    "$isReal",
+    Types.BOOLEAN,
+    [new Commands.FormalParameter(Types.STRING, "str", false)],
+    block
+  );
   return func;
 }
 
@@ -56,19 +62,22 @@ export function createIsIntFun () {
       if (val instanceof IntLiteral) {
         result = true;
       }
-    } catch { 
+    } catch {
       // ignore
     }
     const temp = new StoreValue(Types.BOOLEAN, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
     return sto;
-  }
+  };
 
-  const block = new Commands.CommandBlock([],  [new Commands.SysCall(isIntFun)]);
-  const func = new Commands.Function('$isInt', Types.BOOLEAN,
-    [new Commands.FormalParameter(Types.STRING, 'str', false)],
-    block);
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(isIntFun)]);
+  const func = new Commands.Function(
+    "$isInt",
+    Types.BOOLEAN,
+    [new Commands.FormalParameter(Types.STRING, "str", false)],
+    block
+  );
   return func;
 }
 
@@ -82,19 +91,25 @@ export function createIsBoolFun () {
       if (val instanceof BoolLiteral) {
         result = true;
       }
-    } catch (error) { 
+    } catch (error) {
       // ignore
     }
     const temp = new StoreValue(Types.BOOLEAN, result);
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
     return sto;
-  }
+  };
 
-  const block = new Commands.CommandBlock([],  [new Commands.SysCall(isBoolFun)]);
-  const func = new Commands.Function('$isBool', Types.BOOLEAN,
-    [new Commands.FormalParameter(Types.STRING, 'str', false)],
-    block);
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(isBoolFun)]
+  );
+  const func = new Commands.Function(
+    "$isBool",
+    Types.BOOLEAN,
+    [new Commands.FormalParameter(Types.STRING, "str", false)],
+    block
+  );
   return func;
 }
 
@@ -115,25 +130,44 @@ export function createCastRealFun () {
         try {
           const result = parser.parseTerm();
           if (result instanceof RealLiteral) {
-            const temp = new StoreValue(Types.REAL, result.value);
+            const temp = new StoreValue(
+              Types.REAL,
+              Parsers.toReal(result.value)
+            );
             sto.insertStore("$", temp);
             sto.mode = Modes.RETURN;
             return sto;
           }
         } catch (error) {
           // ignore
-         }
+        }
       }
     }
     const typeStringInfoArray = Types.REAL.stringInfo();
     const typeInfo = typeStringInfoArray[0];
-    throw 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)]);
-  const func = new Commands.Function('$castReal', Types.REAL,
-    [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.STRING]), 'val', false)],
-    block);
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(castRealFun)]
+  );
+  const func = new Commands.Function(
+    "$castReal",
+    Types.REAL,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.STRING]),
+        "val",
+        false
+      ),
+    ],
+    block
+  );
   return func;
 }
 
@@ -144,13 +178,19 @@ export function createCastIntFun () {
     switch (val.type.ord) {
       case Types.REAL.ord: {
         value = value.toNumber();
-        const temp = new StoreValue(Types.INTEGER, Math.floor(value));
+        const temp = new StoreValue(
+          Types.INTEGER,
+          Parsers.toInt(Math.floor(value))
+        );
         sto.insertStore("$", temp);
         sto.mode = Modes.RETURN;
         return sto;
       }
       case Types.CHAR.ord: {
-        const temp = new StoreValue(Types.INTEGER, value.charCodeAt(0));
+        const temp = new StoreValue(
+          Types.INTEGER,
+          Parsers.toInt(value.charCodeAt(0))
+        );
         sto.insertStore("$", temp);
         sto.mode = Modes.RETURN;
         return sto;
@@ -160,32 +200,51 @@ export function createCastIntFun () {
         try {
           const result = parser.parseTerm();
           if (result instanceof IntLiteral) {
-            const temp = new StoreValue(Types.INTEGER, result.value);
+            const temp = new StoreValue(
+              Types.INTEGER,
+              Parsers.toInt(result.value)
+            );
             sto.insertStore("$", temp);
             sto.mode = Modes.RETURN;
             return sto;
           }
-        } catch (error) { 
+        } catch (error) {
           // ignore
         }
       }
     }
     const typeStringInfoArray = Types.INTEGER.stringInfo();
     const typeInfo = typeStringInfoArray[0];
-    throw 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)]);
-  const func = new Commands.Function('$castInt', Types.INTEGER,
-    [new Commands.FormalParameter(new MultiType([Types.REAL, Types.STRING]), 'val', false)],
-    block);
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(castIntFun)]
+  );
+  const func = new Commands.Function(
+    "$castInt",
+    Types.INTEGER,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.REAL, Types.STRING, Types.CHAR]),
+        "val",
+        false
+      ),
+    ],
+    block
+  );
   return func;
 }
 
 export function createCastBoolFun () {
   const castBoolFun = async (sto, _) => {
     const str = sto.applyStore("str");
-    const value = str.get(); 
+    const value = str.get();
     const parser = IVProgParser.createParser(value);
     try {
       const val = parser.parseTerm();
@@ -195,51 +254,74 @@ export function createCastBoolFun () {
         sto.mode = Modes.RETURN;
         return sto;
       }
-    } catch (error) { 
+    } catch (error) {
       // ignore
     }
     const typeStringInfoArray = Types.BOOLEAN.stringInfo();
     const typeInfo = typeStringInfoArray[0];
-    throw 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)]);
-  const func = new Commands.Function('$castBool', Types.BOOLEAN,
-    [new Commands.FormalParameter(Types.STRING, 'str', false)],
-    block);
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(castBoolFun)]
+  );
+  const func = new Commands.Function(
+    "$castBool",
+    Types.BOOLEAN,
+    [new Commands.FormalParameter(Types.STRING, "str", false)],
+    block
+  );
   return func;
 }
 
 export function createCastStringFun () {
   const castStringFun = async function (store, _) {
-    const val = store.applyStore('str');
+    const val = store.applyStore("str");
     const result = Parsers.convertToString(val.get(), val.type);
     const temp = new StoreValue(Types.STRING, result);
     store.insertStore("$", temp);
     store.mode = Modes.RETURN;
     return store;
-  }
-  const block = new Commands.CommandBlock([], [new Commands.SysCall(castStringFun)]);
-  const func = new Commands.Function('$castString', Types.STRING,
-    [new Commands.FormalParameter(Types.ALL, 'str', false)],
-    block);
+  };
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(castStringFun)]
+  );
+  const func = new Commands.Function(
+    "$castString",
+    Types.STRING,
+    [new Commands.FormalParameter(Types.ALL, "str", false)],
+    block
+  );
   return func;
 }
 
 export function createCastCharFun () {
   const castCharFun = (store, _) => {
-    const valSV = store.applyStore('charCode');
+    const valSV = store.applyStore("charCode");
     // Force the int value to the range [0, 255]
-    const value = valSV.get().toNumber() & 0xFF;
+    const value = valSV.get().toNumber() & 0xff;
     const result = String.fromCharCode(value);
     const temp = new StoreValue(Types.CHAR, result);
     store.insertStore("$", temp);
     store.mode = Modes.RETURN;
     return store;
-  }
-  const block = new Commands.CommandBlock([], [new Commands.SysCall(castCharFun)]);
-  const func = new Commands.Function('$castChar', Types.CHAR,
-    [new Commands.FormalParameter(Types.INTEGER, 'charCode', false)],
-    block);
+  };
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(castCharFun)]
+  );
+  const func = new Commands.Function(
+    "$castChar",
+    Types.CHAR,
+    [new Commands.FormalParameter(Types.INTEGER, "charCode", false)],
+    block
+  );
   return func;
-}
+}
+