🤔 Add a type inference engine, along with typed literals. (#4)
The typed literal formatting mirrors that of Rust. If no type can be inferred for an untagged literal, the type inference engine will warn the user and then assume that they meant an unsigned 64-bit number. (This is slightly inconvenient, because there can be cases in which our Arbitrary instance may generate a unary negation, in which we should assume that it's a signed 64-bit number; we may want to revisit this later.) The type inference engine is a standard two phase one, in which we first generate a series of type constraints, and then we solve those constraints. In this particular implementation, we actually use a third phase to generate a final AST. Finally, to increase the amount of testing performed, I've removed the overflow checking in the evaluator. The only thing we now check for is division by zero. This does make things a trace slower in testing, but hopefully we get more coverage this way.
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::eval::primtype::PrimitiveType;
|
||||
use crate::eval::value::Value;
|
||||
|
||||
/// Errors that can occur running primitive operations in the evaluators.
|
||||
@@ -22,6 +23,13 @@ pub enum PrimOpError {
|
||||
BadArgCount(String, usize),
|
||||
#[error("Unknown primitive operation {0}")]
|
||||
UnknownPrimOp(String),
|
||||
#[error("Unsafe cast from {from} to {to}")]
|
||||
UnsafeCast {
|
||||
from: PrimitiveType,
|
||||
to: PrimitiveType,
|
||||
},
|
||||
#[error("Unknown primitive type {0}")]
|
||||
UnknownPrimType(String),
|
||||
}
|
||||
|
||||
// Implementing primitives in an interpreter like this is *super* tedious,
|
||||
@@ -37,39 +45,95 @@ pub enum PrimOpError {
|
||||
macro_rules! run_op {
|
||||
($op: ident, $left: expr, $right: expr) => {
|
||||
match $op {
|
||||
"+" => $left
|
||||
.checked_add($right)
|
||||
.ok_or(PrimOpError::MathFailure("+"))
|
||||
.map(Into::into),
|
||||
"-" => $left
|
||||
.checked_sub($right)
|
||||
.ok_or(PrimOpError::MathFailure("-"))
|
||||
.map(Into::into),
|
||||
"*" => $left
|
||||
.checked_mul($right)
|
||||
.ok_or(PrimOpError::MathFailure("*"))
|
||||
.map(Into::into),
|
||||
"/" => $left
|
||||
.checked_div($right)
|
||||
.ok_or(PrimOpError::MathFailure("/"))
|
||||
.map(Into::into),
|
||||
"+" => Ok($left.wrapping_add($right).into()),
|
||||
"-" => Ok($left.wrapping_sub($right).into()),
|
||||
"*" => Ok($left.wrapping_mul($right).into()),
|
||||
"/" if $right == 0 => Err(PrimOpError::MathFailure("/")),
|
||||
"/" => Ok($left.wrapping_div($right).into()),
|
||||
_ => Err(PrimOpError::UnknownPrimOp($op.to_string())),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl Value {
|
||||
fn unary_op(operation: &str, value: &Value) -> Result<Value, PrimOpError> {
|
||||
match operation {
|
||||
"-" => match value {
|
||||
Value::I8(x) => Ok(Value::I8(x.wrapping_neg())),
|
||||
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::BadArgCount(operation.to_owned(), 1)),
|
||||
}
|
||||
}
|
||||
|
||||
fn binary_op(operation: &str, left: &Value, right: &Value) -> Result<Value, PrimOpError> {
|
||||
match left {
|
||||
// for now we only have one type, but in the future this is
|
||||
// going to be very irritating.
|
||||
Value::I8(x) => match right {
|
||||
Value::I8(y) => run_op!(operation, x, *y),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
Value::I16(x) => match right {
|
||||
Value::I16(y) => run_op!(operation, x, *y),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
Value::I32(x) => match right {
|
||||
Value::I32(y) => run_op!(operation, x, *y),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
Value::I64(x) => match right {
|
||||
Value::I64(y) => run_op!(operation, x, *y),
|
||||
// _ => Err(PrimOpError::TypeMismatch(
|
||||
// operation.to_string(),
|
||||
// left.clone(),
|
||||
// right.clone(),
|
||||
// )),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
Value::U8(x) => match right {
|
||||
Value::U8(y) => run_op!(operation, x, *y),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
Value::U16(x) => match right {
|
||||
Value::U16(y) => run_op!(operation, x, *y),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
Value::U32(x) => match right {
|
||||
Value::U32(y) => run_op!(operation, x, *y),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
Value::U64(x) => match right {
|
||||
Value::U64(y) => run_op!(operation, x, *y),
|
||||
_ => Err(PrimOpError::TypeMismatch(
|
||||
operation.to_string(),
|
||||
left.clone(),
|
||||
right.clone(),
|
||||
)),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -83,13 +147,10 @@ impl Value {
|
||||
/// its worth being careful to make sure that your inputs won't cause either
|
||||
/// condition.
|
||||
pub fn calculate(operation: &str, values: Vec<Value>) -> Result<Value, PrimOpError> {
|
||||
if values.len() == 2 {
|
||||
Value::binary_op(operation, &values[0], &values[1])
|
||||
} else {
|
||||
Err(PrimOpError::BadArgCount(
|
||||
operation.to_string(),
|
||||
values.len(),
|
||||
))
|
||||
match values.len() {
|
||||
1 => Value::unary_op(operation, &values[0]),
|
||||
2 => Value::binary_op(operation, &values[0], &values[1]),
|
||||
x => Err(PrimOpError::BadArgCount(operation.to_string(), x)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user