Class: Kumi::Core::Analyzer::Passes::Codegen::Ruby::OutputBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb

Instance Method Summary collapse

Constructor Details

#initializeOutputBuffer

Returns a new instance of OutputBuffer.



10
11
12
13
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 10

def initialize
  @out = []
  @indent = 0
end

Instance Method Details

#dedent!Object



22
23
24
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 22

def dedent!
  @indent -= 1
end

#emit_class_methods(_decl_names) ⇒ Object



38
39
40
41
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 38

def emit_class_methods(_decl_names)
  # No boilerplate needed anymore - all declarations are pure module functions
  # This method remains for backward compatibility with the emit pipeline
end


32
33
34
35
36
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 32

def emit_footer
  dedent!
  rewrite_line("end") if last_line == "end\n"
  write "end"
end

#emit_header(module_name) ⇒ Object



26
27
28
29
30
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 26

def emit_header(module_name)
  write "# Autogenerated by Kumi Codegen"
  write "module Kumi::Compiled::#{module_name}"
  indent!
end

#indent!Object



18
19
20
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 18

def indent!
  @indent += 1
end

#indentedObject



48
49
50
51
52
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 48

def indented
  indent!
  yield
  dedent!
end

#last_lineObject



58
59
60
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 58

def last_line
  line_at(-2)
end

#line_at(idx) ⇒ Object



62
63
64
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 62

def line_at(idx)
  @out[idx]
end

#reset!Object



16
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 16

def reset! = @out.clear

#rewrite_line(new_line_content, idx = -2)) ⇒ Object

Replaces the last line in the buffer.



67
68
69
70
71
72
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 67

def rewrite_line(new_line_content, idx = -2)
  return if @out.empty?

  # Preserve the indentation of the line being replaced.
  @out[idx] = new_line_content
end

#section(name) ⇒ Object



43
44
45
46
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 43

def section(name)
  write "#{name}\n"
  yield
end

#to_sObject



15
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 15

def to_s = @out.join

#write(s, indent = @indent) ⇒ Object



54
55
56
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/output_buffer.rb', line 54

def write(s, indent = @indent)
  @out << ("  " * indent) << s << "\n"
end