# File lib/parslet/bytecode/vm.rb, line 13
    def run(program, io)
      init(program, io)

      loop do
        old_ip = @ip
        instruction = fetch
        break unless instruction
        
        # Diagnostics
        printf("executing %5d: %s\n", old_ip, instruction) if debug?

        # Run the current instruction
        instruction.run(self)
        
        # Diagnostics
        dump_state(0) if debug?
        break if @stop
      end

      fail "Stack contains too many values." if @values.size>1

      # In the best case, we have successfully matched and consumed all input. 
      # This is what we want, from now on down it's all error cases.
      return flatten(@values.last) if success? && source.eof?

      # Maybe we've matched some, but not all of the input? In parslets books, 
      # this is an error as well. 
      if success?
        # assert: not source.eof?
        current_pos = source.pos
        source.error(
          "Don't know what to do with #{source.read(100)}", current_pos).
          raise(Parslet::UnconsumedInput)
      end

      # assert: ! @error.nil?

      # And maybe we just could not do it for a reason. Raise that. 
      @error.raise
      
    rescue => ex
      dump_state(-1) unless ex.kind_of?(Parslet::ParseFailed)
      raise
    end