Skip to content

Commit

Permalink
Fix optimization for double functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sellig6792 committed Dec 27, 2022
1 parent 722f3a4 commit f62420b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/evaluation/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}
12 changes: 11 additions & 1 deletion src/optimization/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit f62420b

Please sign in to comment.