stak-vm 0.12.13

Virtual machines for Stak Scheme
Documentation
use crate::exception::Exception;
use core::{
    error,
    fmt::{self, Debug, Display, Formatter},
};

/// An error of a virtual machine.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
    /// Mismatched numbers of call arguments and procedure parameters.
    ArgumentCount,
    /// A cons expected.
    ConsExpected,
    /// An unexpected end of bytecode.
    BytecodeEnd,
    /// A format error.
    Format(fmt::Error),
    /// Invalid memory access.
    InvalidMemoryAccess,
    /// An illegal instruction detected.
    IllegalInstruction,
    /// An illegal primitive detected.
    IllegalPrimitive,
    /// A number expected.
    NumberExpected,
    /// Out of memory.
    OutOfMemory,
    /// A procedure expected.
    ProcedureExpected,
}

impl Exception for Error {
    fn is_critical(&self) -> bool {
        match self {
            Self::ArgumentCount
            | Self::InvalidMemoryAccess
            | Self::IllegalPrimitive
            | Self::NumberExpected
            | Self::ConsExpected
            | Self::ProcedureExpected => false,
            Self::BytecodeEnd | Self::Format(_) | Self::IllegalInstruction | Self::OutOfMemory => {
                true
            }
        }
    }
}

impl error::Error for Error {}

impl Display for Error {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            Self::ArgumentCount => write!(formatter, "invalid argument count"),
            Self::BytecodeEnd => write!(formatter, "unexpected end of bytecode"),
            Self::ConsExpected => write!(formatter, "cons expected"),
            Self::Format(error) => write!(formatter, "{error}"),
            Self::InvalidMemoryAccess => write!(formatter, "invalid memory access"),
            Self::IllegalInstruction => write!(formatter, "illegal instruction"),
            Self::IllegalPrimitive => write!(formatter, "illegal primitive"),
            Self::NumberExpected => write!(formatter, "number expected"),
            Self::OutOfMemory => write!(formatter, "out of memory"),
            Self::ProcedureExpected => write!(formatter, "procedure expected"),
        }
    }
}

impl From<fmt::Error> for Error {
    fn from(error: fmt::Error) -> Self {
        Self::Format(error)
    }
}