Class: Dry::Transformer::Compiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/transformer/compiler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Constant Summary collapse

InvalidFunctionNameError =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry, transformer = nil) ⇒ Compiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Compiler.

API:

  • private



11
12
13
14
# File 'lib/dry/transformer/compiler.rb', line 11

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

Instance Attribute Details

#registryObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



9
10
11
# File 'lib/dry/transformer/compiler.rb', line 9

def registry
  @registry
end

#transformerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



9
10
11
# File 'lib/dry/transformer/compiler.rb', line 9

def transformer
  @transformer
end

Instance Method Details

#call(ast) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



16
17
18
# File 'lib/dry/transformer/compiler.rb', line 16

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

#visit(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



20
21
22
23
# File 'lib/dry/transformer/compiler.rb', line 20

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

#visit_arg(arg) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



38
39
40
# File 'lib/dry/transformer/compiler.rb', line 38

def visit_arg(arg)
  arg
end

#visit_fn(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dry/transformer/compiler.rb', line 25

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

#visit_t(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



42
43
44
# File 'lib/dry/transformer/compiler.rb', line 42

def visit_t(node)
  call(node)
end