Class: TRuby::IRCodeGenerator

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

Overview

IR-aware code generator for source-preserving transformation

Instance Method Summary collapse

Constructor Details

#initializeIRCodeGenerator

Returns a new instance of IRCodeGenerator.



173
174
175
# File 'lib/t_ruby/compiler.rb', line 173

def initialize
  @output = []
end

Instance Method Details

#generate(program) ⇒ Object

Generate Ruby code from IR program



178
179
180
181
# File 'lib/t_ruby/compiler.rb', line 178

def generate(program)
  generator = IR::CodeGenerator.new
  generator.generate(program)
end

#generate_with_source(program, source) ⇒ Object

Generate Ruby code while preserving source structure



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/t_ruby/compiler.rb', line 184

def generate_with_source(program, source)
  result = source.dup

  # Collect type alias names to remove
  type_alias_names = program.declarations
    .select { |d| d.is_a?(IR::TypeAlias) }
    .map(&:name)

  # Collect interface names to remove
  interface_names = program.declarations
    .select { |d| d.is_a?(IR::Interface) }
    .map(&:name)

  # Remove type alias definitions
  result = result.gsub(/^\s*type\s+\w+\s*=\s*.+?$\n?/, '')

  # Remove interface definitions (multi-line)
  result = result.gsub(/^\s*interface\s+\w+.*?^\s*end\s*$/m, '')

  # Remove parameter type annotations using IR info
  # Enhanced: Handle complex types (generics, unions, etc.)
  result = erase_parameter_types(result)

  # Remove return type annotations
  result = erase_return_types(result)

  # Clean up extra blank lines
  result = result.gsub(/\n{3,}/, "\n\n")

  result
end