Class: Code::Node::Call

Inherits:
Code::Node show all
Defined in:
lib/code/node/call.rb

Instance Method Summary collapse

Constructor Details

#initialize(call) ⇒ Call

Returns a new instance of Call.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/code/node/call.rb', line 4

def initialize(call)
  @left = ::Code::Node::Statement.new(call.fetch(:left))

  @arguments = call.fetch(:arguments, [])
  @arguments.map! { |argument| ::Code::Node::CallArgument.new(argument) }

  if call.key?(:right)
    @right = call.fetch(:right).map do |right|
      ::Code::Node::ChainedCall.new(right)
    end
  end

  if call.key?(:block)
    @block = ::Code::Node::Block.new(call.fetch(:block))
  end
end

Instance Method Details

#evaluate(**args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/code/node/call.rb', line 21

def evaluate(**args)
  if @right
    left = @left.evaluate(**args)
    @right.reduce(left) do |acc, element|
      element.evaluate(**args.merge(object: acc))
    end
  else
    arguments =
      @arguments.map do |argument|
        ::Code::Object::Argument.new(
          argument.evaluate(**args),
          name: argument.name,
          splat: argument.splat?,
          keyword_splat: argument.keyword_splat?,
          block: argument.block?,
        )
      end

    if @block
      arguments << ::Code::Object::Argument.new(
        @block.evaluate(**args),
        block: true,
      )
    end

    @left.evaluate(**args.merge(arguments: arguments))
  end
end