-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtypes.rs
More file actions
299 lines (255 loc) · 7.98 KB
/
Copy pathtypes.rs
File metadata and controls
299 lines (255 loc) · 7.98 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use bitmask_enum::bitmask;
use crate::instructions::{
HqBooleanFields, HqFloatFields, HqIntegerFields, HqTextFields, IrOpcode,
};
use crate::prelude::*;
use crate::sb3::VarVal;
/// a bitmask of possible IR types
#[bitmask(u32)]
#[bitmask_config(vec_debug, flags_iter)]
pub enum Type {
IntZero,
IntPos,
IntNeg,
IntNonZero = Self::IntPos.or(Self::IntNeg).bits,
Int = Self::IntNonZero.or(Self::IntZero).bits,
FloatPosZero,
FloatNegZero,
FloatZero = Self::FloatPosZero.or(Self::FloatNegZero).bits,
FloatPosInt,
FloatPosFrac,
FloatPosReal = Self::FloatPosInt.or(Self::FloatPosFrac).bits,
FloatNegInt,
FloatNegFrac,
FloatNegReal = Self::FloatNegInt.or(Self::FloatNegFrac).bits,
FloatPosInf,
FloatNegInf,
FloatInf = Self::FloatPosInf.or(Self::FloatNegInf).bits,
FloatNan,
FloatPos = Self::FloatPosReal.or(Self::FloatPosInf).bits,
FloatNeg = Self::FloatNegReal.or(Self::FloatNegInf).bits,
FloatPosWhole = Self::FloatPosInt.or(Self::FloatPosZero).bits,
FloatNegWhole = Self::FloatNegInt.or(Self::FloatNegZero).bits,
FloatInt = Self::FloatPosWhole.or(Self::FloatNegWhole).bits,
FloatFrac = Self::FloatPosFrac.or(Self::FloatNegFrac).bits,
FloatReal = Self::FloatInt.or(Self::FloatFrac).bits,
FloatNotNan = Self::FloatReal.or(Self::FloatInf).bits,
Float = Self::FloatNotNan.or(Self::FloatNan).bits,
BooleanTrue,
BooleanFalse,
Boolean = Self::BooleanTrue.or(Self::BooleanFalse).bits,
QuasiInt = Self::Int.or(Self::Boolean).bits,
Number = Self::QuasiInt.or(Self::Float).bits,
StringNumber, // a string which can be interpreted as a non-nan number
StringBoolean, // "true" or "false"
StringNan, // some other string which can only be interpreted as NaN
String = Self::StringNumber
.or(Self::StringBoolean)
.or(Self::StringNan)
.bits,
QuasiBoolean = Self::Boolean.or(Self::StringBoolean).bits,
QuasiNumber = Self::Number.or(Self::StringNumber).bits,
Any = Self::String.or(Self::Number).bits,
// two different colour types are needed because calling `set pen colour to ()` without an alpha component
// resets the pen transparency to 0.
ColorRGB,
ColorARGB,
Color = Self::ColorRGB.or(Self::ColorARGB).bits,
AnyOrColor = Self::Any.or(Self::Color).bits,
}
impl Type {
// float must always be last in this list because it's more difficult to check if a boxed value
// *doesn't* match any other pattern
pub const BASE_TYPES: [Self; 6] = [
Self::String,
Self::Int,
Self::Boolean,
Self::ColorRGB,
Self::ColorARGB,
Self::Float,
];
#[must_use]
pub fn is_base_type(self) -> bool {
(!self.is_none()) && Self::BASE_TYPES.iter().any(|ty| ty.contains(self))
}
#[must_use]
pub fn base_type(self) -> Option<Self> {
if !self.is_base_type() {
return None;
}
Self::BASE_TYPES
.iter()
.copied()
.find(|&ty| ty.contains(self))
}
#[must_use]
pub fn base_types(self) -> Box<dyn Iterator<Item = Self>> {
if self.is_none() {
return Box::new(core::iter::empty());
}
Box::new(
Self::BASE_TYPES
.iter()
.filter(move |ty| ty.intersects(self))
.copied(),
)
}
#[must_use]
pub const fn maybe_positive(self) -> bool {
self.contains(Self::IntPos)
|| self.intersects(Self::FloatPos)
|| self.contains(Self::BooleanTrue)
|| self.contains(Self::Color)
}
#[must_use]
pub const fn maybe_negative(self) -> bool {
self.contains(Self::IntNeg) || self.intersects(Self::FloatNeg)
}
#[must_use]
pub const fn maybe_zero(self) -> bool {
self.contains(Self::IntZero)
|| self.contains(Self::BooleanFalse)
|| self.intersects(Self::FloatZero)
|| self.contains(Self::Color)
}
#[must_use]
pub const fn maybe_nan(self) -> bool {
self.intersects(Self::FloatNan) || self.contains(Self::StringNan)
}
#[must_use]
pub const fn maybe_inf(self) -> bool {
self.intersects(Self::FloatInf)
}
#[must_use]
pub const fn none_if_false(condition: bool, if_true: Self) -> Self {
if condition { if_true } else { Self::none() }
}
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match Self::flags().find(|(_, flag)| flag == self) {
Some((n, _)) => (*n).to_string(),
None => format!("{self:?}"),
}
)
}
}
#[derive(Clone, Debug)]
pub enum ReturnType {
None,
Singleton(Type),
MultiValue(Rc<[Type]>),
}
impl ReturnType {
pub fn singleton_or_else<E, F>(self, err: F) -> Result<Type, E>
where
F: FnOnce() -> E,
{
if let Self::Singleton(ty) = self {
Ok(ty)
} else {
Err(err())
}
}
}
pub fn base_types(inputs: &[Type]) -> HQResult<Box<[Box<[Type]>]>> {
inputs
.iter()
.copied()
.map(|ty| {
Type::base_types(ty)
.map(|bty| bty.and(ty))
.collect::<Box<[_]>>()
})
.map(|tys| {
if tys.is_empty() {
hq_bug!("got empty type in base_types!!!")
}
Ok(tys)
})
.collect()
}
#[must_use]
pub fn var_val_instruction(var_val: &VarVal) -> IrOpcode {
match var_val {
VarVal::Float(f) => IrOpcode::hq_float(HqFloatFields(*f)),
VarVal::Int(i) => IrOpcode::hq_integer(HqIntegerFields(*i)),
VarVal::Bool(b) => IrOpcode::hq_boolean(HqBooleanFields(*b)),
VarVal::String(s) => IrOpcode::hq_text(HqTextFields(s.clone())),
}
}
pub fn var_val_type(var_val: &VarVal) -> HQResult<Type> {
// todo: when can we say that the varval is an int? maybe only at a later point in the compilation process?
var_val_instruction(var_val)
.output_type(Rc::from([]))?
.singleton_or_else(|| make_hq_bug!("got non-singleton output type for const"))
}
#[derive(Debug, Clone, PartialEq)]
pub enum TypeStack {
Nil,
Cons(Type, Rc<Self>),
}
impl TypeStack {
#[must_use]
pub const fn is_nil(&self) -> bool {
matches!(self, Self::Nil)
}
pub fn push_mut(self: &mut Rc<Self>, ty: Type) {
*self = Rc::new(Self::Cons(ty, Rc::clone(self)));
}
pub fn pop_mut(self: &mut Rc<Self>) -> Option<Type> {
match &*Rc::clone(self) {
Self::Nil => None,
Self::Cons(head, tail) => {
*self = Rc::clone(tail);
Some(*head)
}
}
}
/// Drops the top n elements. Returns false if there were less than
/// n elements on the stack, true otherwise.
pub fn drop_mut(self: &mut Rc<Self>, n: usize) -> bool {
let mut i = n;
while i > 0
&& let Some(_) = self.pop_mut()
{
i -= 1;
}
i == 0
}
/// Removes the top n elements and returns them in order of removal
/// (i.e. in reverse order from how they were inserted).
///
/// If there are not n elements on the stack, this does not panic;
/// it simply returns a shorter vec containing all of the elements
/// on the stack.
pub fn take_n(self: &mut Rc<Self>, n: usize) -> Vec<Type> {
let mut ret = vec![];
let mut i = n;
while i > 0
&& let Some(el) = self.pop_mut()
{
i -= 1;
ret.push(el);
}
ret
}
}
impl Iterator for Rc<TypeStack> {
type Item = Type;
fn next(&mut self) -> Option<Self::Item> {
self.pop_mut()
}
}
impl FromIterator<Type> for Rc<TypeStack> {
fn from_iter<T: IntoIterator<Item = Type>>(iter: T) -> Self {
let mut stack = Self::new(TypeStack::Nil);
for ty in iter {
stack.push_mut(ty);
}
stack
}
}