Skip to content

Commit

Permalink
Clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sellig6792 committed Jan 5, 2023
1 parent df75989 commit d853d11
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ pub struct Parser {
impl Parser {
pub fn new(program: String) -> Parser {
let program = program
.replace(' ', "")
.replace('\t', "")
.replace('\r', "")
.replace('\n', "");
.replace([' ', '\t', '\r', '\n'], "");

Parser { program }
}

pub fn parse(&mut self) -> Vec<Instruction> {
let (instructions, _) = self._parse(None, None);
return instructions;
instructions
}

fn _parse(&self, index: Option<usize>, stop_char: Option<char>) -> (Vec<Instruction>, usize) {
Expand Down Expand Up @@ -99,6 +96,6 @@ impl Parser {
index += 1;
}

return (instructions, index);
(instructions, index)
}
}
2 changes: 1 addition & 1 deletion src/evaluation/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Cell {
pub fn sub<T: PrimInt>(&mut self, value: T) {
let sub: isize = self.value as isize - value.to_isize().unwrap();
self.value = if sub < 0 {
u8::MAX - (sub.abs() as u8 - 1)
u8::MAX - (sub.unsigned_abs() as u8 - 1)
} else {
sub as u8
};
Expand Down
6 changes: 3 additions & 3 deletions src/evaluation/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod tests {
let program = String::from("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
let mut parser = ast::Parser::new(program);
let instructions = parser.parse();
let mut optimizer = optimization::Optimizer::new(instructions.clone());
let mut optimizer = optimization::Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();
let mut brainfuck = Evaluator::new(optimized_instructions);
brainfuck.evaluate(None, Some(false));
Expand All @@ -195,7 +195,7 @@ mod tests {
let program = String::from("{++++[>++++++++++++<-]>.}{++++[>++++++++++++<-]>+.}=");
let mut parser = ast::Parser::new(program);
let instructions = parser.parse();
let mut optimizer = optimization::Optimizer::new(instructions.clone());
let mut optimizer = optimization::Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();
let mut brainfuck = Evaluator::new(optimized_instructions);
brainfuck.evaluate(None, Some(false));
Expand All @@ -207,7 +207,7 @@ mod tests {
let program = String::from("{+++++[>+++++++++++++<-]>.<}==");
let mut parser = ast::Parser::new(program);
let instructions = parser.parse();
let mut optimizer = optimization::Optimizer::new(instructions.clone());
let mut optimizer = optimization::Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();
let mut brainfuck = Evaluator::new(optimized_instructions);
brainfuck.evaluate(None, Some(false));
Expand Down
2 changes: 1 addition & 1 deletion src/evaluation/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
let sub: isize = self.index as isize - amount as isize;

self.index = if sub < 0 {
30000 - (sub.abs() as usize)
30000 - sub.unsigned_abs()
} else {
sub as usize
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Optimize the program if "optimize" is true
if args.optimize {
let mut optimizer = optimization::Optimizer::new(instructions.clone());
let mut optimizer = optimization::Optimizer::new(instructions);
let optimized_instructions = optimizer.optimize();

// Evaluate the optimized program
Expand Down
8 changes: 4 additions & 4 deletions src/optimization/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Optimizer {
optimizer.optimize()
}

fn merge_instructions(&self, optimized_instructions: &mut Vec<OptimizedInstruction>) -> () {
fn merge_instructions(&self, optimized_instructions: &mut Vec<OptimizedInstruction>) {
for instruction in self.instructions.iter() {
match optimized_instructions.last_mut() {
Some(last_optimized_instruction) => {
Expand Down Expand Up @@ -79,11 +79,11 @@ impl Optimizer {
fn cancel_opposed_instructions(
&self,
optimized_instructions: &mut Vec<OptimizedInstruction>,
) -> () {
) {
let mut new_optimized_instructions: Vec<OptimizedInstruction> = vec![];

for optimized_instruction in optimized_instructions.iter() {
match new_optimized_instructions.last().clone() {
match new_optimized_instructions.last() {
Some(last_optimized_instruction) => {
if last_optimized_instruction.is_opposed(optimized_instruction) {
let last_amount = last_optimized_instruction.get_amount();
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Optimizer {
*optimized_instructions = new_optimized_instructions;
}

fn recognize_patterns(&self, optimized_instructions: &mut Vec<OptimizedInstruction>) -> () {
fn recognize_patterns(&self, optimized_instructions: &mut Vec<OptimizedInstruction>) {
let mut new_optimized_instructions: Vec<OptimizedInstruction> =
optimized_instructions.clone();

Expand Down

0 comments on commit d853d11

Please sign in to comment.