checkpoint in reconstruction

This commit is contained in:
2023-12-26 21:08:01 -08:00
parent 2c2268925a
commit e5db6640f2
21 changed files with 759 additions and 153 deletions

View File

@@ -36,14 +36,16 @@ pub enum PrimOpError<IR> {
ValuePrimitiveTypeError(#[from] ValuePrimitiveTypeError),
}
impl<IR1: Clone,IR2: Clone> PartialEq<PrimOpError<IR2>> for PrimOpError<IR1> {
impl<IR1: Clone, IR2: Clone> PartialEq<PrimOpError<IR2>> for PrimOpError<IR1> {
fn eq(&self, other: &PrimOpError<IR2>) -> bool {
match (self, other) {
(PrimOpError::MathFailure(a), PrimOpError::MathFailure(b)) => a == b,
(PrimOpError::TypeMismatch(a, b, c), PrimOpError::TypeMismatch(x, y, z)) => {
a == x && b.strip() == y.strip() && c.strip() == z.strip()
}
(PrimOpError::BadTypeFor(a, b), PrimOpError::BadTypeFor(x, y)) => a == x && b.strip() == y.strip(),
(PrimOpError::BadTypeFor(a, b), PrimOpError::BadTypeFor(x, y)) => {
a == x && b.strip() == y.strip()
}
(PrimOpError::BadArgCount(a, b), PrimOpError::BadArgCount(x, y)) => a == x && b == y,
(PrimOpError::UnknownPrimOp(a), PrimOpError::UnknownPrimOp(x)) => a == x,
(

View File

@@ -63,6 +63,7 @@ impl<'a, IR> TryFrom<&'a Value<IR>> for PrimitiveType {
impl From<ConstantType> for PrimitiveType {
fn from(value: ConstantType) -> Self {
match value {
ConstantType::Void => PrimitiveType::Void,
ConstantType::I8 => PrimitiveType::I8,
ConstantType::I16 => PrimitiveType::I16,
ConstantType::I32 => PrimitiveType::I32,
@@ -100,44 +101,43 @@ impl FromStr for PrimitiveType {
}
impl PrimitiveType {
/// Return the set of types that this type can be safely cast to
pub fn allowed_casts(&self) -> &'static [PrimitiveType] {
match self {
PrimitiveType::Void => &[PrimitiveType::Void],
PrimitiveType::U8 => &[
PrimitiveType::U8,
PrimitiveType::U16,
PrimitiveType::U32,
PrimitiveType::U64,
PrimitiveType::I16,
PrimitiveType::I32,
PrimitiveType::I64,
],
PrimitiveType::U16 => &[
PrimitiveType::U16,
PrimitiveType::U32,
PrimitiveType::U64,
PrimitiveType::I32,
PrimitiveType::I64,
],
PrimitiveType::U32 => &[PrimitiveType::U32, PrimitiveType::U64, PrimitiveType::I64],
PrimitiveType::U64 => &[PrimitiveType::U64],
PrimitiveType::I8 => &[
PrimitiveType::I8,
PrimitiveType::I16,
PrimitiveType::I32,
PrimitiveType::I64,
],
PrimitiveType::I16 => &[PrimitiveType::I16, PrimitiveType::I32, PrimitiveType::I64],
PrimitiveType::I32 => &[PrimitiveType::I32, PrimitiveType::I64],
PrimitiveType::I64 => &[PrimitiveType::I64],
}
}
/// Return true if this type can be safely cast into the target type.
pub fn can_cast_to(&self, target: &PrimitiveType) -> bool {
match self {
PrimitiveType::Void => matches!(target, PrimitiveType::Void),
PrimitiveType::U8 => matches!(
target,
PrimitiveType::U8
| PrimitiveType::U16
| PrimitiveType::U32
| PrimitiveType::U64
| PrimitiveType::I16
| PrimitiveType::I32
| PrimitiveType::I64
),
PrimitiveType::U16 => matches!(
target,
PrimitiveType::U16
| PrimitiveType::U32
| PrimitiveType::U64
| PrimitiveType::I32
| PrimitiveType::I64
),
PrimitiveType::U32 => matches!(
target,
PrimitiveType::U32 | PrimitiveType::U64 | PrimitiveType::I64
),
PrimitiveType::U64 => target == &PrimitiveType::U64,
PrimitiveType::I8 => matches!(
target,
PrimitiveType::I8 | PrimitiveType::I16 | PrimitiveType::I32 | PrimitiveType::I64
),
PrimitiveType::I16 => matches!(
target,
PrimitiveType::I16 | PrimitiveType::I32 | PrimitiveType::I64
),
PrimitiveType::I32 => matches!(target, PrimitiveType::I32 | PrimitiveType::I64),
PrimitiveType::I64 => target == &PrimitiveType::I64,
}
self.allowed_casts().contains(target)
}
/// Try to cast the given value to this type, returning the new value.
@@ -192,4 +192,16 @@ impl PrimitiveType {
PrimitiveType::I64 => Some(i64::MAX as u64),
}
}
pub fn valid_operators(&self) -> &'static [(&'static str, usize)] {
match self {
PrimitiveType::Void => &[],
PrimitiveType::U8 | PrimitiveType::U16 | PrimitiveType::U32 | PrimitiveType::U64 => {
&[("+", 2), ("-", 2), ("*", 2), ("/", 2)]
}
PrimitiveType::I8 | PrimitiveType::I16 | PrimitiveType::I32 | PrimitiveType::I64 => {
&[("+", 2), ("-", 1), ("-", 2), ("*", 2), ("/", 2)]
}
}
}
}

View File

@@ -44,9 +44,7 @@ impl<IR: Clone> Value<IR> {
Value::I32(x) => Value::I32(*x),
Value::I64(x) => Value::I64(*x),
Value::Closure(name, env, args, _) => {
let new_env = env
.clone()
.map_values(|x| x.strip());
let new_env = env.clone().map_values(|x| x.strip());
Value::Closure(name.clone(), new_env, args.clone(), ())
}
}