class Prism::Compiler
A compiler is a visitor that returns the value of each node as it visits. This is as opposed to a visitor which will only walk the tree. This can be useful when you are trying to compile a tree into a different format.
For example, to build a representation of the tree as s-expressions, you could write:
class SExpressions < Prism::Compiler def visit_arguments_node(node) = [:arguments, super] def visit_call_node(node) = [:call, super] def visit_integer_node(node) = [:integer] def visit_program_node(node) = [:program, super] end Prism.parse("1 + 2").value.accept(SExpressions.new) # => [:program, [[[:call, [[:integer], [:arguments, [[:integer]]]]]]]]
Public Instance Methods
Visit an individual node.
# File lib/prism/compiler.rb, line 29 def visit(node) node&.accept(self) end
Compile a AliasGlobalVariableNode
node
Visit a list of nodes.
# File lib/prism/compiler.rb, line 34 def visit_all(nodes) nodes.map { |node| node&.accept(self) } end
Compile a AlternationPatternNode
node
Compile a BackReferenceReadNode
node
Compile a BlockLocalVariableNode
node
Compile a CallOperatorWriteNode
node
Visit the child nodes of the given node.
# File lib/prism/compiler.rb, line 39 def visit_child_nodes(node) node.compact_child_nodes.map { |node| node.accept(self) } end
Compile a ClassVariableAndWriteNode
node
Compile a ClassVariableOperatorWriteNode
node
Compile a ClassVariableOrWriteNode
node
Compile a ClassVariableReadNode
node
Compile a ClassVariableTargetNode
node
Compile a ClassVariableWriteNode
node
Compile a ConstantAndWriteNode
node
Compile a ConstantOperatorWriteNode
node
Compile a ConstantPathAndWriteNode
node
Compile a ConstantPathOperatorWriteNode
node
Compile a ConstantPathOrWriteNode
node
Compile a ConstantPathTargetNode
node
Compile a ConstantPathWriteNode
node
Compile a EmbeddedStatementsNode
node
Compile a ForwardingArgumentsNode
node
Compile a ForwardingParameterNode
node
Compile a GlobalVariableAndWriteNode
node
Compile a GlobalVariableOperatorWriteNode
node
Compile a GlobalVariableOrWriteNode
node
Compile a GlobalVariableReadNode
node
Compile a GlobalVariableTargetNode
node
Compile a GlobalVariableWriteNode
node
Compile a IndexOperatorWriteNode
node
Compile a InstanceVariableAndWriteNode
node
Compile a InstanceVariableOperatorWriteNode
node
Compile a InstanceVariableOrWriteNode
node
Compile a InstanceVariableReadNode
node
Compile a InstanceVariableTargetNode
node
Compile a InstanceVariableWriteNode
node
Compile a InterpolatedMatchLastLineNode
node
Compile a InterpolatedRegularExpressionNode
node
Compile a InterpolatedStringNode
node
Compile a InterpolatedSymbolNode
node
Compile a InterpolatedXStringNode
node
Compile a ItLocalVariableReadNode
node
Compile a KeywordRestParameterNode
node
Compile a LocalVariableAndWriteNode
node
Compile a LocalVariableOperatorWriteNode
node
Compile a LocalVariableOrWriteNode
node
Compile a LocalVariableReadNode
node
Compile a LocalVariableTargetNode
node
Compile a LocalVariableWriteNode
node
Compile a NoKeywordsParameterNode
node
Compile a NumberedParametersNode
node
Compile a NumberedReferenceReadNode
node
Compile a OptionalKeywordParameterNode
node
Compile a OptionalParameterNode
node
Compile a RegularExpressionNode
node
Compile a RequiredKeywordParameterNode
node
Compile a RequiredParameterNode
node