Implement Record/TupleTypeSpec

This commit is contained in:
Shunsuke Shibayama 2022-10-21 14:42:53 +09:00
parent 9a88266bde
commit b28c6bd118
5 changed files with 83 additions and 4 deletions

View file

@ -1768,7 +1768,7 @@ pub enum TypeSpec {
Set(SetTypeSpec),
Tuple(Vec<TypeSpec>),
Dict(Vec<(TypeSpec, TypeSpec)>),
// Dict(),
Record(Vec<(Identifier, TypeSpec)>),
// Option(),
And(Box<TypeSpec>, Box<TypeSpec>),
Not(Box<TypeSpec>, Box<TypeSpec>),
@ -1799,11 +1799,18 @@ impl fmt::Display for TypeSpec {
Self::Tuple(tys) => write!(f, "({})", fmt_vec(tys)),
Self::Dict(dict) => {
write!(f, "{{")?;
for (k, v) in dict {
for (k, v) in dict.iter() {
write!(f, "{k}: {v}, ")?;
}
write!(f, "}}")
}
Self::Record(rec) => {
write!(f, "{{")?;
for (k, v) in rec.iter() {
write!(f, "{k} = {v}; ")?;
}
write!(f, "}}")
}
Self::Enum(elems) => write!(f, "{{{elems}}}"),
Self::Interval { op, lhs, rhs } => write!(f, "{lhs}{}{rhs}", op.inspect()),
Self::Subr(s) => write!(f, "{s}"),
@ -1824,6 +1831,7 @@ impl Locational for TypeSpec {
// TODO: ユニット
Self::Tuple(tys) => Location::concat(tys.first().unwrap(), tys.last().unwrap()),
Self::Dict(dict) => Location::concat(&dict.first().unwrap().0, &dict.last().unwrap().1),
Self::Record(rec) => Location::concat(&rec.first().unwrap().0, &rec.last().unwrap().1),
Self::Enum(set) => set.loc(),
Self::Interval { lhs, rhs, .. } => Location::concat(lhs, rhs),
Self::Subr(s) => s.loc(),