use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryOp {
Add, Sub, Mul, Div, Mod, Eq, Ne, Lt, Lte, Gt, Gte, And, Or, }
impl BinaryOp {
pub fn precedence(self) -> u8 {
match self {
BinaryOp::Or => 1,
BinaryOp::And => 2,
BinaryOp::Eq | BinaryOp::Ne => 3,
BinaryOp::Lt | BinaryOp::Lte | BinaryOp::Gt | BinaryOp::Gte => 4,
BinaryOp::Add | BinaryOp::Sub => 5,
BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => 6,
}
}
}
impl fmt::Display for BinaryOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BinaryOp::Add => write!(f, "+"),
BinaryOp::Sub => write!(f, "-"),
BinaryOp::Mul => write!(f, "*"),
BinaryOp::Div => write!(f, "/"),
BinaryOp::Mod => write!(f, "%"),
BinaryOp::Eq => write!(f, "=="),
BinaryOp::Ne => write!(f, "!="),
BinaryOp::Lt => write!(f, "<"),
BinaryOp::Lte => write!(f, "<="),
BinaryOp::Gt => write!(f, ">"),
BinaryOp::Gte => write!(f, ">="),
BinaryOp::And => write!(f, "and"),
BinaryOp::Or => write!(f, "or"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnaryOp {
Not, Neg, Pos, }
impl fmt::Display for UnaryOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UnaryOp::Not => write!(f, "not"),
UnaryOp::Neg => write!(f, "-"),
UnaryOp::Pos => write!(f, "+"),
}
}
}