2021-11-03 16:35:16 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2021-11-03 16:34:05 +08:00
|
|
|
pub enum Constant {
|
|
|
|
None,
|
|
|
|
Bool(bool),
|
|
|
|
Str(String),
|
|
|
|
Bytes(Vec<u8>),
|
2021-12-26 00:23:54 +08:00
|
|
|
Int(Option<i64>),
|
2021-11-03 16:34:05 +08:00
|
|
|
Tuple(Vec<Constant>),
|
|
|
|
Float(f64),
|
|
|
|
Complex { real: f64, imag: f64 },
|
|
|
|
Ellipsis,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Constant {
|
|
|
|
fn from(s: String) -> Constant {
|
|
|
|
Self::Str(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<Vec<u8>> for Constant {
|
|
|
|
fn from(b: Vec<u8>) -> Constant {
|
|
|
|
Self::Bytes(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<bool> for Constant {
|
|
|
|
fn from(b: bool) -> Constant {
|
|
|
|
Self::Bool(b)
|
|
|
|
}
|
|
|
|
}
|
2021-12-26 00:23:54 +08:00
|
|
|
impl From<i32> for Constant {
|
|
|
|
fn from(i: i32) -> Constant {
|
|
|
|
Self::Int(Some(i as i64))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<i64> for Constant {
|
|
|
|
fn from(i: i64) -> Constant {
|
|
|
|
Self::Int(Some(i))
|
2021-11-03 16:34:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transforms a value prior to formatting it.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum ConversionFlag {
|
|
|
|
/// Converts by calling `str(<value>)`.
|
|
|
|
Str = b's',
|
|
|
|
/// Converts by calling `ascii(<value>)`.
|
|
|
|
Ascii = b'a',
|
|
|
|
/// Converts by calling `repr(<value>)`.
|
|
|
|
Repr = b'r',
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConversionFlag {
|
|
|
|
pub fn try_from_byte(b: u8) -> Option<Self> {
|
|
|
|
match b {
|
|
|
|
b's' => Some(Self::Str),
|
|
|
|
b'a' => Some(Self::Ascii),
|
|
|
|
b'r' => Some(Self::Repr),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "constant-optimization")]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ConstantOptimizer {
|
|
|
|
_priv: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "constant-optimization")]
|
|
|
|
impl ConstantOptimizer {
|
|
|
|
#[inline]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { _priv: () }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "constant-optimization")]
|
|
|
|
impl<U> crate::fold::Fold<U> for ConstantOptimizer {
|
|
|
|
type TargetU = U;
|
|
|
|
type Error = std::convert::Infallible;
|
|
|
|
#[inline]
|
|
|
|
fn map_user(&mut self, user: U) -> Result<Self::TargetU, Self::Error> {
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
fn fold_expr(&mut self, node: crate::Expr<U>) -> Result<crate::Expr<U>, Self::Error> {
|
|
|
|
match node.node {
|
|
|
|
crate::ExprKind::Tuple { elts, ctx } => {
|
|
|
|
let elts = elts
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| self.fold_expr(x))
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
let expr = if elts
|
|
|
|
.iter()
|
|
|
|
.all(|e| matches!(e.node, crate::ExprKind::Constant { .. }))
|
|
|
|
{
|
|
|
|
let tuple = elts
|
|
|
|
.into_iter()
|
|
|
|
.map(|e| match e.node {
|
|
|
|
crate::ExprKind::Constant { value, .. } => value,
|
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
crate::ExprKind::Constant {
|
|
|
|
value: Constant::Tuple(tuple),
|
|
|
|
kind: None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
crate::ExprKind::Tuple { elts, ctx }
|
|
|
|
};
|
|
|
|
Ok(crate::Expr {
|
|
|
|
node: expr,
|
|
|
|
custom: node.custom,
|
|
|
|
location: node.location,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => crate::fold::fold_expr(self, node),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[cfg(feature = "constant-optimization")]
|
|
|
|
#[test]
|
|
|
|
fn test_constant_opt() {
|
|
|
|
use super::*;
|
|
|
|
use crate::fold::Fold;
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
let location = Location::new(0, 0);
|
|
|
|
let custom = ();
|
|
|
|
let ast = Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Tuple {
|
|
|
|
ctx: ExprContext::Load,
|
|
|
|
elts: vec![
|
|
|
|
Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Constant {
|
2021-12-26 00:23:54 +08:00
|
|
|
value: 1.into(),
|
2021-11-03 16:34:05 +08:00
|
|
|
kind: None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Constant {
|
2021-12-26 00:23:54 +08:00
|
|
|
value: 2.into(),
|
2021-11-03 16:34:05 +08:00
|
|
|
kind: None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Tuple {
|
|
|
|
ctx: ExprContext::Load,
|
|
|
|
elts: vec![
|
|
|
|
Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Constant {
|
2021-12-26 00:23:54 +08:00
|
|
|
value: 3.into(),
|
2021-11-03 16:34:05 +08:00
|
|
|
kind: None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Constant {
|
2021-12-26 00:23:54 +08:00
|
|
|
value: 4.into(),
|
2021-11-03 16:34:05 +08:00
|
|
|
kind: None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Constant {
|
2021-12-26 00:23:54 +08:00
|
|
|
value: 5.into(),
|
2021-11-03 16:34:05 +08:00
|
|
|
kind: None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let new_ast = ConstantOptimizer::new()
|
|
|
|
.fold_expr(ast)
|
|
|
|
.unwrap_or_else(|e| match e {});
|
|
|
|
assert_eq!(
|
|
|
|
new_ast,
|
|
|
|
Located {
|
|
|
|
location,
|
|
|
|
custom,
|
|
|
|
node: ExprKind::Constant {
|
|
|
|
value: Constant::Tuple(vec![
|
2021-12-26 00:23:54 +08:00
|
|
|
1.into(),
|
|
|
|
2.into(),
|
2021-11-03 16:34:05 +08:00
|
|
|
Constant::Tuple(vec![
|
2021-12-26 00:23:54 +08:00
|
|
|
3.into(),
|
|
|
|
4.into(),
|
|
|
|
5.into(),
|
2021-11-03 16:34:05 +08:00
|
|
|
])
|
|
|
|
]),
|
|
|
|
kind: None
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|