Skip to content

Commit

Permalink
Prefer matches! where it's more concise
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Jun 16, 2023
1 parent 5d58aa3 commit db06364
Showing 1 changed file with 46 additions and 47 deletions.
93 changes: 46 additions & 47 deletions src/peephole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,25 +370,25 @@ fn remove_dead_loops(instrs: Vec<AstNode>) -> Vec<AstNode> {
.into_iter()
.enumerate()
.filter(|&(index, ref instr)| {
match *instr {
Loop { .. } => {}
if !matches!(instr, Loop { .. }) {
// Keep all instructions that aren't loops.
_ => {
return true;
}
return true;
}

// Find the previous change instruction:
if let Some(prev_change_index) = previous_cell_change(&instrs, index) {
let prev_instr = &instrs[prev_change_index];
// If the previous instruction set to zero, our loop is dead.
// TODO: MultiplyMove also zeroes the current cell.
if let Set {
amount: Wrapping(0),
offset: 0,
..
} = *prev_instr
{
// TODO: define an is_set_zero() helper.
if matches!(
prev_instr,
Set {
amount: Wrapping(0),
offset: 0,
..
}
) {
return false;
}
}
Expand All @@ -410,23 +410,23 @@ fn sort_by_offset(instrs: Vec<AstNode>) -> Vec<AstNode> {
let mut result = vec![];

for instr in instrs {
match instr {
Increment { .. } | Set { .. } | PointerIncrement { .. } => {
sequence.push(instr);
if matches!(
instr,
Increment { .. } | Set { .. } | PointerIncrement { .. }
) {
sequence.push(instr);
} else {
if !sequence.is_empty() {
result.extend(sort_sequence_by_offset(sequence));
sequence = vec![];
}
_ => {
if !sequence.is_empty() {
result.extend(sort_sequence_by_offset(sequence));
sequence = vec![];
}
if let Loop { body, position } = instr {
result.push(Loop {
body: sort_by_offset(body),
position,
});
} else {
result.push(instr);
}
if let Loop { body, position } = instr {
result.push(Loop {
body: sort_by_offset(body),
position,
});
} else {
result.push(instr);
}
}
}
Expand Down Expand Up @@ -609,12 +609,14 @@ fn remove_redundant_sets(instrs: Vec<AstNode>) -> Vec<AstNode> {

// Remove a set zero at the beginning of the program, since cells
// are initialised to zero anyway.
if let Some(&Set {
amount: Wrapping(0),
offset: 0,
..
}) = reduced.first()
{
if matches!(
reduced.first(),
Some(Set {
amount: Wrapping(0),
offset: 0,
..
})
) {
reduced.remove(0);
}

Expand All @@ -625,22 +627,19 @@ fn remove_redundant_sets_inner(instrs: Vec<AstNode>) -> Vec<AstNode> {
let mut redundant_instr_positions = HashSet::new();

for (index, instr) in instrs.iter().enumerate() {
match *instr {
Loop { .. } | MultiplyMove { .. } => {
// There's no point setting to zero after a loop, as
// the cell is already zero.
if let Some(next_index) = next_cell_change(&instrs, index) {
if let Set {
amount: Wrapping(0),
offset: 0,
..
} = instrs[next_index]
{
redundant_instr_positions.insert(next_index);
}
if matches!(instr, Loop { .. } | MultiplyMove { .. }) {
// There's no point setting to zero after a loop, as
// the cell is already zero.
if let Some(next_index) = next_cell_change(&instrs, index) {
if let Set {
amount: Wrapping(0),
offset: 0,
..
} = instrs[next_index]
{
redundant_instr_positions.insert(next_index);
}
}
_ => {}
}
}

Expand Down

0 comments on commit db06364

Please sign in to comment.