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(SharedValueUnaryOperator),
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::Unbox => {
39 UnaryOperator::Reference(SharedValueUnaryOperator::Unbox)
40 }
41 _ => {
42 core::todo!(
43 "Unary operator for instruction {:?} not implemented",
44 instruction
45 );
46 }
47 }
48 }
49}
50
51impl From<RegularInstruction> for UnaryOperator {
52 fn from(instruction: RegularInstruction) -> Self {
53 UnaryOperator::from(&instruction)
54 }
55}
56
57impl Display for UnaryOperator {
58 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
59 match self {
60 UnaryOperator::Reference(op) => core::write!(f, "{}", op),
61 UnaryOperator::Arithmetic(op) => core::write!(f, "{}", op),
62 UnaryOperator::Bitwise(op) => core::write!(f, "{}", op),
63 UnaryOperator::Logical(op) => core::write!(f, "{}", op),
64 }
65 }
66}
67
68#[derive(Clone, Debug, PartialEq, Copy, Eq)]
69pub enum SharedValueUnaryOperator {
70 Unbox, }
72
73impl From<&SharedValueUnaryOperator> for InstructionCode {
74 fn from(op: &SharedValueUnaryOperator) -> Self {
75 match op {
76 SharedValueUnaryOperator::Unbox => InstructionCode::UNBOX,
77 }
78 }
79}
80
81impl Display for SharedValueUnaryOperator {
82 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
83 match self {
84 SharedValueUnaryOperator::Unbox => core::write!(f, "*"),
85 }
86 }
87}
88
89#[derive(Clone, Debug, PartialEq, Copy, Eq)]
90pub enum ArithmeticUnaryOperator {
91 Increment, Decrement, Plus, Minus, }
96
97impl From<&ArithmeticUnaryOperator> for InstructionCode {
98 fn from(op: &ArithmeticUnaryOperator) -> Self {
99 match op {
100 ArithmeticUnaryOperator::Increment => InstructionCode::INCREMENT,
101 ArithmeticUnaryOperator::Decrement => InstructionCode::DECREMENT,
102 ArithmeticUnaryOperator::Plus => InstructionCode::UNARY_PLUS,
103 ArithmeticUnaryOperator::Minus => InstructionCode::UNARY_MINUS,
104 }
105 }
106}
107
108impl Display for ArithmeticUnaryOperator {
109 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
110 match self {
111 ArithmeticUnaryOperator::Increment => core::write!(f, "++"),
112 ArithmeticUnaryOperator::Decrement => core::write!(f, "--"),
113 ArithmeticUnaryOperator::Plus => core::write!(f, "+"),
114 ArithmeticUnaryOperator::Minus => core::write!(f, "-"),
115 }
116 }
117}
118
119#[derive(Clone, Debug, PartialEq, Copy, Eq)]
120pub enum BitwiseUnaryOperator {
121 Not, }
123
124impl From<&BitwiseUnaryOperator> for InstructionCode {
125 fn from(op: &BitwiseUnaryOperator) -> Self {
126 match op {
127 BitwiseUnaryOperator::Not => InstructionCode::BITWISE_NOT,
128 }
129 }
130}
131
132impl Display for BitwiseUnaryOperator {
133 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
134 match self {
135 BitwiseUnaryOperator::Not => core::write!(f, "~"),
136 }
137 }
138}
139
140#[derive(Clone, Debug, PartialEq, Copy, Eq)]
141pub enum LogicalUnaryOperator {
142 Not, }
144
145impl From<&LogicalUnaryOperator> for InstructionCode {
146 fn from(op: &LogicalUnaryOperator) -> Self {
147 match op {
148 LogicalUnaryOperator::Not => InstructionCode::NOT,
149 }
150 }
151}
152
153impl Display for LogicalUnaryOperator {
154 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
155 match self {
156 LogicalUnaryOperator::Not => core::write!(f, "!"),
157 }
158 }
159}