1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Module for types.
use std::fmt;
/// Enum of possible types for `Value`s.
#[derive(Clone, Debug)]
pub enum Type {
/// A type used to indicate an empty Arr.
Any,
/// Null value.
Null,
/// A boolean type.
Bool,
/// A signed integer type.
Int,
/// A fractional type.
Frac,
/// A character type.
Char,
/// A string type.
Str,
/// An array type, containing the type of its sub-elements.
Arr(Box<Type>),
/// A tuple type, containing the types of its sub-elements.
Tup(Vec<Type>),
/// An object type.
Obj,
}
impl Type {
/// Returns true if this type is strictly the same as `other`.
/// Usually you want to use `eq()` instead.
pub fn is(&self, other: &Type) -> bool {
use self::Type::*;
match *self {
Any => {
if let Any = *other {
true
} else {
false
}
}
Null => {
if let Null = *other {
true
} else {
false
}
}
Bool => {
if let Bool = *other {
true
} else {
false
}
}
Int => {
if let Int = *other {
true
} else {
false
}
}
Frac => {
if let Frac = *other {
true
} else {
false
}
}
Char => {
if let Char = *other {
true
} else {
false
}
}
Str => {
if let Str = *other {
true
} else {
false
}
}
Obj => {
if let Obj = *other {
true
} else {
false
}
}
Arr(ref t1) => {
if let Arr(ref t2) = *other {
t1.is(t2)
} else {
false
}
}
Tup(ref tvec1) => {
if let Tup(ref tvec2) = *other {
if tvec1.len() != tvec2.len() {
return false;
}
tvec1.iter().zip(tvec2.iter()).all(|(t1, t2)| t1.is(t2))
} else {
false
}
}
}
}
/// Returns true if this `Type` contains `Any`.
pub fn has_any(&self) -> bool {
match *self {
Type::Any => true,
Type::Arr(ref t) => Self::has_any(t),
Type::Tup(ref tvec) => tvec.iter().any(|t| Self::has_any(t)),
_ => false,
}
}
/// Returns a type with the most specificity that can be applied to the two input types as well
/// as `true` if the returned type is not maximally specific, that is, it contains `Any`. If no
/// single type can be applied to both input types (e.g. the types are `Str` and `Int`), returns
/// `None`.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate over;
/// # fn main() {
///
/// use over::types::Type;
/// use over::types::Type::*;
/// use over::value::Value;
///
/// let val1: Value = tup!(arr![], arr![2]).into();
/// let val2: Value = tup!(arr!['c'], arr![]).into();
///
/// let (specific_type, has_any) =
/// Type::most_specific(&val1.get_type(), &val2.get_type()).unwrap();
///
/// assert_eq!(specific_type, Tup(vec![Arr(Box::new(Char)), Arr(Box::new(Int))]));
/// assert!(!has_any);
///
/// # }
/// ```
pub fn most_specific(type1: &Type, type2: &Type) -> Option<(Type, bool)> {
use self::Type::*;
if let Any = *type2 {
return Some((type1.clone(), type1.has_any()));
}
match *type1 {
Any => Some((type2.clone(), type2.has_any())),
Arr(ref t1) => {
if let Arr(ref t2) = *type2 {
Self::most_specific(t1, t2).map(|(t, any)| (Arr(Box::new(t)), any))
} else {
None
}
}
Tup(ref tvec1) => {
if let Tup(ref tvec2) = *type2 {
if tvec1.len() == tvec2.len() {
let mut has_any = false;
let tvec: Option<Vec<Type>> = tvec1
.iter()
.zip(tvec2.iter())
.map(|(t1, t2)| {
Self::most_specific(t1, t2).map(|(t, any)| {
if !has_any && any {
has_any = any;
}
t
})
})
.collect();
tvec.map(|tvec| (Tup(tvec), has_any))
} else {
None
}
} else {
None
}
}
ref t => {
if t == type2 {
Some((t.clone(), false))
} else {
None
}
}
}
}
}
/// Two types are considered equal if one of them is Any or they have the same variant.
/// In the case of `Arr` and `Tup`, the inner types are recursively checked for equality.
impl PartialEq for Type {
fn eq(&self, other: &Self) -> bool {
use self::Type::*;
// If either is Any, always return `true`.
if let Any = *other {
return true;
}
match *self {
Any => true,
Arr(ref box1) => {
if let Arr(ref box2) = *other {
box1 == box2
} else {
false
}
}
Tup(ref tvec1) => {
if let Tup(ref tvec2) = *other {
tvec1 == tvec2
} else {
false
}
}
_ => self.is(other),
}
}
}
impl Eq for Type {}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Type::*;
match *self {
Any => write!(f, "Any"),
Null => write!(f, "Null"),
Bool => write!(f, "Bool"),
Int => write!(f, "Int"),
Frac => write!(f, "Frac"),
Char => write!(f, "Char"),
Str => write!(f, "Str"),
Arr(ref boxxy) => write!(f, "Arr({})", boxxy),
Tup(ref tvec) => write!(
f,
"Tup({})",
match tvec.get(0) {
Some(t1) => tvec
.iter()
.skip(1)
.fold(format!("{}", t1), |s, t| format!("{}, {}", s, t)),
None => String::from(""),
}
),
Obj => write!(f, "Obj"),
}
}
}