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



317
318
319
# File 'lib/t_ruby/compiler.rb', line 317

def initialize
  @output = []
end

Instance Method Details

#generate(program) ⇒ Object

Generate Ruby code from IR program



322
323
324
325
# File 'lib/t_ruby/compiler.rb', line 322

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

#generate_with_source(program, source) ⇒ Object

Generate Ruby code while preserving source structure



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/t_ruby/compiler.rb', line 328

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

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

  # Collect interface names to remove
  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.gsub(/\n{3,}/, "\n\n")
end