Sha256: bfeb3d15123b2aae353fb60905e97c4c85278e4ec0bb582914ce6547334da45b

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

require_relative 'node'

module Dentaku
  module AST
    class Function < Node
      def initialize(*args)
        @args = args
      end

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

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

      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.flat_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)
        Dentaku::AST.const_set(function_class, function)

        function.implementation = implementation
        function.type = type

        registry[function_name(name)] = function
      end

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

      private

      def self.function_name(name)
        name.to_s.downcase
      end

      def self.registry
        @registry ||= {}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dentaku-2.0.5 lib/dentaku/ast/function.rb
dentaku-2.0.4 lib/dentaku/ast/function.rb
dentaku-2.0.3 lib/dentaku/ast/function.rb
dentaku-2.0.2 lib/dentaku/ast/function.rb
dentaku-2.0.1 lib/dentaku/ast/function.rb