Sha256: 4cd8bff2ae0c3859af6459a6117f9e1cf3d9038909e53e53828eaac01d6cd1f5

Contents?: true

Size: 933 Bytes

Versions: 1

Compression:

Stored size: 933 Bytes

Contents

require_relative './arithmetic_operations/comparison'
require_relative './arithmetic_operations/binary_operation'
require_relative './arithmetic_operations/unary_operation'

module Vm
  module Instructions
    class ArithmeticInstruction
      attr_reader :operation

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

      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

      def comment
        "// #{operation}\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/arithmetic_instruction.rb