darklua_core/nodes/statements/
compound_assign.rs

1use crate::nodes::{BinaryOperator, Expression, Token, Variable};
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub enum CompoundOperator {
5    Plus,
6    Minus,
7    Asterisk,
8    Slash,
9    DoubleSlash,
10    Percent,
11    Caret,
12    Concat,
13}
14
15impl CompoundOperator {
16    pub fn to_str(&self) -> &'static str {
17        match self {
18            Self::Plus => "+=",
19            Self::Minus => "-=",
20            Self::Asterisk => "*=",
21            Self::Slash => "/=",
22            Self::DoubleSlash => "//=",
23            Self::Percent => "%=",
24            Self::Caret => "^=",
25            Self::Concat => "..=",
26        }
27    }
28
29    pub fn to_binary_operator(&self) -> BinaryOperator {
30        match self {
31            Self::Plus => BinaryOperator::Plus,
32            Self::Minus => BinaryOperator::Minus,
33            Self::Asterisk => BinaryOperator::Asterisk,
34            Self::Slash => BinaryOperator::Slash,
35            Self::DoubleSlash => BinaryOperator::DoubleSlash,
36            Self::Percent => BinaryOperator::Percent,
37            Self::Caret => BinaryOperator::Caret,
38            Self::Concat => BinaryOperator::Concat,
39        }
40    }
41}
42
43#[derive(Clone, Debug, PartialEq, Eq)]
44pub struct CompoundAssignTokens {
45    pub operator: Token,
46}
47
48impl CompoundAssignTokens {
49    super::impl_token_fns!(target = [operator]);
50}
51
52#[derive(Clone, Debug, PartialEq, Eq)]
53pub struct CompoundAssignStatement {
54    operator: CompoundOperator,
55    variable: Variable,
56    value: Expression,
57    tokens: Option<CompoundAssignTokens>,
58}
59
60impl CompoundAssignStatement {
61    pub fn new<V: Into<Variable>, E: Into<Expression>>(
62        operator: CompoundOperator,
63        variable: V,
64        value: E,
65    ) -> Self {
66        Self {
67            operator,
68            variable: variable.into(),
69            value: value.into(),
70            tokens: None,
71        }
72    }
73
74    pub fn with_tokens(mut self, tokens: CompoundAssignTokens) -> Self {
75        self.tokens = Some(tokens);
76        self
77    }
78
79    #[inline]
80    pub fn set_tokens(&mut self, tokens: CompoundAssignTokens) {
81        self.tokens = Some(tokens);
82    }
83
84    #[inline]
85    pub fn get_tokens(&self) -> Option<&CompoundAssignTokens> {
86        self.tokens.as_ref()
87    }
88
89    #[inline]
90    pub fn get_operator(&self) -> CompoundOperator {
91        self.operator
92    }
93
94    #[inline]
95    pub fn get_variable(&self) -> &Variable {
96        &self.variable
97    }
98
99    #[inline]
100    pub fn get_value(&self) -> &Expression {
101        &self.value
102    }
103
104    #[inline]
105    pub fn extract_assignment(self) -> (Variable, Expression) {
106        (self.variable, self.value)
107    }
108
109    #[inline]
110    pub fn mutate_variable(&mut self) -> &mut Variable {
111        &mut self.variable
112    }
113
114    #[inline]
115    pub fn mutate_value(&mut self) -> &mut Expression {
116        &mut self.value
117    }
118
119    super::impl_token_fns!(iter = [tokens]);
120}