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