Description Usage Arguments Examples
walk ast
1 2 3 4 5 6 | walk_ast(expr, visitor = make_visitor())
make_visitor(leaf = identity, call = identity, hd = identity,
tl = identity, initial = identity, final = identity, ...)
is_visitor(visitor)
|
expr |
a call object |
visitor |
visitor class created by make_visitor() |
leaf |
a function that manipulates an atomic or a symbol object |
call |
a function that manipulates a call object |
hd |
a function that manipulates a function object |
tl |
a function that manipulates parameters of a call object |
initial |
a function that manipulates expr before running AST |
final |
a function that manipulates expr after running AST |
... |
aribtrary functions or variables |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | walk_ast(quote(x + y * z)) # default returns the initial argument itself
walk_ast(quote(x + y * z), show_tree())
walk_ast(quote(x + y * z), make_visitor(call = as.list, final = str)) # the same as above
time2_leaf <- make_visitor(
leaf = function(x) if (is.numeric(x)) x * 2 else x
)
walk_ast(quote(1+2*3), time2_leaf)
add_leaf <-
make_visitor(
leaf = function(x) {if (is.numeric(x)) val <<- val + x},
initial = function(x) {val <<- 0; x}, # need to initialize
final = function(`_`) val,
val = 0
)
count_leaf <-
make_visitor(
leaf = function(`_`) v <<- v + 1,
initial = function(x) {v <<- 0; x},
final = function(`_`) v,
v = 0
)
walk_ast(quote(1 + 2 * 3), add_leaf)
walk_ast(quote(1 + 2 * 3), count_leaf)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.