Skip to content

Commit a58eeb1

Browse files
committed
saji: add helper function to print AST for debug
1 parent 62abbaa commit a58eeb1

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

app/saji/tests/saji.rs

+78-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
extern crate alloc;
88

99
use crate::alloc::string::ToString;
10+
use alloc::rc::Rc;
1011
use alloc::vec::Vec;
12+
use core::borrow::Borrow;
1113
use liumlib::*;
12-
use saji::ast::Parser;
14+
use saji::ast::{Node, Parser};
1315
use saji::runtime::{Runtime, RuntimeValue, Variable};
1416
use saji::token::Lexer;
1517

@@ -47,7 +49,9 @@ macro_rules! run_test {
4749
let mut parser = Parser::new(lexer);
4850
let ast = parser.parse_ast();
4951
println!("---------------------------------");
50-
println!("ast {:?}", ast);
52+
for n in ast.body() {
53+
print_ast(&Some(n.clone()), 0);
54+
}
5155
println!("---------------------------------");
5256

5357
let mut runtime = Runtime::new();
@@ -66,6 +70,78 @@ macro_rules! run_test {
6670
};
6771
}
6872

73+
fn print_ast(node: &Option<Rc<Node>>, depth: usize) {
74+
let n = match node {
75+
Some(n) => n,
76+
None => return,
77+
};
78+
79+
match n.borrow() {
80+
Node::ExpressionStatement(stmt) => {
81+
println!("{}ExpressionStatement", " ".repeat(depth));
82+
print_ast(&stmt, depth + 1);
83+
}
84+
Node::BlockStatement { body } => {
85+
println!("{}BlockStatement", " ".repeat(depth));
86+
for node in body {
87+
print_ast(&node, depth + 1);
88+
}
89+
}
90+
Node::ReturnStatement { argument } => {
91+
println!("{}ReturnStatement", " ".repeat(depth));
92+
print_ast(&argument, depth + 1);
93+
}
94+
Node::FunctionDeclaration { id, params, body } => {
95+
println!("{}FunctionDeclaration", " ".repeat(depth));
96+
print_ast(&id, depth + 1);
97+
for param in params {
98+
print_ast(&param, depth + 1);
99+
}
100+
print_ast(&body, depth + 1);
101+
}
102+
Node::VariableDeclaration { declarations } => {
103+
println!("{}VariableDeclaration", " ".repeat(depth));
104+
for decl in declarations {
105+
print_ast(&decl, depth + 1);
106+
}
107+
}
108+
Node::VariableDeclarator { id, init } => {
109+
println!("{}VariableDeclarator", " ".repeat(depth));
110+
print_ast(&id, depth + 1);
111+
print_ast(&init, depth + 1);
112+
}
113+
Node::BinaryExpression {
114+
operator,
115+
left,
116+
right,
117+
} => {
118+
print!("{}", " ".repeat(depth));
119+
println!("BinaryExpression: {}", operator);
120+
print_ast(&left, depth + 1);
121+
print_ast(&right, depth + 1);
122+
}
123+
Node::CallExpression { callee, arguments } => {
124+
println!("{}CallExpression", " ".repeat(depth));
125+
print_ast(&callee, depth + 1);
126+
for arg in arguments {
127+
print_ast(&arg, depth + 1);
128+
}
129+
}
130+
Node::Identifier(i) => {
131+
print!("{}", " ".repeat(depth));
132+
println!("Identifier: {}", i);
133+
}
134+
Node::NumericLiteral(n) => {
135+
print!("{}", " ".repeat(depth));
136+
println!("NumericLiteral: {}", n);
137+
}
138+
Node::StringLiteral(s) => {
139+
print!("{}", " ".repeat(depth));
140+
println!("StringLiteral: {}", s);
141+
}
142+
}
143+
}
144+
69145
#[cfg(test)]
70146
entry_point!(main);
71147
#[cfg(test)]

0 commit comments

Comments
 (0)