Class: Mirah::Compiler::ASTCompiler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, compiler_class, logging) ⇒ ASTCompiler

Returns a new instance of ASTCompiler.



19
20
21
22
23
# File 'lib/mirah/compiler.rb', line 19

def initialize(config, compiler_class, logging)
  @config = config
  @compiler_class = compiler_class
  @logging = logging
end

Instance Attribute Details

#compilerObject

Returns the value of attribute compiler.



25
26
27
# File 'lib/mirah/compiler.rb', line 25

def compiler
  @compiler
end

#compiler_classObject

Returns the value of attribute compiler_class.



25
26
27
# File 'lib/mirah/compiler.rb', line 25

def compiler_class
  @compiler_class
end

#loggingObject

Returns the value of attribute logging.



25
26
27
# File 'lib/mirah/compiler.rb', line 25

def logging
  @logging
end

Instance Method Details

#compile_ast(ast, scoper, typer, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/mirah/compiler.rb', line 47

def compile_ast(ast, scoper, typer, &block)
  @compiler = compiler_class.new(@config, scoper, typer)
  compiler.visit(ast, nil)
  compiler.generate(&block)
rescue java.lang.UnsupportedOperationException => ex
  raise MirahError.new(ex.message)
rescue java.lang.Throwable => ex
  ex.cause.printStackTrace if ex.cause
  raise ex
end

#compile_asts(nodes, scoper, typer) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mirah/compiler.rb', line 27

def compile_asts(nodes, scoper, typer)
  results = []
  puts "Compiling..." if logging
  nodes.each do |ast|
    puts "  #{ast.position.source.name}" if logging
    compile_ast(ast, scoper, typer) do |filename, builder|
      if builder.respond_to?(:class_name)
        class_name = builder.class_name
        bytes = builder.generate
      else
        class_name = filename
        bytes = String.from_java_bytes(builder)
        filename = class_name.tr('.', '/') + ".class"
      end
      results << CompilerResult.new(filename, class_name, bytes)
    end
  end
  results
end