/
code_loader.rb
47 lines (38 loc) · 1.17 KB
/
code_loader.rb
1
2
3
4
5
6
7
8
9
10
11
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
40
41
42
43
44
45
46
47
module Lambra
class CodeLoader
def self.evaluate(string)
ast = Lambra::Parser.parse string
execute(ast)
end
def self.execute(ast)
visitor = BytecodeCompiler.new
gen = visitor.compile(ast)
gen.encode
cm = gen.package Rubinius::CompiledMethod
require_relative '../bootstrap'
env = Scope
file = if ast.respond_to?(:filename) && ast.filename
ast.filename
else
'(eval)'
end
line, binding, instance = ast.line, env.send(:binding), env
# cm = Noscript::Compiler.compile_eval(code, binding.variables, file, line)
cm.scope = Rubinius::StaticScope.new(GlobalScope)
cm.name = :__lambra__
script = Rubinius::CompiledMethod::Script.new(cm, file, true)
be = Rubinius::BlockEnvironment.new
script.eval_binding = binding
# script.eval_source = string
cm.scope.script = script
be.under_context(binding.variables, cm)
be.from_eval!
be.call_on_instance(instance)
end
def self.execute_file(name)
ast = Lambra::Parser.parse IO.read(name)
def ast.filename; name; end
execute(ast)
end
end
end