Class: TRuby::EnhancedIncrementalCompiler

Inherits:
IncrementalCompiler show all
Defined in:
lib/t_ruby/cache.rb

Overview

Enhanced Incremental Compiler with IR and Cross-file support

Instance Attribute Summary collapse

Attributes inherited from IncrementalCompiler

#dependencies, #file_hashes

Instance Method Summary collapse

Methods inherited from IncrementalCompiler

#add_dependency, #compile, #compile_all, #needs_compile?, #update_file_hash

Constructor Details

#initialize(compiler, cache: nil, enable_cross_file: true) ⇒ EnhancedIncrementalCompiler

Returns a new instance of EnhancedIncrementalCompiler.



665
666
667
668
669
# File 'lib/t_ruby/cache.rb', line 665

def initialize(compiler, cache: nil, enable_cross_file: true)
  super(compiler, cache: cache)
  @ir_cache = {} # file_path => IR::Program
  @cross_file_checker = CrossFileTypeChecker.new if enable_cross_file
end

Instance Attribute Details

#cross_file_checkerObject (readonly)

Returns the value of attribute cross_file_checker.



663
664
665
# File 'lib/t_ruby/cache.rb', line 663

def cross_file_checker
  @cross_file_checker
end

#ir_cacheObject (readonly)

Returns the value of attribute ir_cache.



663
664
665
# File 'lib/t_ruby/cache.rb', line 663

def ir_cache
  @ir_cache
end

Instance Method Details

#clearObject

Clear all caches



746
747
748
749
750
# File 'lib/t_ruby/cache.rb', line 746

def clear
  super
  @ir_cache.clear
  @cross_file_checker&.clear
end

#compile_all_with_checking(file_paths) ⇒ Object

Compile all with cross-file checking Returns diagnostics using unified Diagnostic format



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'lib/t_ruby/cache.rb', line 692

def compile_all_with_checking(file_paths)
  results = {}
  all_diagnostics = []

  # First pass: compile and register all files
  file_paths.each do |file_path|
    source = File.exist?(file_path) ? File.read(file_path) : nil

    begin
      results[file_path] = compile_with_ir(file_path)
    rescue TypeCheckError => e
      all_diagnostics << Diagnostic.from_type_check_error(e, file: file_path, source: source)
    rescue ParseError => e
      all_diagnostics << Diagnostic.from_parse_error(e, file: file_path, source: source)
    rescue Scanner::ScanError => e
      all_diagnostics << Diagnostic.from_scan_error(e, file: file_path, source: source)
    rescue StandardError => e
      all_diagnostics << Diagnostic.new(
        code: "TR0001",
        message: e.message,
        file: file_path,
        line: 1,
        column: 1
      )
    end
  end

  # Second pass: cross-file type checking
  if @cross_file_checker
    check_result = @cross_file_checker.check_all
    check_result[:errors].each do |e|
      all_diagnostics << Diagnostic.new(
        code: "TR2002",
        message: e[:message],
        file: e[:file],
        line: 1,
        column: 1
      )
    end
  end

  {
    results: results,
    diagnostics: all_diagnostics,
    success: all_diagnostics.empty?,
  }
end

#compile_with_ir(file_path) ⇒ Object

Compile with IR caching



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/t_ruby/cache.rb', line 672

def compile_with_ir(file_path)
  return @compiled_files[file_path] unless needs_compile?(file_path)

  # Get IR from compiler
  ir_program = @compiler.compile_to_ir(file_path)
  @ir_cache[file_path] = ir_program

  # Register with cross-file checker
  @cross_file_checker&.register_file(file_path, ir_program)

  # Compile from IR
  result = @compiler.compile(file_path)
  @file_hashes[file_path] = file_hash(file_path)
  @compiled_files[file_path] = result

  result
end

#get_ir(file_path) ⇒ Object

Get cached IR for a file



741
742
743
# File 'lib/t_ruby/cache.rb', line 741

def get_ir(file_path)
  @ir_cache[file_path]
end