darklua_core/nodes/statements/
compound_assign.rs

1use crate::nodes::{BinaryOperator, Expression, Token, Variable};
2
3/// Represents compound assignment operators (e.g., `+=`, `-=`, etc.).
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum CompoundOperator {
6    /// Addition and assignment (`+=`)
7    Plus,
8    /// Subtraction and assignment (`-=`)
9    Minus,
10    /// Multiplication and assignment (`*=`)
11    Asterisk,
12    /// Division and assignment (`/=`)
13    Slash,
14    /// Floor division and assignment (`//=`)
15    DoubleSlash,
16    /// Modulo and assignment (`%=`)
17    Percent,
18    /// Exponentiation and assignment (`^=`)
19    Caret,
20    /// Concatenation and assignment (`..=`)
21    Concat,
22}
23
24impl CompoundOperator {
25    /// Returns the string representation of the operator.
26    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    /// Converts this compound operator to its corresponding binary operator.
40    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/// Tokens associated with a compound assignment statement.
55#[derive(Clone, Debug, PartialEq, Eq)]
56pub struct CompoundAssignTokens {
57    /// The operator token for the compound assignment.
58    pub operator: Token,
59}
60
61impl CompoundAssignTokens {
62    super::impl_token_fns!(target = [operator]);
63}
64
65/// Represents a compound assignment statement (e.g., `a += 1`).
66#[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    /// Creates a new compound assignment statement.
76    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    /// Sets the tokens for this compound assignment statement.
90    pub fn with_tokens(mut self, tokens: CompoundAssignTokens) -> Self {
91        self.tokens = Some(tokens);
92        self
93    }
94
95    /// Sets the tokens for this compound assignment statement.
96    #[inline]
97    pub fn set_tokens(&mut self, tokens: CompoundAssignTokens) {
98        self.tokens = Some(tokens);
99    }
100
101    /// Returns the tokens for this compound assignment statement, if any.
102    #[inline]
103    pub fn get_tokens(&self) -> Option<&CompoundAssignTokens> {
104        self.tokens.as_ref()
105    }
106
107    /// Returns the compound operator used in this statement.
108    #[inline]
109    pub fn get_operator(&self) -> CompoundOperator {
110        self.operator
111    }
112
113    /// Returns the variable being assigned to.
114    #[inline]
115    pub fn get_variable(&self) -> &Variable {
116        &self.variable
117    }
118
119    /// Returns the value expression in the assignment.
120    #[inline]
121    pub fn get_value(&self) -> &Expression {
122        &self.value
123    }
124
125    /// Extracts the variable and value from this statement.
126    #[inline]
127    pub fn extract_assignment(self) -> (Variable, Expression) {
128        (self.variable, self.value)
129    }
130
131    /// Returns a mutable reference to the variable.
132    #[inline]
133    pub fn mutate_variable(&mut self) -> &mut Variable {
134        &mut self.variable
135    }
136
137    /// Returns a mutable reference to the value expression.
138    #[inline]
139    pub fn mutate_value(&mut self) -> &mut Expression {
140        &mut self.value
141    }
142
143    /// Returns a mutable reference to the first token for this statement, creating it if missing.
144    pub fn mutate_first_token(&mut self) -> &mut Token {
145        self.variable.mutate_first_token()
146    }
147
148    /// Returns a mutable reference to the last token for this statement,
149    /// creating it if missing.
150    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}