diff --git a/src/ast/parser.rs b/src/ast/parser.rs index 33627f8..29c3582 100644 --- a/src/ast/parser.rs +++ b/src/ast/parser.rs @@ -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 { let (instructions, _) = self._parse(None, None); - return instructions; + instructions } fn _parse(&self, index: Option, stop_char: Option) -> (Vec, usize) { @@ -99,6 +96,6 @@ impl Parser { index += 1; } - return (instructions, index); + (instructions, index) } } diff --git a/src/evaluation/cell.rs b/src/evaluation/cell.rs index ba5780c..6015534 100644 --- a/src/evaluation/cell.rs +++ b/src/evaluation/cell.rs @@ -28,7 +28,7 @@ impl Cell { pub fn sub(&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 }; diff --git a/src/evaluation/evaluator.rs b/src/evaluation/evaluator.rs index d571b51..6a14b95 100644 --- a/src/evaluation/evaluator.rs +++ b/src/evaluation/evaluator.rs @@ -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)); @@ -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)); @@ -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)); diff --git a/src/evaluation/scope.rs b/src/evaluation/scope.rs index e1c5b21..fb813be 100644 --- a/src/evaluation/scope.rs +++ b/src/evaluation/scope.rs @@ -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 } diff --git a/src/main.rs b/src/main.rs index ca5f6af..7cf3e6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn main() -> Result<(), Box> { // 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 diff --git a/src/optimization/optimizer.rs b/src/optimization/optimizer.rs index f4fb5ec..5f0d15d 100644 --- a/src/optimization/optimizer.rs +++ b/src/optimization/optimizer.rs @@ -24,7 +24,7 @@ impl Optimizer { optimizer.optimize() } - fn merge_instructions(&self, optimized_instructions: &mut Vec) -> () { + fn merge_instructions(&self, optimized_instructions: &mut Vec) { for instruction in self.instructions.iter() { match optimized_instructions.last_mut() { Some(last_optimized_instruction) => { @@ -79,11 +79,11 @@ impl Optimizer { fn cancel_opposed_instructions( &self, optimized_instructions: &mut Vec, - ) -> () { + ) { let mut new_optimized_instructions: Vec = 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(); @@ -129,7 +129,7 @@ impl Optimizer { *optimized_instructions = new_optimized_instructions; } - fn recognize_patterns(&self, optimized_instructions: &mut Vec) -> () { + fn recognize_patterns(&self, optimized_instructions: &mut Vec) { let mut new_optimized_instructions: Vec = optimized_instructions.clone();