From c75dedb117a774b912d60ed62103585cfe0f7e28 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 24 Jun 2024 04:16:05 -0400 Subject: [PATCH] Add fields to map type --- dust-lang/src/abstract_tree/type.rs | 14 +++++++++----- dust-lang/src/parser/tests.rs | 30 +++++++++++++++++++++++++++++ dust-lang/src/value.rs | 12 ++++++++---- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/dust-lang/src/abstract_tree/type.rs b/dust-lang/src/abstract_tree/type.rs index 6e9ecb3..04b76bc 100644 --- a/dust-lang/src/abstract_tree/type.rs +++ b/dust-lang/src/abstract_tree/type.rs @@ -244,14 +244,18 @@ impl Display for Type { Type::Integer => write!(f, "int"), Type::List { length, item_type } => write!(f, "[{length}; {}]", item_type), Type::ListOf(item_type) => write!(f, "[{}]", item_type), - Type::Map(item_types) => { - writeln!(f, "{{")?; + Type::Map(map) => { + write!(f, "{{ ")?; - for (identifier, r#type) in item_types { - writeln!(f, "{identifier}: {type}")?; + for (index, (key, r#type)) in map.into_iter().enumerate() { + write!(f, "{key}: {type}")?; + + if index != map.len() - 1 { + write!(f, ", ")?; + } } - write!(f, "}}") + write!(f, " }}") } Type::Range => write!(f, "range"), Type::String => write!(f, "str"), diff --git a/dust-lang/src/parser/tests.rs b/dust-lang/src/parser/tests.rs index fadcedf..b0dd8ec 100644 --- a/dust-lang/src/parser/tests.rs +++ b/dust-lang/src/parser/tests.rs @@ -2,6 +2,36 @@ use crate::lexer::lex; use super::*; +#[test] +fn map_type() { + assert_eq!( + parse(&lex("type Map = { x: int, y: str }").unwrap()).unwrap()[0], + Statement::TypeAlias( + TypeAlias::new( + Identifier::new("Map").with_position((5, 8)), + TypeConstructor::Map( + vec![ + ( + Identifier::new("x").with_position((13, 14)), + TypeConstructor::Raw( + RawTypeConstructor::Integer.with_position((16, 19)) + ) + ), + ( + Identifier::new("y").with_position((21, 22)), + TypeConstructor::Raw( + RawTypeConstructor::String.with_position((24, 27)) + ) + ) + ] + .with_position((11, 29)) + ) + ) + .with_position((0, 29)) + ) + ); +} + #[test] fn type_invokation() { assert_eq!( diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index 82e4b0d..acc7148 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -154,13 +154,17 @@ impl Display for Value { write!(f, "]") } ValueInner::Map(map) => { - write!(f, "[")?; + write!(f, "{{ ")?; - for (key, value) in map { - writeln!(f, "{key} = {value},")?; + for (index, (key, value)) in map.into_iter().enumerate() { + write!(f, "{key} = {value}")?; + + if index != map.len() - 1 { + write!(f, ", ")?; + } } - write!(f, "]") + write!(f, " }}") } ValueInner::Range(_) => todo!(), ValueInner::String(string) => write!(f, "{string}"),