Class: CodeTools::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/code/compiler/locals.rb,
lib/rubinius/code/compiler/stages.rb,
lib/rubinius/code/compiler/version.rb,
lib/rubinius/code/compiler/compiler.rb,
lib/rubinius/code/compiler/printers.rb

Defined Under Namespace

Modules: LocalVariables Classes: ASTPrinter, Bytecode, Encoder, EvalLocalReference, EvalLocalVariable, EvalParser, FileParser, LRUCache, LocalReference, LocalVariable, MethodPrinter, NestedLocalReference, NestedLocalVariable, Packager, Parser, Printer, SexpPrinter, Stage, StringParser, Writer

Constant Summary collapse

Stages =
{ }
VERSION =
"3.36"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Compiler

Returns a new instance of Compiler.



354
355
356
# File 'lib/rubinius/code/compiler/compiler.rb', line 354

def initialize(from, to)
  @start = Stages[from].new self, to
end

Instance Attribute Details

#encoderObject

Returns the value of attribute encoder.



9
10
11
# File 'lib/rubinius/code/compiler/compiler.rb', line 9

def encoder
  @encoder
end

#generatorObject

Returns the value of attribute generator.



9
10
11
# File 'lib/rubinius/code/compiler/compiler.rb', line 9

def generator
  @generator
end

#packagerObject

Returns the value of attribute packager.



9
10
11
# File 'lib/rubinius/code/compiler/compiler.rb', line 9

def packager
  @packager
end

#parserObject

Returns the value of attribute parser.



9
10
11
# File 'lib/rubinius/code/compiler/compiler.rb', line 9

def parser
  @parser
end

#writerObject

Returns the value of attribute writer.



9
10
11
# File 'lib/rubinius/code/compiler/compiler.rb', line 9

def writer
  @writer
end

Class Method Details

.compile(file, output = nil, line = 1, transforms = :default) ⇒ Object



70
71
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
# File 'lib/rubinius/code/compiler/compiler.rb', line 70

def self.compile(file, output=nil, line=1, transforms=:default)
  compiler = new :file, :compiled_file

  parser = compiler.parser
  parser.root AST::Script

  if transforms.kind_of? Array
    transforms.each { |t| parser.enable_category t }
  else
    parser.enable_category transforms
  end

  parser.input file, line

  writer = compiler.writer
  writer.version = Rubinius::RUBY_LIB_VERSION
  writer.name = output ? output : compiled_name(file)

  begin
    compiler.run
  rescue SyntaxError => e
    raise e
  rescue Exception => e
    compiler_error "Error trying to compile #{file}", e
  end

end

.compile_eval(string, variable_scope, file = "(eval)", line = 1) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/rubinius/code/compiler/compiler.rb', line 286

def self.compile_eval(string, variable_scope, file="(eval)", line=1)
  if ec = @eval_cache
    layout = variable_scope.local_layout
    if code = ec.retrieve([string, layout, file, line])
      return code
    end
  end

  compiler = new :eval, :compiled_code

  parser = compiler.parser
  parser.root AST::EvalExpression
  parser.default_transforms
  parser.input string, file, line

  compiler.generator.variable_scope = variable_scope

  code = compiler.run

  code. :for_eval, true

  if ec and parser.should_cache?
    ec.set([string.dup, layout, file, line], code)
  end

  return code
end

.compile_file(file, line = 1) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubinius/code/compiler/compiler.rb', line 103

def self.compile_file(file, line=1)
  compiler = new :file, :compiled_code

  parser = compiler.parser
  parser.root AST::Script
  parser.default_transforms
  parser.input file, line

  begin
    compiler.run
  rescue Exception => e
    compiler_error "Error trying to compile #{file}", e
  end
end

.compile_file_old(file, flags = nil) ⇒ Object

Match old compiler’s signature



99
100
101
# File 'lib/rubinius/code/compiler/compiler.rb', line 99

def self.compile_file_old(file, flags=nil)
  compile_file file, 1
end

.compile_string(string, file = "(eval)", line = 1) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/rubinius/code/compiler/compiler.rb', line 118

def self.compile_string(string, file="(eval)", line=1)
  compiler = new :string, :compiled_code

  parser = compiler.parser
  parser.root AST::Script
  parser.default_transforms
  parser.input string, file, line

  compiler.run
end

.compile_test_bytecode(string, transforms) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/rubinius/code/compiler/compiler.rb', line 341

def self.compile_test_bytecode(string, transforms)
  compiler = new :string, :bytecode

  parser = compiler.parser
  parser.root AST::Snippet
  parser.input string
  transforms.each { |x| parser.enable_transform x }

  compiler.generator.processor Rubinius::TestGenerator

  compiler.run
end

.compiled_cache_writable?(db, dir) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubinius/code/compiler/compiler.rb', line 29

def self.compiled_cache_writable?(db, dir)
  File.writable?(db) or File.writable?(dir)
end

.compiled_name(file) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rubinius/code/compiler/compiler.rb', line 21

def self.compiled_name(file)
  full = "#{File.expand_path(file)}#{Rubinius::RUBY_LIB_VERSION}"
  hash = Rubinius.invoke_primitive :sha1_hash, full
  dir = hash[0,2]

  "#{RBC_DB}/#{dir}/#{hash}"
end

.compiler_error(msg, orig) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/rubinius/code/compiler/compiler.rb', line 11

def self.compiler_error(msg, orig)
  if defined?(RUBY_ENGINE) and RUBY_ENGINE == "rbx"
    raise CompileError, msg, orig
  else
    orig.message.replace("#{orig.message} - #{msg}")
    raise orig
  end
end

.construct_block(string, binding, file = "(eval)", line = 1) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/rubinius/code/compiler/compiler.rb', line 314

def self.construct_block(string, binding, file="(eval)", line=1)
  code = compile_eval string, binding.variables, file, line

  code.scope = binding.lexical_scope
  code.name = binding.variables.method.name

  # This has to be setup so __FILE__ works in eval.
  script = Rubinius::CompiledCode::Script.new(code, file, true)
  script.eval_source = string

  code.scope.script = script

  be = Rubinius::BlockEnvironment.new
  be.under_context binding.variables, code

  # Pass the BlockEnvironment this binding was created from
  # down into the new BlockEnvironment we just created.
  # This indicates the "declaration trace" to the stack trace
  # mechanisms, which can be different from the "call trace"
  # in the case of, say: eval("caller", a_proc_instance)
  if binding.from_proc?
    be.proc_environment = binding.proc_environment
  end

  return be
end

.eval_cacheObject



282
283
284
# File 'lib/rubinius/code/compiler/compiler.rb', line 282

def self.eval_cache
  @eval_cache
end

Instance Method Details

#runObject



358
359
360
# File 'lib/rubinius/code/compiler/compiler.rb', line 358

def run
  @start.run
end