From 6130f73ca8d3383c98f21c97757c6681e74e4378 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 26 Jun 2024 14:21:55 -0400 Subject: [PATCH] Continue writing Display implementations --- dust-lang/src/abstract_tree/mod.rs | 4 +- .../src/abstract_tree/type_constructor.rs | 122 ++++++++++-------- dust-lang/src/value.rs | 4 +- 3 files changed, 69 insertions(+), 61 deletions(-) diff --git a/dust-lang/src/abstract_tree/mod.rs b/dust-lang/src/abstract_tree/mod.rs index bbbf878..aafbdfd 100644 --- a/dust-lang/src/abstract_tree/mod.rs +++ b/dust-lang/src/abstract_tree/mod.rs @@ -49,9 +49,7 @@ pub use self::{ statement::Statement, structure_definition::StructureDefinition, type_alias::TypeAlias, - type_constructor::{ - EnumTypeConstructor, FunctionTypeConstructor, ListTypeConstructor, TypeConstructor, - }, + type_constructor::{FunctionTypeConstructor, ListTypeConstructor, TypeConstructor}, value_node::ValueNode, }; diff --git a/dust-lang/src/abstract_tree/type_constructor.rs b/dust-lang/src/abstract_tree/type_constructor.rs index c72d826..8ec7f16 100644 --- a/dust-lang/src/abstract_tree/type_constructor.rs +++ b/dust-lang/src/abstract_tree/type_constructor.rs @@ -12,7 +12,6 @@ use super::{SourcePosition, Type, WithPosition}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub enum TypeConstructor { - Enum(WithPosition), Function(WithPosition), Invokation(TypeInvokationConstructor), List(WithPosition), @@ -21,20 +20,9 @@ pub enum TypeConstructor { Raw(WithPosition), } -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub enum RawTypeConstructor { - Any, - Boolean, - Float, - Integer, - Range, - String, -} - impl TypeConstructor { pub fn position(&self) -> SourcePosition { match self { - TypeConstructor::Enum(WithPosition { position, .. }) => *position, TypeConstructor::Function(WithPosition { position, .. }) => *position, TypeConstructor::Invokation(TypeInvokationConstructor { identifier, @@ -89,44 +77,6 @@ impl TypeConstructor { invoked_type } } - TypeConstructor::Enum(enum_type_constructor) => { - let EnumTypeConstructor { - name, - type_parameters, - variants, - } = &enum_type_constructor.node; - let mut type_variants = Vec::with_capacity(variants.len()); - - for (variant_name, constructors) in variants { - if let Some(constructors) = constructors { - let mut types = Vec::with_capacity(constructors.len()); - - for constructor in constructors { - let r#type = constructor.construct(context)?; - - types.push(r#type); - } - - type_variants.push((variant_name.node.clone(), Some(types))); - } else { - type_variants.push((variant_name.node.clone(), None)) - } - } - - Type::Enum { - name: name.node.clone(), - type_parameters: type_parameters.as_ref().map(|identifiers| { - identifiers - .into_iter() - .map(|identifier| Type::Generic { - identifier: identifier.node.clone(), - concrete_type: None, - }) - .collect() - }), - variants: type_variants, - } - } TypeConstructor::Function(function_type_constructor) => { let FunctionTypeConstructor { type_parameters: declared_type_parameters, @@ -207,16 +157,42 @@ impl TypeConstructor { } impl Display for TypeConstructor { - fn fmt(&self, _: &mut Formatter) -> fmt::Result { - todo!() + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + TypeConstructor::Function(WithPosition { node, position }) => write!(f, "{node}")?, + TypeConstructor::Invokation(type_invokation) => write!(f, "{type_invokation}")?, + TypeConstructor::List(WithPosition { node, position }) => write!(f, "{node}")?, + TypeConstructor::ListOf(WithPosition { node, position }) => write!(f, "{node}")?, + TypeConstructor::Map(WithPosition { node, position }) => { + write!(f, "{{ ")?; + + for (identifier, constructor) in node { + write!(f, "{}: {constructor}, ", identifier.node)?; + } + + write!(f, "}}")?; + } + TypeConstructor::Raw(WithPosition { node, position }) => write!(f, "{node}")?, + } + + Ok(()) } } #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub struct EnumTypeConstructor { - pub name: WithPosition, - pub type_parameters: Option>>, - pub variants: Vec<(WithPosition, Option>)>, +pub enum RawTypeConstructor { + Any, + Boolean, + Float, + Integer, + Range, + String, +} + +impl Display for RawTypeConstructor { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + todo!() + } } #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] @@ -226,14 +202,48 @@ pub struct FunctionTypeConstructor { pub return_type: Option>, } +impl Display for FunctionTypeConstructor { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + todo!() + } +} + #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub struct ListTypeConstructor { pub length: usize, pub item_type: Box, } +impl Display for ListTypeConstructor { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + todo!() + } +} + #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub struct TypeInvokationConstructor { pub identifier: WithPosition, pub type_arguments: Option>, } +impl Display for TypeInvokationConstructor { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let TypeInvokationConstructor { + identifier, + type_arguments, + } = self; + + write!(f, "{}", identifier.node)?; + + if let Some(arguments) = type_arguments { + write!(f, "(")?; + + for constructor in arguments { + write!(f, "{constructor}, ")?; + } + + write!(f, ")")?; + } + + Ok(()) + } +} diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index ed410df..db2c2ff 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -169,7 +169,7 @@ impl Display for Value { write!(f, "{{ ")?; for (index, (key, value)) in map.into_iter().enumerate() { - write!(f, "{key} = {value:?}")?; + write!(f, "{key} = {value}")?; if index != map.len() - 1 { write!(f, ", ")?; @@ -217,7 +217,7 @@ impl Display for Value { write!(f, "-> {return_type}")? } - write!(f, " {{ {body:?} }}") + write!(f, " {{ {body} }}") } ValueInner::Structure { name, fields } => { write!(f, "{}\n{{", name.node)?;