module StackMachine

Defined in:

stackmachine/builder.cr
stackmachine/bytecode.cr
stackmachine/comparisons.cr
stackmachine/opcode.cr
stackmachine/operations.cr
stackmachine/rpn.cr
stackmachine/value.cr

Class Method Summary

Class Method Detail

def self.build(&) : Array(Bytecode) #

Method to build the stack vm bytecode Use of with ... yield to make the Builder class the receiver

bytecode = StackMachine.build {
  op_num 10
  op_num 20
  op_add
  op_num 40
  op_num 30
  op_sub
  op_num 3
  op_mul
  op_div
}

StackMachine.execute bytecode # => 1

[View source]
def self.execute(code : Array(Bytecode)) : Float64 | Bool #

Given an input array following the Reverse Polish Notation format, this method will execute the RPN expression.

StackMachine.execute [1, 2, StackMachine::Operation::Add] # => 3
StackMachine.execute [1, 2, StackMachine::Operation::Div] # => 0

Use of Load and Store opcodes can allow for variables

StackMachine.execute [1, Store.new 0, Load.new 0] # => 1

TODO create new exceptions for when there are an invalid number of operands on stack and when stack has too many values at the end


[View source]