joinAdjacentOps should recurse

This commit is contained in:
Juhani Krekelä 2018-05-24 22:50:31 +03:00
parent 151c8c9113
commit e046d187ae
2 changed files with 16 additions and 8 deletions

22
gir.js
View File

@ -241,17 +241,23 @@ function joinAdjacentOps(parsed) {
// Start of a possible run of commands // Start of a possible run of commands
prevOfType = true; prevOfType = true;
value = command.value; value = command.value;
} else if(prevOfType && command.type != type) { } else {
// A run has ended, add it to optimized // If a run has ended, add it to optimized
// However, skip it if value is 0 // However, skip it if value is 0
if(value != 0) { if(prevOfType && value != 0) {
optimized.push({type: type, value: value}); optimized.push({type: type, value: value});
} }
// Also add the command for this round
optimized.push(command); // Add the command for this round
prevOfType = false; if(command.type == loop) {
} else { // Recurse
optimized.push(command); optimized.push({type: loop,
contents: worker(
command.contents,
type)});
} else {
optimized.push(command);
}
prevOfType = false; prevOfType = false;
} }
} }

View File

@ -6,6 +6,7 @@ joinAdjacentOps
* *produces*: commands without offsets * *produces*: commands without offsets
* *unknown commands*: passed through unmodified * *unknown commands*: passed through unmodified
* *acts on*: `moveHead`, `add` * *acts on*: `moveHead`, `add`
* *other known commands*: `loop` (recurses)
`joinAdjacentOps` joins adjacent `moveHead`s and `add`s together. Normally `joinAdjacentOps` joins adjacent `moveHead`s and `add`s together. Normally
`parse` joins runs of `<>` or `+-` into one command, but if the runs are `parse` joins runs of `<>` or `+-` into one command, but if the runs are
@ -19,6 +20,7 @@ transformClearLoops
* *produces*: commands without offsets * *produces*: commands without offsets
* *unknown commands*: passed through unmodified * *unknown commands*: passed through unmodified
* *acts on*: `loop` with one command, which is `add 1` or `add -1` * *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 `transformClearLoops` changes loops of the form `[-]` or `[+]` into a single
`clear` command. `clear` command.