datex_core/global/operators/
unary.rs1use crate::global::instruction_codes::InstructionCode;
2use crate::global::protocol_structures::instructions::RegularInstruction;
3use core::fmt::{Display, Formatter};
4use core::prelude::rust_2024::*;
5
6#[derive(Clone, Debug, PartialEq, Copy, Eq)]
7pub enum UnaryOperator {
8 Reference(ReferenceUnaryOperator),
9 Arithmetic(ArithmeticUnaryOperator),
10 Bitwise(BitwiseUnaryOperator),
11 Logical(LogicalUnaryOperator),
12}
13
14impl From<&UnaryOperator> for InstructionCode {
15 fn from(op: &UnaryOperator) -> Self {
16 match op {
17 UnaryOperator::Arithmetic(op) => InstructionCode::from(op),
18 UnaryOperator::Reference(op) => InstructionCode::from(op),
19 UnaryOperator::Logical(op) => InstructionCode::from(op),
20 UnaryOperator::Bitwise(op) => InstructionCode::from(op),
21 }
22 }
23}
24
25impl From<&RegularInstruction> for UnaryOperator {
26 fn from(instruction: &RegularInstruction) -> Self {
27 match instruction {
28 RegularInstruction::UnaryPlus => {
29 UnaryOperator::Arithmetic(ArithmeticUnaryOperator::Plus)
30 }
31 RegularInstruction::UnaryMinus => {
32 UnaryOperator::Arithmetic(ArithmeticUnaryOperator::Minus)
33 }
34 RegularInstruction::BitwiseNot => {
35 UnaryOperator::Bitwise(BitwiseUnaryOperator::Not)
36 }
37 RegularInstruction::CreateRef => {
38 UnaryOperator::Reference(ReferenceUnaryOperator::CreateRef)
39 }
40 RegularInstruction::CreateRefMut => {
41 UnaryOperator::Reference(ReferenceUnaryOperator::CreateRefMut)
42 }
43 RegularInstruction::Deref => {
44 UnaryOperator::Reference(ReferenceUnaryOperator::Deref)
45 }
46 _ => {
47 core::todo!(
48 "Unary operator for instruction {:?} not implemented",
49 instruction
50 );
51 }
52 }
53 }
54}
55
56impl From<RegularInstruction> for UnaryOperator {
57 fn from(instruction: RegularInstruction) -> Self {
58 UnaryOperator::from(&instruction)
59 }
60}
61
62impl Display for UnaryOperator {
63 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
64 match self {
65 UnaryOperator::Reference(op) => core::write!(f, "{}", op),
66 UnaryOperator::Arithmetic(op) => core::write!(f, "{}", op),
67 UnaryOperator::Bitwise(op) => core::write!(f, "{}", op),
68 UnaryOperator::Logical(op) => core::write!(f, "{}", op),
69 }
70 }
71}
72
73#[derive(Clone, Debug, PartialEq, Copy, Eq)]
74pub enum ReferenceUnaryOperator {
75 CreateRef, CreateRefMut, Deref, }
79
80impl From<&ReferenceUnaryOperator> for InstructionCode {
81 fn from(op: &ReferenceUnaryOperator) -> Self {
82 match op {
83 ReferenceUnaryOperator::CreateRef => InstructionCode::CREATE_REF,
84 ReferenceUnaryOperator::CreateRefMut => {
85 InstructionCode::CREATE_REF_MUT
86 }
87 ReferenceUnaryOperator::Deref => InstructionCode::DEREF,
88 }
89 }
90}
91
92impl Display for ReferenceUnaryOperator {
93 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
94 match self {
95 ReferenceUnaryOperator::CreateRef => core::write!(f, "&"),
96 ReferenceUnaryOperator::CreateRefMut => core::write!(f, "&mut"),
97 ReferenceUnaryOperator::Deref => core::write!(f, "*"),
98 }
99 }
100}
101
102#[derive(Clone, Debug, PartialEq, Copy, Eq)]
103pub enum ArithmeticUnaryOperator {
104 Increment, Decrement, Plus, Minus, }
109
110impl From<&ArithmeticUnaryOperator> for InstructionCode {
111 fn from(op: &ArithmeticUnaryOperator) -> Self {
112 match op {
113 ArithmeticUnaryOperator::Increment => InstructionCode::INCREMENT,
114 ArithmeticUnaryOperator::Decrement => InstructionCode::DECREMENT,
115 ArithmeticUnaryOperator::Plus => InstructionCode::UNARY_PLUS,
116 ArithmeticUnaryOperator::Minus => InstructionCode::UNARY_MINUS,
117 }
118 }
119}
120
121impl Display for ArithmeticUnaryOperator {
122 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
123 match self {
124 ArithmeticUnaryOperator::Increment => core::write!(f, "++"),
125 ArithmeticUnaryOperator::Decrement => core::write!(f, "--"),
126 ArithmeticUnaryOperator::Plus => core::write!(f, "+"),
127 ArithmeticUnaryOperator::Minus => core::write!(f, "-"),
128 }
129 }
130}
131
132#[derive(Clone, Debug, PartialEq, Copy, Eq)]
133pub enum BitwiseUnaryOperator {
134 Not, }
136
137impl From<&BitwiseUnaryOperator> for InstructionCode {
138 fn from(op: &BitwiseUnaryOperator) -> Self {
139 match op {
140 BitwiseUnaryOperator::Not => InstructionCode::BITWISE_NOT,
141 }
142 }
143}
144
145impl Display for BitwiseUnaryOperator {
146 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
147 match self {
148 BitwiseUnaryOperator::Not => core::write!(f, "~"),
149 }
150 }
151}
152
153#[derive(Clone, Debug, PartialEq, Copy, Eq)]
154pub enum LogicalUnaryOperator {
155 Not, }
157
158impl From<&LogicalUnaryOperator> for InstructionCode {
159 fn from(op: &LogicalUnaryOperator) -> Self {
160 match op {
161 LogicalUnaryOperator::Not => InstructionCode::NOT,
162 }
163 }
164}
165
166impl Display for LogicalUnaryOperator {
167 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
168 match self {
169 LogicalUnaryOperator::Not => core::write!(f, "!"),
170 }
171 }
172}