From abd9c712b8f3fea02726a74a2daf5c7463aa790f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 28 Apr 2024 09:54:36 +0200 Subject: rebel-lang: move code around to make following diff more readable --- crates/rebel-lang/src/typing.rs | 174 ++++++++++++++++---------------- crates/rebel-lang/src/value.rs | 216 ++++++++++++++++++++-------------------- 2 files changed, 195 insertions(+), 195 deletions(-) (limited to 'crates') 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 { - 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 { - 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, @@ -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 { + 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 { + 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(()), + } + } +} diff --git a/crates/rebel-lang/src/value.rs b/crates/rebel-lang/src/value.rs index c07f62c..735ae63 100644 --- a/crates/rebel-lang/src/value.rs +++ b/crates/rebel-lang/src/value.rs @@ -63,114 +63,6 @@ impl Value { } } -impl Display for Value { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Value::Unit => f.write_str("()"), - Value::Boolean(value) => value.fmt(f), - Value::Integer(value) => value.fmt(f), - Value::Str(value) => write!(f, "{value:?}"), - Value::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(")") - } - Value::Array(elems) => { - let mut first = true; - f.write_str("[")?; - for elem in elems { - if !first { - f.write_str(", ")?; - } - first = false; - elem.fmt(f)?; - } - f.write_str("]") - } - Value::Map(entries) => { - let mut first = true; - f.write_str("{")?; - for (key, value) in entries { - if !first { - f.write_str(", ")?; - } - first = false; - write!(f, "{key} = {value}")?; - } - f.write_str("}") - } - Value::Fn(func) => func.typ.fmt(f), - } - } -} - -#[derive(Debug)] -struct Stringify<'a>(&'a Value); - -impl<'a> Display for Stringify<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self.0 { - Value::Boolean(value) => value.fmt(f), - Value::Integer(value) => value.fmt(f), - Value::Str(value) => value.fmt(f), - _ => Err(std::fmt::Error), - } - } -} - -#[derive(Debug)] -struct ScriptStringify<'a>(&'a Value); - -impl<'a> ScriptStringify<'a> { - fn fmt_list(elems: &'a [Value], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut first = true; - for elem in elems { - if !first { - f.write_char(' ')?; - } - ScriptStringify(elem).fmt(f)?; - first = false; - } - Ok(()) - } -} - -impl<'a> Display for ScriptStringify<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self.0 { - Value::Boolean(value) => { - value.fmt(f)?; - } - Value::Integer(value) => { - value.fmt(f)?; - } - Value::Str(value) => { - f.write_char('\'')?; - f.write_str(&value.replace('\'', "'\\''"))?; - f.write_char('\'')?; - } - Value::Array(elems) => { - Self::fmt_list(elems, f)?; - } - Value::Tuple(elems) => { - Self::fmt_list(elems, f)?; - } - _ => return Err(std::fmt::Error), - }; - Ok(()) - } -} - #[derive(Debug, Default)] pub struct Context { pub values: Module, @@ -394,6 +286,114 @@ impl Context { } } +impl Display for Value { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Value::Unit => f.write_str("()"), + Value::Boolean(value) => value.fmt(f), + Value::Integer(value) => value.fmt(f), + Value::Str(value) => write!(f, "{value:?}"), + Value::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(")") + } + Value::Array(elems) => { + let mut first = true; + f.write_str("[")?; + for elem in elems { + if !first { + f.write_str(", ")?; + } + first = false; + elem.fmt(f)?; + } + f.write_str("]") + } + Value::Map(entries) => { + let mut first = true; + f.write_str("{")?; + for (key, value) in entries { + if !first { + f.write_str(", ")?; + } + first = false; + write!(f, "{key} = {value}")?; + } + f.write_str("}") + } + Value::Fn(func) => func.typ.fmt(f), + } + } +} + +#[derive(Debug)] +struct Stringify<'a>(&'a Value); + +impl<'a> Display for Stringify<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.0 { + Value::Boolean(value) => value.fmt(f), + Value::Integer(value) => value.fmt(f), + Value::Str(value) => value.fmt(f), + _ => Err(std::fmt::Error), + } + } +} + +#[derive(Debug)] +struct ScriptStringify<'a>(&'a Value); + +impl<'a> ScriptStringify<'a> { + fn fmt_list(elems: &'a [Value], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut first = true; + for elem in elems { + if !first { + f.write_char(' ')?; + } + ScriptStringify(elem).fmt(f)?; + first = false; + } + Ok(()) + } +} + +impl<'a> Display for ScriptStringify<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.0 { + Value::Boolean(value) => { + value.fmt(f)?; + } + Value::Integer(value) => { + value.fmt(f)?; + } + Value::Str(value) => { + f.write_char('\'')?; + f.write_str(&value.replace('\'', "'\\''"))?; + f.write_char('\'')?; + } + Value::Array(elems) => { + Self::fmt_list(elems, f)?; + } + Value::Tuple(elems) => { + Self::fmt_list(elems, f)?; + } + _ => return Err(std::fmt::Error), + }; + Ok(()) + } +} + #[derive(Debug)] struct StrDisplay<'a> { pieces: &'a [ast::StrPiece<'a>], -- cgit v1.2.3