Skip to main content

pepl_eval/
error.rs

1//! Runtime error types for the PEPL evaluator.
2
3use std::fmt;
4
5/// Evaluation error — runtime traps, assertion failures, invariant violations.
6#[derive(Debug, Clone)]
7pub enum EvalError {
8    /// Division by zero, sqrt of negative, overflow, etc.
9    ArithmeticTrap(String),
10    /// `core.assert` failure
11    AssertionFailed(String),
12    /// Invariant check failed after action commit
13    InvariantViolation(String),
14    /// Nil access: `nil.field`, `nil[i]`, `nil` used as bool, etc.
15    NilAccess(String),
16    /// `?` on an `Err` variant
17    UnwrapError(String),
18    /// Unknown variable
19    UndefinedVariable(String),
20    /// Unknown action
21    UndefinedAction(String),
22    /// Type mismatch at runtime
23    TypeMismatch(String),
24    /// Stdlib call error
25    StdlibError(String),
26    /// Unknown module or function
27    UnknownFunction(String),
28    /// Gas exhaustion
29    GasExhausted,
30    /// `return` statement (used internally for control flow)
31    Return(pepl_stdlib::Value),
32    /// Generic runtime error
33    Runtime(String),
34}
35
36impl fmt::Display for EvalError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::ArithmeticTrap(msg) => write!(f, "arithmetic trap: {msg}"),
40            Self::AssertionFailed(msg) => write!(f, "assertion failed: {msg}"),
41            Self::InvariantViolation(msg) => write!(f, "invariant violation: {msg}"),
42            Self::NilAccess(msg) => write!(f, "nil access: {msg}"),
43            Self::UnwrapError(msg) => write!(f, "unwrap error: {msg}"),
44            Self::UndefinedVariable(name) => write!(f, "undefined variable: {name}"),
45            Self::UndefinedAction(name) => write!(f, "undefined action: {name}"),
46            Self::TypeMismatch(msg) => write!(f, "type mismatch: {msg}"),
47            Self::StdlibError(msg) => write!(f, "stdlib error: {msg}"),
48            Self::UnknownFunction(msg) => write!(f, "unknown function: {msg}"),
49            Self::GasExhausted => write!(f, "gas exhausted"),
50            Self::Return(_) => write!(f, "return"),
51            Self::Runtime(msg) => write!(f, "runtime error: {msg}"),
52        }
53    }
54}
55
56impl std::error::Error for EvalError {}
57
58/// Result alias for evaluator operations.
59pub type EvalResult<T> = Result<T, EvalError>;