use super::Symbol;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Type {
Integer(IntegerSize),
Unsigned(IntegerSize),
Float(u16),
Bool,
Symbol(Symbol),
Array(Box<Type>, usize),
Struct {
fields: Vec<(Option<String>, Type)>,
},
Ptr(Box<Type>, bool),
FnPtr {
params: Vec<Type>,
return_type: Box<Type>,
},
Error,
}
impl Type {
pub fn hashable_name(&self) -> String {
let fmt_size = |size| match size {
IntegerSize::Bits(bits) => bits.to_string(),
IntegerSize::Size => "size".to_owned(),
};
match self {
Type::Integer(size) => format!("i{}", fmt_size(*size)),
Type::Unsigned(size) => format!("u{}", fmt_size(*size)),
Type::Float(size) => format!("f{size}"),
Type::Bool => "bool".to_owned(),
Type::Symbol(sym) => sym.to_string(),
Type::Array(ty, len) => format!("{}[{len}]", ty.hashable_name()),
Type::Struct { fields } => fields
.iter()
.map(|(_, ty)| ty.hashable_name())
.collect::<Vec<_>>()
.join(" "),
Type::Ptr(ty, mutable) => {
format!(
"*{} {}",
match mutable {
true => "mut",
false => "const",
},
ty.hashable_name()
)
}
Type::FnPtr {
params,
return_type,
} => format!(
"({}) -> {}",
params
.iter()
.map(Type::hashable_name)
.collect::<Vec<_>>()
.join(", "),
return_type.hashable_name()
),
Type::Error => "<error>".to_owned(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IntegerSize {
Bits(u16),
Size,
}