#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Concat,
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
And,
Or,
}
impl BinOp {
pub fn precedence(self) -> u8 {
match self {
BinOp::Or => 10,
BinOp::And => 20,
BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge => 30,
BinOp::Concat => 40,
BinOp::Add | BinOp::Sub => 50,
BinOp::Mul | BinOp::Div | BinOp::Mod => 60,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn precedence_groups_match_parser_contract() {
assert_eq!(BinOp::Or.precedence(), 10);
assert_eq!(BinOp::And.precedence(), 20);
for op in [
BinOp::Eq,
BinOp::Ne,
BinOp::Lt,
BinOp::Le,
BinOp::Gt,
BinOp::Ge,
] {
assert_eq!(op.precedence(), 30, "{op:?}");
}
assert_eq!(BinOp::Concat.precedence(), 40);
for op in [BinOp::Add, BinOp::Sub] {
assert_eq!(op.precedence(), 50, "{op:?}");
}
for op in [BinOp::Mul, BinOp::Div, BinOp::Mod] {
assert_eq!(op.precedence(), 60, "{op:?}");
}
}
}