class HRM::Machine
Constants
- MAX_MEMORY_SIZE
Public Class Methods
new(source)
click to toggle source
# File lib/hrm/machine.rb, line 11 def initialize(source) @source = source @im = [nil] + parse_source_code @value = nil @pc = 1 @memory = Array.new(MAX_MEMORY_SIZE) end
run(filepath)
click to toggle source
# File lib/hrm/machine.rb, line 7 def self.run(filepath) new(File.read(filepath)).run end
Public Instance Methods
run()
click to toggle source
# File lib/hrm/machine.rb, line 19 def run loop do instruction, arg = @im[@pc] @pc += 1 exit(0) if instruction.nil? if arg.nil? public_send(instruction) else if arg =~ /\[\s*(\d+)\s*\]/ public_send(instruction, @memory[$1.to_i]) else public_send(instruction, arg.to_i) end end end end
Private Instance Methods
parse_source_code()
click to toggle source
# File lib/hrm/machine.rb, line 40 def parse_source_code @source.split(/\R/).map do |line| if line.start_with?("#") nil else instruction, arg = line.split [instruction.downcase.to_sym, arg] end end.compact end