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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum OpPriority {
Or,
And,
Comparison,
AddOrSub,
MulOrDiv,
Power,
Negation,
Call,
}
impl OpPriority {
pub const fn max_priority() -> Self {
Self::Call
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum UnaryOp {
Neg,
Not,
}
impl fmt::Display for UnaryOp {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UnaryOp::Neg => formatter.write_str("negation"),
UnaryOp::Not => formatter.write_str("logical negation"),
}
}
}
impl UnaryOp {
pub fn priority(self) -> OpPriority {
match self {
Self::Neg | Self::Not => OpPriority::Negation,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BinaryOp {
Add,
Sub,
Mul,
Div,
Power,
Eq,
NotEq,
And,
Or,
Gt,
Lt,
Ge,
Le,
}
impl fmt::Display for BinaryOp {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::Add => "addition",
Self::Sub => "subtraction",
Self::Mul => "multiplication",
Self::Div => "division",
Self::Power => "exponentiation",
Self::Eq => "equality comparison",
Self::NotEq => "non-equality comparison",
Self::And => "AND",
Self::Or => "OR",
Self::Gt => "greater comparison",
Self::Lt => "lesser comparison",
Self::Ge => "greater-or-equal comparison",
Self::Le => "lesser-or-equal comparison",
})
}
}
impl BinaryOp {
pub fn as_str(self) -> &'static str {
match self {
Self::Add => "+",
Self::Sub => "-",
Self::Mul => "*",
Self::Div => "/",
Self::Power => "^",
Self::Eq => "==",
Self::NotEq => "!=",
Self::And => "&&",
Self::Or => "||",
Self::Gt => ">",
Self::Lt => "<",
Self::Ge => ">=",
Self::Le => "<=",
}
}
pub fn priority(self) -> OpPriority {
match self {
Self::Or => OpPriority::Or,
Self::And => OpPriority::And,
Self::Eq | Self::NotEq | Self::Gt | Self::Lt | Self::Le | Self::Ge => {
OpPriority::Comparison
}
Self::Add | Self::Sub => OpPriority::AddOrSub,
Self::Mul | Self::Div => OpPriority::MulOrDiv,
Self::Power => OpPriority::Power,
}
}
pub fn is_arithmetic(self) -> bool {
matches!(
self,
Self::Add | Self::Sub | Self::Mul | Self::Div | Self::Power
)
}
pub fn is_comparison(self) -> bool {
matches!(
self,
Self::Eq | Self::NotEq | Self::Gt | Self::Lt | Self::Le | Self::Ge
)
}
pub fn is_order_comparison(self) -> bool {
matches!(self, Self::Gt | Self::Lt | Self::Le | Self::Ge)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Op {
Unary(UnaryOp),
Binary(BinaryOp),
}
impl fmt::Display for Op {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unary(inner) => fmt::Display::fmt(inner, formatter),
Self::Binary(inner) => fmt::Display::fmt(inner, formatter),
}
}
}
impl From<UnaryOp> for Op {
fn from(value: UnaryOp) -> Self {
Self::Unary(value)
}
}
impl From<BinaryOp> for Op {
fn from(value: BinaryOp) -> Self {
Self::Binary(value)
}
}