Class: Cenit::Algorithms::Interpreter
- Inherits:
-
Object
- Object
- Cenit::Algorithms::Interpreter
show all
- Defined in:
- lib/cenit/algorithms/interpreter.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Interpreter.
7
8
9
10
11
12
13
14
15
|
# File 'lib/cenit/algorithms/interpreter.rb', line 7
def initialize
@__prefixes__ = Hash.new do |linkers, linker_id|
linkers[linker_id] = Hash.new do |methods, method|
methods[method] = "__#{linker_id}_"
end
end
@__algorithms__ = {}
@__options__ = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/cenit/algorithms/interpreter.rb', line 17
def method_missing(symbol, *args, &block)
if (algorithm = @__algorithms__[symbol.to_s])
if algorithm.is_a?(Proc)
define_singleton_method(symbol, algorithm)
else
instance_eval "define_singleton_method(:#{symbol},
->(#{algorithm.parameters.collect { |p| p['name'] }.join(', ')}) {
#{__parse__(algorithm)}
})"
end
send(symbol, *args, &block)
else
super
end
end
|
Class Method Details
.run(algorithm, *args) ⇒ Object
61
62
63
|
# File 'lib/cenit/algorithms/interpreter.rb', line 61
def run(algorithm, *args)
new.__run__(algorithm, *args)
end
|
Instance Method Details
#__parse__(algorithm) ⇒ Object
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/cenit/algorithms/interpreter.rb', line 49
def __parse__(algorithm)
code = algorithm.code
algorithm.parameters.each do |p|
code = "#{p['name']} ||= nil\r\n" + code
end
buffer = ::Parser::Source::Buffer.new('code')
buffer.source = code
ast = Parser::CurrentRuby.new.parse(buffer)
Rewriter.new(self_linker: algorithm, interpreter: self).rewrite(buffer, ast)
end
|
#__prefix__(method, linker) ⇒ Object
43
44
45
46
47
|
# File 'lib/cenit/algorithms/interpreter.rb', line 43
def __prefix__(method, linker)
prefix = @__prefixes__[linker.id][method]
@__algorithms__[prefix + method.to_s] = linker.link(method)
prefix
end
|
#__run__(algorithm, *args) ⇒ Object
37
38
39
40
41
|
# File 'lib/cenit/algorithms/interpreter.rb', line 37
def __run__(algorithm, *args)
run_name ="__run__#{algorithm.name}"
@__algorithms__[run_name] = algorithm
send run_name, *args
end
|
#respond_to?(*args) ⇒ Boolean
33
34
35
|
# File 'lib/cenit/algorithms/interpreter.rb', line 33
def respond_to?(*args)
@__algorithms__.has_key?(args[0])
end
|