Add fields to map type
This commit is contained in:
parent
5e8945cab5
commit
c75dedb117
@ -244,11 +244,15 @@ impl Display for Type {
|
|||||||
Type::Integer => write!(f, "int"),
|
Type::Integer => write!(f, "int"),
|
||||||
Type::List { length, item_type } => write!(f, "[{length}; {}]", item_type),
|
Type::List { length, item_type } => write!(f, "[{length}; {}]", item_type),
|
||||||
Type::ListOf(item_type) => write!(f, "[{}]", item_type),
|
Type::ListOf(item_type) => write!(f, "[{}]", item_type),
|
||||||
Type::Map(item_types) => {
|
Type::Map(map) => {
|
||||||
writeln!(f, "{{")?;
|
write!(f, "{{ ")?;
|
||||||
|
|
||||||
for (identifier, r#type) in item_types {
|
for (index, (key, r#type)) in map.into_iter().enumerate() {
|
||||||
writeln!(f, "{identifier}: {type}")?;
|
write!(f, "{key}: {type}")?;
|
||||||
|
|
||||||
|
if index != map.len() - 1 {
|
||||||
|
write!(f, ", ")?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(f, " }}")
|
write!(f, " }}")
|
||||||
|
@ -2,6 +2,36 @@ use crate::lexer::lex;
|
|||||||
|
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn type_invokation() {
|
fn type_invokation() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -154,13 +154,17 @@ impl Display for Value {
|
|||||||
write!(f, "]")
|
write!(f, "]")
|
||||||
}
|
}
|
||||||
ValueInner::Map(map) => {
|
ValueInner::Map(map) => {
|
||||||
write!(f, "[")?;
|
write!(f, "{{ ")?;
|
||||||
|
|
||||||
for (key, value) in map {
|
for (index, (key, value)) in map.into_iter().enumerate() {
|
||||||
writeln!(f, "{key} = {value},")?;
|
write!(f, "{key} = {value}")?;
|
||||||
|
|
||||||
|
if index != map.len() - 1 {
|
||||||
|
write!(f, ", ")?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(f, "]")
|
write!(f, " }}")
|
||||||
}
|
}
|
||||||
ValueInner::Range(_) => todo!(),
|
ValueInner::Range(_) => todo!(),
|
||||||
ValueInner::String(string) => write!(f, "{string}"),
|
ValueInner::String(string) => write!(f, "{string}"),
|
||||||
|
Loading…
Reference in New Issue
Block a user