gbf_core/decompiler/ast/
meta.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
#![deny(missing_docs)]

use std::collections::HashMap;

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

use crate::utils::Gs2BytecodeAddress;

use super::{AstKind, AstVisitable};

/// Represents a metadata node in the AST
#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
#[convert_to(AstKind::Meta)]
pub struct MetaNode {
    node: Box<AstKind>,
    comment: Option<String>,
    source_location: Option<Gs2BytecodeAddress>,
    properties: HashMap<String, String>,
}

impl MetaNode {
    /// Creates a new `MetaNode` with the given `node`, `comment`, `source_location`, and `properties`.
    ///
    /// # Arguments
    /// - `node` - The inner node.
    /// - `comment` - The comment for the node.
    /// - `source_location` - The source location of the node.
    /// - `properties` - The properties of the node.
    ///
    /// # Returns
    /// A new `MetaNode`.
    pub fn new(
        node: Box<AstKind>,
        comment: Option<String>,
        source_location: Option<Gs2BytecodeAddress>,
        properties: HashMap<String, String>,
    ) -> Self {
        Self {
            node,
            comment,
            source_location,
            properties,
        }
    }

    /// Returns the inner node.
    pub fn node(&self) -> &AstKind {
        &self.node
    }

    /// Returns the comment.
    pub fn comment(&self) -> Option<&String> {
        self.comment.as_ref()
    }

    /// Returns the source location.
    pub fn source_location(&self) -> Option<Gs2BytecodeAddress> {
        self.source_location
    }

    /// Returns the properties.
    pub fn properties(&self) -> &HashMap<String, String> {
        &self.properties
    }
}

// == Other implementations for literal ==
impl AstVisitable for MetaNode {
    fn accept(&self, visitor: &mut dyn super::visitors::AstVisitor) {
        visitor.visit_meta(self);
    }
}

impl PartialEq for MetaNode {
    fn eq(&self, other: &Self) -> bool {
        self.node == other.node
            && self.comment == other.comment
            && self.source_location == other.source_location
            && self.properties == other.properties
    }
}

#[cfg(test)]
mod tests {
    use crate::decompiler::ast::{comment, emit, new_id, statement};

    #[test]
    fn test_comment_emit() {
        let statement = statement(new_id("foo"), new_id("bar"));
        let comment = comment(statement, "Sets foo to bar");
        assert_eq!(emit(comment), "// Sets foo to bar\nfoo = bar;");
    }

    #[test]
    fn test_comment_equality() {
        let statement = statement(new_id("foo"), new_id("bar"));
        let comment1 = comment(statement.clone(), "Sets foo to bar");
        let comment2 = comment(statement.clone(), "Sets foo to bar");
        let comment3 = comment(statement.clone(), "Sets foo to baz");

        assert_eq!(comment1, comment2);
        assert_ne!(comment1, comment3);
    }
}