2024-11-06 00:38:26 +00:00
|
|
|
//! Value types and conflict handling.
|
2024-06-24 08:02:44 +00:00
|
|
|
use std::{
|
2024-08-17 08:06:13 +00:00
|
|
|
cmp::Ordering,
|
|
|
|
collections::HashMap,
|
2024-06-24 08:02:44 +00:00
|
|
|
fmt::{self, Display, Formatter},
|
|
|
|
};
|
2024-03-07 03:15:35 +00:00
|
|
|
|
2024-06-04 18:47:15 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-03-20 12:36:18 +00:00
|
|
|
|
2024-07-15 19:49:34 +00:00
|
|
|
/// Description of a kind of value.
|
2024-08-17 14:07:38 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2024-02-29 02:04:38 +00:00
|
|
|
pub enum Type {
|
2024-03-06 17:15:03 +00:00
|
|
|
Any,
|
2024-02-29 02:04:38 +00:00
|
|
|
Boolean,
|
2024-08-16 21:07:49 +00:00
|
|
|
Byte,
|
|
|
|
Character,
|
|
|
|
Enum(EnumType),
|
2024-02-29 02:04:38 +00:00
|
|
|
Float,
|
2024-08-16 21:07:49 +00:00
|
|
|
Function(FunctionType),
|
2024-06-19 04:05:58 +00:00
|
|
|
Generic {
|
2024-11-04 20:38:58 +00:00
|
|
|
identifier_index: u8,
|
2024-06-19 04:05:58 +00:00
|
|
|
concrete_type: Option<Box<Type>>,
|
|
|
|
},
|
2024-02-29 02:04:38 +00:00
|
|
|
Integer,
|
2024-06-17 14:10:06 +00:00
|
|
|
List {
|
|
|
|
item_type: Box<Type>,
|
2024-08-13 17:12:13 +00:00
|
|
|
length: usize,
|
|
|
|
},
|
2024-08-16 21:07:49 +00:00
|
|
|
ListEmpty,
|
2024-08-13 17:12:13 +00:00
|
|
|
ListOf {
|
|
|
|
item_type: Box<Type>,
|
2024-06-17 14:10:06 +00:00
|
|
|
},
|
2024-08-17 14:07:38 +00:00
|
|
|
Map {
|
2024-11-04 20:38:58 +00:00
|
|
|
pairs: HashMap<u8, Type>,
|
2024-08-17 14:07:38 +00:00
|
|
|
},
|
2024-11-10 02:40:33 +00:00
|
|
|
None,
|
2024-08-17 14:07:38 +00:00
|
|
|
Range {
|
2024-11-11 00:28:21 +00:00
|
|
|
r#type: Box<Type>,
|
2024-08-17 14:07:38 +00:00
|
|
|
},
|
2024-11-10 02:40:33 +00:00
|
|
|
SelfChunk,
|
2024-08-20 22:48:25 +00:00
|
|
|
String {
|
|
|
|
length: Option<usize>,
|
|
|
|
},
|
2024-08-13 17:28:22 +00:00
|
|
|
Struct(StructType),
|
2024-08-23 16:16:56 +00:00
|
|
|
Tuple {
|
|
|
|
fields: Option<Vec<Type>>,
|
|
|
|
},
|
2024-02-29 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 17:15:03 +00:00
|
|
|
impl Type {
|
2024-07-13 14:47:24 +00:00
|
|
|
/// Returns a concrete type, either the type itself or the concrete type of a generic type.
|
|
|
|
pub fn concrete_type(&self) -> &Type {
|
2024-08-20 06:25:22 +00:00
|
|
|
if let Type::Generic {
|
|
|
|
concrete_type: Some(concrete_type),
|
|
|
|
..
|
|
|
|
} = self
|
|
|
|
{
|
|
|
|
concrete_type.concrete_type()
|
|
|
|
} else {
|
|
|
|
self
|
2024-07-13 14:47:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that the type is compatible with another type.
|
2024-03-17 04:49:01 +00:00
|
|
|
pub fn check(&self, other: &Type) -> Result<(), TypeConflict> {
|
2024-07-13 14:47:24 +00:00
|
|
|
match (self.concrete_type(), other.concrete_type()) {
|
2024-03-06 17:15:03 +00:00
|
|
|
(Type::Any, _)
|
|
|
|
| (_, Type::Any)
|
|
|
|
| (Type::Boolean, Type::Boolean)
|
2024-08-20 15:40:37 +00:00
|
|
|
| (Type::Byte, Type::Byte)
|
|
|
|
| (Type::Character, Type::Character)
|
2024-03-06 17:15:03 +00:00
|
|
|
| (Type::Float, Type::Float)
|
|
|
|
| (Type::Integer, Type::Integer)
|
2024-11-11 00:28:21 +00:00
|
|
|
| (Type::None, Type::None)
|
2024-08-20 22:48:25 +00:00
|
|
|
| (Type::String { .. }, Type::String { .. }) => return Ok(()),
|
2024-06-19 04:05:58 +00:00
|
|
|
(
|
|
|
|
Type::Generic {
|
|
|
|
concrete_type: left,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
Type::Generic {
|
|
|
|
concrete_type: right,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) => match (left, right) {
|
2024-06-17 21:38:24 +00:00
|
|
|
(Some(left), Some(right)) => {
|
2024-07-12 14:20:52 +00:00
|
|
|
if left.check(right).is_ok() {
|
2024-06-17 21:38:24 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(None, None) => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2024-06-19 04:05:58 +00:00
|
|
|
(Type::Generic { concrete_type, .. }, other)
|
|
|
|
| (other, Type::Generic { concrete_type, .. }) => {
|
|
|
|
if let Some(concrete_type) = concrete_type {
|
|
|
|
if other == concrete_type.as_ref() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-13 17:28:22 +00:00
|
|
|
(Type::Struct(left_struct_type), Type::Struct(right_struct_type)) => {
|
|
|
|
if left_struct_type == right_struct_type {
|
2024-03-20 05:29:07 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2024-06-17 15:02:13 +00:00
|
|
|
(
|
|
|
|
Type::List {
|
|
|
|
item_type: left_type,
|
2024-08-13 17:12:13 +00:00
|
|
|
length: left_length,
|
2024-06-17 15:02:13 +00:00
|
|
|
},
|
|
|
|
Type::List {
|
|
|
|
item_type: right_type,
|
2024-08-13 17:12:13 +00:00
|
|
|
length: right_length,
|
2024-06-17 15:02:13 +00:00
|
|
|
},
|
|
|
|
) => {
|
2024-08-13 17:12:13 +00:00
|
|
|
if left_length != right_length {
|
|
|
|
return Err(TypeConflict {
|
|
|
|
actual: other.clone(),
|
|
|
|
expected: self.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:18:13 +00:00
|
|
|
if left_type.check(right_type).is_err() {
|
2024-06-17 15:02:13 +00:00
|
|
|
return Err(TypeConflict {
|
|
|
|
actual: other.clone(),
|
|
|
|
expected: self.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-13 17:12:13 +00:00
|
|
|
(
|
|
|
|
Type::ListOf {
|
|
|
|
item_type: left_type,
|
|
|
|
},
|
|
|
|
Type::ListOf {
|
|
|
|
item_type: right_type,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
if left_type.check(right_type).is_err() {
|
|
|
|
return Err(TypeConflict {
|
|
|
|
actual: other.clone(),
|
|
|
|
expected: self.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(
|
|
|
|
Type::List {
|
|
|
|
item_type: list_item_type,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
Type::ListOf {
|
|
|
|
item_type: list_of_item_type,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
| (
|
|
|
|
Type::ListOf {
|
|
|
|
item_type: list_of_item_type,
|
|
|
|
},
|
|
|
|
Type::List {
|
|
|
|
item_type: list_item_type,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
// TODO: This is a hack, remove it.
|
|
|
|
if let Type::Any = **list_of_item_type {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if list_item_type.check(list_of_item_type).is_err() {
|
|
|
|
return Err(TypeConflict {
|
|
|
|
actual: other.clone(),
|
|
|
|
expected: self.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 05:29:07 +00:00
|
|
|
(
|
2024-08-16 21:07:49 +00:00
|
|
|
Type::Function(FunctionType {
|
2024-06-17 14:10:06 +00:00
|
|
|
type_parameters: left_type_parameters,
|
|
|
|
value_parameters: left_value_parameters,
|
2024-03-20 12:36:18 +00:00
|
|
|
return_type: left_return,
|
2024-08-16 21:07:49 +00:00
|
|
|
}),
|
|
|
|
Type::Function(FunctionType {
|
2024-06-17 14:10:06 +00:00
|
|
|
type_parameters: right_type_parameters,
|
|
|
|
value_parameters: right_value_parameters,
|
2024-03-20 12:36:18 +00:00
|
|
|
return_type: right_return,
|
2024-08-16 21:07:49 +00:00
|
|
|
}),
|
2024-03-20 05:29:07 +00:00
|
|
|
) => {
|
2024-10-13 00:19:21 +00:00
|
|
|
if left_return != right_return
|
2024-08-17 14:07:38 +00:00
|
|
|
|| left_type_parameters != right_type_parameters
|
|
|
|
|| left_value_parameters != right_value_parameters
|
|
|
|
{
|
2024-08-16 21:07:49 +00:00
|
|
|
return Err(TypeConflict {
|
|
|
|
actual: other.clone(),
|
|
|
|
expected: self.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
(Type::Range { r#type: left_type }, Type::Range { r#type: right_type }) => {
|
|
|
|
if left_type == right_type {
|
2024-03-20 05:29:07 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2024-03-06 17:15:03 +00:00
|
|
|
}
|
2024-03-20 05:29:07 +00:00
|
|
|
|
|
|
|
Err(TypeConflict {
|
|
|
|
actual: other.clone(),
|
|
|
|
expected: self.clone(),
|
|
|
|
})
|
2024-03-06 17:15:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 03:15:35 +00:00
|
|
|
impl Display for Type {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Type::Any => write!(f, "any"),
|
2024-03-23 21:07:41 +00:00
|
|
|
Type::Boolean => write!(f, "bool"),
|
2024-08-16 21:07:49 +00:00
|
|
|
Type::Byte => write!(f, "byte"),
|
|
|
|
Type::Character => write!(f, "char"),
|
2024-08-17 14:07:38 +00:00
|
|
|
Type::Enum(EnumType { name, .. }) => write!(f, "{name}"),
|
2024-03-07 03:15:35 +00:00
|
|
|
Type::Float => write!(f, "float"),
|
2024-08-16 21:07:49 +00:00
|
|
|
Type::Function(function_type) => write!(f, "{function_type}"),
|
2024-06-19 04:05:58 +00:00
|
|
|
Type::Generic { concrete_type, .. } => {
|
2024-06-26 15:35:39 +00:00
|
|
|
match concrete_type.clone().map(|r#box| *r#box) {
|
2024-11-04 20:38:58 +00:00
|
|
|
Some(Type::Generic {
|
|
|
|
identifier_index: identifier,
|
|
|
|
..
|
|
|
|
}) => write!(f, "{identifier}"),
|
2024-06-26 15:35:39 +00:00
|
|
|
Some(concrete_type) => write!(f, "implied to be {concrete_type}"),
|
|
|
|
None => write!(f, "unknown"),
|
2024-06-17 21:38:24 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 21:07:41 +00:00
|
|
|
Type::Integer => write!(f, "int"),
|
2024-08-13 17:12:13 +00:00
|
|
|
Type::List { item_type, length } => write!(f, "[{item_type}; {length}]"),
|
2024-08-16 21:07:49 +00:00
|
|
|
Type::ListEmpty => write!(f, "[]"),
|
2024-08-16 09:14:00 +00:00
|
|
|
Type::ListOf { item_type } => write!(f, "[{item_type}]"),
|
2024-08-17 14:07:38 +00:00
|
|
|
Type::Map { pairs } => {
|
|
|
|
write!(f, "map ")?;
|
|
|
|
|
|
|
|
write!(f, "{{")?;
|
|
|
|
|
|
|
|
for (index, (key, value)) in pairs.iter().enumerate() {
|
|
|
|
write!(f, "{key}: {value}")?;
|
|
|
|
|
|
|
|
if index != pairs.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "}}")
|
|
|
|
}
|
2024-11-10 02:40:33 +00:00
|
|
|
Type::None => write!(f, "none"),
|
2024-08-17 14:07:38 +00:00
|
|
|
Type::Range { r#type } => write!(f, "{type} range"),
|
2024-11-10 02:40:33 +00:00
|
|
|
Type::SelfChunk => write!(f, "self"),
|
2024-08-20 22:48:25 +00:00
|
|
|
Type::String { .. } => write!(f, "str"),
|
2024-08-20 04:15:19 +00:00
|
|
|
Type::Struct(struct_type) => write!(f, "{struct_type}"),
|
2024-08-23 16:16:56 +00:00
|
|
|
Type::Tuple { fields } => {
|
|
|
|
if let Some(fields) = fields {
|
|
|
|
write!(f, "(")?;
|
2024-06-24 08:02:44 +00:00
|
|
|
|
2024-08-23 16:16:56 +00:00
|
|
|
for (index, r#type) in fields.iter().enumerate() {
|
|
|
|
write!(f, "{type}")?;
|
2024-06-24 08:16:05 +00:00
|
|
|
|
2024-08-23 16:16:56 +00:00
|
|
|
if index != fields.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
2024-06-24 08:16:05 +00:00
|
|
|
}
|
2024-06-24 08:02:44 +00:00
|
|
|
|
2024-08-23 16:16:56 +00:00
|
|
|
write!(f, ")")
|
|
|
|
} else {
|
|
|
|
write!(f, "tuple")
|
|
|
|
}
|
2024-06-24 08:02:44 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-09 02:26:49 +00:00
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
impl PartialOrd for Type {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Type {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(Type::Any, Type::Any) => Ordering::Equal,
|
|
|
|
(Type::Any, _) => Ordering::Greater,
|
|
|
|
(Type::Boolean, Type::Boolean) => Ordering::Equal,
|
|
|
|
(Type::Boolean, _) => Ordering::Greater,
|
|
|
|
(Type::Byte, Type::Byte) => Ordering::Equal,
|
|
|
|
(Type::Byte, _) => Ordering::Greater,
|
|
|
|
(Type::Character, Type::Character) => Ordering::Equal,
|
|
|
|
(Type::Character, _) => Ordering::Greater,
|
|
|
|
(Type::Enum(left_enum), Type::Enum(right_enum)) => left_enum.cmp(right_enum),
|
|
|
|
(Type::Enum(_), _) => Ordering::Greater,
|
|
|
|
(Type::Float, Type::Float) => Ordering::Equal,
|
|
|
|
(Type::Float, _) => Ordering::Greater,
|
|
|
|
(Type::Function(left_function), Type::Function(right_function)) => {
|
|
|
|
left_function.cmp(right_function)
|
|
|
|
}
|
|
|
|
(Type::Function(_), _) => Ordering::Greater,
|
|
|
|
(Type::Generic { .. }, Type::Generic { .. }) => Ordering::Equal,
|
|
|
|
(Type::Generic { .. }, _) => Ordering::Greater,
|
|
|
|
(Type::Integer, Type::Integer) => Ordering::Equal,
|
|
|
|
(Type::Integer, _) => Ordering::Greater,
|
|
|
|
(
|
|
|
|
Type::List {
|
|
|
|
item_type: left_item_type,
|
|
|
|
length: left_length,
|
|
|
|
},
|
|
|
|
Type::List {
|
|
|
|
item_type: right_item_type,
|
|
|
|
length: right_length,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
if left_length == right_length {
|
|
|
|
left_item_type.cmp(right_item_type)
|
|
|
|
} else {
|
|
|
|
left_length.cmp(right_length)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Type::List { .. }, _) => Ordering::Greater,
|
|
|
|
(Type::ListEmpty, Type::ListEmpty) => Ordering::Equal,
|
|
|
|
(Type::ListEmpty, _) => Ordering::Greater,
|
|
|
|
(
|
|
|
|
Type::ListOf {
|
|
|
|
item_type: left_item_type,
|
|
|
|
},
|
|
|
|
Type::ListOf {
|
|
|
|
item_type: right_item_type,
|
|
|
|
},
|
|
|
|
) => left_item_type.cmp(right_item_type),
|
|
|
|
(Type::ListOf { .. }, _) => Ordering::Greater,
|
|
|
|
(Type::Map { pairs: left_pairs }, Type::Map { pairs: right_pairs }) => {
|
|
|
|
left_pairs.iter().cmp(right_pairs.iter())
|
|
|
|
}
|
|
|
|
(Type::Map { .. }, _) => Ordering::Greater,
|
2024-11-10 02:40:33 +00:00
|
|
|
(Type::None, Type::None) => Ordering::Equal,
|
|
|
|
(Type::None, _) => Ordering::Greater,
|
2024-08-17 14:07:38 +00:00
|
|
|
(Type::Range { r#type: left_type }, Type::Range { r#type: right_type }) => {
|
|
|
|
left_type.cmp(right_type)
|
|
|
|
}
|
|
|
|
(Type::Range { .. }, _) => Ordering::Greater,
|
2024-11-10 02:40:33 +00:00
|
|
|
(Type::SelfChunk, Type::SelfChunk) => Ordering::Equal,
|
|
|
|
(Type::SelfChunk, _) => Ordering::Greater,
|
2024-08-20 22:48:25 +00:00
|
|
|
(Type::String { length: left }, Type::String { length: right }) => left.cmp(right),
|
|
|
|
(Type::String { .. }, _) => Ordering::Greater,
|
2024-08-17 14:07:38 +00:00
|
|
|
(Type::Struct(left_struct), Type::Struct(right_struct)) => {
|
|
|
|
left_struct.cmp(right_struct)
|
|
|
|
}
|
|
|
|
(Type::Struct(_), _) => Ordering::Greater,
|
2024-08-23 16:16:56 +00:00
|
|
|
|
|
|
|
(Type::Tuple { fields: left }, Type::Tuple { fields: right }) => left.cmp(right),
|
|
|
|
(Type::Tuple { .. }, _) => Ordering::Greater,
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
|
|
pub struct FunctionType {
|
2024-11-04 20:38:58 +00:00
|
|
|
pub type_parameters: Option<Vec<u8>>,
|
|
|
|
pub value_parameters: Option<Vec<(u8, Type)>>,
|
2024-11-10 02:40:33 +00:00
|
|
|
pub return_type: Box<Type>,
|
2024-08-16 21:07:49 +00:00
|
|
|
}
|
2024-06-26 18:44:23 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl Display for FunctionType {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "fn ")?;
|
2024-06-17 14:10:06 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
if let Some(type_parameters) = &self.type_parameters {
|
|
|
|
write!(f, "<")?;
|
2024-06-17 14:10:06 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
for (index, type_parameter) in type_parameters.iter().enumerate() {
|
2024-10-13 00:19:21 +00:00
|
|
|
if index > 0 {
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, ", ")?;
|
2024-03-09 02:26:49 +00:00
|
|
|
}
|
2024-10-13 00:19:21 +00:00
|
|
|
|
|
|
|
write!(f, "{type_parameter}")?;
|
2024-08-16 21:07:49 +00:00
|
|
|
}
|
2024-03-09 02:26:49 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
|
|
|
if let Some(value_parameters) = &self.value_parameters {
|
|
|
|
for (index, (identifier, r#type)) in value_parameters.iter().enumerate() {
|
2024-10-13 00:19:21 +00:00
|
|
|
if index > 0 {
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, ", ")?;
|
2024-06-22 04:58:30 +00:00
|
|
|
}
|
2024-10-13 00:19:21 +00:00
|
|
|
|
|
|
|
write!(f, "{identifier}: {type}")?;
|
2024-03-09 02:26:49 +00:00
|
|
|
}
|
2024-08-13 17:28:22 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
|
|
|
|
write!(f, ")")?;
|
|
|
|
|
2024-11-10 02:40:33 +00:00
|
|
|
if *self.return_type != Type::None {
|
|
|
|
write!(f, " -> {}", self.return_type)?;
|
2024-08-16 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2024-08-13 17:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2024-08-13 17:28:22 +00:00
|
|
|
pub enum StructType {
|
2024-11-04 20:38:58 +00:00
|
|
|
Unit { name: u8 },
|
|
|
|
Tuple { name: u8, fields: Vec<Type> },
|
|
|
|
Fields { name: u8, fields: HashMap<u8, Type> },
|
2024-08-13 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
impl StructType {
|
2024-11-04 20:38:58 +00:00
|
|
|
pub fn name(&self) -> u8 {
|
2024-08-13 17:28:22 +00:00
|
|
|
match self {
|
2024-11-04 20:38:58 +00:00
|
|
|
StructType::Unit { name } => *name,
|
|
|
|
StructType::Tuple { name, .. } => *name,
|
|
|
|
StructType::Fields { name, .. } => *name,
|
2024-03-07 03:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-17 08:06:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
impl Display for StructType {
|
2024-08-17 14:07:38 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2024-08-20 04:15:19 +00:00
|
|
|
match self {
|
|
|
|
StructType::Unit { name } => write!(f, "{name}"),
|
|
|
|
StructType::Tuple { name, fields } => {
|
|
|
|
write!(f, "{name}(")?;
|
2024-08-17 14:07:38 +00:00
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
for (index, field) in fields.iter().enumerate() {
|
|
|
|
write!(f, "{field}")?;
|
2024-08-17 14:07:38 +00:00
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
if index != fields.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ")")
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
2024-08-20 04:15:19 +00:00
|
|
|
StructType::Fields { name, fields } => {
|
|
|
|
write!(f, "{name} {{")?;
|
|
|
|
|
|
|
|
for (index, (identifier, r#type)) in fields.iter().enumerate() {
|
|
|
|
write!(f, "{identifier}: {type}")?;
|
|
|
|
|
|
|
|
if index != fields.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
}
|
2024-08-17 14:07:38 +00:00
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
write!(f, "}}")
|
|
|
|
}
|
|
|
|
}
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
impl PartialOrd for StructType {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
2024-08-17 08:06:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
impl Ord for StructType {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(StructType::Unit { name: left_name }, StructType::Unit { name: right_name }) => {
|
|
|
|
left_name.cmp(right_name)
|
|
|
|
}
|
|
|
|
(StructType::Unit { .. }, _) => Ordering::Greater,
|
|
|
|
(
|
|
|
|
StructType::Tuple {
|
|
|
|
name: left_name,
|
|
|
|
fields: left_fields,
|
|
|
|
},
|
|
|
|
StructType::Tuple {
|
|
|
|
name: right_name,
|
|
|
|
fields: right_fields,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
let name_cmp = left_name.cmp(right_name);
|
|
|
|
|
|
|
|
if name_cmp == Ordering::Equal {
|
|
|
|
left_fields.cmp(right_fields)
|
|
|
|
} else {
|
|
|
|
name_cmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(StructType::Tuple { .. }, _) => Ordering::Greater,
|
|
|
|
(
|
|
|
|
StructType::Fields {
|
|
|
|
name: left_name,
|
|
|
|
fields: left_fields,
|
|
|
|
},
|
|
|
|
StructType::Fields {
|
|
|
|
name: right_name,
|
|
|
|
fields: right_fields,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
let name_cmp = left_name.cmp(right_name);
|
2024-08-17 14:07:38 +00:00
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
if name_cmp == Ordering::Equal {
|
|
|
|
let len_cmp = left_fields.len().cmp(&right_fields.len());
|
2024-08-17 14:07:38 +00:00
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
if len_cmp == Ordering::Equal {
|
|
|
|
left_fields.iter().cmp(right_fields.iter())
|
|
|
|
} else {
|
|
|
|
len_cmp
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name_cmp
|
|
|
|
}
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
2024-08-20 04:15:19 +00:00
|
|
|
(StructType::Fields { .. }, _) => Ordering::Greater,
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
|
|
pub struct EnumType {
|
2024-11-04 20:38:58 +00:00
|
|
|
pub name: u8,
|
2024-08-20 04:15:19 +00:00
|
|
|
pub variants: Vec<StructType>,
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl Display for EnumType {
|
2024-08-17 14:07:38 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
let EnumType { name, variants } = self;
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
write!(f, "enum {name} {{ ")?;
|
2024-08-17 14:07:38 +00:00
|
|
|
|
|
|
|
for (index, variant) in variants.iter().enumerate() {
|
|
|
|
write!(f, "{variant}")?;
|
|
|
|
|
|
|
|
if index != self.variants.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
write!(f, " }}")
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct TypeConflict {
|
|
|
|
pub expected: Type,
|
|
|
|
pub actual: Type,
|
|
|
|
}
|