Class: Kumi::Core::Analyzer::Passes::Codegen::Loop::Ruby::OutputBuffer
- Inherits:
-
Object
- Object
- Kumi::Core::Analyzer::Passes::Codegen::Loop::Ruby::OutputBuffer
- Defined in:
- lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb
Instance Method Summary collapse
- #dedent! ⇒ Object
- #emit_footer ⇒ Object
- #emit_header(module_name) ⇒ Object
- #indent! ⇒ Object
- #indented ⇒ Object
-
#initialize ⇒ OutputBuffer
constructor
A new instance of OutputBuffer.
- #last_line ⇒ Object
- #line_at(idx) ⇒ Object
- #reset! ⇒ Object
- #rewrite_line(new_line_content, idx = -2)) ⇒ Object
- #section(name) ⇒ Object
- #to_s ⇒ Object
- #write(s, indent = @indent) ⇒ Object
Constructor Details
#initialize ⇒ OutputBuffer
Returns a new instance of OutputBuffer.
11 12 13 14 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 11 def initialize @out = [] @indent = 0 end |
Instance Method Details
#dedent! ⇒ Object
23 24 25 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 23 def dedent! @indent -= 1 end |
#emit_footer ⇒ Object
33 34 35 36 37 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 33 def dedent! rewrite_line("end") if last_line == "end\n" write "end" end |
#emit_header(module_name) ⇒ Object
27 28 29 30 31 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 27 def emit_header(module_name) write "# Autogenerated by Kumi Codegen" write "module Kumi::Compiled::#{module_name}" indent! end |
#indent! ⇒ Object
19 20 21 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 19 def indent! @indent += 1 end |
#indented ⇒ Object
44 45 46 47 48 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 44 def indented indent! yield dedent! end |
#last_line ⇒ Object
54 55 56 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 54 def last_line line_at(-2) end |
#line_at(idx) ⇒ Object
58 59 60 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 58 def line_at(idx) @out[idx] end |
#reset! ⇒ Object
17 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 17 def reset! = @out.clear |
#rewrite_line(new_line_content, idx = -2)) ⇒ Object
62 63 64 65 66 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 62 def rewrite_line(new_line_content, idx = -2) return if @out.empty? @out[idx] = new_line_content end |
#section(name) ⇒ Object
39 40 41 42 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 39 def section(name) write "#{name}\n" yield end |
#to_s ⇒ Object
16 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 16 def to_s = @out.join |
#write(s, indent = @indent) ⇒ Object
50 51 52 |
# File 'lib/kumi/core/analyzer/passes/codegen/loop/ruby/output_buffer.rb', line 50 def write(s, indent = @indent) @out << (" " * indent) << s << "\n" end |