Module: HackAssembler::Assembler

Defined in:
lib/hack_assembler/assembler.rb

Class Method Summary collapse

Class Method Details

.translate(source_code) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hack_assembler/assembler.rb', line 3

def self.translate(source_code)
  machine_code = ''

  source_code_extract = source_code.gsub(/^[\s]*$\n/, '')

  source_code_extract.each_line do |line|
    next if line.start_with? '//'
    
    clean_line = line.strip

    instruction = clean_line.start_with?('@') ? AInstruction : CInstruction

    machine_code << instruction.translate(clean_line) << "\n"
  end

  machine_code
end