Class: TRuby::IRCodeGenerator
- Inherits:
-
Object
- Object
- TRuby::IRCodeGenerator
- Defined in:
- lib/t_ruby/compiler.rb
Overview
IR-aware code generator for source-preserving transformation
Instance Attribute Summary collapse
-
#emitter ⇒ Object
readonly
Returns the value of attribute emitter.
Instance Method Summary collapse
-
#generate(program) ⇒ Object
Generate Ruby code from IR program.
-
#generate_with_source(program, source) ⇒ Object
Generate Ruby code while preserving source structure.
-
#initialize(target_ruby: "3.0") ⇒ IRCodeGenerator
constructor
A new instance of IRCodeGenerator.
Constructor Details
#initialize(target_ruby: "3.0") ⇒ IRCodeGenerator
Returns a new instance of IRCodeGenerator.
608 609 610 611 |
# File 'lib/t_ruby/compiler.rb', line 608 def initialize(target_ruby: "3.0") @output = [] @emitter = CodeEmitter.for_version(target_ruby) end |
Instance Attribute Details
#emitter ⇒ Object (readonly)
Returns the value of attribute emitter.
605 606 607 |
# File 'lib/t_ruby/compiler.rb', line 605 def emitter @emitter end |
Instance Method Details
#generate(program) ⇒ Object
Generate Ruby code from IR program
614 615 616 617 |
# File 'lib/t_ruby/compiler.rb', line 614 def generate(program) generator = IR::CodeGenerator.new generator.generate(program) end |
#generate_with_source(program, source) ⇒ Object
Generate Ruby code while preserving source structure
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/t_ruby/compiler.rb', line 620 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) # Apply version-specific transformations result = @emitter.transform(result) # Clean up extra blank lines result.gsub(/\n{3,}/, "\n\n") end |