Class: Transproc::Compiler Private

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

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.

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.

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.



10
11
12
13
# File 'lib/transproc/compiler.rb', line 10

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.



8
9
10
# File 'lib/transproc/compiler.rb', line 8

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.



8
9
10
# File 'lib/transproc/compiler.rb', line 8

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.



15
16
17
# File 'lib/transproc/compiler.rb', line 15

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.



19
20
21
22
# File 'lib/transproc/compiler.rb', line 19

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.



37
38
39
# File 'lib/transproc/compiler.rb', line 37

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.



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

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.



41
42
43
# File 'lib/transproc/compiler.rb', line 41

def visit_t(node)
  call(node)
end