Bladeren bron

Fix merge conlfics with rand-impl in lib/math.js

Rework some functions using async/await pattern
Lucas de Souza 4 jaren geleden
bovenliggende
commit
cc14247167
1 gewijzigde bestanden met toevoegingen van 199 en 69 verwijderingen
  1. 199 69
      js/processor/lib/math.js

+ 199 - 69
js/processor/lib/math.js

@@ -22,7 +22,7 @@ import { ProcessorErrorFactory } from '../error/processorErrorFactory';
  * min
  */
 
-function convertToRadians (degrees) {
+function convertToRadians(degrees) {
   return degrees.times(Decimal.acos(-1)).div(180);
 }
 
@@ -46,10 +46,19 @@ export function createSinFun () {
      return sto;
    };
 
-  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)],
-    block);
+  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
+      )
+    ],
+    block
+  );
   return func;
 }
 
@@ -58,12 +67,12 @@ export function createCosFun () {
     const x = sto.applyStore('x');
     const angle = x.get().mod(360);
     let result = null;
-    if(angle.eq(90)) {
+    if (angle.eq(90)) {
       result = new Decimal(0);
     } else if (angle.eq(180)) {
       result = new Decimal(-1);
     } else if (angle.eq(270)) {
-      result = new Decimal(0)
+      result = new Decimal(0);
     }
     result = Decimal.cos(convertToRadians(angle));
     const temp = new StoreValue(Types.REAL, result);
@@ -72,11 +81,20 @@ export function createCosFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(cosFun)]);
- const func = new Commands.Function('$cos', Types.REAL,
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(cosFun)]);
+  const func = new Commands.Function(
+    "$cos",
+    Types.REAL,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
 export function createTanFun () {
@@ -94,11 +112,20 @@ export function createTanFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(tanFun)]);
- const func = new Commands.Function('$tan', Types.REAL,
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(tanFun)]);
+  const func = new Commands.Function(
+    "$tan",
+    Types.REAL,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
 export function createSqrtFun () {
@@ -114,11 +141,20 @@ export function createSqrtFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(sqrtFun)]);
- const func = new Commands.Function('$sqrt', Types.REAL,
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(sqrtFun)]);
+  const func = new Commands.Function(
+    "$sqrt",
+    Types.REAL,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
 export function createPowFun () {
@@ -132,19 +168,34 @@ export function createPowFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(powFun)]);
- const func = new Commands.Function('$pow', Types.REAL,
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false),
-    new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'y', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(powFun)]);
+  const func = new Commands.Function(
+    "$pow",
+    Types.REAL,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      ),
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "y",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
 export function createLogFun () {
   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()));
+      return Promise.reject(
+        ProcessorErrorFactory.negative_log_value(this.function_call_stack.pop())
+      );
     }
     const result = Decimal.log10(x.get());
     const temp = new StoreValue(Types.REAL, result);
@@ -153,11 +204,20 @@ export function createLogFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(logFun)]);
- const func = new Commands.Function('$log', Types.REAL,
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(logFun)]);
+  const func = new Commands.Function(
+    "$log",
+    Types.REAL,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
 export function createAbsFun () {
@@ -170,11 +230,20 @@ export function createAbsFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(absFun)]);
- const func = new Commands.Function('$abs', new MultiType([Types.INTEGER, Types.REAL]),
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(absFun)]);
+  const func = new Commands.Function(
+    "$abs",
+    new MultiType([Types.INTEGER, Types.REAL]),
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
 export function createNegateFun () {
@@ -187,15 +256,27 @@ export function createNegateFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(negateFun)]);
- const func = new Commands.Function('$negate', new MultiType([Types.INTEGER, Types.REAL]),
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(negateFun)]
+  );
+  const func = new Commands.Function(
+    "$negate",
+    new MultiType([Types.INTEGER, Types.REAL]),
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
-export function createInvertFun () {
-  const invertFun = (sto, _) => {
+export function createInvertFun() {
+  const invertFun = async (sto, _) => {
     const x = sto.applyStore('x');
     const result = Parsers.toReal(1).dividedBy(x.get());
     const temp = new StoreValue(Types.REAL, result);
@@ -204,11 +285,23 @@ export function createInvertFun () {
     return sto;
   };
 
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(invertFun)]);
- const func = new Commands.Function('$invert', Types.REAL,
-   [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
-   block);
- return func;
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(invertFun)]
+  );
+  const func = new Commands.Function(
+    "$invert",
+    Types.REAL,
+    [
+      new Commands.FormalParameter(
+        new MultiType([Types.INTEGER, Types.REAL]),
+        "x",
+        false
+      )
+    ],
+    block
+  );
+  return func;
 }
 
 export function createMaxFun () {
@@ -221,12 +314,18 @@ export function createMaxFun () {
     sto.mode = Modes.RETURN;
     return sto;
   };
- const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(maxFun)]);
- const func = new Commands.Function('$max', new MultiType([Types.INTEGER, Types.REAL]),
-   [new Commands.FormalParameter(paramType, 'x', false)],
-   block);
- return func;
+  const paramType = new ArrayType(
+    new MultiType([Types.INTEGER, Types.REAL]),
+    1
+  );
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(maxFun)]);
+  const func = new Commands.Function(
+    "$max",
+    new MultiType([Types.INTEGER, Types.REAL]),
+    [new Commands.FormalParameter(paramType, "x", false)],
+    block
+  );
+  return func;
 }
 
 export function createMinFun () {
@@ -239,23 +338,54 @@ export function createMinFun () {
     sto.mode = Modes.RETURN;
     return sto;
   };
- const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
- const block = new Commands.CommandBlock([],  [new Commands.SysCall(minFun)]);
- const func = new Commands.Function('$min', new MultiType([Types.INTEGER, Types.REAL]),
-   [new Commands.FormalParameter(paramType, 'x', false)],
-   block);
- return func;
+  const paramType = new ArrayType(
+    new MultiType([Types.INTEGER, Types.REAL]),
+    1
+  );
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(minFun)]);
+  const func = new Commands.Function(
+    "$min",
+    new MultiType([Types.INTEGER, Types.REAL]),
+    [new Commands.FormalParameter(paramType, "x", false)],
+    block
+  );
+  return func;
 }
 
-export function createRandFun () {
-  const randFun = (sto, _) => {
-    const val = Math.random();
+let seed = Date.now();
+export function createRandFun() {
+  const a = 16807;
+  const m = 2147483647;
+  const c = 12345;
+  const randFun = async (sto, _) => {
+    seed = (a * seed + c) % m;
+    const val = seed / (m - 1);
     const temp = new StoreValue(Types.REAL, new Decimal(val));
     sto.insertStore("$", temp);
     sto.mode = Modes.RETURN;
-    return Promise.resolve(sto);
+    return sto;
+  };
+  const block = new Commands.CommandBlock([], [new Commands.SysCall(randFun)]);
+  const func = new Commands.Function("$rand", Types.REAL, [], block);
+  return func;
+}
+
+export function createSetSeedFun() {
+  const setSeedFun = async (sto, _) => {
+    const value = sto.applyStore("x");
+    seed = value.get().toNumber();
+    sto.mode = Modes.RETURN;
+    return sto;
   };
-  const block = new Commands.CommandBlock([],  [new Commands.SysCall(randFun)]);
-  const func = new Commands.Function('$rand', Types.REAL, [], block);
+  const block = new Commands.CommandBlock(
+    [],
+    [new Commands.SysCall(setSeedFun)]
+  );
+  const func = new Commands.Function(
+    "$setSeed",
+    Types.VOID,
+    [new Commands.FormalParameter(Types.INTEGER, "x", false)],
+    block
+  );
   return func;
 }