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