FlowControl.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import Recipe from "./Recipe";
  2. import Dish from "./Dish";
  3. /**
  4. * Flow Control operations.
  5. *
  6. * @author n1474335 [n1474335@gmail.com]
  7. * @copyright Crown Copyright 2016
  8. * @license Apache-2.0
  9. *
  10. * @namespace
  11. */
  12. const FlowControl = {
  13. /**
  14. * Fork operation.
  15. *
  16. * @param {Object} state - The current state of the recipe.
  17. * @param {number} state.progress - The current position in the recipe.
  18. * @param {Dish} state.dish - The Dish being operated on.
  19. * @param {Operation[]} state.opList - The list of operations in the recipe.
  20. * @returns {Object} The updated state of the recipe.
  21. */
  22. runFork: async function(state) {
  23. let opList = state.opList,
  24. inputType = opList[state.progress].inputType,
  25. outputType = opList[state.progress].outputType,
  26. input = state.dish.get(inputType),
  27. ings = opList[state.progress].ingValues,
  28. splitDelim = ings[0],
  29. mergeDelim = ings[1],
  30. ignoreErrors = ings[2],
  31. subOpList = [],
  32. inputs = [],
  33. i;
  34. if (input)
  35. inputs = input.split(splitDelim);
  36. // Create subOpList for each tranche to operate on
  37. // (all remaining operations unless we encounter a Merge)
  38. for (i = state.progress + 1; i < opList.length; i++) {
  39. if (opList[i].name === "Merge" && !opList[i].disabled) {
  40. break;
  41. } else {
  42. subOpList.push(opList[i]);
  43. }
  44. }
  45. let recipe = new Recipe(),
  46. output = "",
  47. progress = 0;
  48. state.forkOffset += state.progress + 1;
  49. recipe.addOperations(subOpList);
  50. // Take a deep(ish) copy of the ingredient values
  51. const ingValues = subOpList.map(op => JSON.parse(JSON.stringify(op.ingValues)));
  52. // Run recipe over each tranche
  53. for (i = 0; i < inputs.length; i++) {
  54. log.debug(`Entering tranche ${i + 1} of ${inputs.length}`);
  55. // Baseline ing values for each tranche so that registers are reset
  56. subOpList.forEach((op, i) => {
  57. op.ingValues = JSON.parse(JSON.stringify(ingValues[i]));
  58. });
  59. const dish = new Dish(inputs[i], inputType);
  60. try {
  61. progress = await recipe.execute(dish, 0, state);
  62. } catch (err) {
  63. if (!ignoreErrors) {
  64. throw err;
  65. }
  66. progress = err.progress + 1;
  67. }
  68. output += dish.get(outputType) + mergeDelim;
  69. }
  70. state.dish.set(output, outputType);
  71. state.progress += progress;
  72. return state;
  73. },
  74. /**
  75. * Merge operation.
  76. *
  77. * @param {Object} state - The current state of the recipe.
  78. * @param {number} state.progress - The current position in the recipe.
  79. * @param {Dish} state.dish - The Dish being operated on.
  80. * @param {Operation[]} state.opList - The list of operations in the recipe.
  81. * @returns {Object} The updated state of the recipe.
  82. */
  83. runMerge: function(state) {
  84. // No need to actually do anything here. The fork operation will
  85. // merge when it sees this operation.
  86. return state;
  87. },
  88. /**
  89. * Register operation.
  90. *
  91. * @param {Object} state - The current state of the recipe.
  92. * @param {number} state.progress - The current position in the recipe.
  93. * @param {Dish} state.dish - The Dish being operated on.
  94. * @param {Operation[]} state.opList - The list of operations in the recipe.
  95. * @returns {Object} The updated state of the recipe.
  96. */
  97. runRegister: function(state) {
  98. const ings = state.opList[state.progress].ingValues,
  99. extractorStr = ings[0],
  100. i = ings[1],
  101. m = ings[2];
  102. let modifiers = "";
  103. if (i) modifiers += "i";
  104. if (m) modifiers += "m";
  105. const extractor = new RegExp(extractorStr, modifiers),
  106. input = state.dish.get(Dish.STRING),
  107. registers = input.match(extractor);
  108. if (!registers) return state;
  109. if (ENVIRONMENT_IS_WORKER()) {
  110. self.setRegisters(state.forkOffset + state.progress, state.numRegisters, registers.slice(1));
  111. }
  112. /**
  113. * Replaces references to registers (e.g. $R0) with the contents of those registers.
  114. *
  115. * @param {string} str
  116. * @returns {string}
  117. */
  118. function replaceRegister(str) {
  119. // Replace references to registers ($Rn) with contents of registers
  120. return str.replace(/(\\*)\$R(\d{1,2})/g, (match, slashes, regNum) => {
  121. const index = parseInt(regNum, 10) + 1;
  122. if (index <= state.numRegisters || index >= state.numRegisters + registers.length)
  123. return match;
  124. if (slashes.length % 2 !== 0) return match.slice(1); // Remove escape
  125. return slashes + registers[index - state.numRegisters];
  126. });
  127. }
  128. // Step through all subsequent ops and replace registers in args with extracted content
  129. for (let i = state.progress + 1; i < state.opList.length; i++) {
  130. if (state.opList[i].disabled) continue;
  131. let args = state.opList[i].ingValues;
  132. args = args.map(arg => {
  133. if (typeof arg !== "string" && typeof arg !== "object") return arg;
  134. if (typeof arg === "object" && arg.hasOwnProperty("string")) {
  135. arg.string = replaceRegister(arg.string);
  136. return arg;
  137. }
  138. return replaceRegister(arg);
  139. });
  140. state.opList[i].setIngValues(args);
  141. }
  142. state.numRegisters += registers.length - 1;
  143. return state;
  144. },
  145. /**
  146. * Jump operation.
  147. *
  148. * @param {Object} state - The current state of the recipe.
  149. * @param {number} state.progress - The current position in the recipe.
  150. * @param {Dish} state.dish - The Dish being operated on.
  151. * @param {Operation[]} state.opList - The list of operations in the recipe.
  152. * @param {number} state.numJumps - The number of jumps taken so far.
  153. * @returns {Object} The updated state of the recipe.
  154. */
  155. runJump: function(state) {
  156. const ings = state.opList[state.progress].ingValues,
  157. label = ings[0],
  158. maxJumps = ings[1],
  159. jmpIndex = FlowControl._getLabelIndex(label, state);
  160. if (state.numJumps >= maxJumps || jmpIndex === -1) {
  161. log.debug("Maximum jumps reached or label cannot be found");
  162. return state;
  163. }
  164. state.progress = jmpIndex;
  165. state.numJumps++;
  166. log.debug(`Jumping to label '${label}' at position ${jmpIndex} (jumps = ${state.numJumps})`);
  167. return state;
  168. },
  169. /**
  170. * Conditional Jump operation.
  171. *
  172. * @param {Object} state - The current state of the recipe.
  173. * @param {number} state.progress - The current position in the recipe.
  174. * @param {Dish} state.dish - The Dish being operated on.
  175. * @param {Operation[]} state.opList - The list of operations in the recipe.
  176. * @param {number} state.numJumps - The number of jumps taken so far.
  177. * @returns {Object} The updated state of the recipe.
  178. */
  179. runCondJump: function(state) {
  180. const ings = state.opList[state.progress].ingValues,
  181. dish = state.dish,
  182. regexStr = ings[0],
  183. invert = ings[1],
  184. label = ings[2],
  185. maxJumps = ings[3],
  186. jmpIndex = FlowControl._getLabelIndex(label, state);
  187. if (state.numJumps >= maxJumps || jmpIndex === -1) {
  188. log.debug("Maximum jumps reached or label cannot be found");
  189. return state;
  190. }
  191. if (regexStr !== "") {
  192. let strMatch = dish.get(Dish.STRING).search(regexStr) > -1;
  193. if (!invert && strMatch || invert && !strMatch) {
  194. state.progress = jmpIndex;
  195. state.numJumps++;
  196. log.debug(`Jumping to label '${label}' at position ${jmpIndex} (jumps = ${state.numJumps})`);
  197. }
  198. }
  199. return state;
  200. },
  201. /**
  202. * Return operation.
  203. *
  204. * @param {Object} state - The current state of the recipe.
  205. * @param {number} state.progress - The current position in the recipe.
  206. * @param {Dish} state.dish - The Dish being operated on.
  207. * @param {Operation[]} state.opList - The list of operations in the recipe.
  208. * @returns {Object} The updated state of the recipe.
  209. */
  210. runReturn: function(state) {
  211. state.progress = state.opList.length;
  212. return state;
  213. },
  214. /**
  215. * Comment operation.
  216. *
  217. * @param {Object} state - The current state of the recipe.
  218. * @param {number} state.progress - The current position in the recipe.
  219. * @param {Dish} state.dish - The Dish being operated on.
  220. * @param {Operation[]} state.opList - The list of operations in the recipe.
  221. * @returns {Object} The updated state of the recipe.
  222. */
  223. runComment: function(state) {
  224. return state;
  225. },
  226. /**
  227. * Returns the index of a label.
  228. *
  229. * @private
  230. * @param {Object} state
  231. * @param {string} name
  232. * @returns {number}
  233. */
  234. _getLabelIndex: function(name, state) {
  235. for (let o = 0; o < state.opList.length; o++) {
  236. let operation = state.opList[o];
  237. if (operation.name === "Label"){
  238. let ings = operation.ingValues;
  239. if (name === ings[0]) {
  240. return o;
  241. }
  242. }
  243. }
  244. return -1;
  245. },
  246. };
  247. export default FlowControl;