Module: Pushdown::Automaton::InstanceMethods

Defined in:
lib/pushdown/automaton.rb

Overview

A mixin to add instance methods for setting up pushdown states.

Instance Method Summary collapse

Instance Method Details

#handle_pushdown_result(stack, result, name) ⇒ Object

The body of the event handler, called by the #handle_name_event.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pushdown/automaton.rb', line 62

def handle_pushdown_result( stack, result, name )
  if result.is_a?( Symbol )
    current_state = stack.last
    result = current_state.transition( result, self, name )
  end

  if result.is_a?( Pushdown::Transition )
    new_stack = result.apply( stack )
    stack.replace( new_stack )
  end

  return result
end

#initializeObject

Overload the initializer to push the initial state object for any pushdown states.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pushdown/automaton.rb', line 37

def initialize( * )
  super if defined?( super )

  self.class.pushdown_states.each do |name, config|
    self.log.debug "Pushing initial %s" % [ name ]

    state_class = self.class.public_send( "initial_#{name}" ) or
      raise "unset initial_%s while pushing initial state" % [ name ]

    data = if self.respond_to?( "initial_#{name}_data" )
        self.public_send( "initial_#{name}_data" )
      else
        nil
      end

    self.log.info "Pushing an instance of %p as the initial state." % [ state_class ]
    transition = Pushdown::Transition.create( :push, :initial, state_class, data )
    self.log.debug " applying %p to an new empty stack" % [ transition ]
    stack = transition.apply( [] )
    self.instance_variable_set( "@#{name}_stack", stack )
  end
end