Skip to main content

darklua_core/nodes/statements/
mod.rs

1mod assign;
2mod compound_assign;
3mod do_statement;
4mod function;
5mod generic_for;
6mod if_statement;
7mod last_statement;
8mod local_assign;
9mod local_function;
10mod numeric_for;
11mod repeat_statement;
12mod type_declaration;
13mod type_function;
14mod while_statement;
15
16pub use assign::*;
17pub use compound_assign::*;
18pub use do_statement::*;
19pub use function::*;
20pub use generic_for::*;
21pub use if_statement::*;
22pub use last_statement::*;
23pub use local_assign::*;
24pub use local_function::*;
25pub use numeric_for::*;
26pub use repeat_statement::*;
27pub use type_declaration::*;
28pub use type_function::*;
29pub use while_statement::*;
30
31use crate::nodes::FunctionCall;
32
33use super::impl_token_fns;
34
35/// Represents all possible statement.
36#[derive(Clone, Debug, PartialEq, Eq)]
37pub enum Statement {
38    /// An assignment statement (e.g., `a = 1`)
39    Assign(AssignStatement),
40    /// A do statement (e.g., `do ... end`)
41    Do(DoStatement),
42    /// A function call statement (e.g., `print("Hello")`)
43    Call(FunctionCall),
44    /// A compound assignment statement (e.g., `a += 1`)
45    CompoundAssign(CompoundAssignStatement),
46    /// A function declaration statement (e.g., `function name() ... end`)
47    Function(Box<FunctionStatement>),
48    /// A generic for loop (e.g., `for k, v in pairs(t) do ... end`)
49    GenericFor(GenericForStatement),
50    /// An if statement (e.g., `if condition then ... elseif ... else ... end`)
51    If(IfStatement),
52    /// A local variable assignment (e.g., `local a, b = 1, 2`)
53    LocalAssign(LocalAssignStatement),
54    /// A local function declaration (e.g., `local function name() ... end`)
55    LocalFunction(Box<LocalFunctionStatement>),
56    /// A numeric for loop (e.g., `for i = 1, 10, 2 do ... end`)
57    NumericFor(Box<NumericForStatement>),
58    /// A repeat loop (e.g., `repeat ... until condition`)
59    Repeat(RepeatStatement),
60    /// A while loop (e.g., `while condition do ... end`)
61    While(WhileStatement),
62    /// A type declaration statement (e.g., `type T = string | number`)
63    TypeDeclaration(TypeDeclarationStatement),
64    /// A type function declaration (e.g., `type function name() ... end`)
65    TypeFunction(Box<TypeFunctionStatement>),
66}
67
68impl Statement {
69    /// Returns a mutable reference to the first token of this statement.
70    pub fn mutate_first_token(&mut self) -> &mut crate::nodes::Token {
71        match self {
72            Self::Assign(assign) => assign.mutate_first_token(),
73            Self::Do(do_stmt) => do_stmt.mutate_first_token(),
74            Self::Call(call) => call.mutate_first_token(),
75            Self::CompoundAssign(compound) => compound.mutate_first_token(),
76            Self::Function(function) => function.mutate_first_token(),
77            Self::GenericFor(generic_for) => generic_for.mutate_first_token(),
78            Self::If(if_stmt) => if_stmt.mutate_first_token(),
79            Self::LocalAssign(local_assign) => local_assign.mutate_first_token(),
80            Self::LocalFunction(local_function) => local_function.mutate_first_token(),
81            Self::NumericFor(numeric_for) => numeric_for.mutate_first_token(),
82            Self::Repeat(repeat_stmt) => repeat_stmt.mutate_first_token(),
83            Self::While(while_stmt) => while_stmt.mutate_first_token(),
84            Self::TypeDeclaration(type_decl) => type_decl.mutate_first_token(),
85            Self::TypeFunction(type_function) => type_function.mutate_first_token(),
86        }
87    }
88
89    /// Returns a mutable reference to the last token of this statement,
90    /// creating it if missing.
91    pub fn mutate_last_token(&mut self) -> &mut crate::nodes::Token {
92        match self {
93            Self::Assign(assign) => assign.mutate_last_token(),
94            Self::Do(do_stmt) => do_stmt.mutate_last_token(),
95            Self::Call(call) => call.mutate_last_token(),
96            Self::CompoundAssign(compound) => compound.mutate_last_token(),
97            Self::Function(function) => function.mutate_last_token(),
98            Self::GenericFor(generic_for) => generic_for.mutate_last_token(),
99            Self::If(if_stmt) => if_stmt.mutate_last_token(),
100            Self::LocalAssign(local_assign) => local_assign.mutate_last_token(),
101            Self::LocalFunction(local_function) => local_function.mutate_last_token(),
102            Self::NumericFor(numeric_for) => numeric_for.mutate_last_token(),
103            Self::Repeat(repeat_stmt) => repeat_stmt.mutate_last_token(),
104            Self::While(while_stmt) => while_stmt.mutate_last_token(),
105            Self::TypeDeclaration(type_decl) => type_decl.mutate_last_token(),
106            Self::TypeFunction(type_function) => type_function.mutate_last_token(),
107        }
108    }
109}
110
111impl From<AssignStatement> for Statement {
112    fn from(assign: AssignStatement) -> Statement {
113        Statement::Assign(assign)
114    }
115}
116
117impl From<DoStatement> for Statement {
118    fn from(do_statement: DoStatement) -> Statement {
119        Statement::Do(do_statement)
120    }
121}
122
123impl From<CompoundAssignStatement> for Statement {
124    fn from(statement: CompoundAssignStatement) -> Statement {
125        Statement::CompoundAssign(statement)
126    }
127}
128
129impl From<FunctionCall> for Statement {
130    fn from(call: FunctionCall) -> Statement {
131        Statement::Call(call)
132    }
133}
134
135impl From<FunctionStatement> for Statement {
136    fn from(function: FunctionStatement) -> Statement {
137        Statement::Function(Box::new(function))
138    }
139}
140
141impl From<GenericForStatement> for Statement {
142    fn from(generic_for: GenericForStatement) -> Statement {
143        Statement::GenericFor(generic_for)
144    }
145}
146
147impl From<IfStatement> for Statement {
148    fn from(if_statement: IfStatement) -> Statement {
149        Statement::If(if_statement)
150    }
151}
152
153impl From<LocalAssignStatement> for Statement {
154    fn from(assign: LocalAssignStatement) -> Statement {
155        Statement::LocalAssign(assign)
156    }
157}
158
159impl From<LocalFunctionStatement> for Statement {
160    fn from(function: LocalFunctionStatement) -> Statement {
161        Statement::LocalFunction(Box::new(function))
162    }
163}
164
165impl From<NumericForStatement> for Statement {
166    fn from(numeric_for: NumericForStatement) -> Statement {
167        Statement::NumericFor(numeric_for.into())
168    }
169}
170
171impl From<RepeatStatement> for Statement {
172    fn from(repeat_statement: RepeatStatement) -> Statement {
173        Statement::Repeat(repeat_statement)
174    }
175}
176
177impl From<WhileStatement> for Statement {
178    fn from(while_statement: WhileStatement) -> Statement {
179        Statement::While(while_statement)
180    }
181}
182
183impl From<TypeDeclarationStatement> for Statement {
184    fn from(type_declaration: TypeDeclarationStatement) -> Statement {
185        Statement::TypeDeclaration(type_declaration)
186    }
187}
188
189impl From<TypeFunctionStatement> for Statement {
190    fn from(type_function: TypeFunctionStatement) -> Statement {
191        Statement::TypeFunction(Box::new(type_function))
192    }
193}