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