Class: SolidRail::Compiler

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

Overview

Main compiler module that orchestrates the transpilation process

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compiler



6
7
8
9
10
# File 'lib/solidrail/compiler.rb', line 6

def initialize(options = {})
  @options = options
  @errors = []
  @warnings = []
end

Instance Method Details

#compile(source_code, output_file = nil) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/solidrail/compiler.rb', line 12

def compile(source_code, output_file = nil)
  # Step 1: Validate Ruby code
  ruby_errors = Validator.validate_ruby_code(source_code)
  raise CompilationError, "Ruby validation failed:\n#{ruby_errors.join("\n")}" if ruby_errors.any?

  # Step 2: Parse Ruby code to AST
  ast = Parser.parse(source_code)

  # Step 3: Generate Solidity code
  solidity_code = Generator.generate_solidity(ast)

  # Step 4: Optimize the generated code
  optimized_code = Optimizer.optimize(solidity_code)

  # Step 5: Validate generated Solidity code
  solidity_errors = Validator.validate_solidity_code(optimized_code)
  @warnings += solidity_errors if solidity_errors.any?

  # Step 6: Write output if file specified
  File.write(output_file, optimized_code) if output_file

  {
    code: optimized_code,
    errors: @errors,
    warnings: @warnings,
    ast: ast
  }
end

#compile_file(input_file, output_file = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/solidrail/compiler.rb', line 41

def compile_file(input_file, output_file = nil)
  source_code = File.read(input_file)
  output_file ||= input_file.sub(/\.rb$/, '.sol')

  compile(source_code, output_file)
end