use super::{parse, Instruction};
pub enum ProgramStatus {
Run,
Exit,
}
#[derive(Default)]
pub struct Program {
instructions: Vec<Instruction>,
pub counter: usize,
len: usize,
}
impl Program {
pub fn current(&self) -> &Instruction {
if self.len == 0 {
panic!("Called .current() on Program but .len is zero.")
}
&self.instructions[self.counter.min(self.len - 1)]
}
pub fn step(&mut self) -> ProgramStatus {
self.counter += 1;
if self.counter >= self.len {
return ProgramStatus::Exit;
}
ProgramStatus::Run
}
}
impl<T: AsRef<str>> From<T> for Program {
fn from(s: T) -> Self {
let iv = parse(s);
Self {
len: iv.len(),
instructions: iv,
counter: 0,
}
}
}