class TuringMachine::InstructionsParser

Public: Parser for a newline separated instruction set.

Instruction format


Here is an example file with four instructions :

“` 0 A => 1 R B 1 A => 1 L B 0 B => 1 L A 1 B => 1 R HALT “`

So each instruction is :

“` scanned-symbol current-state => symbol-to-write move next-state “`

`symbol-to-write` is usually `1` or `0`. Note that `N` is a special symbol meaning «write nothing».

`move` could be either `L` for left, `R` for right, or `N` for no movement.

Public Class Methods

new(raw_instructions) click to toggle source
# File lib/turing_machine/instructions_parser.rb, line 30
def initialize(raw_instructions)
  @lines = raw_instructions.split("\n")
  @instructions = {}
end

Public Instance Methods

parse() click to toggle source
# File lib/turing_machine/instructions_parser.rb, line 35
def parse
  build_instructions
  @instructions
end

Private Instance Methods

build_instructions() click to toggle source
# File lib/turing_machine/instructions_parser.rb, line 42
def build_instructions
  @lines.each do |instruction|
    keys, value = instruction.split('=>')
    key_symbol, key_state = keys.split
    write, move, next_state = value.split
    @instructions[[key_symbol, key_state]] = {
      write: write, move: move, next_state: next_state
    }
  end
end