FlowControl.js 8.8 KB

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