浏览代码

Implement random number generator en/pt

Lucas de Souza 5 年之前
父节点
当前提交
9bcc1128fd

+ 2 - 1
grammar/en/langFunctions.js

@@ -29,6 +29,7 @@ export default {
   $negate: "negate",
   $invert: "invert",
   $max: "maximum",
-  $min: "minimum"
+  $min: "minimum",
+  $rand: "random"
 
 }

+ 2 - 1
grammar/pt/langFunctions.js

@@ -29,5 +29,6 @@ export default {
   $negate: "trocar_sinal",
   $invert: "inverter_valor",
   $max: "maximo",
-  $min: "minimo"
+  $min: "minimo",
+  $rand: "numero_aleatorio"
 }

+ 1 - 0
i18n/en/ui.json

@@ -116,6 +116,7 @@
   "$invert": "invert",
   "$max": "maximum",
   "$min": "minimum",
+  "$rand":"random",
   "$substring": "substring",
   "$length": "length",
   "$uppercase": "uppercase",

+ 1 - 0
i18n/pt/ui.json

@@ -122,6 +122,7 @@
   "$invert": "inverter_valor",
   "$max": "maximo",
   "$min": "minimo",
+  "$rand": "numero_aleatorio",
   "$substring": "subcadeia",
   "$length": "comprimento",
   "$uppercase": "caixa_alta",

+ 3 - 2
js/processor/definedFunctions.js

@@ -14,7 +14,7 @@ import {createAbsFun, createCosFun,
   createMaxFun, createMinFun,
   createNegateFun, createPowFun,
   createSinFun, createSqrtFun,
-  createTanFun} from './lib/math';
+  createTanFun, createRandFun} from './lib/math';
 
 function valueToKey (value, object) {
   for (const key in object) {
@@ -52,7 +52,8 @@ const libsObject = {
     $negate: createNegateFun(),
     $invert: createInvertFun(),
     $max: createMaxFun(),
-    $min: createMinFun()
+    $min: createMinFun(),
+    $rand: createRandFun()
   },
   $ioLib: {
     $read: createInputFun(),

+ 12 - 0
js/processor/lib/math.js

@@ -253,3 +253,15 @@ export function createMinFun () {
    block);
  return func;
 }
+
+export function createRandFun () {
+  const randFun = (sto, _) => {
+    const val = Math.random();
+    const result = new StoreObject(Types.REAL, new Decimal(val));
+    sto.mode = Modes.RETURN;
+    return Promise.resolve(sto.updateStore("$", result));
+  };
+  const block = new Commands.CommandBlock([],  [new Commands.SysCall(randFun)]);
+  const func = new Commands.Function('$rand', Types.REAL, [], block);
+  return func;
+}

+ 2 - 0
js/visualUI/functions.js

@@ -52,6 +52,8 @@ window.system_functions.push(new Models.SystemFunction('$max', Types.REAL, 0, [n
   null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
 window.system_functions.push(new Models.SystemFunction('$min', Types.REAL, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],
   null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
+window.system_functions.push(new Models.SystemFunction('$rand', Types.REAL, 0, [],
+  null, Models.SYSTEM_FUNCTIONS_CATEGORIES.math));
 // Adding text functions:
 window.system_functions.push(new Models.SystemFunction('$substring', Types.TEXT, 0, [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
   new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)],