7
7
extern crate alloc;
8
8
9
9
use crate :: alloc:: string:: ToString ;
10
+ use alloc:: rc:: Rc ;
10
11
use alloc:: vec:: Vec ;
12
+ use core:: borrow:: Borrow ;
11
13
use liumlib:: * ;
12
- use saji:: ast:: Parser ;
14
+ use saji:: ast:: { Node , Parser } ;
13
15
use saji:: runtime:: { Runtime , RuntimeValue , Variable } ;
14
16
use saji:: token:: Lexer ;
15
17
@@ -47,7 +49,9 @@ macro_rules! run_test {
47
49
let mut parser = Parser :: new( lexer) ;
48
50
let ast = parser. parse_ast( ) ;
49
51
println!( "---------------------------------" ) ;
50
- println!( "ast {:?}" , ast) ;
52
+ for n in ast. body( ) {
53
+ print_ast( & Some ( n. clone( ) ) , 0 ) ;
54
+ }
51
55
println!( "---------------------------------" ) ;
52
56
53
57
let mut runtime = Runtime :: new( ) ;
@@ -66,6 +70,78 @@ macro_rules! run_test {
66
70
} ;
67
71
}
68
72
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
+
69
145
#[ cfg( test) ]
70
146
entry_point ! ( main) ;
71
147
#[ cfg( test) ]
0 commit comments