Sha256: c3a6c10ae7b3385fcfc50560527be5d22161221fb418327f3df02e2b60496299

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require_relative '../command'
require_relative '../vm/translator'

module Hackasm
  module Commands
    class Vm2Asm < Hackasm::Command
      attr_reader :base_path

      def initialize(base_path, options)
        @base_path = base_path
        @options = options
      end

      def execute(input: $stdin, output: $stdout)
        if File.directory?(base_path)
          assembler_code = Dir[File.join(base_path, "*")].inject do |code_buffer, path|
            object_name = File.basename(path, ".vm")
            vm_code = File.read(path)
            code_buffer + Vm::Translator.new(vm_code, object_name).translate
          end
          file_name = "#{base_path}.asm"
        else
          object_name = File.basename(base_path, ".vm")
          vm_code = File.read(base_path)
          assembler_code = Vm::Translator.new(vm_code, object_name).translate
          file_name = File.join(File.dirname(base_path), "#{object_name}.asm")
        end

        output.puts assembler_code
        File.write(file_name, assembler_code)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hackasm-0.1.0 lib/hackasm/commands/vm2asm.rb