sphinx/parser/
operator.rs1use core::fmt;
2
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum UnaryOp {
8 Neg, Pos, Inv, Not,
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum BinaryOp {
15 Mul, Div, Mod,
17
18 Add, Sub,
20
21 LShift, RShift,
23
24 BitAnd,
26
27 BitXor,
29
30 BitOr,
32
33 LT, GT, LE, GE,
35
36 EQ, NE,
38
39 And,
41
42 Or,
44}
45
46pub type Precedence = u8;
47pub const PRECEDENCE_END: Precedence = 0; pub const PRECEDENCE_START: Precedence = 10; impl BinaryOp {
51
52 pub const fn precedence_level(&self) -> Precedence {
53 match self {
54 BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => 1,
55
56 BinaryOp::Add | BinaryOp::Sub => 2,
57
58 BinaryOp::LShift | BinaryOp::RShift => 3,
59
60 BinaryOp::BitAnd => 4,
61 BinaryOp::BitXor => 5,
62 BinaryOp::BitOr => 6,
63
64 BinaryOp::LT | BinaryOp::GT | BinaryOp::LE | BinaryOp::GE => 7,
65 BinaryOp::EQ | BinaryOp::NE => 8,
66
67 BinaryOp::And => 9,
68 BinaryOp::Or => 10,
69 }
70 }
71}
72
73impl fmt::Display for BinaryOp {
74 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
75 let symbol = match self {
76 BinaryOp::Mul => "*",
77 BinaryOp::Div => "/",
78 BinaryOp::Mod => "%",
79 BinaryOp::Add => "+",
80 BinaryOp::Sub => "-",
81 BinaryOp::LShift => "<<",
82 BinaryOp::RShift => ">>",
83 BinaryOp::BitAnd => "&",
84 BinaryOp::BitXor => "^",
85 BinaryOp::BitOr => "|",
86 BinaryOp::LT => "<",
87 BinaryOp::GT => ">",
88 BinaryOp::LE => "<=",
89 BinaryOp::GE => ">=",
90 BinaryOp::EQ => "==",
91 BinaryOp::NE => "!=",
92 BinaryOp::And => "and",
93 BinaryOp::Or => "or",
94 };
95 fmt.write_str(symbol)
96 }
97}