浏览代码

Added Register operation

n1474335 7 年之前
父节点
当前提交
b86dceb3c6
共有 4 个文件被更改,包括 86 次插入0 次删除
  1. 60 0
      src/core/FlowControl.js
  2. 1 0
      src/core/config/Categories.js
  3. 24 0
      src/core/config/OperationConfig.js
  4. 1 0
      src/core/config/modules/Default.js

+ 60 - 0
src/core/FlowControl.js

@@ -90,6 +90,66 @@ const FlowControl = {
     },
 
 
+    /**
+     * Register operation.
+     *
+     * @param {Object} state - The current state of the recipe.
+     * @param {number} state.progress - The current position in the recipe.
+     * @param {Dish} state.dish - The Dish being operated on.
+     * @param {Operation[]} state.opList - The list of operations in the recipe.
+     * @returns {Object} The updated state of the recipe.
+     */
+    runRegister: function(state) {
+        const ings = state.opList[state.progress].getIngValues(),
+            extractorStr = ings[0],
+            i = ings[1],
+            m = ings[2];
+
+        let modifiers = "";
+        if (i) modifiers += "i";
+        if (m) modifiers += "m";
+
+        const extractor = new RegExp(extractorStr, modifiers),
+            input = state.dish.get(Dish.STRING),
+            registers = input.match(extractor);
+
+        /**
+         * Replaces references to registers (e.g. $R0) with the contents of those registers.
+         *
+         * @param {string} str
+         * @returns {string}
+         */
+        function replaceRegister(str) {
+            // Replace references to registers ($Rn) with contents of registers
+            str = str.replace(/((?:^|[^\\])(?:\\.|[^\\])*?)\$R(\d{1,2})/g, (match, pre, regNum) => {
+                const index = parseInt(regNum, 10) + 1;
+                return (index >= registers.length) ? match : pre + registers[index];
+            });
+
+            // Unescape remaining register references
+            return str.replace(/\\\$R(\d{1,2})/, "$R$1");
+        }
+
+        // Step through all subsequent ops and replace registers in args with extracted content
+        for (let i = state.progress + 1; i < state.opList.length; i++) {
+            let args = state.opList[i].getIngValues();
+            args = args.map(arg => {
+                if (typeof arg !== "string" && typeof arg !== "object") return arg;
+
+                if (typeof arg === "object" && arg.string) {
+                    arg.string = replaceRegister(arg.string);
+                    return arg;
+                }
+
+                return replaceRegister(arg);
+            });
+            state.opList[i].setIngValues(args);
+        }
+
+        return state;
+    },
+
+
     /**
      * Jump operation.
      *

+ 1 - 0
src/core/config/Categories.js

@@ -317,6 +317,7 @@ const Categories = [
         ops: [
             "Fork",
             "Merge",
+            "Register",
             "Jump",
             "Conditional Jump",
             "Return",

+ 24 - 0
src/core/config/OperationConfig.js

@@ -116,6 +116,30 @@ const OperationConfig = {
         flowControl: true,
         args: []
     },
+    "Register": {
+        module: "Default",
+        description: "Extract data from the input and store it in registers which can then be passed into subsequent operations as arguments. Regular expression capture groups are used to select the data to extract.<br><br>To use registers in arguments, refer to them using the notation <code>$Rn</code> where n is the register number, starting at 0.<br><br>For example:<br>Input: <code>Test</code><br>Extractor: <code>(.*)</code><br>Argument: <code>$R0</code> becomes <code>Test</code><br><br>Registers can be escaped in arguments using a backslash. e.g. <code>\\$R0</code> would become <code>$R0</code> rather than <code>Test</code>.",
+        inputType: "string",
+        outputType: "string",
+        flowControl: true,
+        args: [
+            {
+                name: "Extractor",
+                type: "binaryString",
+                value: "([\\s\\S]*)"
+            },
+            {
+                name: "Case insensitive",
+                type: "boolean",
+                value: true
+            },
+            {
+                name: "Multiline matching",
+                type: "boolean",
+                value: false
+            },
+        ]
+    },
     "Jump": {
         module: "Default",
         description: "Jump forwards or backwards over the specified number of operations.",

+ 1 - 0
src/core/config/modules/Default.js

@@ -154,6 +154,7 @@ OpModules.Default = {
     "Generate HOTP":        OTP.runHOTP,
     "Fork":                 FlowControl.runFork,
     "Merge":                FlowControl.runMerge,
+    "Register":             FlowControl.runRegister,
     "Jump":                 FlowControl.runJump,
     "Conditional Jump":     FlowControl.runCondJump,
     "Return":               FlowControl.runReturn,