Class: CodeTools::Compiler
- Inherits:
-
Object
- Object
- CodeTools::Compiler
show all
- Defined in:
- lib/rubinius/compiler/locals.rb,
lib/rubinius/compiler/stages.rb,
lib/rubinius/compiler/version.rb,
lib/rubinius/compiler/compiler.rb,
lib/rubinius/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 =
"2.3.0"
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.compile(file, output = nil, line = 1, transforms = :default) ⇒ Object
-
.compile_eval(string, variable_scope, file = "(eval)", line = 1) ⇒ Object
-
.compile_file(file, line = 1) ⇒ Object
-
.compile_file_old(file, flags = nil) ⇒ Object
Match old compiler’s signature.
-
.compile_string(string, file = "(eval)", line = 1) ⇒ Object
-
.compile_test_bytecode(string, transforms) ⇒ Object
-
.compiled_cache_writable?(db, dir) ⇒ Boolean
-
.compiled_name(file) ⇒ Object
-
.compiler_error(msg, orig) ⇒ Object
-
.construct_block(string, binding, file = "(eval)", line = 1) ⇒ Object
-
.eval_cache ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(from, to) ⇒ Compiler
Returns a new instance of Compiler.
356
357
358
|
# File 'lib/rubinius/compiler/compiler.rb', line 356
def initialize(from, to)
@start = Stages[from].new self, to
end
|
Instance Attribute Details
#encoder ⇒ Object
Returns the value of attribute encoder.
9
10
11
|
# File 'lib/rubinius/compiler/compiler.rb', line 9
def encoder
@encoder
end
|
#generator ⇒ Object
Returns the value of attribute generator.
9
10
11
|
# File 'lib/rubinius/compiler/compiler.rb', line 9
def generator
@generator
end
|
#packager ⇒ Object
Returns the value of attribute packager.
9
10
11
|
# File 'lib/rubinius/compiler/compiler.rb', line 9
def packager
@packager
end
|
#parser ⇒ Object
Returns the value of attribute parser.
9
10
11
|
# File 'lib/rubinius/compiler/compiler.rb', line 9
def parser
@parser
end
|
#writer ⇒ Object
Returns the value of attribute writer.
9
10
11
|
# File 'lib/rubinius/compiler/compiler.rb', line 9
def writer
@writer
end
|
Class Method Details
.compile(file, output = nil, line = 1, transforms = :default) ⇒ 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
|
# File 'lib/rubinius/compiler/compiler.rb', line 72
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
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
313
314
|
# File 'lib/rubinius/compiler/compiler.rb', line 288
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, 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.add_metadata :for_eval, true
if ec and parser.should_cache?
ec.set([string.dup, layout, line], code)
end
return code
end
|
.compile_file(file, line = 1) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/rubinius/compiler/compiler.rb', line 105
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
101
102
103
|
# File 'lib/rubinius/compiler/compiler.rb', line 101
def self.compile_file_old(file, flags=nil)
compile_file file, 1
end
|
.compile_string(string, file = "(eval)", line = 1) ⇒ Object
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/rubinius/compiler/compiler.rb', line 120
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
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'lib/rubinius/compiler/compiler.rb', line 343
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
29
30
31
|
# File 'lib/rubinius/compiler/compiler.rb', line 29
def self.compiled_cache_writable?(db, dir)
File.owned?(db) or File.owned?(dir)
end
|
.compiled_name(file) ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/rubinius/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/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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/rubinius/compiler/compiler.rb', line 316
def self.construct_block(string, binding, file="(eval)", line=1)
code = compile_eval string, binding.variables, file, line
code.scope = binding.constant_scope
code.name = binding.variables.method.name
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
if binding.from_proc?
be.proc_environment = binding.proc_environment
end
return be
end
|
.eval_cache ⇒ Object
284
285
286
|
# File 'lib/rubinius/compiler/compiler.rb', line 284
def self.eval_cache
@eval_cache
end
|
Instance Method Details
#run ⇒ Object
360
361
362
|
# File 'lib/rubinius/compiler/compiler.rb', line 360
def run
@start.run
end
|