File tree 2 files changed +26
-0
lines changed
2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -223,6 +223,17 @@ function ast_walker() {
223
223
}
224
224
} ;
225
225
226
+ function dive ( ast ) {
227
+ if ( ast == null )
228
+ return null ;
229
+ try {
230
+ stack . push ( ast ) ;
231
+ return walkers [ ast [ 0 ] ] . apply ( ast , ast . slice ( 1 ) ) ;
232
+ } finally {
233
+ stack . pop ( ) ;
234
+ }
235
+ } ;
236
+
226
237
function with_walkers ( walkers , cont ) {
227
238
var save = { } , i ;
228
239
for ( i in walkers ) if ( HOP ( walkers , i ) ) {
@@ -239,6 +250,7 @@ function ast_walker() {
239
250
240
251
return {
241
252
walk : walk ,
253
+ dive : dive ,
242
254
with_walkers : with_walkers ,
243
255
parent : function ( ) {
244
256
return stack [ stack . length - 2 ] ; // last one is current node
Original file line number Diff line number Diff line change @@ -11,6 +11,20 @@ var ast = jsp.parse(code);
11
11
12
12
ast = pro . ast_lift_variables ( ast ) ;
13
13
14
+ var w = pro . ast_walker ( ) ;
15
+ ast = w . with_walkers ( {
16
+ "function" : function ( ) {
17
+ var node = w . dive ( this ) ; // walk depth first
18
+ console . log ( pro . gen_code ( node , { beautify : true } ) ) ;
19
+ return node ;
20
+ } ,
21
+ "name" : function ( name ) {
22
+ return [ this [ 0 ] , "X" ] ;
23
+ }
24
+ } , function ( ) {
25
+ return w . walk ( ast ) ;
26
+ } ) ;
27
+
14
28
console . log ( pro . gen_code ( ast , {
15
29
beautify : true
16
30
} ) ) ;
You can’t perform that action at this time.
0 commit comments