Class: Noofakku::OpenBracket

Inherits:
Object
  • Object
show all
Defined in:
lib/noofakku/instruction/open_bracket.rb

Instance Method Summary collapse

Constructor Details

#initialize(open, closed) ⇒ OpenBracket

Returns a new instance of OpenBracket.



5
6
7
# File 'lib/noofakku/instruction/open_bracket.rb', line 5

def initialize(open, closed)
  @open, @closed = open, closed
end

Instance Method Details

#perform(processor, memory, program, input, output) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/noofakku/instruction/open_bracket.rb', line 9

def perform(processor, memory, program, input, output)
  raise "processor cannot be nil" unless processor != nil
  raise "memory cannot be nil" unless memory != nil
  raise "program cannot be nil" unless program != nil
  raise "instruction pointer must point to a '#{@open}'" unless program[processor.instruction_pointer] == @open
  if memory[processor.data_pointer] != 0 then return end
  unmatched_brackets = 1
  pointer_to_current_instruction = processor.instruction_pointer
  until unmatched_brackets == 0
  	processor.instruction_pointer += 1
  	if processor.instruction_pointer == program.length then raise "no matching '#{@closed}' for '#{@open}' at #{pointer_to_current_instruction}"
  	elsif program[processor.instruction_pointer] == @open then unmatched_brackets += 1
  	elsif program[processor.instruction_pointer] == @closed then unmatched_brackets -= 1
    end
  end
end