Class: Whitespace::ISA::Binop

Inherits:
Instruction show all
Defined in:
lib/whitespace/instructions/arithmetic/binop.rb

Direct Known Subclasses

Add, Div, Mod, Mul, Sub

Instance Attribute Summary

Attributes inherited from Instruction

#vm

Instance Method Summary collapse

Constructor Details

#initialize(vm, op) ⇒ Binop

Returns a new instance of Binop.



3
4
5
6
7
8
9
# File 'lib/whitespace/instructions/arithmetic/binop.rb', line 3

def initialize(vm, op)
  unless Whitespace::Util.is_binop?(op)
    raise ArgumentError, "must be a binary operator: #{op}"
  end
  super(vm)
  @op = op
end

Instance Method Details

#executeObject



11
12
13
14
15
16
# File 'lib/whitespace/instructions/arithmetic/binop.rb', line 11

def execute
  right = vm.vstack.pop
  left = vm.vstack.pop

  vm.vstack.push Whitespace::Util::BINOPS[@op].call(left, right)
end