Skip to content

Commit

Permalink
Add comments between two #
Browse files Browse the repository at this point in the history
  • Loading branch information
Sellig6792 committed Jan 5, 2023
1 parent 76518cf commit 8b6a547
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Parser {
fn _parse(&self, index: Option<usize>, stop_char: Option<char>) -> (Vec<Instruction>, usize) {
let mut index = index.unwrap_or(0);
let mut instructions = vec![];
let mut comment = false;

while index < self.program.len() {
let char = match self.program.chars().nth(index) {
Expand All @@ -33,6 +34,15 @@ impl Parser {
return (instructions, index);
}

if char == '#' {
comment = !comment;
}

if comment {
index += 1;
continue;
}

match char {
'+' => instructions.push(Instruction::new(InstructionType::Increment, None)),
'-' => instructions.push(Instruction::new(InstructionType::Decrement, None)),
Expand Down
10 changes: 10 additions & 0 deletions src/evaluation/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,14 @@ mod tests {
let mut evaluator = Evaluator::new(instructions);
evaluator.evaluate(None, Some(false));
}

#[test]
fn test_comments() {
let program = String::from("++++++[>++++++++<-]>#+#.");
let mut parser = ast::Parser::new(program);
let instructions = parser.parse();
let mut evaluator = Evaluator::new(instructions);
evaluator.evaluate(None, Some(false));
assert_eq!(String::from_utf8(evaluator.output_buffer).unwrap(), "0");
}
}

0 comments on commit 8b6a547

Please sign in to comment.