gbf_core/decompiler/ast/
unary_op.rs

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![deny(missing_docs)]

use gbf_macros::AstNodeTransform;
use serde::{Deserialize, Serialize};

use crate::define_ast_enum_type;

use super::{expr::ExprKind, visitors::AstVisitor, AstKind, AstNodeError, AstVisitable};

define_ast_enum_type!(
    UnaryOpType {
        LogicalNot => "!",
        BitwiseNot => "~",
        Negate => "-",
    }
);

/// Represents a unary operation node in the AST, such as `-a` or `!b`.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
#[convert_to(ExprKind::UnaryOp, AstKind::Expression)]
pub struct UnaryOperationNode {
    /// The operand of the unary operation.
    pub operand: Box<ExprKind>,
    /// The unary operation type.
    pub op_type: UnaryOpType,
}

impl UnaryOperationNode {
    /// Creates a new `UnaryOperationNode` after validating the operand.
    ///
    /// # Arguments
    /// - `operand` - The operand for the unary operation.
    /// - `op_type` - The unary operation type.
    ///
    /// # Returns
    /// A new `UnaryOperationNode`.
    ///
    /// # Errors
    /// Returns an `AstNodeError` if `operand` is of an unsupported type.
    pub fn new(operand: Box<ExprKind>, op_type: UnaryOpType) -> Result<Self, AstNodeError> {
        Self::validate_operand(&operand)?;

        Ok(Self { operand, op_type })
    }

    fn validate_operand(expr: &ExprKind) -> Result<(), AstNodeError> {
        // Most expressions are ok except for string literals.
        if let ExprKind::Literal(crate::decompiler::ast::literal::LiteralNode::String(_)) = expr {
            return Err(AstNodeError::InvalidOperand(
                "BinaryOperationNode".to_string(),
                "Unsupported operand type".to_string(),
                vec!["LiteralNode".to_string()],
                format!("{:?}", expr),
            ));
        }
        Ok(())
    }
}

impl AstVisitable for UnaryOperationNode {
    fn accept(&self, visitor: &mut dyn AstVisitor) {
        visitor.visit_unary_op(self);
    }
}

// == Other implementations for unary operations ==
impl PartialEq for UnaryOperationNode {
    fn eq(&self, other: &Self) -> bool {
        self.operand == other.operand && self.op_type == other.op_type
    }
}

#[cfg(test)]
mod tests {
    use crate::decompiler::ast::{
        bin_op::BinOpType, emit, new_bin_op, new_id, new_str, new_unary_op, AstNodeError,
    };

    use super::UnaryOpType;

    #[test]
    fn test_unary_op_emit() -> Result<(), AstNodeError> {
        for op_type in UnaryOpType::all_variants() {
            let expr = new_unary_op(new_id("a"), op_type.clone())?;
            assert_eq!(emit(expr), format!("{}a", op_type.as_str()));
        }
        Ok(())
    }

    #[test]
    fn test_nested_unary_op_emit() -> Result<(), AstNodeError> {
        for op_type in UnaryOpType::all_variants() {
            let expr = new_unary_op(new_unary_op(new_id("a"), op_type.clone())?, op_type.clone())?;
            assert_eq!(
                emit(expr),
                format!("{}({}a)", op_type.as_str(), op_type.as_str())
            );
        }
        Ok(())
    }

    #[test]
    fn test_unary_op_binary_operand() -> Result<(), AstNodeError> {
        let result = new_unary_op(
            new_bin_op(new_id("a"), new_id("b"), BinOpType::Add)?,
            UnaryOpType::Negate,
        )?;

        assert_eq!(emit(result), "-(a + b)");

        Ok(())
    }

    #[test]
    fn test_unary_op_invalid_operand() {
        let result = new_unary_op(new_str("a"), UnaryOpType::Negate);
        assert!(result.is_err());
    }

    #[test]
    fn test_unary_op_equality() -> Result<(), AstNodeError> {
        let unary1 = new_unary_op(new_id("a"), UnaryOpType::Negate)?;
        let unary2 = new_unary_op(new_id("a"), UnaryOpType::Negate)?;
        assert_eq!(unary1, unary2);

        let unary3 = new_unary_op(new_id("b"), UnaryOpType::Negate)?;
        assert_ne!(unary1, unary3);
        Ok(())
    }
}