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.



437
438
439
# File 'lib/t_ruby/compiler.rb', line 437

def initialize
  @output = []
end

Instance Method Details

#generate(program) ⇒ Object

Generate Ruby code from IR program



442
443
444
445
# File 'lib/t_ruby/compiler.rb', line 442

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

#generate_with_source(program, source) ⇒ Object

Generate Ruby code while preserving source structure



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/t_ruby/compiler.rb', line 448

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