Amo/src/ir/types.rs

71 lines
1.2 KiB
Rust

use core::fmt;
use super::Identifier;
#[derive(Clone, Debug)]
pub struct Variant {
pub name: Identifier,
pub elements: Vec<Type>,
}
impl fmt::Display for Variant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"{} {}",
self.name,
self.elements.iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join(" "),
)
}
}
#[derive(Clone, Debug)]
pub enum Type {
UserDefined(Vec<Variant>),
Function(Box<Type>, Box<Type>),
Primitive(PrimitiveType),
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UserDefined(variants) =>
f.write_str(
variants.into_iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(" | ")
.as_str()
),
Self::Function(args, return_type) =>
write!(f,
"{} -> {}",
args,
return_type
),
Self::Primitive(p) => p.fmt(f),
}
}
}
#[derive(Clone, Debug, Copy)]
pub enum PrimitiveType {
Int,
Str,
Type,
}
impl fmt::Display for PrimitiveType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(
match self {
Self::Int => "int",
Self::Str => "str",
Self::Type => "Type",
}
)
}
}