🧪 Add evaluation tests to ensure that passes retain NGR semantics. (#2)
This change adds `Arbitrary` instances to the key IR data types (both as syntax and as native IR), as well as evaluator functions for both. This way, we can ensure that the evaluation of one version of the IR is observationally equivalent to another version of the IR, or even a later IR. It also adds a similar ability through both static file compilation and the JIT, to ensure that the translation through Cranelift and our runtime works as expected. This actually found a couple issues in its creation, and I hope is helpful extensions into more interesting programs.
This commit was merged in pull request #2.
This commit is contained in:
61
src/eval.rs
Normal file
61
src/eval.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
mod env;
|
||||
mod primop;
|
||||
mod value;
|
||||
|
||||
use cranelift_module::ModuleError;
|
||||
pub use env::{EvalEnvironment, LookupError};
|
||||
pub use primop::PrimOpError;
|
||||
pub use value::Value;
|
||||
|
||||
use crate::backend::BackendError;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum EvalError {
|
||||
#[error(transparent)]
|
||||
Lookup(#[from] LookupError),
|
||||
#[error(transparent)]
|
||||
PrimOp(#[from] PrimOpError),
|
||||
#[error(transparent)]
|
||||
Backend(#[from] BackendError),
|
||||
#[error("IO error: {0}")]
|
||||
IO(String),
|
||||
#[error(transparent)]
|
||||
Module(#[from] ModuleError),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for EvalError {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
EvalError::IO(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for EvalError {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match self {
|
||||
EvalError::Lookup(a) => match other {
|
||||
EvalError::Lookup(b) => a == b,
|
||||
_ => false,
|
||||
},
|
||||
|
||||
EvalError::PrimOp(a) => match other {
|
||||
EvalError::PrimOp(b) => a == b,
|
||||
_ => false,
|
||||
},
|
||||
|
||||
EvalError::Backend(a) => match other {
|
||||
EvalError::Backend(b) => a == b,
|
||||
_ => false,
|
||||
},
|
||||
|
||||
EvalError::IO(a) => match other {
|
||||
EvalError::IO(b) => a == b,
|
||||
_ => false,
|
||||
},
|
||||
|
||||
EvalError::Module(a) => match other {
|
||||
EvalError::Module(b) => a.to_string() == b.to_string(),
|
||||
_ => false,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user