Module: HackAssembler::CInstruction

Defined in:
lib/hack_assembler/c_instruction.rb

Class Method Summary collapse

Class Method Details

.translate(instruction) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/hack_assembler/c_instruction.rb', line 3

def self.translate(instruction)
  matched = /((.+)=)?([^;]+)(;([A-Z]+))?/.match(instruction)
  destination = matched[2]
  computation = matched[3]
  jump = matched[5]

  bytecode = '1110000000000000'
  bytecode[3..9] = ComputationTranslator.translate(computation)
  bytecode[10..12] = DestinationTranslator.translate(destination)
  bytecode[13..15] = JumpTranslator.translate(jump)

  bytecode
end