Class: Code::Node::Call

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

Defined Under Namespace

Classes: Block

Instance Method Summary collapse

Constructor Details

#initialize(parsed) ⇒ Call

Returns a new instance of Call.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/code/node/call.rb', line 26

def initialize(parsed)
  return if parsed.blank?

  @name = parsed.delete(:name).presence
  @explicit_arguments = parsed.key?(:arguments)
  @arguments = parsed.delete(:arguments).presence || []
  @arguments = [@arguments] unless @arguments.is_a?(Array)
  @arguments.map! { |argument| CallArgument.new(argument) }

  return unless parsed.key?(:block)

  @block = Call::Block.new(parsed.delete(:block).presence)
end

Instance Method Details

#evaluate(**args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/code/node/call.rb', line 40

def evaluate(**args)
  arguments = []

  (@arguments || []).each do |argument|
    if argument.expansion?
      append_expanded_argument(arguments, argument, **args)
    elsif argument.keyword?
      if arguments.last.is_a?(Object::Dictionary)
        arguments.last.code_merge!(argument.evaluate(**args))
      else
        arguments << argument.evaluate(**args)
      end
    else
      arguments << argument.evaluate(**args)
    end
  end

  arguments << @block.evaluate(**args) if @block

  name = Object::String.new(@name)

  object = args.fetch(:object)
  dynamic_result =
    object.code_dynamic_call(
      name,
      operator: name,
      arguments: Object::List.new(arguments),
      explicit_arguments: @explicit_arguments,
      **args
    )
  return dynamic_result if dynamic_result

  object.call(
    operator: name,
    arguments: Object::List.new(arguments),
    explicit_arguments: @explicit_arguments,
    **args
  )
end

#resolve(**_args) ⇒ Object



80
81
82
# File 'lib/code/node/call.rb', line 80

def resolve(**_args)
  Object::String.new(@name)
end