Class: MicroCisc::Compile::LabelGenerator
- Inherits:
-
Object
- Object
- MicroCisc::Compile::LabelGenerator
- Defined in:
- lib/micro_cisc/compile/label_generator.rb
Instance Method Summary collapse
- #end_label ⇒ Object
-
#initialize ⇒ LabelGenerator
constructor
A new instance of LabelGenerator.
- #last_open ⇒ Object
- #pop_context ⇒ Object
- #push_context ⇒ Object
- #start_label ⇒ Object
Constructor Details
#initialize ⇒ LabelGenerator
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_label ⇒ Object
18 19 20 |
# File 'lib/micro_cisc/compile/label_generator.rb', line 18 def end_label last_open.sub('{', '}') end |
#last_open ⇒ Object
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_context ⇒ Object
14 15 16 |
# File 'lib/micro_cisc/compile/label_generator.rb', line 14 def pop_context @labels << last_open.sub('{', '}') end |
#push_context ⇒ Object
9 10 11 12 |
# File 'lib/micro_cisc/compile/label_generator.rb', line 9 def push_context @count += 1 @labels << "{#{@count}" end |
#start_label ⇒ Object
22 23 24 |
# File 'lib/micro_cisc/compile/label_generator.rb', line 22 def start_label last_open end |