Class: Vm::Instructions::ArithmeticInstruction

Inherits:
Object
  • Object
show all
Defined in:
lib/hackasm/vm/instructions/arithmetic_instruction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instruction) ⇒ ArithmeticInstruction

Returns a new instance of ArithmeticInstruction.



10
11
12
# File 'lib/hackasm/vm/instructions/arithmetic_instruction.rb', line 10

def initialize(instruction)
  @operation = instruction[:operation].to_s
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



8
9
10
# File 'lib/hackasm/vm/instructions/arithmetic_instruction.rb', line 8

def operation
  @operation
end

Instance Method Details

#commentObject



25
26
27
# File 'lib/hackasm/vm/instructions/arithmetic_instruction.rb', line 25

def comment
  "// #{operation}\n"
end

#to_asmObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/hackasm/vm/instructions/arithmetic_instruction.rb', line 14

def to_asm
  comment + case operation
  when *ArithmeticOperations::Comparison.operations
    ArithmeticOperations::Comparison.new(operation).to_asm
  when *ArithmeticOperations::UnaryOperation.operations
    ArithmeticOperations::UnaryOperation.new(operation).to_asm
  when *ArithmeticOperations::BinaryOperation.operations
    ArithmeticOperations::BinaryOperation.new(operation).to_asm
  end.split("\n").map(&:strip).join("\n")
end