diff --git a/src/ast2/mod.rs b/src/ast2/mod.rs index c899aa4..e2f4bac 100644 --- a/src/ast2/mod.rs +++ b/src/ast2/mod.rs @@ -23,7 +23,7 @@ pub mod intern { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[repr(u8)] pub enum SimpleType { - F32, + F32 = 0, F64, Bool, Void, @@ -32,6 +32,21 @@ pub mod intern { ComptimeInt, } + impl From for SimpleType { + fn from(value: u8) -> Self { + match value { + 0 => Self::F32, + 1 => Self::F64, + 2 => Self::Bool, + 3 => Self::Void, + 4 => Self::USize, + 5 => Self::ISize, + 6 => Self::ComptimeInt, + _ => panic!("{value} is not a simple type"), + } + } + } + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Tag { String, @@ -271,7 +286,6 @@ pub mod intern { } } - #[derive(Debug)] pub struct InternPool { tags: Vec, indices: Vec, @@ -281,6 +295,32 @@ pub mod intern { hashed: BTreeMap, } + impl std::fmt::Debug for InternPool { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("InternPool") + .field_with("keys", |f| { + let mut list = f.debug_list(); + + let keys = (0..self.indices.len()) + .map(|i| Index(i as u32)) + .map(|idx| (idx, self.get_key(idx))); + for (idx, key) in keys { + list.entry_with(|f| write!(f, "{}: {key:?}", idx.0)); + } + + list.finish() + }) + .field_with("hashed", |f| { + let mut list = f.debug_list(); + for (hash, idx) in self.hashed.iter() { + list.entry_with(|f| write!(f, "{hash}: {}", idx.0)); + } + list.finish() + }) + .finish_non_exhaustive() + } + } + const STATIC_KEYS: [Key; 19] = [ Key::SimpleType { ty: SimpleType::Bool, @@ -694,7 +734,7 @@ pub mod intern { Key::SIntType { bits } } Tag::SimpleType => { - let ty = self.words[item.idx()] as u8; + let ty = item.idx() as u8; Key::SimpleType { ty: unsafe { core::mem::transmute::(ty) },