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

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

use super::{ast_vec::AstVec, expr::ExprKind, AstKind, AstVisitable};

/// Represents a metadata node in the AST
#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
#[convert_to(AstKind::Function)]
pub struct FunctionNode {
    name: Option<String>,
    params: AstVec<ExprKind>,
    body: AstVec<AstKind>,
}

impl FunctionNode {
    /// Creates a new `FunctionNode` with the given `params` and `body`.
    ///
    /// # Arguments
    /// - `params` - The parameters of the function.
    /// - `body` - The body of the function.
    ///
    /// # Returns
    /// A new `FunctionNode`.
    pub fn new<N>(name: N, params: AstVec<ExprKind>, body: AstVec<AstKind>) -> Self
    where
        N: Into<Option<String>>,
    {
        Self {
            name: name.into(),
            params,
            body,
        }
    }

    /// Returns the parameters of the function.
    pub fn params(&self) -> &Vec<ExprKind> {
        &self.params
    }

    /// Returns the body of the function.
    pub fn body(&self) -> &Vec<AstKind> {
        &self.body
    }

    /// Returns the name of the function.
    pub fn name(&self) -> &Option<String> {
        &self.name
    }
}

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

impl PartialEq for FunctionNode {
    fn eq(&self, other: &Self) -> bool {
        self.params == other.params && self.body == other.body
    }
}