Sha256: 0d38f090cff7fa548b01e44f02e92c84bea6b836cea77f05551e16267bf3cd2e

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Tataru
  # thing that runs a quest
  class Runner
    attr_reader :memory, :oplog

    def initialize(instruction_list)
      @memory = Memory.new
      @instruction_list = instruction_list
      @oplog = []
    end

    def ended?
      @memory.program_counter >= @instruction_list.length ||
        !@memory.error.nil? ||
        @memory.end
    end

    def log_operation(instr)
      return unless instr.is_a? Instructions::ResourceInstruction

      oplog << {
        operation: instr.class.name.underscore
                        .sub(%r{^tataru/instructions/}, '')
                        .sub(/_instruction$/, '').upcase.to_s,
        resource: (memory.hash[:temp][:resource_name]).to_s
      }
    end

    def run_next
      return if ended?

      instr = @instruction_list[@memory.program_counter]

      log_operation(instr)

      instr.execute(@memory)
      @memory.program_counter += 1
    rescue RuntimeError => e
      @memory.error = e
    rescue StandardError => e
      @memory.error = e
    end

    def current_state; end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tataru-0.2.0 lib/tataru/runner.rb