This repository was archived by the owner on Jul 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathoptimizer.rs
More file actions
131 lines (124 loc) · 4.03 KB
/
Copy pathoptimizer.rs
File metadata and controls
131 lines (124 loc) · 4.03 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
use crate::builtin::Constant;
#[non_exhaustive]
#[derive(Default)]
pub struct ConstantOptimizer {}
impl ConstantOptimizer {
#[inline]
pub fn new() -> Self {
Self {}
}
}
#[cfg(feature = "constant-optimization")]
impl<U> crate::fold::Fold<U> for ConstantOptimizer {
type TargetU = U;
type Error = std::convert::Infallible;
type UserContext = ();
#[inline(always)]
fn will_map_user(&mut self, _user: &U) -> Self::UserContext {}
#[inline]
fn map_user(&mut self, user: U, _context: ()) -> Result<Self::TargetU, Self::Error> {
Ok(user)
}
fn fold_expr(&mut self, node: crate::Expr<U>) -> Result<crate::Expr<U>, Self::Error> {
match node {
crate::Expr::Tuple(crate::ExprTuple { elts, ctx, range }) => {
let elts = elts
.into_iter()
.map(|x| self.fold_expr(x))
.collect::<Result<Vec<_>, _>>()?;
let expr = if elts.iter().all(|e| e.is_constant_expr()) {
let tuple = elts
.into_iter()
.map(|e| match e {
crate::Expr::Constant(crate::ExprConstant { value, .. }) => value,
_ => unreachable!(),
})
.collect();
crate::Expr::Constant(crate::ExprConstant {
value: Constant::Tuple(tuple),
kind: None,
range,
})
} else {
crate::Expr::Tuple(crate::ExprTuple { elts, ctx, range })
};
Ok(expr)
}
_ => crate::fold::fold_expr(self, node),
}
}
}
#[cfg(test)]
mod tests {
use num_bigint::BigInt;
use rustpython_parser_core::text_size::TextRange;
#[cfg(feature = "constant-optimization")]
#[test]
fn test_constant_opt() {
use crate::{fold::Fold, *};
let range = TextRange::default();
let ast = ExprTuple {
ctx: ExprContext::Load,
elts: vec![
ExprConstant {
value: BigInt::from(1).into(),
kind: None,
range,
}
.into(),
ExprConstant {
value: BigInt::from(2).into(),
kind: None,
range,
}
.into(),
ExprTuple {
ctx: ExprContext::Load,
elts: vec![
ExprConstant {
value: BigInt::from(3).into(),
kind: None,
range,
}
.into(),
ExprConstant {
value: BigInt::from(4).into(),
kind: None,
range,
}
.into(),
ExprConstant {
value: BigInt::from(5).into(),
kind: None,
range,
}
.into(),
],
range,
}
.into(),
],
range,
};
let new_ast = ConstantOptimizer::new()
.fold_expr(ast.into())
.unwrap_or_else(|e| match e {});
assert_eq!(
new_ast,
ExprConstant {
value: Constant::Tuple(vec![
BigInt::from(1).into(),
BigInt::from(2).into(),
Constant::Tuple(vec![
BigInt::from(3).into(),
BigInt::from(4).into(),
BigInt::from(5).into(),
])
]),
kind: None,
range,
}
.into(),
);
}
}