Skip to content

Instantly share code, notes, and snippets.

@dahu
Created July 31, 2012 23:17
Show Gist options
  • Select an option

  • Save dahu/3221647 to your computer and use it in GitHub Desktop.

Select an option

Save dahu/3221647 to your computer and use it in GitHub Desktop.
Simple AST walker with multi-child AST
" VimL Example of Walking a multi-child AST
" Barry Arthur, 2012 08 01
" The same simple AST walker
func! Walk(ast, visitor)
return type(a:ast) == type([]) ? call(a:visitor[a:ast[0]], [a:ast[1]], a:visitor) : a:ast
endfunc
" rainbows and unicorns
function! RnU()
let rnu = {}
func rnu.print(args) dict
let lst = []
for arg in a:args
call add(lst, Walk(arg, self))
unlet arg
endfor
return join(lst, ' ')
endfunc
func rnu.upper(args) dict
let lst = []
for arg in a:args
call add(lst, toupper(Walk(arg, self)))
unlet arg
endfor
return join(lst, ' ')
endfunc
return rnu
endfunction
let ast = ['print', ['Unicorns', 'now', 'served', 'with', ['upper', ['lots', "of", 'extra', 'cheese']], 'and', 'sauce!']]
echo Walk(ast, RnU())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment