darklua_core/nodes/statements/
compound_assign.rs1use crate::nodes::{BinaryOperator, Expression, Token, Variable};
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum CompoundOperator {
6 Plus,
8 Minus,
10 Asterisk,
12 Slash,
14 DoubleSlash,
16 Percent,
18 Caret,
20 Concat,
22}
23
24impl CompoundOperator {
25 pub fn to_str(&self) -> &'static str {
27 match self {
28 Self::Plus => "+=",
29 Self::Minus => "-=",
30 Self::Asterisk => "*=",
31 Self::Slash => "/=",
32 Self::DoubleSlash => "//=",
33 Self::Percent => "%=",
34 Self::Caret => "^=",
35 Self::Concat => "..=",
36 }
37 }
38
39 pub fn to_binary_operator(&self) -> BinaryOperator {
41 match self {
42 Self::Plus => BinaryOperator::Plus,
43 Self::Minus => BinaryOperator::Minus,
44 Self::Asterisk => BinaryOperator::Asterisk,
45 Self::Slash => BinaryOperator::Slash,
46 Self::DoubleSlash => BinaryOperator::DoubleSlash,
47 Self::Percent => BinaryOperator::Percent,
48 Self::Caret => BinaryOperator::Caret,
49 Self::Concat => BinaryOperator::Concat,
50 }
51 }
52}
53
54#[derive(Clone, Debug, PartialEq, Eq)]
56pub struct CompoundAssignTokens {
57 pub operator: Token,
59}
60
61impl CompoundAssignTokens {
62 super::impl_token_fns!(target = [operator]);
63}
64
65#[derive(Clone, Debug, PartialEq, Eq)]
67pub struct CompoundAssignStatement {
68 operator: CompoundOperator,
69 variable: Variable,
70 value: Expression,
71 tokens: Option<CompoundAssignTokens>,
72}
73
74impl CompoundAssignStatement {
75 pub fn new<V: Into<Variable>, E: Into<Expression>>(
77 operator: CompoundOperator,
78 variable: V,
79 value: E,
80 ) -> Self {
81 Self {
82 operator,
83 variable: variable.into(),
84 value: value.into(),
85 tokens: None,
86 }
87 }
88
89 pub fn with_tokens(mut self, tokens: CompoundAssignTokens) -> Self {
91 self.tokens = Some(tokens);
92 self
93 }
94
95 #[inline]
97 pub fn set_tokens(&mut self, tokens: CompoundAssignTokens) {
98 self.tokens = Some(tokens);
99 }
100
101 #[inline]
103 pub fn get_tokens(&self) -> Option<&CompoundAssignTokens> {
104 self.tokens.as_ref()
105 }
106
107 #[inline]
109 pub fn get_operator(&self) -> CompoundOperator {
110 self.operator
111 }
112
113 #[inline]
115 pub fn get_variable(&self) -> &Variable {
116 &self.variable
117 }
118
119 #[inline]
121 pub fn get_value(&self) -> &Expression {
122 &self.value
123 }
124
125 #[inline]
127 pub fn extract_assignment(self) -> (Variable, Expression) {
128 (self.variable, self.value)
129 }
130
131 #[inline]
133 pub fn mutate_variable(&mut self) -> &mut Variable {
134 &mut self.variable
135 }
136
137 #[inline]
139 pub fn mutate_value(&mut self) -> &mut Expression {
140 &mut self.value
141 }
142
143 pub fn mutate_first_token(&mut self) -> &mut Token {
145 self.variable.mutate_first_token()
146 }
147
148 pub fn mutate_last_token(&mut self) -> &mut Token {
151 self.value.mutate_last_token()
152 }
153
154 super::impl_token_fns!(iter = [tokens]);
155}