Class: MiniRuby::AST::FunctionCallNode

Inherits:
ExpressionNode show all
Defined in:
lib/miniruby/ast.rb

Overview

Represents a function call like ‘foo(1, bar)`

Instance Attribute Summary collapse

Attributes inherited from Node

#span

Instance Method Summary collapse

Constructor Details

#initialize(name:, arguments: [], span: Span::ZERO) ⇒ FunctionCallNode

: (name: String, ?arguments: Array, ?span: Span) -> void



744
745
746
747
748
# File 'lib/miniruby/ast.rb', line 744

def initialize(name:, arguments: [], span: Span::ZERO)
  @span = span
  @name = name
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

: Array



741
742
743
# File 'lib/miniruby/ast.rb', line 741

def arguments
  @arguments
end

#nameObject (readonly)

: String



738
739
740
# File 'lib/miniruby/ast.rb', line 738

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

: (Object other) -> bool



751
752
753
754
755
756
# File 'lib/miniruby/ast.rb', line 751

def ==(other)
  return false unless other.is_a?(FunctionCallNode)

  @name == other.name &&
    @arguments == other.arguments
end

#inspect(indent = 0) ⇒ Object

: (?Integer indent) -> String



776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/miniruby/ast.rb', line 776

def inspect(indent = 0)
  buff = String.new
  buff << "#{INDENT_UNIT * indent}(call\n"

  buff << "#{INDENT_UNIT * (indent + 1)}#{@name}"
  @arguments.each do |arg|
    buff << "\n" << arg.inspect(indent + 1)
  end
  buff << ')'

  buff
end

#to_s(indent = 0) ⇒ Object

: (?Integer indent) -> String



760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/miniruby/ast.rb', line 760

def to_s(indent = 0)
  buff = String.new
  indent_str = INDENT_UNIT * indent

  buff << "#{indent_str}#{@name}("
  @arguments.each.with_index do |arg, i|
    buff << ', ' if i > 0
    buff << arg.to_s
  end
  buff << ')'

  buff
end