Keep pushing forward on type inference.

This commit is contained in:
2023-07-21 21:34:35 -07:00
parent 1ad3d6c517
commit 9fb6bf3b86
11 changed files with 577 additions and 275 deletions

View File

@@ -4,7 +4,7 @@ use crate::{
};
use std::{fmt::Display, str::FromStr};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PrimitiveType {
U8,
U16,
@@ -83,56 +83,39 @@ impl PrimitiveType {
/// 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::U8 => match target {
PrimitiveType::U8 => true,
PrimitiveType::U16 => true,
PrimitiveType::U32 => true,
PrimitiveType::U64 => true,
PrimitiveType::I16 => true,
PrimitiveType::I32 => true,
PrimitiveType::I64 => true,
_ => false,
},
PrimitiveType::U16 => match target {
PrimitiveType::U16 => true,
PrimitiveType::U32 => true,
PrimitiveType::U64 => true,
PrimitiveType::I32 => true,
PrimitiveType::I64 => true,
_ => false,
},
PrimitiveType::U32 => match target {
PrimitiveType::U32 => true,
PrimitiveType::U64 => true,
PrimitiveType::I64 => true,
_ => false,
},
PrimitiveType::U64 => match target {
PrimitiveType::U64 => true,
_ => false,
},
PrimitiveType::I8 => match target {
PrimitiveType::I8 => true,
PrimitiveType::I16 => true,
PrimitiveType::I32 => true,
PrimitiveType::I64 => true,
_ => false,
},
PrimitiveType::I16 => match target {
PrimitiveType::I16 => true,
PrimitiveType::I32 => true,
PrimitiveType::I64 => true,
_ => false,
},
PrimitiveType::I32 => match target {
PrimitiveType::I32 => true,
PrimitiveType::I64 => true,
_ => false,
},
PrimitiveType::I64 => match target {
PrimitiveType::I64 => true,
_ => false,
},
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,
}
}
@@ -174,4 +157,17 @@ impl PrimitiveType {
}),
}
}
pub fn max_value(&self) -> u64 {
match self {
PrimitiveType::U8 => u8::MAX as u64,
PrimitiveType::U16 => u16::MAX as u64,
PrimitiveType::U32 => u32::MAX as u64,
PrimitiveType::U64 => u64::MAX,
PrimitiveType::I8 => i8::MAX as u64,
PrimitiveType::I16 => i16::MAX as u64,
PrimitiveType::I32 => i32::MAX as u64,
PrimitiveType::I64 => i64::MAX as u64,
}
}
}