Created
September 30, 2012 19:52
-
-
Save txus/3808285 to your computer and use it in GitHub Desktop.
Visitor without relying on Rubinius::AST::Node#visit.
This file contains 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
class Visitor | |
attr_reader :output | |
def initialize | |
@output = "" | |
end | |
def visit(node) | |
send node.node_name, node | |
end | |
def array_literal(o) | |
emit '[' | |
o.body.each_with_index do |n, idx| | |
visit n | |
emit ', ' unless o.body.length - 1 == idx | |
end | |
emit ']' | |
end | |
def fixnum_literal(o) | |
emit "#{o.value}" | |
end | |
def local_variable_assignment(o) | |
emit "var #{o.name} = " | |
visit(o.value) | |
emit ";" | |
end | |
def emit(str) | |
@output << str | |
end | |
end | |
ast = "a = [1,3]".to_ast | |
visitor = Visitor.new | |
visitor.visit ast | |
puts visitor.output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment