Sha256: e0a8b205e2782049e0d84e2278f1e22763c91454dd77511ba6333776221bca11

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Tataru
  # representation of a set of instructions
  class InstructionHash
    def initialize(thehash)
      @thehash = thehash
    end

    def instruction_list
      @instruction_list ||= instructions
    end

    def to_h
      @thehash
    end

    def instructions
      return [] unless @thehash[:instructions]

      @thehash[:instructions].map do |action|
        if action == :init
          init_instruction
        elsif action.is_a? Hash
          # immediate mode instruction
          instruction_for(action.keys[0]).new(action.values[0])
        else
          instruction_for(action).new
        end
      end.to_a
    end

    def instruction_for(action)
      instr_const = "#{action}_instruction".camelize
      unless Tataru::Instructions.const_defined? instr_const
        raise "Unknown instruction '#{action}'"
      end

      Tataru::Instructions.const_get(instr_const)
    end

    def init_instruction
      init = Tataru::Instructions::InitInstruction.new

      inithash = @thehash[:init]
      if inithash
        %i[remote_ids outputs deleted rom labels].each do |member|
          init.send(:"#{member}=", inithash[member]) if inithash.key? member
        end
      end
      init
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tataru-0.2.0 lib/tataru/instruction_hash.rb