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
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;
}
}

View File

@ -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.