Sha256: c535b7b7ec4c4e1ea66274f4a1bf3c2c59916cb40405dee2c2edd9ea77d982ed

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

class Code
  class Node
    class Call < Node
      class Block < Node
        def initialize(parsed)
          return if parsed.blank?
          @parameters = parsed.delete(:parameters) { [] }.presence || []
          @parameters.map! { |parameter| FunctionParameter.new(parameter) }

          @body = Code.new(parsed.delete(:body).presence)
        end

        def evaluate(**_args)
          Object::Argument.new(Object::Function.new(@parameters, @body))
        end
      end

      def initialize(parsed)
        return if parsed.blank?
        @name = parsed.delete(:name).presence
        @arguments = parsed.delete(:arguments).presence || []
        @arguments.map! { |argument| CallArgument.new(argument) }

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

      def evaluate(**args)
        arguments = []

        (@arguments || []).each do |argument|
          if argument.keyword?
            if arguments.last&.value.is_a?(Object::Dictionary)
              arguments.last.value.code_set(
                argument.name,
                argument.evaluate(**args).value
              )
            else
              arguments << Object::Argument.new(
                Object::Dictionary.new(
                  { argument.name => argument.evaluate(**args).value }
                )
              )
            end
          else
            arguments << argument.evaluate(**args)
          end
        end

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

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

        args.fetch(:object).call(operator: name, arguments:, **args)
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
code-ruby-0.13.1 lib/code/node/call.rb
code-ruby-0.13.0 lib/code/node/call.rb
code-ruby-0.12.0 lib/code/node/call.rb