Class: TRuby::Compiler
- Inherits:
-
Object
- Object
- TRuby::Compiler
- Defined in:
- lib/t_ruby/compiler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#declaration_loader ⇒ Object
readonly
Returns the value of attribute declaration_loader.
-
#optimizer ⇒ Object
readonly
Returns the value of attribute optimizer.
-
#use_ir ⇒ Object
readonly
Returns the value of attribute use_ir.
Instance Method Summary collapse
-
#add_declaration_path(path) ⇒ Object
Add a search path for declaration files.
- #compile(input_path) ⇒ Object
-
#compile_from_ir(ir_program, output_path) ⇒ Object
Compile from IR program directly.
-
#compile_to_ir(input_path) ⇒ Object
Compile to IR without generating output files.
-
#initialize(config, use_ir: true, optimize: true) ⇒ Compiler
constructor
A new instance of Compiler.
-
#load_declaration(name) ⇒ Object
Load external declarations from a file.
-
#optimization_stats ⇒ Object
Get optimization statistics (only available after IR compilation).
Constructor Details
#initialize(config, use_ir: true, optimize: true) ⇒ Compiler
Returns a new instance of Compiler.
9 10 11 12 13 14 15 16 |
# File 'lib/t_ruby/compiler.rb', line 9 def initialize(config, use_ir: true, optimize: true) @config = config @use_ir = use_ir @optimize = optimize @declaration_loader = DeclarationLoader.new @optimizer = IR::Optimizer.new if use_ir && optimize setup_declaration_paths end |
Instance Attribute Details
#declaration_loader ⇒ Object (readonly)
Returns the value of attribute declaration_loader.
7 8 9 |
# File 'lib/t_ruby/compiler.rb', line 7 def declaration_loader @declaration_loader end |
#optimizer ⇒ Object (readonly)
Returns the value of attribute optimizer.
7 8 9 |
# File 'lib/t_ruby/compiler.rb', line 7 def optimizer @optimizer end |
#use_ir ⇒ Object (readonly)
Returns the value of attribute use_ir.
7 8 9 |
# File 'lib/t_ruby/compiler.rb', line 7 def use_ir @use_ir end |
Instance Method Details
#add_declaration_path(path) ⇒ Object
Add a search path for declaration files
99 100 101 |
# File 'lib/t_ruby/compiler.rb', line 99 def add_declaration_path(path) @declaration_loader.add_search_path(path) end |
#compile(input_path) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/t_ruby/compiler.rb', line 18 def compile(input_path) unless File.exist?(input_path) raise ArgumentError, "File not found: #{input_path}" end unless input_path.end_with?(".trb") raise ArgumentError, "Expected .trb file, got: #{input_path}" end source = File.read(input_path) # Parse with IR support parser = Parser.new(source, use_combinator: @use_ir) parse_result = parser.parse # Transform source to Ruby code output = @use_ir ? transform_with_ir(source, parser) : transform_legacy(source, parse_result) out_dir = @config.out_dir FileUtils.mkdir_p(out_dir) base_filename = File.basename(input_path, ".trb") output_path = File.join(out_dir, base_filename + ".rb") File.write(output_path, output) # Generate .rbs file if enabled in config if @config.emit["rbs"] if @use_ir && parser.ir_program generate_rbs_from_ir(base_filename, out_dir, parser.ir_program) else generate_rbs_file(base_filename, out_dir, parse_result) end end # Generate .d.trb file if enabled in config if @config.emit["dtrb"] generate_dtrb_file(input_path, out_dir) end output_path end |
#compile_from_ir(ir_program, output_path) ⇒ Object
Compile from IR program directly
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/t_ruby/compiler.rb', line 74 def compile_from_ir(ir_program, output_path) out_dir = File.dirname(output_path) FileUtils.mkdir_p(out_dir) # Optimize if enabled program = ir_program if @optimize && @optimizer result = @optimizer.optimize(program) program = result[:program] end # Generate Ruby code generator = IRCodeGenerator.new output = generator.generate(program) File.write(output_path, output) output_path end |
#compile_to_ir(input_path) ⇒ Object
Compile to IR without generating output files
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/t_ruby/compiler.rb', line 62 def compile_to_ir(input_path) unless File.exist?(input_path) raise ArgumentError, "File not found: #{input_path}" end source = File.read(input_path) parser = Parser.new(source, use_combinator: true) parser.parse parser.ir_program end |
#load_declaration(name) ⇒ Object
Load external declarations from a file
94 95 96 |
# File 'lib/t_ruby/compiler.rb', line 94 def load_declaration(name) @declaration_loader.load(name) end |
#optimization_stats ⇒ Object
Get optimization statistics (only available after IR compilation)
104 105 106 |
# File 'lib/t_ruby/compiler.rb', line 104 def optimization_stats @optimizer&.stats end |