1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::lua_generator::{LuaGenerator, ToLua};
use crate::nodes::Expression;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnaryOperator {
    Length,
    Minus,
    Not,
}

fn break_minus(last_string: &str) -> bool {
    if let Some(last_char) = last_string.chars().last() {
        last_char == '-'
    } else {
        false
    }
}

impl ToLua for UnaryOperator {
    fn to_lua(&self, generator: &mut LuaGenerator) {
        match self {
            Self::Length => generator.push_char('#'),
            Self::Minus => generator.push_char_and_break_if('-', break_minus),
            Self::Not => generator.push_str("not"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnaryExpression {
    operator: UnaryOperator,
    expression: Expression,
}

impl UnaryExpression {
    pub fn new(operator: UnaryOperator, expression: Expression) -> Self {
        Self {
            operator,
            expression,
        }
    }

    pub fn get_expression(&self) -> &Expression {
        &self.expression
    }

    pub fn mutate_expression(&mut self) -> &mut Expression {
        &mut self.expression
    }

    pub fn operator(&self) -> UnaryOperator {
        self.operator
    }
}

impl ToLua for UnaryExpression {
    fn to_lua(&self, generator: &mut LuaGenerator) {
        self.operator.to_lua(generator);
        self.expression.to_lua(generator);
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn generate_unary_expression() {
        let output = UnaryExpression::new(
            UnaryOperator::Not,
            Expression::True,
        ).to_lua_string();

        assert_eq!(output, "not true");
    }

    #[test]
    fn generate_two_unary_minus_breaks_between_them() {
        let output = UnaryExpression::new(
            UnaryOperator::Minus,
            UnaryExpression::new(
                UnaryOperator::Minus,
                Expression::Identifier("a".to_owned()),
            ).into(),
        ).to_lua_string();

        assert_eq!(output, "- -a");
    }
}