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?

Constructor Details

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

Returns a new instance of EnhancedIncrementalCompiler.



656
657
658
659
660
# File 'lib/t_ruby/cache.rb', line 656

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.



654
655
656
# File 'lib/t_ruby/cache.rb', line 654

def cross_file_checker
  @cross_file_checker
end

#ir_cacheObject (readonly)

Returns the value of attribute ir_cache.



654
655
656
# File 'lib/t_ruby/cache.rb', line 654

def ir_cache
  @ir_cache
end

Instance Method Details

#clearObject

Clear all caches



714
715
716
717
718
# File 'lib/t_ruby/cache.rb', line 714

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

#compile_all_with_checking(file_paths) ⇒ Object

Compile all with cross-file checking



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/t_ruby/cache.rb', line 682

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

  # First pass: compile and register all files
  file_paths.each do |file_path|
    begin
      results[file_path] = compile_with_ir(file_path)
    rescue => e
      errors << { file: file_path, error: e.message }
    end
  end

  # Second pass: cross-file type checking
  if @cross_file_checker
    check_result = @cross_file_checker.check_all
    errors.concat(check_result[:errors])
  end

  {
    results: results,
    errors: errors,
    success: errors.empty?
  }
end

#compile_with_ir(file_path) ⇒ Object

Compile with IR caching



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/t_ruby/cache.rb', line 663

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



709
710
711
# File 'lib/t_ruby/cache.rb', line 709

def get_ir(file_path)
  @ir_cache[file_path]
end