Skip to content

Commit

Permalink
Ensure the optimisation happens even at the end of the file.
Browse files Browse the repository at this point in the history
If `[-]` was the last sequence in the program, the bounds check was too
strict, and would prevent it being optimised. The resulting program
still ran correctly, but unnecessarily slow. This commit fixes that.
  • Loading branch information
ltratt committed Oct 28, 2023
1 parent 3a35f8a commit c28988e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion interp7/src/main.rs
Expand Up @@ -42,7 +42,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
}
let mut i = 0;
while i < prog.len() {
if i + 3 < prog.len() && matches!(prog[i..i+3], [Ops::LBrack(_), Ops::Sub(1), Ops::RBrack(_)]) {
if i + 2 < prog.len() && matches!(prog[i..i+3], [Ops::LBrack(_), Ops::Sub(1), Ops::RBrack(_)]) {
prog.splice(i..i+3, [Ops::Zero]);
i += 3;
} else { i += 1; }
Expand Down
2 changes: 1 addition & 1 deletion interp8/src/main.rs
Expand Up @@ -11,7 +11,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
fn optimise(prog: &mut Vec<Ops>) {
let mut i = 0;
while i < prog.len() {
if i + 3 < prog.len() && matches!(prog[i..i+3], [Ops::LBrack(_), Ops::Sub(1), Ops::RBrack(_)]) {
if i + 2 < prog.len() && matches!(prog[i..i+3], [Ops::LBrack(_), Ops::Sub(1), Ops::RBrack(_)]) {
prog.splice(i..i+3, [Ops::Zero]);
i += 3;
} else { i += 1; }
Expand Down

0 comments on commit c28988e

Please sign in to comment.