gbf_core/decompiler/ast/new.rs
1#![deny(missing_docs)]
2
3use gbf_macros::AstNodeTransform;
4use serde::{Deserialize, Serialize};
5
6use super::{AstKind, AstNodeError, AstVisitable, expr::ExprKind, ptr::P, visitors::AstVisitor};
7
8/// Represents a return node in the AST, such as `return 5`.
9#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
10#[convert_to(ExprKind::New, AstKind::Expression)]
11pub struct NewNode {
12 /// The type of object to create.
13 pub new_type: ExprKind,
14 /// The arguments to pass to the object.
15 pub arg: ExprKind,
16}
17
18impl NewNode {
19 /// Creates a new `NewNode` with the provided type and arguments.
20 ///
21 /// # Arguments
22 /// - `new_type`: The type of object to create.
23 /// - `arg`: The arguments to pass to the object.
24 ///
25 /// # Returns
26 /// - A `NewNode` instance containing the provided type and arguments.
27 pub fn new(new_type: ExprKind, arg: ExprKind) -> Result<Self, AstNodeError> {
28 Ok(Self { new_type, arg })
29 }
30}
31
32impl AstVisitable for P<NewNode> {
33 fn accept<V: AstVisitor>(&self, visitor: &mut V) -> V::Output {
34 visitor.visit_new(self)
35 }
36}
37
38// == Other implementations for return ==
39impl PartialEq for NewNode {
40 fn eq(&self, other: &Self) -> bool {
41 self.new_type == other.new_type && self.arg == other.arg
42 }
43}
44
45// #[cfg(test)]
46// mod tests {
47// use crate::decompiler::ast::{emit, new_num, new_return};
48
49// #[test]
50// fn test_return_node() {
51// let ret = new_return(new_num(5));
52// assert_eq!(ret.ret, new_return(new_num(5)).ret);
53// }
54
55// #[test]
56// fn test_emit() {
57// let ret = new_return(new_num(5));
58// assert_eq!(emit(ret), "return 5;");
59// }
60
61// #[test]
62// fn test_equality() {
63// let ret = new_return(new_num(5));
64// let ret2 = new_return(new_num(5));
65// let ret3 = new_return(new_num(6));
66// assert_eq!(ret, ret2);
67// assert_ne!(ret, ret3);
68// }
69// }