datex_core/global/operators/
binary.rs1use core::fmt::Display;
2
3use crate::global::{
4 instruction_codes::InstructionCode,
5 protocol_structures::instructions::RegularInstruction,
6};
7
8use crate::prelude::*;
9#[derive(Clone, Debug, PartialEq, Copy)]
10pub enum BinaryOperator {
11 Arithmetic(ArithmeticOperator),
12 Logical(LogicalOperator),
13 Bitwise(BitwiseOperator),
14 Range(RangeOperator),
15}
16impl From<ArithmeticOperator> for BinaryOperator {
17 fn from(op: ArithmeticOperator) -> Self {
18 BinaryOperator::Arithmetic(op)
19 }
20}
21impl From<LogicalOperator> for BinaryOperator {
22 fn from(op: LogicalOperator) -> Self {
23 BinaryOperator::Logical(op)
24 }
25}
26impl From<BitwiseOperator> for BinaryOperator {
27 fn from(op: BitwiseOperator) -> Self {
28 BinaryOperator::Bitwise(op)
29 }
30}
31
32#[derive(Clone, Debug, PartialEq, Copy, Eq, Hash)]
33pub enum ArithmeticOperator {
34 Add, Subtract, Multiply, Divide, Modulo, Power, }
41impl Display for ArithmeticOperator {
42 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43 core::write!(
44 f,
45 "{}",
46 match self {
47 ArithmeticOperator::Add => "+",
48 ArithmeticOperator::Subtract => "-",
49 ArithmeticOperator::Multiply => "*",
50 ArithmeticOperator::Divide => "/",
51 ArithmeticOperator::Modulo => "%",
52 ArithmeticOperator::Power => "^",
53 }
54 )
55 }
56}
57impl From<&ArithmeticOperator> for InstructionCode {
58 fn from(op: &ArithmeticOperator) -> Self {
59 match op {
60 ArithmeticOperator::Add => InstructionCode::ADD,
61 ArithmeticOperator::Subtract => InstructionCode::SUBTRACT,
62 ArithmeticOperator::Multiply => InstructionCode::MULTIPLY,
63 ArithmeticOperator::Divide => InstructionCode::DIVIDE,
64 ArithmeticOperator::Modulo => InstructionCode::MODULO,
65 ArithmeticOperator::Power => InstructionCode::POWER,
66 }
67 }
68}
69
70#[derive(Clone, Debug, PartialEq, Copy)]
71pub enum LogicalOperator {
72 And, Or, }
75
76impl From<&LogicalOperator> for InstructionCode {
77 fn from(op: &LogicalOperator) -> Self {
78 match op {
79 LogicalOperator::And => InstructionCode::AND,
80 LogicalOperator::Or => InstructionCode::OR,
81 }
82 }
83}
84
85impl Display for LogicalOperator {
86 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
87 core::write!(
88 f,
89 "{}",
90 match self {
91 LogicalOperator::And => "and",
92 LogicalOperator::Or => "or",
93 }
94 )
95 }
96}
97
98#[derive(Clone, Debug, PartialEq, Copy)]
99pub enum BitwiseOperator {
100 And, Or, Xor, Not, }
105
106#[derive(Clone, Debug, PartialEq, Copy)]
107pub enum RangeOperator {
108 Exclusive, Inclusive, }
111
112impl From<&RangeOperator> for InstructionCode {
113 fn from(op: &RangeOperator) -> Self {
114 match op {
115 RangeOperator::Inclusive => InstructionCode::RANGE,
116 _ => {
117 core::todo!(
118 "Bitwise operator {:?} not implemented for InstructionCode",
119 op
120 )
121 }
122 }
123 }
124}
125
126impl Display for RangeOperator {
127 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
128 core::write!(
129 f,
130 "{}",
131 match self {
132 RangeOperator::Exclusive => "..",
133 RangeOperator::Inclusive => "..=",
134 }
135 )
136 }
137}
138
139impl From<&BitwiseOperator> for InstructionCode {
140 fn from(op: &BitwiseOperator) -> Self {
141 match op {
142 BitwiseOperator::And => InstructionCode::AND,
143 BitwiseOperator::Or => InstructionCode::OR,
144 BitwiseOperator::Not => InstructionCode::NOT,
145 _ => {
146 core::todo!(
147 "Bitwise operator {:?} not implemented for InstructionCode",
148 op
149 )
150 }
151 }
152 }
153}
154
155impl Display for BitwiseOperator {
156 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
157 core::write!(
158 f,
159 "{}",
160 match self {
161 BitwiseOperator::And => "&",
162 BitwiseOperator::Or => "|",
163 BitwiseOperator::Xor => "^",
164 BitwiseOperator::Not => "~",
165 }
166 )
167 }
168}
169
170impl Display for BinaryOperator {
171 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
172 core::write!(
173 f,
174 "{}",
175 match self {
176 BinaryOperator::Arithmetic(op) => op.to_string(),
177 BinaryOperator::Logical(op) => op.to_string(),
178 BinaryOperator::Bitwise(op) => op.to_string(),
179 BinaryOperator::Range(op) => op.to_string(),
180 }
181 )
182 }
183}
184
185impl From<&BinaryOperator> for InstructionCode {
186 fn from(op: &BinaryOperator) -> Self {
187 match op {
188 BinaryOperator::Arithmetic(op) => InstructionCode::from(op),
189 BinaryOperator::Logical(op) => InstructionCode::from(op),
190 BinaryOperator::Bitwise(op) => InstructionCode::from(op),
191 BinaryOperator::Range(op) => InstructionCode::from(op),
192 }
193 }
194}
195
196impl From<BinaryOperator> for InstructionCode {
197 fn from(op: BinaryOperator) -> Self {
198 InstructionCode::from(&op)
199 }
200}
201
202impl From<&InstructionCode> for BinaryOperator {
203 fn from(code: &InstructionCode) -> Self {
204 match code {
205 InstructionCode::ADD => {
206 BinaryOperator::Arithmetic(ArithmeticOperator::Add)
207 }
208 InstructionCode::SUBTRACT => {
209 BinaryOperator::Arithmetic(ArithmeticOperator::Subtract)
210 }
211 InstructionCode::MULTIPLY => {
212 BinaryOperator::Arithmetic(ArithmeticOperator::Multiply)
213 }
214 InstructionCode::DIVIDE => {
215 BinaryOperator::Arithmetic(ArithmeticOperator::Divide)
216 }
217 InstructionCode::MODULO => {
218 BinaryOperator::Arithmetic(ArithmeticOperator::Modulo)
219 }
220 InstructionCode::POWER => {
221 BinaryOperator::Arithmetic(ArithmeticOperator::Power)
222 }
223 InstructionCode::AND => {
224 BinaryOperator::Logical(LogicalOperator::And)
225 }
226 InstructionCode::OR => BinaryOperator::Logical(LogicalOperator::Or),
227 InstructionCode::UNION => {
228 BinaryOperator::Bitwise(BitwiseOperator::And)
229 }
230 InstructionCode::RANGE => {
231 BinaryOperator::Range(RangeOperator::Inclusive)
232 }
233 _ => core::todo!(
234 "#154 Binary operator for {:?} not implemented",
235 code
236 ),
237 }
238 }
239}
240
241impl From<InstructionCode> for BinaryOperator {
242 fn from(code: InstructionCode) -> Self {
243 BinaryOperator::from(&code)
244 }
245}
246
247impl From<&RegularInstruction> for BinaryOperator {
248 fn from(instruction: &RegularInstruction) -> Self {
249 match instruction {
250 RegularInstruction::Add => {
251 BinaryOperator::Arithmetic(ArithmeticOperator::Add)
252 }
253 RegularInstruction::Subtract => {
254 BinaryOperator::Arithmetic(ArithmeticOperator::Subtract)
255 }
256 RegularInstruction::Multiply => {
257 BinaryOperator::Arithmetic(ArithmeticOperator::Multiply)
258 }
259 RegularInstruction::Divide => {
260 BinaryOperator::Arithmetic(ArithmeticOperator::Divide)
261 }
262 RegularInstruction::Range => {
263 BinaryOperator::Range(RangeOperator::Inclusive)
264 }
265
266 _ => {
267 core::todo!(
268 "#155 Binary operator for instruction {:?} not implemented",
269 instruction
270 );
271 }
272 }
273 }
274}
275
276impl From<RegularInstruction> for BinaryOperator {
277 fn from(instruction: RegularInstruction) -> Self {
278 BinaryOperator::from(&instruction)
279 }
280}