gbf_core/decompiler/ast/
statement.rs#![deny(missing_docs)]
use gbf_macros::AstNodeTransform;
use serde::{Deserialize, Serialize};
use super::{
assignable::AssignableKind, expr::ExprKind, visitors::AstVisitor, AstKind, AstNodeError,
};
use crate::decompiler::ast::AstVisitable;
#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
#[convert_to(AstKind::Statement)]
pub struct StatementNode {
pub lhs: Box<AssignableKind>,
pub rhs: Box<ExprKind>,
}
impl StatementNode {
pub fn new(lhs: Box<AssignableKind>, rhs: Box<ExprKind>) -> Result<Self, AstNodeError> {
Ok(Self { lhs, rhs })
}
}
impl AstVisitable for StatementNode {
fn accept(&self, visitor: &mut dyn AstVisitor) {
visitor.visit_statement(self);
}
}
impl PartialEq for StatementNode {
fn eq(&self, other: &Self) -> bool {
self.lhs == other.lhs && self.rhs == other.rhs
}
}
#[cfg(test)]
mod tests {
use crate::decompiler::ast::{emit, member_access, new_id, new_str, statement, AstNodeError};
#[test]
fn test_statement_emit() -> Result<(), AstNodeError> {
let stmt = statement(new_id("test1"), new_id("test2"));
assert_eq!(emit(stmt), "test1 = test2;");
let stmt = statement(
member_access(new_id("player"), new_id("chat"))?,
new_str("Hello, world!"),
);
assert_eq!(emit(stmt), "player.chat = \"Hello, world!\";");
Ok(())
}
}