Class: Ibex::TableSimulation::Simulator
- Inherits:
-
Object
- Object
- Ibex::TableSimulation::Simulator
- Defined in:
- lib/ibex/table_simulation/simulator.rb
Overview
Deterministic, semantic-action-free table interpreter.
Constant Summary collapse
- DEFAULT_MAX_STEPS =
: Integer
100_000- DEFAULT_MAX_STACK =
: Integer
10_000- EOF_NAME =
: String
"$eof"- ERROR_NAME =
: String
"error"- IMPLICIT_ERROR =
: IR::error_action
{ type: :error }.freeze
Instance Attribute Summary collapse
-
#automaton ⇒ Object
readonly
: IR::Automaton.
-
#eof ⇒ Object
readonly
: IR::GrammarSymbol.
-
#max_stack ⇒ Object
readonly
: Integer.
-
#max_steps ⇒ Object
readonly
: Integer.
Instance Method Summary collapse
-
#initialize(automaton, max_steps: DEFAULT_MAX_STEPS, max_stack: DEFAULT_MAX_STACK) ⇒ Simulator
constructor
A new instance of Simulator.
- #simulate(tokens) ⇒ Object
- #start ⇒ Object
- #terminal(spelling) ⇒ Object
Constructor Details
#initialize(automaton, max_steps: DEFAULT_MAX_STEPS, max_stack: DEFAULT_MAX_STACK) ⇒ Simulator
Returns a new instance of Simulator.
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
#automaton ⇒ Object (readonly)
: IR::Automaton
16 17 18 |
# File 'lib/ibex/table_simulation/simulator.rb', line 16 def automaton @automaton end |
#eof ⇒ Object (readonly)
: IR::GrammarSymbol
19 20 21 |
# File 'lib/ibex/table_simulation/simulator.rb', line 19 def eof @eof end |
#max_stack ⇒ Object (readonly)
: Integer
18 19 20 |
# File 'lib/ibex/table_simulation/simulator.rb', line 18 def max_stack @max_stack end |
#max_steps ⇒ Object (readonly)
: Integer
17 18 19 |
# File 'lib/ibex/table_simulation/simulator.rb', line 17 def max_steps @max_steps end |
Instance Method Details
#simulate(tokens) ⇒ Object
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 |
#start ⇒ Object
35 36 37 |
# File 'lib/ibex/table_simulation/simulator.rb', line 35 def start Session.new(self) end |
#terminal(spelling) ⇒ Object
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 |