forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_expr.rs
275 lines (249 loc) · 8.4 KB
/
type_expr.rs
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
//! Support for "type expressions", used in evaluating dynamic/generic return
//! types.
use std::collections::HashMap;
use dm::ast::*;
use dm::constants::Constant;
use dm::objtree::{ObjectTree, ProcRef};
use dm::{DMError, Location};
use crate::{Analysis, StaticType};
pub struct TypeExprContext<'o, 't> {
pub objtree: &'o ObjectTree,
pub param_name_map: HashMap<&'t str, Analysis<'o>>,
pub param_idx_map: HashMap<usize, Analysis<'o>>,
}
impl<'o, 't> TypeExprContext<'o, 't> {
fn get(&self, name: &str, idx: usize) -> Option<&Analysis<'o>> {
if let Some(a) = self.param_name_map.get(name) {
Some(a)
} else if let Some(a) = self.param_idx_map.get(&idx) {
Some(a)
} else {
None
}
}
}
#[derive(Debug, Clone)]
pub enum TypeExpr<'o> {
// Static type literal (`null` => `None` included).
Static(StaticType<'o>),
// The value of a parameter, if it is a typepath.
ParamTypepath {
name: String,
p_idx: usize,
// Number of /list to strip.
index_ct: usize,
},
// The static type of a parameter.
ParamStaticType {
name: String,
p_idx: usize,
// Number of /list to strip.
index_ct: usize,
},
// from `&&`, `||`, and `?:`
Condition {
cond: Box<TypeExpr<'o>>,
if_: Box<TypeExpr<'o>>,
else_: Box<TypeExpr<'o>>,
},
}
impl<'o> TypeExpr<'o> {
pub fn compile(
proc: ProcRef<'o>,
location: Location,
expression: &Expression,
) -> Result<TypeExpr<'o>, DMError> {
TypeExprCompiler {
objtree: proc.tree(),
proc,
}
.visit_expression(location, expression)
}
pub fn evaluate(
&self,
location: Location,
ec: &TypeExprContext<'o, '_>,
) -> Result<StaticType<'o>, DMError> {
match self {
TypeExpr::Static(st) => Ok(st.clone()),
TypeExpr::Condition { cond, if_, else_ } => {
if cond.evaluate(location, ec)?.is_truthy() {
if_.evaluate(location, ec)
} else {
else_.evaluate(location, ec)
}
}
TypeExpr::ParamTypepath {
name,
p_idx,
index_ct: _,
} => {
if let Some(analysis) = ec.get(name, *p_idx) {
if let Some(Constant::Prefab(ref pop)) = analysis.value {
crate::static_type(ec.objtree, location, &pop.path)
} else {
Ok(StaticType::None)
}
} else {
Ok(StaticType::None)
}
}
TypeExpr::ParamStaticType {
name,
p_idx,
index_ct,
} => {
if let Some(analysis) = ec.get(name, *p_idx) {
Ok(analysis.static_ty.clone().strip_lists(*index_ct))
} else {
Ok(StaticType::None)
}
}
}
}
}
impl<'o> From<StaticType<'o>> for TypeExpr<'o> {
fn from(static_type: StaticType<'o>) -> TypeExpr<'o> {
TypeExpr::Static(static_type)
}
}
struct TypeExprCompiler<'o> {
objtree: &'o ObjectTree,
proc: ProcRef<'o>,
}
impl<'o> TypeExprCompiler<'o> {
fn visit_expression(
&mut self,
location: Location,
expr: &Expression,
) -> Result<TypeExpr<'o>, DMError> {
match expr {
Expression::Base {
unary,
term,
follow,
} => {
if let Some(op) = unary.first() {
return Err(DMError::new(
location,
format!("type expr: bad unary {}", op.name()),
));
}
let mut ty = self.visit_term(term.location, &term.elem)?;
for each in follow.iter() {
ty = self.visit_follow(each.location, ty, &each.elem)?;
}
Ok(ty)
}
Expression::BinaryOp {
op: BinaryOp::Or,
lhs,
rhs,
} => {
// `A || B` => `A ? A : B`
let lty = self.visit_expression(location, lhs)?;
let rty = self.visit_expression(location, rhs)?;
Ok(TypeExpr::Condition {
cond: Box::new(lty.clone()),
if_: Box::new(lty),
else_: Box::new(rty),
})
}
Expression::BinaryOp {
op: BinaryOp::And,
lhs,
rhs,
} => {
// `A && B` => `A ? B : A`
let lty = self.visit_expression(location, lhs)?;
let rty = self.visit_expression(location, rhs)?;
Ok(TypeExpr::Condition {
cond: Box::new(lty.clone()),
if_: Box::new(rty),
else_: Box::new(lty),
})
}
Expression::TernaryOp { cond, if_, else_ } => Ok(TypeExpr::Condition {
cond: Box::new(self.visit_expression(location, cond)?),
if_: Box::new(self.visit_expression(location, if_)?),
else_: Box::new(self.visit_expression(location, else_)?),
}),
_ => Err(DMError::new(location, "type expr: bad expression node")),
}
}
fn visit_term(&mut self, location: Location, term: &Term) -> Result<TypeExpr<'o>, DMError> {
match term {
Term::Null => Ok(TypeExpr::from(StaticType::None)),
Term::Ident(unscoped_name) => {
for (i, param) in self.proc.parameters.iter().enumerate() {
if *unscoped_name == param.name {
return Ok(TypeExpr::ParamTypepath {
name: unscoped_name.to_owned(),
p_idx: i,
index_ct: 0,
});
}
}
Err(DMError::new(
location,
format!("type expr: no such parameter {:?}", unscoped_name),
))
}
Term::Expr(expr) => self.visit_expression(location, expr),
Term::Prefab(fab) => {
let bits: Vec<_> = fab.path.iter().map(|(_, name)| name.to_owned()).collect();
let ty = crate::static_type(self.objtree, location, &bits)?;
Ok(TypeExpr::from(ty))
}
_ => Err(DMError::new(location, "type expr: bad term node")),
}
}
fn visit_follow(
&mut self,
location: Location,
lhs: TypeExpr<'o>,
rhs: &Follow,
) -> Result<TypeExpr<'o>, DMError> {
match rhs {
// X[_] => static type of argument X with one /list stripped
Follow::Index(_, expr) => match expr.as_term() {
Some(Term::Ident(name)) if name == "_" => match lhs {
TypeExpr::ParamTypepath {
name,
p_idx,
index_ct,
} => Ok(TypeExpr::ParamTypepath {
name,
p_idx,
index_ct: index_ct + 1,
}),
_ => Err(DMError::new(
location,
"type expr: cannot index non-parameters",
)),
},
_ => Err(DMError::new(
location,
"type expr: cannot index by anything but `_`",
)),
},
// X.type => static type of argument X
Follow::Field(_, name) if name == "type" => match lhs {
TypeExpr::ParamTypepath {
name,
p_idx,
index_ct,
} => Ok(TypeExpr::ParamStaticType {
name,
p_idx,
index_ct,
}),
_ => Err(DMError::new(
location,
"type expr: cannot take .type of non-parameters",
)),
},
_ => Err(DMError::new(location, "type expr: bad follow node")),
}
}
}