Class: Vm::Instructions::MemoryAccessInstruction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instruction, object_name) ⇒ MemoryAccessInstruction

Returns a new instance of MemoryAccessInstruction.



11
12
13
14
15
16
# File 'lib/hackasm/vm/instructions/memory_access_instruction.rb', line 11

def initialize(instruction, object_name)
  @segment = instruction[:segment].to_s
  @operation = instruction[:operation].to_s
  @index = instruction[:index].to_s
  @object_name = object_name
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



9
10
11
# File 'lib/hackasm/vm/instructions/memory_access_instruction.rb', line 9

def index
  @index
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



9
10
11
# File 'lib/hackasm/vm/instructions/memory_access_instruction.rb', line 9

def object_name
  @object_name
end

#operationObject (readonly)

Returns the value of attribute operation.



9
10
11
# File 'lib/hackasm/vm/instructions/memory_access_instruction.rb', line 9

def operation
  @operation
end

#segmentObject (readonly)

Returns the value of attribute segment.



9
10
11
# File 'lib/hackasm/vm/instructions/memory_access_instruction.rb', line 9

def segment
  @segment
end

Instance Method Details

#commentObject



31
32
33
# File 'lib/hackasm/vm/instructions/memory_access_instruction.rb', line 31

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

#to_asmObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hackasm/vm/instructions/memory_access_instruction.rb', line 18

def to_asm
  comment + case segment
  when *MemoryAccessOperations::ConstantOperation.segments
    MemoryAccessOperations::ConstantOperation.new(index).to_asm
  when *MemoryAccessOperations::StaticOperation.segments
    MemoryAccessOperations::StaticOperation.new(index, object_name).to_asm
  when *MemoryAccessOperations::VirtualSegmentOperation.segments
    MemoryAccessOperations::VirtualSegmentOperation.new(operation, segment, index).to_asm
  when *MemoryAccessOperations::FixedSegmentOperation.segments
    MemoryAccessOperations::FixedSegmentOperation.new(operation, segment, index).to_asm
  end.split("\n").map(&:strip).join("\n")
end