Sha256: 9f832b8d931265a957a7afa7a8e0d26582a8e1b6ebf0a7acc3fd17a64b6f5620

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require_relative './memory_access_operations/static_operation'
require_relative './memory_access_operations/constant_operation'
require_relative './memory_access_operations/fixed_segment_operation'
require_relative './memory_access_operations/virtual_segment_operation.rb'

module Vm
  module Instructions
    class MemoryAccessInstruction
      attr_reader :segment, :operation, :index, :object_name

      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

      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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hackasm-0.1.0 lib/hackasm/vm/instructions/memory_access_instruction.rb