Module: Pegarus::Machine

Defined in:
lib/pegarus/machine.rb,
lib/pegarus/machine/state.rb,
lib/pegarus/machine/compiler.rb,
lib/pegarus/machine/interpreter.rb,
lib/pegarus/machine/instructions.rb

Defined Under Namespace

Modules: Instructions Classes: Compiler, State

Class Method Summary collapse

Class Method Details

.execute(program, subject) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pegarus/machine/interpreter.rb', line 3

def self.execute(program, subject)
  state = State.new subject
  _, fail = Instructions[:fail]

  total = program.size
  while state.ip < total
    break if state.fail?

    width, code = Instructions[program[state.ip]]
    case width
    when 1
      code[state]
    when 2
      code[state, program[state.ip+1]]
    when 3
      code[state, program[state.ip+1], program[state.ip+2]]
    end

    if state.fail?
      fail[state]
    else
      state.ip += width
    end
  end

  return state.failure? ? nil : state.index
end

.new_executor(pattern, subject) ⇒ Object



8
9
10
11
# File 'lib/pegarus/machine.rb', line 8

def self.new_executor(pattern, subject)
  Compiler.new.compile pattern
  pattern.match subject
end