FlowControl.js 9.2 KB

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