2016-08-23 12:39:36 -05:00
|
|
|
//! Utility functions about comparison operators.
|
|
|
|
|
|
|
|
#![deny(missing_docs_in_private_items)]
|
|
|
|
|
2016-04-07 10:46:48 -05:00
|
|
|
use rustc::hir::{BinOp_, Expr};
|
2016-03-26 00:57:03 -05:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
2016-08-23 12:39:36 -05:00
|
|
|
/// Represent a normalized comparison operator.
|
2016-03-26 00:57:03 -05:00
|
|
|
pub enum Rel {
|
2016-08-23 12:39:36 -05:00
|
|
|
/// `<`
|
2016-03-26 00:57:03 -05:00
|
|
|
Lt,
|
2016-08-23 12:39:36 -05:00
|
|
|
/// `<=`
|
2016-03-26 00:57:03 -05:00
|
|
|
Le,
|
2016-08-23 12:39:36 -05:00
|
|
|
/// `==`
|
2016-03-29 00:06:57 -05:00
|
|
|
Eq,
|
2016-08-23 12:39:36 -05:00
|
|
|
/// `!=`
|
2016-03-29 00:06:57 -05:00
|
|
|
Ne,
|
2016-03-26 00:57:03 -05:00
|
|
|
}
|
|
|
|
|
2016-08-23 12:39:36 -05:00
|
|
|
/// Put the expression in the form `lhs < rhs`, `lhs <= rhs`, `lhs == rhs` or `lhs != rhs`.
|
2016-12-20 11:21:30 -06:00
|
|
|
pub fn normalize_comparison<'a>(op: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(Rel, &'a Expr, &'a Expr)> {
|
2016-03-26 00:57:03 -05:00
|
|
|
match op {
|
|
|
|
BinOp_::BiLt => Some((Rel::Lt, lhs, rhs)),
|
|
|
|
BinOp_::BiLe => Some((Rel::Le, lhs, rhs)),
|
|
|
|
BinOp_::BiGt => Some((Rel::Lt, rhs, lhs)),
|
|
|
|
BinOp_::BiGe => Some((Rel::Le, rhs, lhs)),
|
2016-03-29 00:06:57 -05:00
|
|
|
BinOp_::BiEq => Some((Rel::Eq, rhs, lhs)),
|
|
|
|
BinOp_::BiNe => Some((Rel::Ne, rhs, lhs)),
|
2016-03-26 00:57:03 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|