hrm_interpreter 0.2.0

a small interpreter for the HRM language, used in Human Resource Machine
Documentation
use Location;
use Value;
use state::InternalState;

#[derive(Debug)]
pub enum Error {
    PointerCellContainsChar(Location, Value),
    NoValue(Location)
}

pub fn extract_memory_position(cell: Location, s: &InternalState) -> Result<usize, Error> {
    match cell {
        Location::Cell(mempos) => Ok(mempos),
        Location::Address(mempos) => {
            let value_from_memory = s.memory[mempos].clone();
            match value_from_memory {
                Some(Value::Number{value: pointed_cell}) => Ok(pointed_cell as usize),
                Some(Value::Character{value: _}) =>
                    Err(Error::PointerCellContainsChar(cell, value_from_memory.unwrap())),
                None => Err(Error::NoValue(Location::Cell(mempos)))
            }
        }
    }
}

pub fn explain(error: Error) -> String {
    match error {
        Error::NoValue(Location::Cell(_cell)) =>
            format!("There is no value at cell {:?}", _cell),
        Error::NoValue(Location::Address(_cell)) =>
            format!("There is no value at cell {:?}", _cell),
        Error::PointerCellContainsChar(cell, value) =>
            format!("Cell {:?} should contain a number, not a char({:?})", cell, value)
    }
}