class SyntaxTree::YARV::Compiler

This class is an experiment in transforming Syntax Tree nodes into their corresponding YARV instruction sequences. It attempts to mirror the behavior of RubyVM::InstructionSequence.compile.

You use this as with any other visitor. First you parse code into a tree, then you visit it with this compiler. Visiting the root node of the tree will return a SyntaxTree::YARV::Compiler::InstructionSequence object. With that object you can call to_a on it, which will return a serialized form of the instruction sequence as an array. This array should mirror the array given by RubyVM::InstructionSequence#to_a.

As an example, here is how you would compile a single expression:

program = SyntaxTree.parse("1 + 2")
program.accept(SyntaxTree::YARV::Compiler.new).to_a

[
  "YARVInstructionSequence/SimpleDataFormat",
  3,
  1,
  1,
  {:arg_size=>0, :local_size=>0, :stack_max=>2},
  "<compiled>",
  "<compiled>",
  "<compiled>",
  1,
  :top,
  [],
  {},
  [],
  [
    [:putobject_INT2FIX_1_],
    [:putobject, 2],
    [:opt_plus, {:mid=>:+, :flag=>16, :orig_argc=>1}],
    [:leave]
  ]
]

Note that this is the same output as calling:

RubyVM::InstructionSequence.compile("1 + 2").to_a