You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
791 B

use crate::instructions::Instruction;
use std::io::Read;
pub fn run(instructions: &Vec<Instruction>, tape: &mut Vec<u8>, ptr: &mut usize) {
for instruction in instructions {
match instruction {
Instruction::Increment => tape[*ptr] += 1,
Instruction::Decrement => tape[*ptr] -= 1,
Instruction::MoveRight => *ptr += 1,
Instruction::MoveLeft => *ptr -= 1,
Instruction::Print => print!("{}", tape[*ptr] as char),
Instruction::Loop(nested) => {
while tape[*ptr] != 0 {
run(&nested, tape, ptr)
}
}
Instruction::Read => {
let mut input: [u8; 1] = [0; 1];
std::io::stdin()
.read_exact(&mut input)
.expect("failed to read stdin");
tape[*ptr] = input[0];
}
}
}
}