Class: Dentaku::AST::FunctionRegistry

Inherits:
Hash
  • Object
show all
Defined in:
lib/dentaku/ast/function_registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



85
86
87
# File 'lib/dentaku/ast/function_registry.rb', line 85

def self.default
  Dentaku::AST::Function.registry
end

Instance Method Details

#defaultObject



81
82
83
# File 'lib/dentaku/ast/function_registry.rb', line 81

def default
  self.class.default
end

#get(name) ⇒ Object



4
5
6
7
8
9
# File 'lib/dentaku/ast/function_registry.rb', line 4

def get(name)
  name = function_name(name)
  return self[name] if has_key?(name)
  return default[name] if default.has_key?(name)
  nil
end

#register(name, type, implementation, callback = nil) ⇒ Object



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

#register_class(name, function_class) ⇒ Object



77
78
79
# File 'lib/dentaku/ast/function_registry.rb', line 77

def register_class(name, function_class)
  self[function_name(name)] = function_class
end