Class: Tataru::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/tataru/runner.rb

Overview

thing that runs a quest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instruction_list) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
# File 'lib/tataru/runner.rb', line 8

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

Instance Attribute Details

#memoryObject (readonly)

Returns the value of attribute memory.



6
7
8
# File 'lib/tataru/runner.rb', line 6

def memory
  @memory
end

#oplogObject (readonly)

Returns the value of attribute oplog.



6
7
8
# File 'lib/tataru/runner.rb', line 6

def oplog
  @oplog
end

Instance Method Details

#current_stateObject



46
# File 'lib/tataru/runner.rb', line 46

def current_state; end

#ended?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/tataru/runner.rb', line 14

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

#log_operation(instr) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/tataru/runner.rb', line 20

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

#run_nextObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tataru/runner.rb', line 31

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