Compare commits
2 Commits
0.14.6
...
dcreager/c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7295fafb73 | ||
|
|
cd5732c1b0 |
@@ -18,107 +18,40 @@
|
||||
use crate as ast;
|
||||
use crate::{Expr, Number};
|
||||
use std::borrow::Cow;
|
||||
use std::hash::Hash;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum ComparableBoolOp {
|
||||
And,
|
||||
Or,
|
||||
trait Comparable: Eq + PartialEq + Hash {}
|
||||
|
||||
impl<T> Comparable for T where T: Eq + PartialEq + Hash {}
|
||||
|
||||
struct ComparableIterator<T>(T);
|
||||
|
||||
impl<T> Eq for ComparableIterator<T>
|
||||
where
|
||||
T: Clone + Iterator,
|
||||
T::Item: Eq,
|
||||
{
|
||||
}
|
||||
|
||||
impl From<ast::BoolOp> for ComparableBoolOp {
|
||||
fn from(op: ast::BoolOp) -> Self {
|
||||
match op {
|
||||
ast::BoolOp::And => Self::And,
|
||||
ast::BoolOp::Or => Self::Or,
|
||||
}
|
||||
impl<T> PartialEq for ComparableIterator<T>
|
||||
where
|
||||
T: Clone + Iterator,
|
||||
T::Item: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0.clone().eq(other.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum ComparableOperator {
|
||||
Add,
|
||||
Sub,
|
||||
Mult,
|
||||
MatMult,
|
||||
Div,
|
||||
Mod,
|
||||
Pow,
|
||||
LShift,
|
||||
RShift,
|
||||
BitOr,
|
||||
BitXor,
|
||||
BitAnd,
|
||||
FloorDiv,
|
||||
}
|
||||
|
||||
impl From<ast::Operator> for ComparableOperator {
|
||||
fn from(op: ast::Operator) -> Self {
|
||||
match op {
|
||||
ast::Operator::Add => Self::Add,
|
||||
ast::Operator::Sub => Self::Sub,
|
||||
ast::Operator::Mult => Self::Mult,
|
||||
ast::Operator::MatMult => Self::MatMult,
|
||||
ast::Operator::Div => Self::Div,
|
||||
ast::Operator::Mod => Self::Mod,
|
||||
ast::Operator::Pow => Self::Pow,
|
||||
ast::Operator::LShift => Self::LShift,
|
||||
ast::Operator::RShift => Self::RShift,
|
||||
ast::Operator::BitOr => Self::BitOr,
|
||||
ast::Operator::BitXor => Self::BitXor,
|
||||
ast::Operator::BitAnd => Self::BitAnd,
|
||||
ast::Operator::FloorDiv => Self::FloorDiv,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum ComparableUnaryOp {
|
||||
Invert,
|
||||
Not,
|
||||
UAdd,
|
||||
USub,
|
||||
}
|
||||
|
||||
impl From<ast::UnaryOp> for ComparableUnaryOp {
|
||||
fn from(op: ast::UnaryOp) -> Self {
|
||||
match op {
|
||||
ast::UnaryOp::Invert => Self::Invert,
|
||||
ast::UnaryOp::Not => Self::Not,
|
||||
ast::UnaryOp::UAdd => Self::UAdd,
|
||||
ast::UnaryOp::USub => Self::USub,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum ComparableCmpOp {
|
||||
Eq,
|
||||
NotEq,
|
||||
Lt,
|
||||
LtE,
|
||||
Gt,
|
||||
GtE,
|
||||
Is,
|
||||
IsNot,
|
||||
In,
|
||||
NotIn,
|
||||
}
|
||||
|
||||
impl From<ast::CmpOp> for ComparableCmpOp {
|
||||
fn from(op: ast::CmpOp) -> Self {
|
||||
match op {
|
||||
ast::CmpOp::Eq => Self::Eq,
|
||||
ast::CmpOp::NotEq => Self::NotEq,
|
||||
ast::CmpOp::Lt => Self::Lt,
|
||||
ast::CmpOp::LtE => Self::LtE,
|
||||
ast::CmpOp::Gt => Self::Gt,
|
||||
ast::CmpOp::GtE => Self::GtE,
|
||||
ast::CmpOp::Is => Self::Is,
|
||||
ast::CmpOp::IsNot => Self::IsNot,
|
||||
ast::CmpOp::In => Self::In,
|
||||
ast::CmpOp::NotIn => Self::NotIn,
|
||||
}
|
||||
impl<T> Hash for ComparableIterator<T>
|
||||
where
|
||||
T: Clone + ExactSizeIterator,
|
||||
T::Item: Hash,
|
||||
{
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
let iter = self.0.clone();
|
||||
state.write_usize(iter.len());
|
||||
iter.for_each(|elem| elem.hash(state));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +61,12 @@ pub struct ComparableAlias<'a> {
|
||||
asname: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl ast::Alias {
|
||||
fn comparable(&self) -> impl Comparable + '_ {
|
||||
(self.name.as_str(), self.asname.as_deref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::Alias> for ComparableAlias<'a> {
|
||||
fn from(alias: &'a ast::Alias) -> Self {
|
||||
Self {
|
||||
@@ -719,7 +658,7 @@ impl<'a> From<&'a ast::BytesLiteral> for ComparableBytesLiteral<'a> {
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprBoolOp<'a> {
|
||||
op: ComparableBoolOp,
|
||||
op: ast::BoolOp,
|
||||
values: Vec<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
@@ -732,13 +671,13 @@ pub struct ExprNamed<'a> {
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprBinOp<'a> {
|
||||
left: Box<ComparableExpr<'a>>,
|
||||
op: ComparableOperator,
|
||||
op: ast::Operator,
|
||||
right: Box<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprUnaryOp<'a> {
|
||||
op: ComparableUnaryOp,
|
||||
op: ast::UnaryOp,
|
||||
operand: Box<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
@@ -823,7 +762,7 @@ pub struct ExprYieldFrom<'a> {
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprCompare<'a> {
|
||||
left: Box<ComparableExpr<'a>>,
|
||||
ops: Vec<ComparableCmpOp>,
|
||||
ops: Vec<ast::CmpOp>,
|
||||
comparators: Vec<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
@@ -968,7 +907,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
||||
values,
|
||||
range: _,
|
||||
}) => Self::BoolOp(ExprBoolOp {
|
||||
op: (*op).into(),
|
||||
op: *op,
|
||||
values: values.iter().map(Into::into).collect(),
|
||||
}),
|
||||
ast::Expr::Named(ast::ExprNamed {
|
||||
@@ -986,7 +925,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
||||
range: _,
|
||||
}) => Self::BinOp(ExprBinOp {
|
||||
left: left.into(),
|
||||
op: (*op).into(),
|
||||
op: *op,
|
||||
right: right.into(),
|
||||
}),
|
||||
ast::Expr::UnaryOp(ast::ExprUnaryOp {
|
||||
@@ -994,7 +933,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
||||
operand,
|
||||
range: _,
|
||||
}) => Self::UnaryOp(ExprUnaryOp {
|
||||
op: (*op).into(),
|
||||
op: *op,
|
||||
operand: operand.into(),
|
||||
}),
|
||||
ast::Expr::Lambda(ast::ExprLambda {
|
||||
@@ -1306,7 +1245,7 @@ pub struct StmtAssign<'a> {
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtAugAssign<'a> {
|
||||
target: ComparableExpr<'a>,
|
||||
op: ComparableOperator,
|
||||
op: ast::Operator,
|
||||
value: ComparableExpr<'a>,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user