Skip to content

Commit

Permalink
Fix functions that were only run once time instead of [amount]
Browse files Browse the repository at this point in the history
  • Loading branch information
Sellig6792 committed Dec 27, 2022
1 parent d0f01cd commit 7646100
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/evaluation/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where

InstructionType::Loop => {
while *self.scopes.get_current_cell() != 0 {
self.evaluate(Some(instruction.clone()), Some(false))
self.evaluate(Some(instruction.clone()), Some(false));
}
}
InstructionType::Function => {
Expand All @@ -86,18 +86,20 @@ where
}

InstructionType::CallFunction => {
self.scopes.push();
self.evaluate(
Some(
self.scopes
.get_scope_at(self.scopes.get_scope_index() - 1)
.unwrap()
.get_function(self.scopes.get_index())
.clone(),
),
Some(false),
);
self.scopes.pop();
for _ in 0..instruction.get_amount() {
self.scopes.push();
self.evaluate(
Some(
self.scopes
.get_scope_at(self.scopes.get_scope_index() - 1)
.unwrap()
.get_function(self.scopes.get_index())
.clone(),
),
Some(false),
);
self.scopes.pop();
}
}

InstructionType::MoveLeftScope => {
Expand Down Expand Up @@ -151,7 +153,7 @@ where
}

match show_output {
None => println!("{}", String::from_utf8(self.output_buffer.clone()).unwrap()),
None | Some(true) => println!("{}", String::from_utf8(self.output_buffer.clone()).unwrap()),
_ => (),
}
}
Expand Down Expand Up @@ -190,4 +192,16 @@ mod tests {
brainfuck.evaluate(None, Some(false));
assert_eq!(String::from_utf8(brainfuck.output_buffer).unwrap(), "1");
}

#[test]
fn test_call_multiple_time_functions() {
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(), "AA");
}
}

0 comments on commit 7646100

Please sign in to comment.