2017-03-23 17:52:20 +00:00
|
|
|
import Recipe from "./Recipe.js";
|
|
|
|
import Dish from "./Dish.js";
|
2017-03-21 22:41:44 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Flow Control operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const FlowControl = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fork 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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-04-21 17:48:42 -04:00
|
|
|
runFork: async function(state) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let opList = state.opList,
|
2017-01-31 18:24:56 +00:00
|
|
|
inputType = opList[state.progress].inputType,
|
|
|
|
outputType = opList[state.progress].outputType,
|
|
|
|
input = state.dish.get(inputType),
|
|
|
|
ings = opList[state.progress].getIngValues(),
|
|
|
|
splitDelim = ings[0],
|
|
|
|
mergeDelim = ings[1],
|
|
|
|
ignoreErrors = ings[2],
|
|
|
|
subOpList = [],
|
2017-04-13 18:31:26 +01:00
|
|
|
inputs = [],
|
|
|
|
i;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (input)
|
2017-01-31 18:24:56 +00:00
|
|
|
inputs = input.split(splitDelim);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
// Create subOpList for each tranche to operate on
|
2016-11-28 10:42:58 +00:00
|
|
|
// (all remaining operations unless we encounter a Merge)
|
2017-04-13 18:31:26 +01:00
|
|
|
for (i = state.progress + 1; i < opList.length; i++) {
|
2017-01-31 18:24:56 +00:00
|
|
|
if (opList[i].name === "Merge" && !opList[i].isDisabled()) {
|
2016-11-28 10:42:58 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2017-01-31 18:24:56 +00:00
|
|
|
subOpList.push(opList[i]);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
let recipe = new Recipe(),
|
2016-11-28 10:42:58 +00:00
|
|
|
output = "",
|
2017-01-16 15:58:38 +00:00
|
|
|
progress = 0;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
recipe.addOperations(subOpList);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Run recipe over each tranche
|
|
|
|
for (i = 0; i < inputs.length; i++) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const dish = new Dish(inputs[i], inputType);
|
2017-01-16 15:58:38 +00:00
|
|
|
try {
|
2017-04-21 17:48:42 -04:00
|
|
|
progress = await recipe.execute(dish, 0);
|
2017-02-09 15:09:33 +00:00
|
|
|
} catch (err) {
|
2017-01-31 18:24:56 +00:00
|
|
|
if (!ignoreErrors) {
|
2017-01-16 15:58:38 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
progress = err.progress + 1;
|
|
|
|
}
|
2017-01-31 18:24:56 +00:00
|
|
|
output += dish.get(outputType) + mergeDelim;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
state.dish.set(output, outputType);
|
2016-11-28 10:42:58 +00:00
|
|
|
state.progress += progress;
|
|
|
|
return state;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Merge 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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runMerge: function(state) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// No need to actually do anything here. The fork operation will
|
|
|
|
// merge when it sees this operation.
|
|
|
|
return state;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2017-09-28 16:27:39 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2017-09-28 17:35:52 +00:00
|
|
|
if (!registers) return state;
|
|
|
|
|
|
|
|
if (ENVIRONMENT_IS_WORKER()) {
|
2017-09-28 18:39:35 +00:00
|
|
|
self.setRegisters(state.progress, state.numRegisters, registers.slice(1));
|
2017-09-28 17:35:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 16:27:39 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2017-09-28 18:39:35 +00:00
|
|
|
str = str ? str.replace(/((?:^|[^\\])(?:\\.|[^\\])*?)\$R(\d{1,2})/g, (match, pre, regNum) => {
|
2017-09-28 16:27:39 +00:00
|
|
|
const index = parseInt(regNum, 10) + 1;
|
2017-09-28 18:39:35 +00:00
|
|
|
return (index <= state.numRegisters || index >= state.numRegisters + registers.length) ?
|
|
|
|
match : pre + registers[index - state.numRegisters];
|
|
|
|
}) : str;
|
2017-09-28 16:27:39 +00:00
|
|
|
|
|
|
|
// Unescape remaining register references
|
2017-09-28 18:39:35 +00:00
|
|
|
return str ? str.replace(/\\\$R(\d{1,2})/, "$R$1") : str;
|
2017-09-28 16:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step through all subsequent ops and replace registers in args with extracted content
|
|
|
|
for (let i = state.progress + 1; i < state.opList.length; i++) {
|
2017-09-28 18:39:35 +00:00
|
|
|
if (state.opList[i].isDisabled()) continue;
|
|
|
|
|
2017-09-28 16:27:39 +00:00
|
|
|
let args = state.opList[i].getIngValues();
|
|
|
|
args = args.map(arg => {
|
|
|
|
if (typeof arg !== "string" && typeof arg !== "object") return arg;
|
|
|
|
|
2017-09-28 18:39:35 +00:00
|
|
|
if (typeof arg === "object" && arg.hasOwnProperty("string")) {
|
2017-09-28 16:27:39 +00:00
|
|
|
arg.string = replaceRegister(arg.string);
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
return replaceRegister(arg);
|
|
|
|
});
|
|
|
|
state.opList[i].setIngValues(args);
|
|
|
|
}
|
|
|
|
|
2017-09-28 18:39:35 +00:00
|
|
|
state.numRegisters += registers.length - 1;
|
2017-09-28 16:27:39 +00:00
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Jump 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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
|
|
|
* @param {number} state.numJumps - The number of jumps taken so far.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runJump: function(state) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let ings = state.opList[state.progress].getIngValues(),
|
2017-01-31 18:24:56 +00:00
|
|
|
jumpNum = ings[0],
|
|
|
|
maxJumps = ings[1];
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-03-04 10:57:14 +00:00
|
|
|
if (jumpNum < 0) {
|
|
|
|
jumpNum--;
|
|
|
|
}
|
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (state.numJumps >= maxJumps) {
|
2017-01-16 16:00:44 +00:00
|
|
|
return state;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
state.progress += jumpNum;
|
|
|
|
state.numJumps++;
|
2016-11-28 10:42:58 +00:00
|
|
|
return state;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Conditional Jump 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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
|
|
|
* @param {number} state.numJumps - The number of jumps taken so far.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runCondJump: function(state) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let ings = state.opList[state.progress].getIngValues(),
|
2017-01-31 18:24:56 +00:00
|
|
|
dish = state.dish,
|
|
|
|
regexStr = ings[0],
|
|
|
|
jumpNum = ings[1],
|
|
|
|
maxJumps = ings[2];
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-03-04 10:57:14 +00:00
|
|
|
if (jumpNum < 0) {
|
|
|
|
jumpNum--;
|
|
|
|
}
|
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (state.numJumps >= maxJumps) {
|
2017-01-16 16:00:44 +00:00
|
|
|
return state;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (regexStr !== "" && dish.get(Dish.STRING).search(regexStr) > -1) {
|
|
|
|
state.progress += jumpNum;
|
|
|
|
state.numJumps++;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return state;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Return 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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runReturn: function(state) {
|
|
|
|
state.progress = state.opList.length;
|
2016-11-28 10:42:58 +00:00
|
|
|
return state;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-04-27 13:05:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Comment operation.
|
2017-04-27 13:12:45 +00:00
|
|
|
*
|
2017-04-27 13:05:29 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
runComment: function(state) {
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default FlowControl;
|