Class: Tataru::Compiler

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

Overview

compiler

Instance Method Summary collapse

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

#deletablesObject



54
55
56
# File 'lib/tataru/compiler.rb', line 54

def deletables
  @extant.reject { |k, _| @dsl.resources.key? k }
end

#generate_labelsObject



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_subroutinesObject



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_instructionsObject



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_hashObject



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

#labelsObject



24
25
26
# File 'lib/tataru/compiler.rb', line 24

def labels
  @labels ||= generate_labels
end

#subroutine_instructionsObject



40
41
42
43
# File 'lib/tataru/compiler.rb', line 40

def subroutine_instructions
  @subroutine_instructions ||=
    subroutines.values.flat_map(&:body_instructions)
end

#subroutinesObject



28
29
30
# File 'lib/tataru/compiler.rb', line 28

def subroutines
  @subroutines ||= generate_subroutines
end

#top_instructionsObject



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

#updatablesObject



58
59
60
# File 'lib/tataru/compiler.rb', line 58

def updatables
  @dsl.resources
end