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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/dentaku/ast/function_registry.rb', line 11
def register(name, type, implementation, callback = nil)
function = Class.new(Function) do
def self.name=(name)
@name = name
end
def self.name
@name
end
def self.implementation=(impl)
@implementation = impl
end
def self.implementation
@implementation
end
def self.type=(type)
@type = type
end
def self.type
@type
end
def self.callback=(callback)
@callback = callback
end
def self.callback
@callback
end
def self.arity
@implementation.arity < 0 ? nil : @implementation.arity
end
def self.min_param_count
@implementation.parameters.select { |type, _name| type == :req }.count
end
def self.max_param_count
@implementation.parameters.select { |type, _name| type == :rest }.any? ? Float::INFINITY : @implementation.parameters.count
end
def value(context = {})
args = @args.map { |a| a.value(context) }
self.class.implementation.call(*args)
end
def type
self.class.type
end
end
define_class(name, function)
function.name = name
function.type = type
function.implementation = implementation
function.callback = callback
self[function_name(name)] = function
end
|