Class: TRuby::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/compiler.rb

Direct Known Subclasses

LegacyCompiler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, 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 = nil, use_ir: true, optimize: true)
  @config = config || Config.new
  @use_ir = use_ir
  @optimize = optimize
  @declaration_loader = DeclarationLoader.new
  @optimizer = IR::Optimizer.new if use_ir && optimize
  setup_declaration_paths if @config
end

Instance Attribute Details

#declaration_loaderObject (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

#optimizerObject (readonly)

Returns the value of attribute optimizer.



7
8
9
# File 'lib/t_ruby/compiler.rb', line 7

def optimizer
  @optimizer
end

#use_irObject (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



153
154
155
# File 'lib/t_ruby/compiler.rb', line 153

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
60
61
62
63
64
65
# 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

  # Handle .rb files separately
  if input_path.end_with?(".rb")
    return copy_ruby_file(input_path)
  end

  unless input_path.end_with?(".trb")
    raise ArgumentError, "Expected .trb or .rb 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)

  # Compute output path (respects preserve_structure setting)
  output_path = compute_output_path(input_path, @config.ruby_dir, ".rb")
  FileUtils.mkdir_p(File.dirname(output_path))

  File.write(output_path, output)

  # Generate .rbs file if enabled in config
  if @config.compiler["generate_rbs"]
    rbs_path = compute_output_path(input_path, @config.rbs_dir, ".rbs")
    FileUtils.mkdir_p(File.dirname(rbs_path))
    if @use_ir && parser.ir_program
      generate_rbs_from_ir_to_path(rbs_path, parser.ir_program)
    else
      generate_rbs_file_to_path(rbs_path, parse_result)
    end
  end

  # Generate .d.trb file if enabled in config (legacy support)
  # TODO: Add compiler.generate_dtrb option in future
  if @config.compiler.key?("generate_dtrb") && @config.compiler["generate_dtrb"]
    generate_dtrb_file(input_path, @config.ruby_dir)
  end

  output_path
end

#compile_from_ir(ir_program, output_path) ⇒ Object

Compile from IR program directly



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/t_ruby/compiler.rb', line 128

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_string(source, options = {}) ⇒ Hash

Compile T-Ruby source code from a string (useful for WASM/playground)

Parameters:

  • T-Ruby source code

  • (defaults to: {})

    Options for compilation

Options Hash (options):

  • :rbs (Boolean)

    Whether to generate RBS output (default: true)

Returns:

  • Result with :ruby, :rbs, :errors keys



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/t_ruby/compiler.rb', line 72

def compile_string(source, options = {})
  generate_rbs = options.fetch(:rbs, true)

  parser = Parser.new(source, use_combinator: @use_ir)
  parse_result = parser.parse

  # Transform source to Ruby code
  ruby_output = @use_ir ? transform_with_ir(source, parser) : transform_legacy(source, parse_result)

  # Generate RBS if requested
  rbs_output = ""
  if generate_rbs
    if @use_ir && parser.ir_program
      generator = IR::RBSGenerator.new
      rbs_output = generator.generate(parser.ir_program)
    else
      generator = RBSGenerator.new
      rbs_output = generator.generate(
        parse_result[:functions] || [],
        parse_result[:type_aliases] || []
      )
    end
  end

  {
    ruby: ruby_output,
    rbs: rbs_output,
    errors: [],
  }
rescue ParseError => e
  {
    ruby: "",
    rbs: "",
    errors: [e.message],
  }
rescue StandardError => e
  {
    ruby: "",
    rbs: "",
    errors: ["Compilation error: #{e.message}"],
  }
end

#compile_to_ir(input_path) ⇒ Object

Compile to IR without generating output files



116
117
118
119
120
121
122
123
124
125
# File 'lib/t_ruby/compiler.rb', line 116

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

#compute_output_path(input_path, output_dir, new_extension) ⇒ String

Compute output path for a source file

Parameters:

  • path to source file

  • base output directory

  • new file extension (e.g., “.rb”, “.rbs”)

Returns:

  • computed output path (always preserves directory structure)



167
168
169
170
171
# File 'lib/t_ruby/compiler.rb', line 167

def compute_output_path(input_path, output_dir, new_extension)
  relative = compute_relative_path(input_path)
  base = relative.sub(/\.[^.]+$/, new_extension)
  File.join(output_dir, base)
end

#compute_relative_path(input_path) ⇒ String

Compute relative path from source directory

Parameters:

  • path to source file

Returns:

  • relative path preserving directory structure



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/t_ruby/compiler.rb', line 176

def compute_relative_path(input_path)
  # Use realpath to resolve symlinks (e.g., /var vs /private/var on macOS)
  absolute_input = resolve_path(input_path)
  source_dirs = @config.source_include

  # Check if file is inside any source_include directory
  if source_dirs.size > 1
    # Multiple source directories: include the source dir name in output
    # src/models/user.trb → src/models/user.trb
    source_dirs.each do |src_dir|
      absolute_src = resolve_path(src_dir)
      next unless absolute_input.start_with?("#{absolute_src}/")

      # Return path relative to parent of source dir (includes src dir name)
      parent_of_src = File.dirname(absolute_src)
      return absolute_input.sub("#{parent_of_src}/", "")
    end
  else
    # Single source directory: exclude the source dir name from output
    # src/models/user.trb → models/user.trb
    src_dir = source_dirs.first
    if src_dir
      absolute_src = resolve_path(src_dir)
      if absolute_input.start_with?("#{absolute_src}/")
        return absolute_input.sub("#{absolute_src}/", "")
      end
    end
  end

  # File outside source directories: use path relative to current working directory
  # external/foo.trb → external/foo.trb
  cwd = resolve_path(".")
  if absolute_input.start_with?("#{cwd}/")
    return absolute_input.sub("#{cwd}/", "")
  end

  # Absolute path from outside cwd: use basename only
  File.basename(input_path)
end

#load_declaration(name) ⇒ Object

Load external declarations from a file



148
149
150
# File 'lib/t_ruby/compiler.rb', line 148

def load_declaration(name)
  @declaration_loader.load(name)
end

#optimization_statsObject

Get optimization statistics (only available after IR compilation)



158
159
160
# File 'lib/t_ruby/compiler.rb', line 158

def optimization_stats
  @optimizer&.stats
end