Module: Lernen::Automaton::ProcUtil

Defined in:
lib/lernen/automaton/proc_util.rb

Overview

ProcUtil provides utility functions for words in ‘SPA`.

Class Method Summary collapse

Class Method Details

.expand(return_input, word, proc_to_terminating_sequence) ⇒ Object

: [In, Call, Return] (

  Return return_input,
  Array[In | Call] word,
  Hash[Call, Array[In | Call | Return]] proc_to_terminating_sequence,
) -> Array[In | Call | Return]


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lernen/automaton/proc_util.rb', line 13

def self.expand(return_input, word, proc_to_terminating_sequence)
  expanded_word = []
  word.each do |input|
    terminating_sequence = proc_to_terminating_sequence[input] # steep:ignore
    if terminating_sequence
      expanded_word << input
      expanded_word.concat(terminating_sequence)
      expanded_word << return_input
    else
      expanded_word << input
    end
  end
  expanded_word
end

.find_call_index(call_alphabet_set, return_input, word, index) ⇒ Object

: [In, Call, Return] (

  Set[Call] call_alphabet_set,
  Return return_input,
  Array[In | Call | Return] word,
  Integer index
) -> (Integer | nil)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lernen/automaton/proc_util.rb', line 54

def self.find_call_index(call_alphabet_set, return_input, word, index)
  balance = 0

  (index - 1).downto(0) do |i|
    input = word[i]
    if input == return_input # steep:ignore
      balance += 1
    elsif call_alphabet_set.include?(input) # steep:ignore
      return i if balance == 0
      balance -= 1
    end
  end

  nil
end

.find_return_index(call_alphabet_set, return_input, word, index) ⇒ Object

: [In, Call, Return] (

  Set[Call] call_alphabet_set,
  Return return_input,
  Array[In | Call | Return] word,
  Integer index
) -> (Integer | nil)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lernen/automaton/proc_util.rb', line 76

def self.find_return_index(call_alphabet_set, return_input, word, index)
  balance = 0

  (index...word.size).each do |i|
    input = word[i]
    if call_alphabet_set.include?(input) # steep:ignore
      balance += 1
    elsif input == return_input # steep:ignore
      return i if balance == 0
      balance -= 1
    end
  end

  nil
end

.project(call_alphabet_set, return_input, word) ⇒ Object

: [In, Call, Return] (

  Set[Call] call_alphabet_set,
  Return return_input,
  Array[In | Call | Return] word
) -> Array[In | Call]


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lernen/automaton/proc_util.rb', line 33

def self.project(call_alphabet_set, return_input, word)
  projected_word = []
  index = 0
  while index < word.size
    input = word[index]
    projected_word << input
    if call_alphabet_set.include?(input) # steep:ignore
      return_index = find_return_index(call_alphabet_set, return_input, word, index + 1)
      index = return_index if return_index
    end
    index += 1
  end
  projected_word
end