Class: Hackasm::Commands::Vm2Asm

Inherits:
Hackasm::Command show all
Defined in:
lib/hackasm/commands/vm2asm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hackasm::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(base_path, options) ⇒ Vm2Asm

Returns a new instance of Vm2Asm.



11
12
13
14
# File 'lib/hackasm/commands/vm2asm.rb', line 11

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

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



9
10
11
# File 'lib/hackasm/commands/vm2asm.rb', line 9

def base_path
  @base_path
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hackasm/commands/vm2asm.rb', line 16

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