Skip to content

Commit

Permalink
refactor: switch generic types from unnamed to named
Browse files Browse the repository at this point in the history
As the amount of values contained in generic built-in types grows, it's
better to convert these to named structs so the values have a clearer
meaning. Also, the `hash_map` type is now split into individual fields
for the key and value type.
  • Loading branch information
dnaka91 committed Feb 20, 2024
1 parent e8e42ef commit d0e6ec1
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 276 deletions.
18 changes: 9 additions & 9 deletions crates/mabo-compiler/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,20 +470,20 @@ fn visit_externals<'a>(value: &'a Type<'_>, visit: &mut impl FnMut(&'a ExternalT
| DataType::StringRef
| DataType::Bytes
| DataType::BytesRef
| DataType::NonZero(_, _, _)
| DataType::NonZero { .. }
| DataType::BoxString
| DataType::BoxBytes => {}
DataType::Vec(_, _, ty)
| DataType::HashSet(_, _, ty)
| DataType::Option(_, _, ty)
| DataType::Array(_, ty, _, _) => {
DataType::Vec { ty, .. }
| DataType::HashSet { ty, .. }
| DataType::Option { ty, .. }
| DataType::Array { ty, .. } => {
visit_externals(ty, visit);
}
DataType::HashMap(_, _, _, kv) => {
visit_externals(&kv.0, visit);
visit_externals(&kv.1, visit);
DataType::HashMap { key, value, .. } => {
visit_externals(key, visit);
visit_externals(value, visit);
}
DataType::Tuple(_, types) => {
DataType::Tuple { types, .. } => {
for ty in types {
visit_externals(ty, visit);
}
Expand Down
18 changes: 9 additions & 9 deletions crates/mabo-compiler/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,19 @@ fn simplify_type<'a>(item: &'a mabo_parser::Type<'_>) -> Type<'a> {
mabo_parser::DataType::StringRef => Type::StringRef,
mabo_parser::DataType::Bytes => Type::Bytes,
mabo_parser::DataType::BytesRef => Type::BytesRef,
mabo_parser::DataType::Vec(_, _, ref ty) => Type::Vec(simplify_type(ty).into()),
mabo_parser::DataType::HashMap(_, _, _, ref kv) => {
Type::HashMap((simplify_type(&kv.0), simplify_type(&kv.1)).into())
}
mabo_parser::DataType::HashSet(_, _, ref ty) => Type::HashSet(simplify_type(ty).into()),
mabo_parser::DataType::Option(_, _, ref ty) => Type::Option(simplify_type(ty).into()),
mabo_parser::DataType::NonZero(_, _, ref ty) => Type::NonZero(simplify_type(ty).into()),
mabo_parser::DataType::Vec { ref ty, .. } => Type::Vec(simplify_type(ty).into()),
mabo_parser::DataType::HashMap {
ref key, ref value, ..
} => Type::HashMap((simplify_type(key), simplify_type(value)).into()),
mabo_parser::DataType::HashSet { ref ty, .. } => Type::HashSet(simplify_type(ty).into()),
mabo_parser::DataType::Option { ref ty, .. } => Type::Option(simplify_type(ty).into()),
mabo_parser::DataType::NonZero { ref ty, .. } => Type::NonZero(simplify_type(ty).into()),
mabo_parser::DataType::BoxString => Type::BoxString,
mabo_parser::DataType::BoxBytes => Type::BoxBytes,
mabo_parser::DataType::Tuple(_, ref types) => {
mabo_parser::DataType::Tuple { ref types, .. } => {
Type::Tuple(types.iter().map(|ty| simplify_type(ty)).collect())
}
mabo_parser::DataType::Array(_, ref ty, _, size) => {
mabo_parser::DataType::Array { ref ty, size, .. } => {
Type::Array(simplify_type(ty).into(), size)
}
mabo_parser::DataType::External(ref ty) => Type::External(ExternalType {
Expand Down
18 changes: 9 additions & 9 deletions crates/mabo-compiler/src/validate/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,18 @@ fn visit_externals(value: &Type<'_>, visit: &mut impl FnMut(&ExternalType<'_>))
| DataType::StringRef
| DataType::Bytes
| DataType::BytesRef
| DataType::NonZero(_, _, _)
| DataType::NonZero { .. }
| DataType::BoxString
| DataType::BoxBytes => {}
DataType::Vec(_, _, ty)
| DataType::HashSet(_, _, ty)
| DataType::Option(_, _, ty)
| DataType::Array(_, ty, _, _) => visit_externals(ty, visit),
DataType::HashMap(_, _, _, kv) => {
visit_externals(&kv.0, visit);
visit_externals(&kv.1, visit);
DataType::Vec { ty, .. }
| DataType::HashSet { ty, .. }
| DataType::Option { ty, .. }
| DataType::Array { ty, .. } => visit_externals(ty, visit),
DataType::HashMap { key, value, .. } => {
visit_externals(key, visit);
visit_externals(value, visit);
}
DataType::Tuple(_, types) => {
DataType::Tuple { types, .. } => {
for ty in types {
visit_externals(ty, visit);
}
Expand Down
18 changes: 9 additions & 9 deletions crates/mabo-compiler/src/validate/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ fn visit_tuples<E>(
| DataType::StringRef
| DataType::Bytes
| DataType::BytesRef
| DataType::NonZero(_, _, _)
| DataType::NonZero { .. }
| DataType::BoxString
| DataType::BoxBytes => Ok(()),
DataType::Vec(_, _, ty)
| DataType::HashSet(_, _, ty)
| DataType::Option(_, _, ty)
| DataType::Array(_, ty, _, _) => visit_tuples(ty, visit),
DataType::HashMap(_, _, _, kv) => {
visit_tuples(&kv.0, visit)?;
visit_tuples(&kv.1, visit)
DataType::Vec { ty, .. }
| DataType::HashSet { ty, .. }
| DataType::Option { ty, .. }
| DataType::Array { ty, .. } => visit_tuples(ty, visit),
DataType::HashMap { key, value, .. } => {
visit_tuples(key, visit)?;
visit_tuples(value, visit)
}
DataType::Tuple(_, types) => {
DataType::Tuple { types, .. } => {
visit(types)?;
types.iter().try_for_each(|ty| visit_tuples(ty, visit))
}
Expand Down
29 changes: 20 additions & 9 deletions crates/mabo-lsp/src/handlers/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,31 +364,42 @@ impl<'a> Visitor<'a> {
| DataType::Bytes
| DataType::BytesRef
| DataType::BoxBytes => self.add_span(item, &types::BUILTIN_TYPE, &[]),
DataType::Vec(span, angle, ty)
| DataType::HashSet(span, angle, ty)
| DataType::Option(span, angle, ty)
| DataType::NonZero(span, angle, ty) => {
DataType::Vec { span, angle, ty }
| DataType::HashSet { span, angle, ty }
| DataType::Option { span, angle, ty }
| DataType::NonZero { span, angle, ty } => {
self.add_span(span, &types::BUILTIN_TYPE, &[])?;
self.add_span(&angle.open(), &types::ANGLE, &[])?;
self.visit_type(ty)?;
self.add_span(&angle.close(), &types::ANGLE, &[])
}
DataType::HashMap(span, angle, comma, kv) => {
DataType::HashMap {
span,
angle,
key,
comma,
value,
} => {
self.add_span(span, &types::BUILTIN_TYPE, &[])?;
self.add_span(&angle.open(), &types::ANGLE, &[])?;
self.visit_type(&kv.0)?;
self.visit_type(key)?;
self.add_span(comma, &types::COMMA, &[])?;
self.visit_type(&kv.1)?;
self.visit_type(value)?;
self.add_span(&angle.close(), &types::ANGLE, &[])
}
DataType::Tuple(paren, types) => {
DataType::Tuple { paren, types } => {
self.add_span(&paren.open(), &types::PARENTHESIS, &[])?;
for ty in types {
self.visit_type(ty)?;
}
self.add_span(&paren.close(), &types::PARENTHESIS, &[])
}
DataType::Array(bracket, ty, semicolon, _) => {
DataType::Array {
bracket,
ty,
semicolon,
size: _,
} => {
self.add_span(&bracket.open(), &types::BRACKET, &[])?;
self.visit_type(ty)?;
self.add_span(semicolon, &types::SEMICOLON, &[])?;
Expand Down
81 changes: 67 additions & 14 deletions crates/mabo-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,25 +907,78 @@ pub enum DataType<'a> {
/// Reference version (slice) of `u8` bytes.
BytesRef,
/// Vector of another data type.
Vec(Span, token::Angle, Box<Type<'a>>),
Vec {
/// Source code location of the `vec` type name.
span: Span,
/// Angles `<`...`>` that delimit the type parameter.
angle: token::Angle,
/// Type parameter.
ty: Box<Type<'a>>,
},
/// Key-value hash map of data types.
HashMap(Span, token::Angle, token::Comma, Box<(Type<'a>, Type<'a>)>),
HashMap {
/// Source code location of the `hash_map` type name.
span: Span,
/// Angles `<`...`>` that delimit the type parameters.
angle: token::Angle,
/// First type parameter.
key: Box<Type<'a>>,
/// Separator between first and second type parameter.
comma: token::Comma,
/// Second type parameter.
value: Box<Type<'a>>,
},
/// Hash set of data types (each entry is unique).
HashSet(Span, token::Angle, Box<Type<'a>>),
HashSet {
/// Source code location of the `hash_set` type name.
span: Span,
/// Angles `<`...`>` that delimit the type parameter.
angle: token::Angle,
/// Type parameter.
ty: Box<Type<'a>>,
},
/// Optional value.
Option(Span, token::Angle, Box<Type<'a>>),
Option {
/// Source code location of the `option` type name.
span: Span,
/// Angles `<`...`>` that delimit the type parameter.
angle: token::Angle,
/// Type parameter.
ty: Box<Type<'a>>,
},
/// Non-zero value.
/// - Integers: `n > 0`
/// - Collections: `len() > 0`
NonZero(Span, token::Angle, Box<Type<'a>>),
NonZero {
/// Source code location of the `non_zero` type name.
span: Span,
/// Angles `<`...`>` that delimit the type parameter.
angle: token::Angle,
/// Type parameter.
ty: Box<Type<'a>>,
},
/// Boxed version of a string that is immutable.
BoxString,
/// Boxed version of a byte vector that is immutable.
BoxBytes,
/// Fixed size list of up to 12 types.
Tuple(token::Parenthesis, Vec<Type<'a>>),
Tuple {
/// Parenthesis `(`...`)` that delimits the tuple.
paren: token::Parenthesis,
/// List of types that make up the tuple.
types: Vec<Type<'a>>,
},
/// Continuous list of values with a single time and known length.
Array(token::Bracket, Box<Type<'a>>, token::Semicolon, u32),
Array {
/// Brackets `[`...`]` that delimit the array.
bracket: token::Bracket,
/// The singular repeated type.
ty: Box<Type<'a>>,
/// Separator between type and array size.
semicolon: token::Semicolon,
/// Size, as in count of elements.
size: u32,
},
/// Any external, non-standard data type (like a user defined struct or enum).
External(ExternalType<'a>),
}
Expand All @@ -950,15 +1003,15 @@ impl Display for DataType<'_> {
Self::StringRef => f.write_str("&string"),
Self::Bytes => f.write_str("bytes"),
Self::BytesRef => f.write_str("&bytes"),
Self::Vec(_, _, t) => write!(f, "vec<{t}>"),
Self::HashMap(_, _, _, kv) => write!(f, "hash_map<{}, {}>", kv.0, kv.1),
Self::HashSet(_, _, t) => write!(f, "hash_set<{t}>"),
Self::Option(_, _, t) => write!(f, "option<{t}>"),
Self::NonZero(_, _, t) => write!(f, "non_zero<{t}>"),
Self::Vec { ty, .. } => write!(f, "vec<{ty}>"),
Self::HashMap { key, value, .. } => write!(f, "hash_map<{key}, {value}>"),
Self::HashSet { ty, .. } => write!(f, "hash_set<{ty}>"),
Self::Option { ty, .. } => write!(f, "option<{ty}>"),
Self::NonZero { ty, .. } => write!(f, "non_zero<{ty}>"),
Self::BoxString => f.write_str("box<string>"),
Self::BoxBytes => f.write_str("box<bytes>"),
Self::Tuple(_, l) => concat::<token::Parenthesis>(f, l, ", "),
Self::Array(_, t, _, size) => write!(f, "[{t}; {size}]"),
Self::Tuple { types, .. } => concat::<token::Parenthesis>(f, types, ", "),
Self::Array { ty, size, .. } => write!(f, "[{ty}; {size}]"),
Self::External(t) => t.fmt(f),
}
}
Expand Down
Loading

0 comments on commit d0e6ec1

Please sign in to comment.