datex_core/ast/
unary_operation.rs1use std::fmt::{Display, Formatter};
2
3use crate::global::instruction_codes::InstructionCode;
4
5#[derive(Clone, Debug, PartialEq, Copy)]
6pub enum UnaryOperator {
7 Reference(ReferenceUnaryOperator),
8 Arithmetic(ArithmeticUnaryOperator),
9 Bitwise(BitwiseUnaryOperator),
10 Logical(LogicalUnaryOperator),
11}
12
13impl From<&UnaryOperator> for InstructionCode {
14 fn from(op: &UnaryOperator) -> Self {
15 match op {
16 UnaryOperator::Arithmetic(op) => InstructionCode::from(op),
17 UnaryOperator::Reference(op) => InstructionCode::from(op),
18 UnaryOperator::Logical(op) => InstructionCode::from(op),
19 UnaryOperator::Bitwise(op) => InstructionCode::from(op),
20 UnaryOperator::Reference(op) => InstructionCode::from(op),
21 }
22 }
23}
24
25impl Display for UnaryOperator {
26 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
27 match self {
28 UnaryOperator::Reference(op) => write!(f, "{}", op),
29 UnaryOperator::Arithmetic(op) => write!(f, "{}", op),
30 UnaryOperator::Bitwise(op) => write!(f, "{}", op),
31 UnaryOperator::Logical(op) => write!(f, "{}", op),
32 }
33 }
34}
35
36#[derive(Clone, Debug, PartialEq, Copy)]
37pub enum ReferenceUnaryOperator {
38 CreateRef, CreateRefMut, CreateRefFinal, Deref, }
43
44impl From<&ReferenceUnaryOperator> for InstructionCode {
45 fn from(op: &ReferenceUnaryOperator) -> Self {
46 match op {
47 ReferenceUnaryOperator::CreateRef => InstructionCode::CREATE_REF,
48 ReferenceUnaryOperator::CreateRefMut => {
49 InstructionCode::CREATE_REF_MUT
50 }
51 ReferenceUnaryOperator::CreateRefFinal => {
52 InstructionCode::CREATE_REF_FINAL
53 }
54 ReferenceUnaryOperator::Deref => InstructionCode::DEREF,
55 }
56 }
57}
58
59impl Display for ReferenceUnaryOperator {
60 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
61 match self {
62 ReferenceUnaryOperator::CreateRef => write!(f, "&"),
63 ReferenceUnaryOperator::CreateRefMut => write!(f, "&mut"),
64 ReferenceUnaryOperator::CreateRefFinal => write!(f, "&final"),
65 ReferenceUnaryOperator::Deref => write!(f, "*"),
66 }
67 }
68}
69
70#[derive(Clone, Debug, PartialEq, Copy)]
71pub enum ArithmeticUnaryOperator {
72 Increment, Decrement, Plus, Minus, }
77
78impl From<&ArithmeticUnaryOperator> for InstructionCode {
79 fn from(op: &ArithmeticUnaryOperator) -> Self {
80 match op {
81 ArithmeticUnaryOperator::Increment => InstructionCode::INCREMENT,
82 ArithmeticUnaryOperator::Decrement => InstructionCode::DECREMENT,
83 ArithmeticUnaryOperator::Plus => InstructionCode::UNARY_PLUS,
84 ArithmeticUnaryOperator::Minus => InstructionCode::UNARY_MINUS,
85 }
86 }
87}
88
89impl Display for ArithmeticUnaryOperator {
90 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
91 match self {
92 ArithmeticUnaryOperator::Increment => write!(f, "++"),
93 ArithmeticUnaryOperator::Decrement => write!(f, "--"),
94 ArithmeticUnaryOperator::Plus => write!(f, "+"),
95 ArithmeticUnaryOperator::Minus => write!(f, "-"),
96 }
97 }
98}
99
100#[derive(Clone, Debug, PartialEq, Copy)]
101pub enum BitwiseUnaryOperator {
102 Negation, }
104
105impl From<&BitwiseUnaryOperator> for InstructionCode {
106 fn from(op: &BitwiseUnaryOperator) -> Self {
107 match op {
108 BitwiseUnaryOperator::Negation => InstructionCode::BITWISE_NOT,
109 }
110 }
111}
112
113impl Display for BitwiseUnaryOperator {
114 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
115 match self {
116 BitwiseUnaryOperator::Negation => write!(f, "~"),
117 }
118 }
119}
120
121#[derive(Clone, Debug, PartialEq, Copy)]
122pub enum LogicalUnaryOperator {
123 Not, }
125
126impl From<&LogicalUnaryOperator> for InstructionCode {
127 fn from(op: &LogicalUnaryOperator) -> Self {
128 match op {
129 LogicalUnaryOperator::Not => InstructionCode::NOT,
130 }
131 }
132}
133
134impl Display for LogicalUnaryOperator {
135 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
136 match self {
137 LogicalUnaryOperator::Not => write!(f, "!"),
138 }
139 }
140}