class Prism::Compiler
# => [:program, [[[:call, [[:integer], [:arguments, [[:integer]]]]]]]]
Prism.parse(“1 + 2”).value.accept(SExpressions.new)
end
def visit_program_node(node) = [:program, super]
def visit_integer_node(node) = [:integer]
def visit_call_node(node) = [:call, super]
def visit_arguments_node(node) = [:arguments, super]
class SExpressions < Prism::Compiler
could write:
For example, to build a representation of the tree as s-expressions, you
useful when you are trying to compile a tree into a different format.
This is as opposed to a visitor which will only walk the tree. This can be
A compiler is a visitor that returns the value of each node as it visits.
def visit(node)
def visit(node) node&.accept(self) end
def visit_all(nodes)
def visit_all(nodes) nodes.map { |node| node&.accept(self) } end
def visit_child_nodes(node)
def visit_child_nodes(node) node.compact_child_nodes.map { |node| node.accept(self) } end