Add fields to map type

This commit is contained in:
Jeff 2024-06-24 04:16:05 -04:00
parent 5e8945cab5
commit c75dedb117
3 changed files with 47 additions and 9 deletions

View File

@ -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"),

View File

@ -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!(

View File

@ -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}"),