Skip to content

Commit

Permalink
Write hello world test and fix the print that printed only one time e…
Browse files Browse the repository at this point in the history
…ven if there was more than one "." instruction
  • Loading branch information
Sellig6792 committed Dec 27, 2022
1 parent 723fb9e commit ba2a7af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
43 changes: 35 additions & 8 deletions src/evaluation/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::ast::instructions::{InstructionTrait, InstructionType};
use crate::evaluation::{Cell, Scopes};

pub struct Evaluator<T: InstructionTrait<T>>
where
T: Clone,
where
T: Clone,
{
program: Vec<T>,

Expand All @@ -17,8 +17,8 @@ pub struct Evaluator<T: InstructionTrait<T>>
}

impl<T: InstructionTrait<T> + 'static> Evaluator<T>
where
T: Clone,
where
T: Clone,
{
pub fn new(instructions: Vec<T>) -> Evaluator<T> {
Evaluator {
Expand Down Expand Up @@ -69,8 +69,10 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
.set_value(self.input.remove(0));
}
InstructionType::Output => {
self.output_buffer
.push(self.scopes.get_current_cell().get_value());
for _ in 0..instruction.get_amount() {
self.output_buffer
.push(self.scopes.get_current_cell().get_value());
}
}

InstructionType::Loop => {
Expand Down Expand Up @@ -99,10 +101,12 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
}

InstructionType::MoveLeftScope => {
self.scopes.move_left_scope(instruction.get_amount() as usize);
self.scopes
.move_left_scope(instruction.get_amount() as usize);
}
InstructionType::MoveRightScope => {
self.scopes.move_right_scope(instruction.get_amount() as usize);
self.scopes
.move_right_scope(instruction.get_amount() as usize);
}

InstructionType::Random => {
Expand Down Expand Up @@ -146,3 +150,26 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ast;
use crate::optimization;

#[test]
fn test_hello_world() {
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(),
"Hello World!\n"
);
}
}
12 changes: 3 additions & 9 deletions src/evaluation/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ where
pub fn move_right(&mut self, amount: usize) {
let sum: usize = self.index + amount;

self.index = if sum > 29999 {
sum - 30000
} else {
sum
};

self.index = if sum > 29999 { sum - 30000 } else { sum };
}

pub fn move_left(&mut self, amount: usize) {
Expand Down Expand Up @@ -145,11 +140,10 @@ where
}
}


#[cfg(test)]
mod tests {
use crate::ast::instructions::Instruction;
use super::*;
use crate::ast::instructions::Instruction;

#[test]
fn test_move_right() {
Expand Down Expand Up @@ -234,4 +228,4 @@ mod tests {
scopes.move_left_scope(8);
assert_eq!(scopes.get_scope_index(), 0);
}
}
}

0 comments on commit ba2a7af

Please sign in to comment.