darklua_core/nodes/expressions/
unary.rs

1use crate::nodes::{Expression, Token};
2
3/// Represents the type of operator in a unary expression.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum UnaryOperator {
6    /// The length operator (`#`)
7    Length,
8    /// The minus operator (`-`)
9    Minus,
10    /// The not operator (`not`)
11    Not,
12}
13
14impl UnaryOperator {
15    /// Returns the string representation of this operator.
16    pub fn to_str(&self) -> &'static str {
17        match self {
18            Self::Length => "#",
19            Self::Minus => "-",
20            Self::Not => "not",
21        }
22    }
23}
24
25/// Represents a unary operation applied to an expression.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct UnaryExpression {
28    operator: UnaryOperator,
29    expression: Expression,
30    token: Option<Token>,
31}
32
33impl UnaryExpression {
34    /// Creates a new unary expression with the given operator and expression.
35    pub fn new<E: Into<Expression>>(operator: UnaryOperator, expression: E) -> Self {
36        Self {
37            operator,
38            expression: expression.into(),
39            token: None,
40        }
41    }
42
43    /// Attaches a token to this unary expression.
44    pub fn with_token(mut self, token: Token) -> Self {
45        self.token = Some(token);
46        self
47    }
48
49    /// Sets the token for this unary expression.
50    #[inline]
51    pub fn set_token(&mut self, token: Token) {
52        self.token = Some(token);
53    }
54
55    /// Returns the token associated with this unary expression, if any.
56    #[inline]
57    pub fn get_token(&self) -> Option<&Token> {
58        self.token.as_ref()
59    }
60
61    /// Returns the expression being operated on.
62    #[inline]
63    pub fn get_expression(&self) -> &Expression {
64        &self.expression
65    }
66
67    /// Returns a mutable reference to the expression being operated on.
68    #[inline]
69    pub fn mutate_expression(&mut self) -> &mut Expression {
70        &mut self.expression
71    }
72
73    /// Returns the operator of this unary expression.
74    #[inline]
75    pub fn operator(&self) -> UnaryOperator {
76        self.operator
77    }
78
79    /// Returns a mutable reference to the last token for this unary expression,
80    /// creating it if missing.
81    pub fn mutate_last_token(&mut self) -> &mut Token {
82        self.expression.mutate_last_token()
83    }
84
85    super::impl_token_fns!(iter = [token]);
86}