StackMachine
Class methods
build
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
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
Nested types
- StackMachine::Builder
- StackMachine::Bytecode
- StackMachine::Comparison
- StackMachine::Jump
- StackMachine::JumpBackward
- StackMachine::JumpFalse
- StackMachine::JumpFalseBackward
- StackMachine::JumpFalseForward
- StackMachine::JumpForward
- StackMachine::JumpTrue
- StackMachine::JumpTrueBackward
- StackMachine::JumpTrueForward
- StackMachine::Label
- StackMachine::Load
- StackMachine::LocalArray
- StackMachine::OpCode
- StackMachine::Operation
- StackMachine::Store
- StackMachine::Value