diff --git a/gir.js b/gir.js index dddb751..a273922 100644 --- a/gir.js +++ b/gir.js @@ -241,17 +241,23 @@ function joinAdjacentOps(parsed) { // Start of a possible run of commands prevOfType = true; value = command.value; - } else if(prevOfType && command.type != type) { - // A run has ended, add it to optimized + } else { + // If a run has ended, add it to optimized // However, skip it if value is 0 - if(value != 0) { + if(prevOfType && value != 0) { optimized.push({type: type, value: value}); } - // Also add the command for this round - optimized.push(command); - prevOfType = false; - } else { - optimized.push(command); + + // Add the command for this round + if(command.type == loop) { + // Recurse + optimized.push({type: loop, + contents: worker( + command.contents, + type)}); + } else { + optimized.push(command); + } prevOfType = false; } } diff --git a/optimizations.md b/optimizations.md index 8730442..28d4a77 100644 --- a/optimizations.md +++ b/optimizations.md @@ -6,6 +6,7 @@ joinAdjacentOps * *produces*: commands without offsets * *unknown commands*: passed through unmodified * *acts on*: `moveHead`, `add` +* *other known commands*: `loop` (recurses) `joinAdjacentOps` joins adjacent `moveHead`s and `add`s together. Normally `parse` joins runs of `<>` or `+-` into one command, but if the runs are @@ -19,6 +20,7 @@ transformClearLoops * *produces*: commands without offsets * *unknown commands*: passed through unmodified * *acts on*: `loop` with one command, which is `add 1` or `add -1` +* *other known commands*: `loop` (recurses) `transformClearLoops` changes loops of the form `[-]` or `[+]` into a single `clear` command.