Class: MicroCisc::Compile::LabelGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/micro_cisc/compile/label_generator.rb

Instance Method Summary collapse

Constructor Details

#initializeLabelGenerator

Returns a new instance of LabelGenerator.



4
5
6
7
# File 'lib/micro_cisc/compile/label_generator.rb', line 4

def initialize
  @labels = []
  @count = 0
end

Instance Method Details

#end_labelObject



18
19
20
# File 'lib/micro_cisc/compile/label_generator.rb', line 18

def end_label
  last_open.sub('{', '}')
end

#last_openObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/micro_cisc/compile/label_generator.rb', line 26

def last_open
  if @labels.empty?
    raise ArgumentException, "No open label context"
  end
  # Go backwards until we find an open that we didn't see the close for first
  i = @labels.size - 1
  closed = nil
  while(i >= 0)
    if @labels[i].start_with?('}')
      closed = @labels[i]
    elsif !closed
      return @labels[i]
    elsif closed && @labels[i].end_with?(closed[1..-1])
      closed = nil
    else
      raise 'Invalid state, contexts are out of order'
    end
    i -= 1
  end
end

#pop_contextObject



14
15
16
# File 'lib/micro_cisc/compile/label_generator.rb', line 14

def pop_context
  @labels << last_open.sub('{', '}')
end

#push_contextObject



9
10
11
12
# File 'lib/micro_cisc/compile/label_generator.rb', line 9

def push_context
  @count += 1
  @labels << "{#{@count}"
end

#start_labelObject



22
23
24
# File 'lib/micro_cisc/compile/label_generator.rb', line 22

def start_label
  last_open
end