Module: HRM::Instruction

Included in:
Machine
Defined in:
lib/hrm/instruction.rb

Instance Method Summary collapse

Instance Method Details

#add(address) ⇒ Object



36
37
38
# File 'lib/hrm/instruction.rb', line 36

def add(address)
  @value += @memory[address]
end

#bumpdown(address) ⇒ Object



48
49
50
# File 'lib/hrm/instruction.rb', line 48

def bumpdown(address)
  @value = @memory[address] -= 1
end

#bumpup(address) ⇒ Object



44
45
46
# File 'lib/hrm/instruction.rb', line 44

def bumpup(address)
  @value = @memory[address] += 1
end

#copyfrom(address) ⇒ Object



28
29
30
# File 'lib/hrm/instruction.rb', line 28

def copyfrom(address)
  @value = @memory[address]
end

#copyto(address) ⇒ Object



32
33
34
# File 'lib/hrm/instruction.rb', line 32

def copyto(address)
  @memory[address] = @value
end

#inboxObject



13
14
15
16
17
18
19
20
21
# File 'lib/hrm/instruction.rb', line 13

def inbox
  unless @value = STDIN.gets&.chomp
    exit(0)
  end

  if @value =~ /^[-+]?[0-9]+$/
    @value = @value.to_i
  end
end

#jump(address) ⇒ Object



52
53
54
# File 'lib/hrm/instruction.rb', line 52

def jump(address)
  @pc = address
end

#jump_if_neg(address) ⇒ Object



62
63
64
65
66
# File 'lib/hrm/instruction.rb', line 62

def jump_if_neg(address)
  if @value.negative?
    @pc = address
  end
end

#jump_if_zero(address) ⇒ Object



56
57
58
59
60
# File 'lib/hrm/instruction.rb', line 56

def jump_if_zero(address)
  if @value.zero?
    @pc = address
  end
end

#outboxObject



23
24
25
26
# File 'lib/hrm/instruction.rb', line 23

def outbox
  puts @value
  @value = nil
end

#sub(address) ⇒ Object



40
41
42
# File 'lib/hrm/instruction.rb', line 40

def sub(address)
  @value -= @memory[address]
end