use std::fmt::{Display, Formatter, Result};
pub trait Operator {
fn get_assoc(&self) -> bool;
fn get_precedence(&self) -> u64;
fn get_precedence_and_assoc(&self) -> (u64, bool) {
(self.get_precedence(), self.get_assoc())
}
}
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum NumOp {
Add,
Sub,
Div,
Mul,
Mod,
}
impl Display for NumOp {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
NumOp::Add => "+",
NumOp::Sub => "-",
NumOp::Div => "/",
NumOp::Mul => "*",
NumOp::Mod => "%",
}
)
}
}
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum UnaryOp {
IncrementPost,
IncrementPre,
DecrementPost,
DecrementPre,
Minus,
Plus,
Not,
}
impl Display for UnaryOp {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
UnaryOp::IncrementPost | UnaryOp::IncrementPre => "++",
UnaryOp::DecrementPost | UnaryOp::DecrementPre => "--",
UnaryOp::Plus => "+",
UnaryOp::Minus => "-",
UnaryOp::Not => "!",
}
)
}
}
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum BitOp {
And,
Or,
Xor,
Shl,
Shr,
}
impl Display for BitOp {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
BitOp::And => "&",
BitOp::Or => "|",
BitOp::Xor => "^",
BitOp::Shl => "<<",
BitOp::Shr => ">>",
}
)
}
}
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum CompOp {
Equal,
NotEqual,
StrictEqual,
StrictNotEqual,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual,
}
impl Display for CompOp {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
CompOp::Equal => "==",
CompOp::NotEqual => "!=",
CompOp::StrictEqual => "===",
CompOp::StrictNotEqual => "!==",
CompOp::GreaterThan => ">",
CompOp::GreaterThanOrEqual => ">=",
CompOp::LessThan => "<",
CompOp::LessThanOrEqual => "<=",
}
)
}
}
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum LogOp {
And,
Or,
}
impl Display for LogOp {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
LogOp::And => "&&",
LogOp::Or => "||",
}
)
}
}
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum BinOp {
Num(NumOp),
Bit(BitOp),
Comp(CompOp),
Log(LogOp),
}
impl Operator for BinOp {
fn get_assoc(&self) -> bool {
true
}
fn get_precedence(&self) -> u64 {
match *self {
BinOp::Num(NumOp::Mul) | BinOp::Num(NumOp::Div) | BinOp::Num(NumOp::Mod) => 5,
BinOp::Num(NumOp::Add) | BinOp::Num(NumOp::Sub) => 6,
BinOp::Bit(BitOp::Shl) | BinOp::Bit(BitOp::Shr) => 7,
BinOp::Comp(CompOp::LessThan)
| BinOp::Comp(CompOp::LessThanOrEqual)
| BinOp::Comp(CompOp::GreaterThan)
| BinOp::Comp(CompOp::GreaterThanOrEqual) => 8,
BinOp::Comp(CompOp::Equal)
| BinOp::Comp(CompOp::NotEqual)
| BinOp::Comp(CompOp::StrictEqual)
| BinOp::Comp(CompOp::StrictNotEqual) => 9,
BinOp::Bit(BitOp::And) => 10,
BinOp::Bit(BitOp::Xor) => 11,
BinOp::Bit(BitOp::Or) => 12,
BinOp::Log(LogOp::And) => 13,
BinOp::Log(LogOp::Or) => 14,
}
}
}
impl Display for BinOp {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
BinOp::Num(ref op) => op.to_string(),
BinOp::Bit(ref op) => op.to_string(),
BinOp::Comp(ref op) => op.to_string(),
BinOp::Log(ref op) => op.to_string(),
}
)
}
}