diff options
Diffstat (limited to 'crates/rebel-lang/src/typing.rs')
-rw-r--r-- | crates/rebel-lang/src/typing.rs | 174 |
1 files changed, 87 insertions, 87 deletions
diff --git a/crates/rebel-lang/src/typing.rs b/crates/rebel-lang/src/typing.rs index fd9e2af..2545746 100644 --- a/crates/rebel-lang/src/typing.rs +++ b/crates/rebel-lang/src/typing.rs @@ -79,93 +79,6 @@ impl Type { } } -impl Display for Type { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Type::Free => write!(f, "_"), - Type::Unit => write!(f, "()"), - Type::Bool => write!(f, "bool"), - Type::Int => write!(f, "int"), - Type::Str => write!(f, "str"), - Type::Tuple(elems) => { - let mut first = true; - f.write_str("(")?; - if elems.is_empty() { - f.write_str(",")?; - } - for elem in elems { - if !first { - f.write_str(", ")?; - } - first = false; - elem.fmt(f)?; - } - f.write_str(")") - } - Type::Array(typ, len) => write!(f, "[{typ}{len}]"), - Type::Map(entries) => { - let mut first = true; - f.write_str("{")?; - for (key, typ) in entries { - if !first { - f.write_str(", ")?; - } - first = false; - write!(f, "{key}: {typ}")?; - } - f.write_str("}") - } - /* TODO */ - Type::Fn(func) => write!(f, "{func}"), - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub enum ArrayLen { - Free, - Fixed(usize), - Dynamic, -} - -impl ArrayLen { - fn unify(self, other: Self, coerce: Coerce) -> Result<Self> { - use ArrayLen::*; - - Ok(match (self, other, coerce) { - (Free, len, _) => len, - (len, Free, _) => len, - (l1, l2, _) if l1 == l2 => l1, - (_, _, Coerce::Common) => Dynamic, - (Dynamic, Fixed(_), Coerce::Dynamic | Coerce::Assign) => Dynamic, - (Fixed(_), Dynamic, Coerce::Dynamic) => Dynamic, - _ => return Err(TypeError), - }) - } - - fn add(self, other: Self) -> Result<Self> { - use ArrayLen::*; - - Ok(match (self, other) { - (Free, _) => return Err(TypeError), - (_, Free) => return Err(TypeError), - (Dynamic, _) => Dynamic, - (_, Dynamic) => Dynamic, - (Fixed(l1), Fixed(l2)) => Fixed(l1 + l2), - }) - } -} - -impl Display for ArrayLen { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - ArrayLen::Free => write!(f, "; _"), - ArrayLen::Fixed(len) => write!(f, "; {len}"), - ArrayLen::Dynamic => Ok(()), - } - } -} - #[derive(Debug, Default)] pub struct Context { pub values: Module<Type>, @@ -373,3 +286,90 @@ impl Context { }) } } + +impl Display for Type { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Type::Free => write!(f, "_"), + Type::Unit => write!(f, "()"), + Type::Bool => write!(f, "bool"), + Type::Int => write!(f, "int"), + Type::Str => write!(f, "str"), + Type::Tuple(elems) => { + let mut first = true; + f.write_str("(")?; + if elems.is_empty() { + f.write_str(",")?; + } + for elem in elems { + if !first { + f.write_str(", ")?; + } + first = false; + elem.fmt(f)?; + } + f.write_str(")") + } + Type::Array(typ, len) => write!(f, "[{typ}{len}]"), + Type::Map(entries) => { + let mut first = true; + f.write_str("{")?; + for (key, typ) in entries { + if !first { + f.write_str(", ")?; + } + first = false; + write!(f, "{key}: {typ}")?; + } + f.write_str("}") + } + /* TODO */ + Type::Fn(func) => write!(f, "{func}"), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum ArrayLen { + Free, + Fixed(usize), + Dynamic, +} + +impl ArrayLen { + fn unify(self, other: Self, coerce: Coerce) -> Result<Self> { + use ArrayLen::*; + + Ok(match (self, other, coerce) { + (Free, len, _) => len, + (len, Free, _) => len, + (l1, l2, _) if l1 == l2 => l1, + (_, _, Coerce::Common) => Dynamic, + (Dynamic, Fixed(_), Coerce::Dynamic | Coerce::Assign) => Dynamic, + (Fixed(_), Dynamic, Coerce::Dynamic) => Dynamic, + _ => return Err(TypeError), + }) + } + + fn add(self, other: Self) -> Result<Self> { + use ArrayLen::*; + + Ok(match (self, other) { + (Free, _) => return Err(TypeError), + (_, Free) => return Err(TypeError), + (Dynamic, _) => Dynamic, + (_, Dynamic) => Dynamic, + (Fixed(l1), Fixed(l2)) => Fixed(l1 + l2), + }) + } +} + +impl Display for ArrayLen { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ArrayLen::Free => write!(f, "; _"), + ArrayLen::Fixed(len) => write!(f, "; {len}"), + ArrayLen::Dynamic => Ok(()), + } + } +} |