Class: Dentaku::AST::Function

Inherits:
Node
  • Object
show all
Defined in:
lib/dentaku/ast/function.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

arity, precedence

Constructor Details

#initialize(*args) ⇒ Function

Returns a new instance of Function.



6
7
8
# File 'lib/dentaku/ast/function.rb', line 6

def initialize(*args)
  @args = args
end

Class Method Details

.get(name) ⇒ Object



14
15
16
17
18
# File 'lib/dentaku/ast/function.rb', line 14

def self.get(name)
  registry.fetch(function_name(name)) {
    fail ParseError, "Undefined function #{ name }"
  }
end

.register(name, type, implementation) ⇒ Object



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
# File 'lib/dentaku/ast/function.rb', line 20

def self.register(name, type, implementation)
  function = Class.new(self) do
    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 value(context={})
      args = @args.map { |a| a.value(context) }
      self.class.implementation.call(*args)
    end

    def type
      self.class.type
    end
  end

  function_class = name.to_s.capitalize
  Dentaku::AST.send(:remove_const, function_class) if Dentaku::AST.const_defined?(function_class, false)
  Dentaku::AST.const_set(function_class, function)

  function.implementation = implementation
  function.type = type

  registry[function_name(name)] = function
end

.register_class(name, function_class) ⇒ Object



58
59
60
# File 'lib/dentaku/ast/function.rb', line 58

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

Instance Method Details

#dependencies(context = {}) ⇒ Object



10
11
12
# File 'lib/dentaku/ast/function.rb', line 10

def dependencies(context={})
  @args.flat_map { |a| a.dependencies(context) }
end