1
0

Continue writing Display implementations

This commit is contained in:
Jeff 2024-06-26 14:21:55 -04:00
parent 822f12b44d
commit 6130f73ca8
3 changed files with 69 additions and 61 deletions

View File

@ -49,9 +49,7 @@ pub use self::{
statement::Statement, statement::Statement,
structure_definition::StructureDefinition, structure_definition::StructureDefinition,
type_alias::TypeAlias, type_alias::TypeAlias,
type_constructor::{ type_constructor::{FunctionTypeConstructor, ListTypeConstructor, TypeConstructor},
EnumTypeConstructor, FunctionTypeConstructor, ListTypeConstructor, TypeConstructor,
},
value_node::ValueNode, value_node::ValueNode,
}; };

View File

@ -12,7 +12,6 @@ use super::{SourcePosition, Type, WithPosition};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum TypeConstructor { pub enum TypeConstructor {
Enum(WithPosition<EnumTypeConstructor>),
Function(WithPosition<FunctionTypeConstructor>), Function(WithPosition<FunctionTypeConstructor>),
Invokation(TypeInvokationConstructor), Invokation(TypeInvokationConstructor),
List(WithPosition<ListTypeConstructor>), List(WithPosition<ListTypeConstructor>),
@ -21,20 +20,9 @@ pub enum TypeConstructor {
Raw(WithPosition<RawTypeConstructor>), Raw(WithPosition<RawTypeConstructor>),
} }
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum RawTypeConstructor {
Any,
Boolean,
Float,
Integer,
Range,
String,
}
impl TypeConstructor { impl TypeConstructor {
pub fn position(&self) -> SourcePosition { pub fn position(&self) -> SourcePosition {
match self { match self {
TypeConstructor::Enum(WithPosition { position, .. }) => *position,
TypeConstructor::Function(WithPosition { position, .. }) => *position, TypeConstructor::Function(WithPosition { position, .. }) => *position,
TypeConstructor::Invokation(TypeInvokationConstructor { TypeConstructor::Invokation(TypeInvokationConstructor {
identifier, identifier,
@ -89,44 +77,6 @@ impl TypeConstructor {
invoked_type 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) => { TypeConstructor::Function(function_type_constructor) => {
let FunctionTypeConstructor { let FunctionTypeConstructor {
type_parameters: declared_type_parameters, type_parameters: declared_type_parameters,
@ -207,16 +157,42 @@ impl TypeConstructor {
} }
impl Display for TypeConstructor { impl Display for TypeConstructor {
fn fmt(&self, _: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
todo!() 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)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EnumTypeConstructor { pub enum RawTypeConstructor {
pub name: WithPosition<Identifier>, Any,
pub type_parameters: Option<Vec<WithPosition<Identifier>>>, Boolean,
pub variants: Vec<(WithPosition<Identifier>, Option<Vec<TypeConstructor>>)>, 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)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
@ -226,14 +202,48 @@ pub struct FunctionTypeConstructor {
pub return_type: Option<Box<TypeConstructor>>, pub return_type: Option<Box<TypeConstructor>>,
} }
impl Display for FunctionTypeConstructor {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
todo!()
}
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ListTypeConstructor { pub struct ListTypeConstructor {
pub length: usize, pub length: usize,
pub item_type: Box<TypeConstructor>, pub item_type: Box<TypeConstructor>,
} }
impl Display for ListTypeConstructor {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
todo!()
}
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TypeInvokationConstructor { pub struct TypeInvokationConstructor {
pub identifier: WithPosition<Identifier>, pub identifier: WithPosition<Identifier>,
pub type_arguments: Option<Vec<TypeConstructor>>, pub type_arguments: Option<Vec<TypeConstructor>>,
} }
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(())
}
}

View File

@ -169,7 +169,7 @@ impl Display for Value {
write!(f, "{{ ")?; write!(f, "{{ ")?;
for (index, (key, value)) in map.into_iter().enumerate() { for (index, (key, value)) in map.into_iter().enumerate() {
write!(f, "{key} = {value:?}")?; write!(f, "{key} = {value}")?;
if index != map.len() - 1 { if index != map.len() - 1 {
write!(f, ", ")?; write!(f, ", ")?;
@ -217,7 +217,7 @@ impl Display for Value {
write!(f, "-> {return_type}")? write!(f, "-> {return_type}")?
} }
write!(f, " {{ {body:?} }}") write!(f, " {{ {body} }}")
} }
ValueInner::Structure { name, fields } => { ValueInner::Structure { name, fields } => {
write!(f, "{}\n{{", name.node)?; write!(f, "{}\n{{", name.node)?;