Module: Mirah

Defined in:
lib/mirah/errors.rb,
lib/mirah.rb,
lib/mirah/ast.rb,
lib/mirah/env.rb,
lib/mirah_task.rb,
lib/mirah/typer.rb,
lib/mirah/parser.rb,
lib/mirah/version.rb,
lib/mirah/compiler.rb,
lib/mirah/ast/scope.rb,
lib/mirah/generator.rb,
lib/mirah/commands/run.rb,
lib/mirah/jvm/compiler.rb,
lib/mirah/jvm/compiler.rb,
lib/mirah/util/logging.rb,
lib/mirah/commands/base.rb,
lib/mirah/util/delegate.rb,
lib/mirah/commands/parse.rb,
lib/mirah/jvm/types/type.rb,
lib/mirah/commands/compile.rb,
lib/mirah/jvm/compiler/base.rb,
lib/mirah/jvm/method_lookup.rb,
lib/mirah/transform/ast_ext.rb,
lib/mirah/util/class_loader.rb,
lib/mirah/jvm/types/meta_type.rb,
lib/mirah/jvm/types/null_type.rb,
lib/mirah/jvm/types/void_type.rb,
lib/mirah/util/process_errors.rb,
lib/mirah/jvm/types/array_type.rb,
lib/mirah/jvm/types/block_type.rb,
lib/mirah/transform/transformer.rb,
lib/mirah/jvm/types/generic_type.rb,
lib/mirah/util/compilation_state.rb,
lib/mirah/util/argument_processor.rb,
lib/mirah/jvm/types/primitive_type.rb,
lib/mirah/jvm/compiler/jvm_bytecode.rb,
lib/mirah/jvm/types/type_definition.rb,
lib/mirah/jvm/types/implicit_nil_type.rb,
lib/mirah/jvm/types/interface_definition.rb

Overview

Copyright © 2010-2013 The Mirah project authors. All Rights Reserved. All contributing project authors may be found in the NOTICE file.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: AST, Commands, Compiler, Env, JVM, Logging, Transform, Typer, Util Classes: Generator, InferenceError, InternalCompilerError, MirahError, NodeError, Parser, PathArray, SyntaxError

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.compile(*args) ⇒ Object



47
48
49
# File 'lib/mirah.rb', line 47

def self.compile(*args)
  Mirah::Commands::Compile.new(args).execute
end

.compiler_optionsObject



85
86
87
# File 'lib/mirah_task.rb', line 85

def self.compiler_options
  @compiler_options ||= []
end

.compiler_options=(args) ⇒ Object



89
90
91
# File 'lib/mirah_task.rb', line 89

def self.compiler_options=(args)
  @compiler_options = args
end

.dest_pathObject



41
42
43
# File 'lib/mirah_task.rb', line 41

def self.dest_path
  dest_paths[0] ||= File.expand_path('.')
end

.dest_path=(path) ⇒ Object



45
46
47
# File 'lib/mirah_task.rb', line 45

def self.dest_path=(path)
  dest_paths[0] = File.expand_path(path)
end

.dest_pathsObject



37
38
39
# File 'lib/mirah_task.rb', line 37

def self.dest_paths
  @dest_paths ||= PathArray.new
end

.dest_to_source_path(target_path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mirah_task.rb', line 67

def self.dest_to_source_path(target_path)
  destdir = find_dest(target_path)
  path = File.expand_path(target_path).sub(/^#{destdir}\//, '')
  path.sub!(/\.(java|class)$/, '')
  snake_case = File.basename(path).gsub(/[A-Z]/, '_\0').sub(/_/, '').downcase
  snake_case = "#{File.dirname(path)}/#{snake_case}"
  source_paths.each do |source_path|
    ["mirah", "duby"].each do |suffix|
      filename = "#{source_path}/#{path}.#{suffix}"
      return filename if File.exist?(filename)
      filename = "#{source_path}/#{snake_case}.#{suffix}"
      return filename if File.exist?(filename)
    end
  end
  # No source file exists. Just go with the first source path and .mirah
  return "#{self.source_path}/#{path}.mirah"
end

.find_dest(path) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/mirah_task.rb', line 49

def self.find_dest(path)
  expanded = File.expand_path(path)
  dest_paths.each do |destdir|
    if expanded =~ /^#{destdir}\//
      return destdir
    end
  end
end

.find_source(path) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/mirah_task.rb', line 58

def self.find_source(path)
  expanded = File.expand_path(path)
  source_paths.each do |sourcedir|
    if expanded =~ /^#{sourcedir}\//
      return sourcedir
    end
  end
end

.parse(*args) ⇒ Object



51
52
53
# File 'lib/mirah.rb', line 51

def self.parse(*args)
  Mirah::Commands::Parse.new(args).execute
end

.pluginsObject



55
56
57
# File 'lib/mirah.rb', line 55

def self.plugins
  @plugins ||= []
end


63
64
65
66
67
68
69
70
# File 'lib/mirah.rb', line 63

def self.print_error(message, position)
  if position.nil?
    puts message
    return
  end
  puts "#{position.source.name}:#{position.start_line}: #{message}"
  puts underline(position)
end

.resetObject



59
60
61
# File 'lib/mirah.rb', line 59

def self.reset
  plugins.each {|x| x.reset if x.respond_to?(:reset)}
end

.run(*args) ⇒ Object



43
44
45
# File 'lib/mirah.rb', line 43

def self.run(*args)
  Mirah::Commands::Run.new(args).execute
end

.source_pathObject



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

def self.source_path
  source_paths[0] ||= File.expand_path('.')
end

.source_path=(path) ⇒ Object



33
34
35
# File 'lib/mirah_task.rb', line 33

def self.source_path=(path)
  source_paths[0] = File.expand_path(path)
end

.source_pathsObject



29
30
31
# File 'lib/mirah_task.rb', line 29

def self.source_paths
  @source_paths ||= PathArray.new
end

.underline(position) ⇒ Object



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
# File 'lib/mirah.rb', line 72

def self.underline(position)
  start_line = position.start_line - position.source.initial_line
  end_line = position.end_line - position.source.initial_line

  start_col = position.start_column
  end_col = position.end_column
  adjustment = if start_line == 0
    position.source.initial_column
  else
    1
  end

  start_col -= adjustment
  end_col -= adjustment

  result = ""
  position.source.contents.each_line.with_index do |line, lineno|
    break if lineno > end_line
    next if lineno < start_line

    chomped = line.chomp
    result << chomped
    result << "\n"

    start = 0
    start = start_col if lineno == start_line

    result << " " * start

    endcol = chomped.size
    endcol = end_col if lineno == end_line

    result << "^" * [endcol - start, 1].max
    result << "\n"
  end
  result
end