Sha256: 64c9ec3a67a9d1a7fd22792c7682c907f6476a88a9da401e07c0e5439c4c4e7e

Contents?: true

Size: 1022 Bytes

Versions: 4

Compression:

Stored size: 1022 Bytes

Contents

# frozen_string_literal: true

module Dry
  module Transformer
    # @api private
    class Compiler
      InvalidFunctionNameError = Class.new(StandardError)

      attr_reader :registry, :transformer

      def initialize(registry, transformer = nil)
        @registry = registry
        @transformer = transformer
      end

      def call(ast)
        ast.map(&method(:visit)).reduce(:>>)
      end

      def visit(node)
        id, *rest = node
        public_send(:"visit_#{id}", *rest)
      end

      def visit_fn(node)
        name, rest = node
        args = rest.map { |arg| visit(arg) }

        if registry.contain?(name)
          registry[name, *args]
        elsif transformer.respond_to?(name)
          Function.new(transformer.method(name), name: name, args: args)
        else
          raise InvalidFunctionNameError, "function name +#{name}+ is not valid"
        end
      end

      def visit_arg(arg)
        arg
      end

      def visit_t(node)
        call(node)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-transformer-1.0.1 lib/dry/transformer/compiler.rb
dry-transformer-1.0.0 lib/dry/transformer/compiler.rb
dry-transformer-0.1.1 lib/dry/transformer/compiler.rb
dry-transformer-0.1.0 lib/dry/transformer/compiler.rb