Checkpoint

This commit is contained in:
2023-10-14 16:39:16 -07:00
parent 87d027bf8d
commit 71228b9e09
18 changed files with 402 additions and 78 deletions

View File

@@ -1,6 +1,8 @@
use crate::eval::primtype::PrimitiveType;
use crate::eval::value::Value;
use super::primtype::{UnknownPrimType, ValuePrimitiveTypeError};
/// Errors that can occur running primitive operations in the evaluators.
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
pub enum PrimOpError {
@@ -14,7 +16,7 @@ pub enum PrimOpError {
/// This variant covers when an operator must take a particular
/// type, but the user has provided a different one.
#[error("Bad type for operator {0}: {1}")]
BadTypeFor(&'static str, Value),
BadTypeFor(String, Value),
/// Probably obvious from the name, but just to be very clear: this
/// happens when you pass three arguments to a two argument operator,
/// etc. Technically that's a type error of some sort, but we split
@@ -28,8 +30,10 @@ pub enum PrimOpError {
from: PrimitiveType,
to: PrimitiveType,
},
#[error("Unknown primitive type {0}")]
UnknownPrimType(String),
#[error(transparent)]
UnknownPrimType(#[from] UnknownPrimType),
#[error(transparent)]
ValuePrimitiveTypeError(#[from] ValuePrimitiveTypeError),
}
// Implementing primitives in an interpreter like this is *super* tedious,
@@ -63,7 +67,7 @@ impl Value {
Value::I16(x) => Ok(Value::I16(x.wrapping_neg())),
Value::I32(x) => Ok(Value::I32(x.wrapping_neg())),
Value::I64(x) => Ok(Value::I64(x.wrapping_neg())),
_ => Err(PrimOpError::BadTypeFor("-", value.clone())),
_ => Err(PrimOpError::BadTypeFor("-".to_string(), value.clone())),
},
_ => Err(PrimOpError::BadArgCount(operation.to_owned(), 1)),
}
@@ -135,6 +139,9 @@ impl Value {
right.clone(),
)),
},
Value::Function(_, _) => {
Err(PrimOpError::BadTypeFor(operation.to_string(), left.clone()))
}
}
}