From f62420b45e04f524b63cf07d3aa4c43badc54675 Mon Sep 17 00:00:00 2001 From: Sellig6792 Date: Fri, 23 Dec 2022 20:54:33 +0100 Subject: [PATCH] Fix optimization for double functions --- src/evaluation/evaluator.rs | 15 +++++++++++++++ src/optimization/optimizer.rs | 12 +++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/evaluation/evaluator.rs b/src/evaluation/evaluator.rs index fac1909..e0efd5d 100644 --- a/src/evaluation/evaluator.rs +++ b/src/evaluation/evaluator.rs @@ -172,4 +172,19 @@ mod tests { "Hello World!\n" ); } + + #[test] + fn test_override_function() { + let program = String::from("{++++[>++++++++++++<-]>.}{++++[>++++++++++++<-]>+.}="); + let mut parser = ast::Parser::new(program); + let instructions = parser.parse(); + let mut optimizer = optimization::Optimizer::new(instructions.clone()); + let optimized_instructions = optimizer.optimize(); + let mut brainfuck = Evaluator::new(optimized_instructions); + brainfuck.evaluate(None, Some(false)); + assert_eq!( + String::from_utf8(brainfuck.output_buffer).unwrap(), + "1" + ); + } } diff --git a/src/optimization/optimizer.rs b/src/optimization/optimizer.rs index 8727a78..162f52f 100644 --- a/src/optimization/optimizer.rs +++ b/src/optimization/optimizer.rs @@ -30,7 +30,17 @@ impl Optimizer { if last_optimized_instruction.get_instruction_type() == instruction.get_instruction_type() { - last_optimized_instruction.add(1); + match instruction.get_instruction_type() { + InstructionType::Function => { + optimized_instructions.pop(); + optimized_instructions.push(OptimizedInstruction::new( + instruction.get_instruction_type(), + Some(self.optimize_container(instruction.get_content())), + )); + } + _ => last_optimized_instruction.add(1) + } + } else { match instruction.get_instruction_type() { InstructionType::Function | InstructionType::Loop => {