Class: Hoozuki::VM::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/hoozuki/vm/evaluator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.evaluate(instructions, input, input_pos = 0, pc = 0) ⇒ Object



7
8
9
# File 'lib/hoozuki/vm/evaluator.rb', line 7

def evaluate(instructions, input, input_pos = 0, pc = 0)
  new._evaluate(instructions, input, input_pos, pc)
end

Instance Method Details

#_evaluate(instructions, input, input_pos, pc) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hoozuki/vm/evaluator.rb', line 12

def _evaluate(instructions, input, input_pos, pc)
  loop do
    return false if pc >= instructions.size

    inst = instructions[pc]
    case inst
    when Hoozuki::Instruction::Char
      return false if input_pos >= input.size || input[input_pos] != inst.char

      input_pos += 1
      pc += 1
    when Hoozuki::Instruction::Jmp
      pc = inst.target
    when Hoozuki::Instruction::Split
      return true if _evaluate(instructions, input, input_pos, inst.left)

      pc = inst.right

    when Hoozuki::Instruction::Match
      return input_pos == input.length
    else
      raise "Unknown instruction: #{inst.class}"
    end
  end
end