class Alan::FSM

Public Class Methods

define(&block) click to toggle source
# File lib/alan/fsm.rb, line 3
def self.define(&block)
  word = ARGV.first
  unless word
    puts 'Error: You must enter a word to process'
    exit 1
  end
  
  fsm = new
  fsm.instance_eval &block
  fsm.run(word)
end
new() click to toggle source
# File lib/alan/fsm.rb, line 15
def initialize
  @states = {}
end

Public Instance Methods

accept() click to toggle source
# File lib/alan/fsm.rb, line 41
def accept
  puts 'ACCEPTED'
  exit 0
end
goto(s) click to toggle source
# File lib/alan/fsm.rb, line 31
def goto(s)
  @states[s].call(next_symbol)
  reject
end
next_symbol() click to toggle source
# File lib/alan/fsm.rb, line 19
def next_symbol
  @symbols.shift
end
reject() click to toggle source
# File lib/alan/fsm.rb, line 46
def reject
  puts 'REJECTED'
  exit 0
end
run(symbols) click to toggle source
# File lib/alan/fsm.rb, line 36
def run(symbols)
  @symbols = symbols.chars.to_a
  goto @initial_state
end
start(s) click to toggle source
# File lib/alan/fsm.rb, line 23
def start(s)
  @initial_state = s
end
state(s, &block) click to toggle source
# File lib/alan/fsm.rb, line 27
def state(s, &block)
  @states[s] = block
end