Skip to content

Instantly share code, notes, and snippets.

@txus
Created September 30, 2012 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save txus/3808285 to your computer and use it in GitHub Desktop.
Save txus/3808285 to your computer and use it in GitHub Desktop.
Visitor without relying on Rubinius::AST::Node#visit.
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