Class: Tataru::Compiler
- Inherits:
-
Object
- Object
- Tataru::Compiler
- Defined in:
- lib/tataru/compiler.rb
Overview
compiler
Instance Method Summary collapse
- #deletables ⇒ Object
- #generate_labels ⇒ Object
- #generate_step_order(order, steps) ⇒ Object
- #generate_subroutines ⇒ Object
- #generate_top_instructions ⇒ Object
-
#initialize(dsl, extant_resources = {}, extant_dependencies = {}) ⇒ Compiler
constructor
A new instance of Compiler.
- #instr_hash ⇒ Object
- #labels ⇒ Object
- #subroutine_instructions ⇒ Object
- #subroutines ⇒ Object
- #top_instructions ⇒ Object
- #updatables ⇒ Object
Constructor Details
#initialize(dsl, extant_resources = {}, extant_dependencies = {}) ⇒ Compiler
Returns a new instance of Compiler.
8 9 10 11 12 |
# File 'lib/tataru/compiler.rb', line 8 def initialize(dsl, extant_resources = {}, extant_dependencies = {}) @dsl = dsl @extant = extant_resources @extant_dependencies = extant_dependencies end |
Instance Method Details
#deletables ⇒ Object
54 55 56 |
# File 'lib/tataru/compiler.rb', line 54 def deletables @extant.reject { |k, _| @dsl.resources.key? k } end |
#generate_labels ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/tataru/compiler.rb', line 45 def generate_labels count = 0 subroutines.values.map do |sub| arr = [sub.label, top_instructions.count + count] count += sub.body_instructions.count arr end.to_h end |
#generate_step_order(order, steps) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/tataru/compiler.rb', line 81 def generate_step_order(order, steps) instructions = [] order.each do |level| steps.each do |step| instructions += level.map do |item| subroutines["#{item}_#{step}"].call_instruction end end end instructions end |
#generate_subroutines ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tataru/compiler.rb', line 62 def generate_subroutines result = {} # set up resources for deletion deletables.each do |k, v| desc = Tataru.const_get(v).new rrep = Representations::ResourceRepresentation.new(k, desc, {}) sp = SubPlanner.new(rrep, :delete) result.merge!(sp.subroutines) end # set up resources for updates or creates updatables.each do |k, rrep| action = @extant.key?(k) ? :update : :create sp = SubPlanner.new(rrep, action) result.merge!(sp.subroutines) end result end |
#generate_top_instructions ⇒ Object
93 94 95 96 97 98 |
# File 'lib/tataru/compiler.rb', line 93 def generate_top_instructions order = Bunny::Tsort.tsort(@extant_dependencies.merge(@dsl.dep_graph)) generate_step_order(order, %i[start check]) + generate_step_order(order.reverse, %i[commit finish]) end |
#instr_hash ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/tataru/compiler.rb', line 14 def instr_hash { init: { **InitHashCompiler.new(@dsl).result, labels: labels }, instructions: top_instructions + subroutine_instructions } end |
#labels ⇒ Object
24 25 26 |
# File 'lib/tataru/compiler.rb', line 24 def labels @labels ||= generate_labels end |
#subroutine_instructions ⇒ Object
40 41 42 43 |
# File 'lib/tataru/compiler.rb', line 40 def subroutine_instructions @subroutine_instructions ||= subroutines.values.flat_map(&:body_instructions) end |
#subroutines ⇒ Object
28 29 30 |
# File 'lib/tataru/compiler.rb', line 28 def subroutines @subroutines ||= generate_subroutines end |
#top_instructions ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/tataru/compiler.rb', line 32 def top_instructions @top_instructions ||= [ :init, *generate_top_instructions, :end ] end |
#updatables ⇒ Object
58 59 60 |
# File 'lib/tataru/compiler.rb', line 58 def updatables @dsl.resources end |