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