Created
July 31, 2012 23:17
-
-
Save dahu/3221647 to your computer and use it in GitHub Desktop.
Simple AST walker with multi-child AST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " 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