gbf_core/decompiler/ast/
array_node.rs

1#![deny(missing_docs)]
2
3use gbf_macros::AstNodeTransform;
4use serde::{Deserialize, Serialize};
5
6use super::{
7    AstKind, AstVisitable, array_kind::ArrayKind, expr::ExprKind, ptr::P, visitors::AstVisitor,
8};
9
10/// Represents a function call
11#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
12#[convert_to(ArrayKind::MergedArray, ExprKind::Array, AstKind::Expression)]
13pub struct ArrayNode {
14    /// The arguments to the function.
15    pub elements: Vec<ExprKind>,
16}
17
18impl ArrayNode {
19    /// Creates a new array.
20    ///
21    /// # Arguments
22    /// - `elements`: The elements of the array.
23    pub fn new(elements: Vec<ExprKind>) -> Self {
24        Self { elements }
25    }
26}
27
28impl AstVisitable for P<ArrayNode> {
29    fn accept<V: AstVisitor>(&self, visitor: &mut V) -> V::Output {
30        visitor.visit_array_node(self)
31    }
32}
33
34// == Other implementations for unary operations ==
35impl PartialEq for ArrayNode {
36    fn eq(&self, other: &Self) -> bool {
37        self.elements == other.elements
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use crate::decompiler::ast::{new_array, new_num, new_str};
44
45    #[test]
46    fn test_array_node() {
47        let array = new_array(vec![new_num(5), new_num(6)]);
48        let array_two = new_array(vec![new_num(5), new_num(6)]);
49        assert_eq!(array, array_two);
50    }
51
52    #[test]
53    fn test_array_node_emit() {
54        let array = new_array(vec![new_num(5), new_num(6)]);
55        assert_eq!(crate::decompiler::ast::emit(array), "{5, 6}");
56    }
57
58    #[test]
59    fn test_array_node_emit_str() {
60        let array = new_array(vec![new_str("hello"), new_str("world")]);
61        assert_eq!(crate::decompiler::ast::emit(array), r#"{"hello", "world"}"#);
62    }
63}