better debug impls for types, fix for simpletype key

This commit is contained in:
Janis 2024-09-13 19:36:13 +02:00
parent 5c38969ef8
commit 4432cf198e

View file

@ -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<u8> 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<Tag>,
indices: Vec<u32>,
@ -281,6 +295,32 @@ pub mod intern {
hashed: BTreeMap<u64, Index>,
}
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::<u8, SimpleType>(ty) },