class TuringMachine::Instance

Public: An instance of a Turing machine.

Public Class Methods

new(instructions, initial_state, tape = Tape.new) click to toggle source
# File lib/turing_machine/instance.rb, line 6
def initialize(instructions, initial_state, tape = Tape.new)
  @instructions = Instructions.new(instructions)
  @state = StateRegister.new(initial_state)
  @tape = tape
  @sequence = 1
end

Public Instance Methods

halted?() click to toggle source
# File lib/turing_machine/instance.rb, line 25
def halted?
  @state.current == 'HALT'
end
proceed() click to toggle source
# File lib/turing_machine/instance.rb, line 18
def proceed
  current = action
  update_sequence
  update_tape(current)
  update_state(current)
end
to_s() click to toggle source
# File lib/turing_machine/instance.rb, line 13
def to_s
  "#{'%3d' % @sequence} #{@tape} #{@state}#{instr_to_s}\n    " +
  ' ' * @tape.index + '^'
end

Private Instance Methods

action() click to toggle source
# File lib/turing_machine/instance.rb, line 45
def action
  @instructions.get(@tape.head, @state.current)
end
instr_to_s() click to toggle source
# File lib/turing_machine/instance.rb, line 49
def instr_to_s
  if halted?
    ''
  else
    " -> " + action[:write] + action[:move] + action[:next_state]
  end
end
update_sequence() click to toggle source
# File lib/turing_machine/instance.rb, line 31
def update_sequence
  @sequence += 1
end
update_state(current_action) click to toggle source
# File lib/turing_machine/instance.rb, line 41
def update_state(current_action)
  @state.change(current_action[:next_state])
end
update_tape(current_action) click to toggle source
# File lib/turing_machine/instance.rb, line 35
def update_tape(current_action)
  @tape.head = current_action[:write] unless current_action[:write] == 'N'
  @tape.shift_left if current_action[:move] == 'L'
  @tape.shift_right if current_action[:move] == 'R'
end