use std::io::{self, Read};

use crate::{error::ParseError, memory::BrainFuckMemory, parser::Expression};

fn run<T>(expressions: &[Expression], memory: &mut T) -> Result<(), ParseError>
where
    T: BrainFuckMemory<usize>,
{
    for exp in expressions {
        match exp {
            Expression::Increase(n) => memory.increase(*n),
            Expression::Decrease(n) => memory.decrease(*n),
            Expression::MoveForward(n) => memory.move_forward(*n),
            Expression::MoveBackward(n) => memory.move_backward(*n),
            Expression::Loop(exps) => {
                while memory.read() > 0 {
                    run(exps, memory)?;
                }
            }
            Expression::Input => todo!(),
            Expression::Output => todo!(),
        }
    }
    Ok(())
}

fn read() -> Result<usize, std::io::Error> {
    let mut input: [u8; 1] = [0];
    io::stdin().read_exact(&mut input)?;
    Ok(input[0].into())
}
