From 9a2e4f36491c6ae40f8bc220ae6f1b205f37d88a Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 26 Jun 2024 14:44:23 -0400 Subject: [PATCH] Add more Display implementations --- dust-lang/src/abstract_tree/type.rs | 10 ++-- .../src/abstract_tree/type_constructor.rs | 47 +++++++++++++++---- dust-lang/src/abstract_tree/value_node.rs | 12 +++-- dust-lang/src/value.rs | 4 +- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/dust-lang/src/abstract_tree/type.rs b/dust-lang/src/abstract_tree/type.rs index d05af8e..c963a68 100644 --- a/dust-lang/src/abstract_tree/type.rs +++ b/dust-lang/src/abstract_tree/type.rs @@ -264,16 +264,20 @@ impl Display for Type { value_parameters, return_type, } => { - write!(f, "fn (")?; + write!(f, "fn ")?; if let Some(type_parameters) = type_parameters { + write!(f, "<")?; + for identifier in type_parameters { - write!(f, "{} ", identifier)?; + write!(f, "{}, ", identifier)?; } - write!(f, ")(")?; + write!(f, ">")?; } + write!(f, "(")?; + if let Some(value_parameters) = value_parameters { for (identifier, r#type) in value_parameters { write!(f, "{identifier}: {type}")?; diff --git a/dust-lang/src/abstract_tree/type_constructor.rs b/dust-lang/src/abstract_tree/type_constructor.rs index 8ec7f16..1f31e2e 100644 --- a/dust-lang/src/abstract_tree/type_constructor.rs +++ b/dust-lang/src/abstract_tree/type_constructor.rs @@ -159,11 +159,11 @@ impl TypeConstructor { impl Display for TypeConstructor { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { - TypeConstructor::Function(WithPosition { node, position }) => write!(f, "{node}")?, + TypeConstructor::Function(WithPosition { node, .. }) => 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 }) => { + TypeConstructor::List(WithPosition { node, .. }) => write!(f, "{node}")?, + TypeConstructor::ListOf(WithPosition { node, .. }) => write!(f, "{node}")?, + TypeConstructor::Map(WithPosition { node, .. }) => { write!(f, "{{ ")?; for (identifier, constructor) in node { @@ -172,7 +172,7 @@ impl Display for TypeConstructor { write!(f, "}}")?; } - TypeConstructor::Raw(WithPosition { node, position }) => write!(f, "{node}")?, + TypeConstructor::Raw(WithPosition { node, .. }) => write!(f, "{node}")?, } Ok(()) @@ -191,7 +191,14 @@ pub enum RawTypeConstructor { impl Display for RawTypeConstructor { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - todo!() + match self { + RawTypeConstructor::Any => write!(f, "any"), + RawTypeConstructor::Boolean => write!(f, "bool"), + RawTypeConstructor::Float => write!(f, "float"), + RawTypeConstructor::Integer => write!(f, "int"), + RawTypeConstructor::Range => write!(f, "range"), + RawTypeConstructor::String => write!(f, "str"), + } } } @@ -204,7 +211,29 @@ pub struct FunctionTypeConstructor { impl Display for FunctionTypeConstructor { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - todo!() + write!(f, "fn ")?; + + if let Some(parameters) = &self.type_parameters { + write!(f, "<")?; + + for identifier in parameters { + write!(f, "{}, ", identifier.node)?; + } + + write!(f, ">")?; + } + + if let Some(parameters) = &self.value_parameters { + for (identifier, constructor) in parameters { + write!(f, "{}: {constructor}", identifier.node)?; + } + } + + write!(f, "(")?; + + write!(f, ")")?; + + Ok(()) } } @@ -216,7 +245,9 @@ pub struct ListTypeConstructor { impl Display for ListTypeConstructor { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - todo!() + let ListTypeConstructor { length, item_type } = self; + + write!(f, "[{item_type}; {length}]") } } diff --git a/dust-lang/src/abstract_tree/value_node.rs b/dust-lang/src/abstract_tree/value_node.rs index 0d00591..74223be 100644 --- a/dust-lang/src/abstract_tree/value_node.rs +++ b/dust-lang/src/abstract_tree/value_node.rs @@ -687,18 +687,18 @@ impl Display for ValueNode { value_parameters, return_type, body, - context_template, + context_template: _, }) => { write!(f, "fn ")?; if let Some(type_parameters) = type_parameters { - write!(f, "(")?; + write!(f, "<")?; for identifier in type_parameters { write!(f, "{identifier}")?; } - write!(f, ")")?; + write!(f, ">")?; } if let Some(value_parameters) = value_parameters { @@ -711,7 +711,11 @@ impl Display for ValueNode { write!(f, ")")?; } - write!(f, "{}", body.node) + if let Some(r#type) = return_type { + write!(f, " -> {type}")?; + } + + write!(f, " {}", body.node) } } } diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index db2c2ff..459d9cd 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -190,7 +190,7 @@ impl Display for Value { write!(f, "fn ")?; if let Some(type_parameters) = type_parameters { - write!(f, "(")?; + write!(f, "<")?; for (index, identifier) in type_parameters.into_iter().enumerate() { if index == type_parameters.len() - 1 { @@ -200,7 +200,7 @@ impl Display for Value { } } - write!(f, ")")?; + write!(f, ">")?; } write!(f, "(")?;