Class: Ibex::TableSimulation::Simulator

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/table_simulation/simulator.rb,
sig/ibex/table_simulation/simulator.rbs

Overview

Deterministic, semantic-action-free table interpreter.

Constant Summary collapse

DEFAULT_MAX_STEPS =

Signature:

  • Integer

Returns:

  • (Integer)
100_000
DEFAULT_MAX_STACK =

Signature:

  • Integer

Returns:

  • (Integer)
10_000
EOF_NAME =

Signature:

  • String

Returns:

  • (String)
"$eof"
ERROR_NAME =

Signature:

  • String

Returns:

  • (String)
"error"
IMPLICIT_ERROR =

Signature:

  • IR::error_action

Returns:

  • (IR::error_action)
{ type: :error }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(automaton, max_steps: DEFAULT_MAX_STEPS, max_stack: DEFAULT_MAX_STACK) ⇒ Simulator

Returns a new instance of Simulator.

RBS:

  • (IR::Automaton automaton, ?max_steps: Integer, ?max_stack: Integer) -> void

Parameters:

  • automaton (IR::Automaton)
  • max_steps: (Integer) (defaults to: DEFAULT_MAX_STEPS)
  • max_stack: (Integer) (defaults to: DEFAULT_MAX_STACK)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ibex/table_simulation/simulator.rb', line 22

def initialize(automaton, max_steps: DEFAULT_MAX_STEPS, max_stack: DEFAULT_MAX_STACK)
  raise ArgumentError, "max_steps must be positive" unless max_steps.positive?
  raise ArgumentError, "max_stack must be positive" unless max_stack.positive?

  @automaton = automaton
  @max_steps = max_steps
  @max_stack = max_stack
  @terminals = automaton.grammar.terminals.to_h { |symbol| [symbol.name, symbol] }.freeze
  @terminal_aliases = terminal_aliases(automaton.grammar.terminals).freeze
  @eof = @terminals[EOF_NAME] || raise(Ibex::Error, "(debug):1:1: automaton has no $eof terminal")
end

Instance Attribute Details

#automatonIR::Automaton (readonly)

Signature:

  • IR::Automaton

Returns:



16
17
18
# File 'lib/ibex/table_simulation/simulator.rb', line 16

def automaton
  @automaton
end

#eofIR::GrammarSymbol (readonly)

Signature:

  • IR::GrammarSymbol

Returns:



19
20
21
# File 'lib/ibex/table_simulation/simulator.rb', line 19

def eof
  @eof
end

#max_stackInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


18
19
20
# File 'lib/ibex/table_simulation/simulator.rb', line 18

def max_stack
  @max_stack
end

#max_stepsInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


17
18
19
# File 'lib/ibex/table_simulation/simulator.rb', line 17

def max_steps
  @max_steps
end

Instance Method Details

#simulate(tokens) ⇒ Result

RBS:

  • (Array[String] tokens) -> Result

Parameters:

  • tokens (Array[String])

Returns:



40
41
42
43
44
45
46
47
# File 'lib/ibex/table_simulation/simulator.rb', line 40

def simulate(tokens)
  session = start
  tokens.each do |token|
    session.push(token)
    break if session.status
  end
  session.finish
end

#startSession

RBS:

  • () -> Session

Returns:



35
36
37
# File 'lib/ibex/table_simulation/simulator.rb', line 35

def start
  Session.new(self)
end

#terminal(spelling) ⇒ IR::GrammarSymbol

RBS:

  • (String spelling) -> IR::GrammarSymbol

Parameters:

  • spelling (String)

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ibex/table_simulation/simulator.rb', line 50

def terminal(spelling)
  if [EOF_NAME, ERROR_NAME].include?(spelling)
    raise Ibex::Error, "(debug):1:1: reserved terminal #{spelling.inspect} cannot be supplied"
  end

  terminal = @terminals[spelling]
  return terminal if terminal

  empty = [] #: Array[IR::GrammarSymbol]
  aliases = @terminal_aliases.fetch(spelling, empty)
  return aliases.first if aliases.length == 1

  if aliases.length > 1
    names = aliases.map(&:name).sort.join(", ")
    raise Ibex::Error, "(debug):1:1: terminal display name #{spelling.inspect} is ambiguous: #{names}"
  end

  raise Ibex::Error, "(debug):1:1: unknown terminal #{spelling.inspect}"
end

#terminal_aliases(terminals) ⇒ Hash[String, Array[IR::GrammarSymbol]]

RBS:

  • (Array[IR::GrammarSymbol] terminals) -> Hash[String, Array[IR::GrammarSymbol]]

Parameters:

Returns:



73
74
75
76
77
78
79
80
# File 'lib/ibex/table_simulation/simulator.rb', line 73

def terminal_aliases(terminals)
  aliases = Hash.new { |hash, key| hash[key] = [] } #: Hash[String, Array[IR::GrammarSymbol]]
  terminals.each do |terminal|
    aliases[terminal.display_name] << terminal if terminal.display_name
  end
  aliases.each_value(&:freeze)
  aliases
end