darklua_core/nodes/statements/
assign.rs

1use crate::nodes::{Expression, Token, Variable};
2
3/// Tokens associated with an assignment statement.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct AssignTokens {
6    pub equal: Token,
7    pub variable_commas: Vec<Token>,
8    pub value_commas: Vec<Token>,
9}
10
11impl AssignTokens {
12    super::impl_token_fns!(
13        target = [equal]
14        iter = [variable_commas, value_commas]
15    );
16}
17
18/// Represents a variable assignment statement (e.g., `a, b = 1, 2`).
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub struct AssignStatement {
21    variables: Vec<Variable>,
22    values: Vec<Expression>,
23    tokens: Option<AssignTokens>,
24}
25
26impl AssignStatement {
27    /// Creates a new assignment statement with the given variables and values.
28    pub fn new(variables: Vec<Variable>, values: Vec<Expression>) -> Self {
29        Self {
30            variables,
31            values,
32            tokens: None,
33        }
34    }
35
36    /// Creates a new assignment statement with a single variable and value.
37    pub fn from_variable<V: Into<Variable>, E: Into<Expression>>(variable: V, value: E) -> Self {
38        Self {
39            variables: vec![variable.into()],
40            values: vec![value.into()],
41            tokens: None,
42        }
43    }
44
45    /// Returns the number of variables in the assignment.
46    #[inline]
47    pub fn variables_len(&self) -> usize {
48        self.variables.len()
49    }
50
51    /// Returns the number of values in the assignment.
52    #[inline]
53    pub fn values_len(&self) -> usize {
54        self.values.len()
55    }
56
57    /// Returns the list of variables.
58    #[inline]
59    pub fn get_variables(&self) -> &Vec<Variable> {
60        &self.variables
61    }
62
63    /// Returns an iterator over the variables.
64    #[inline]
65    pub fn iter_variables(&self) -> impl Iterator<Item = &Variable> {
66        self.variables.iter()
67    }
68
69    /// Returns a mutable iterator over the variables.
70    #[inline]
71    pub fn iter_mut_variables(&mut self) -> impl Iterator<Item = &mut Variable> {
72        self.variables.iter_mut()
73    }
74
75    /// Returns the last value in the assignment, if any.
76    #[inline]
77    pub fn last_value(&self) -> Option<&Expression> {
78        self.values.last()
79    }
80
81    /// Returns an iterator over the values.
82    #[inline]
83    pub fn iter_values(&self) -> impl Iterator<Item = &Expression> {
84        self.values.iter()
85    }
86
87    /// Returns a mutable iterator over the values.
88    #[inline]
89    pub fn iter_mut_values(&mut self) -> impl Iterator<Item = &mut Expression> {
90        self.values.iter_mut()
91    }
92
93    /// Returns a mutable reference to the variables vector.
94    #[inline]
95    pub fn mutate_variables(&mut self) -> &mut Vec<Variable> {
96        &mut self.variables
97    }
98
99    /// Adds a new variable and value to the assignment.
100    pub fn append_assignment<V: Into<Variable>, E: Into<Expression>>(
101        mut self,
102        variable: V,
103        value: E,
104    ) -> Self {
105        self.variables.push(variable.into());
106        self.values.push(value.into());
107        self
108    }
109
110    /// Sets the tokens for this assignment statement.
111    pub fn with_tokens(mut self, tokens: AssignTokens) -> Self {
112        self.tokens = Some(tokens);
113        self
114    }
115
116    /// Sets the tokens for this assignment statement.
117    #[inline]
118    pub fn set_tokens(&mut self, tokens: AssignTokens) {
119        self.tokens = Some(tokens);
120    }
121
122    /// Returns the tokens for this assignment statement, if any.
123    #[inline]
124    pub fn get_tokens(&self) -> Option<&AssignTokens> {
125        self.tokens.as_ref()
126    }
127
128    /// Returns a mutable reference to the first token for this assignment statement,
129    /// creating it if missing.
130    pub fn mutate_first_token(&mut self) -> &mut Token {
131        self.variables
132            .iter_mut()
133            .next()
134            .expect("an assign statement must have at least one variable")
135            .mutate_first_token()
136    }
137
138    /// Returns a mutable reference to the last token for this statement,
139    /// creating it if missing.
140    pub fn mutate_last_token(&mut self) -> &mut Token {
141        self.values
142            .last_mut()
143            .expect("an assign statement must have at least one value")
144            .mutate_last_token()
145    }
146
147    super::impl_token_fns!(iter = [tokens]);
148}