Sha256: e1ece7d10e2f55055410e1c4e8565e839b25a437b825aae5673138daa0419a36

Contents?: true

Size: 935 Bytes

Versions: 2

Compression:

Stored size: 935 Bytes

Contents

# frozen_string_literal: true

module Transproc
  # @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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transproc-1.1.1 lib/transproc/compiler.rb
transproc-1.1.0 lib/transproc/compiler.rb