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
use super::{BinaryOp, Expr};
/// A binary operation between two expressions.
///
/// Applies an operator to a left-hand side and right-hand side expression.
/// Supported operators include equality, comparison, and type checking.
///
/// # Examples
///
/// ```text
/// eq(a, b) // a == b
/// ne(a, b) // a != b
/// lt(a, b) // a < b
/// gt(a, b) // a > b
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ExprBinaryOp {
/// The left-hand side expression.
pub lhs: Box<Expr>,
/// The operator to apply.
pub op: BinaryOp,
/// The right-hand side expression.
pub rhs: Box<Expr>,
}
impl Expr {
/// Creates a binary operation expression with the given operator.
pub fn binary_op(lhs: impl Into<Self>, op: BinaryOp, rhs: impl Into<Self>) -> Self {
ExprBinaryOp {
op,
lhs: Box::new(lhs.into()),
rhs: Box::new(rhs.into()),
}
.into()
}
/// Creates an equality (`==`) expression.
pub fn eq(lhs: impl Into<Self>, rhs: impl Into<Self>) -> Self {
Expr::binary_op(lhs, BinaryOp::Eq, rhs)
}
/// Returns true if the expression is a binary expression with the equality operator
pub fn is_eq(&self) -> bool {
matches!(
self,
Self::BinaryOp(ExprBinaryOp {
op: BinaryOp::Eq,
..
})
)
}
/// Creates a greater-than-or-equal (`>=`) expression.
pub fn ge(lhs: impl Into<Self>, rhs: impl Into<Self>) -> Self {
Expr::binary_op(lhs, BinaryOp::Ge, rhs)
}
/// Creates a greater-than (`>`) expression.
pub fn gt(lhs: impl Into<Self>, rhs: impl Into<Self>) -> Self {
Expr::binary_op(lhs, BinaryOp::Gt, rhs)
}
/// Creates a less-than-or-equal (`<=`) expression.
pub fn le(lhs: impl Into<Self>, rhs: impl Into<Self>) -> Self {
Expr::binary_op(lhs, BinaryOp::Le, rhs)
}
/// Creates a less-than (`<`) expression.
pub fn lt(lhs: impl Into<Self>, rhs: impl Into<Self>) -> Self {
Expr::binary_op(lhs, BinaryOp::Lt, rhs)
}
/// Creates a not-equal (`!=`) expression.
pub fn ne(lhs: impl Into<Self>, rhs: impl Into<Self>) -> Self {
Expr::binary_op(lhs, BinaryOp::Ne, rhs)
}
}
impl From<ExprBinaryOp> for Expr {
fn from(value: ExprBinaryOp) -> Self {
Self::BinaryOp(value)
}
}