Class: SyntaxTree::Binary

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Binary represents any expression that involves two sub-expressions with an operator in between. This can be something that looks like a mathematical operation:

1 + 1

but can also be something like pushing a value onto an array:

array << value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(left:, operator:, right:, location:) ⇒ Binary

Returns a new instance of Binary.



2004
2005
2006
2007
2008
2009
2010
# File 'lib/syntax_tree/node.rb', line 2004

def initialize(left:, operator:, right:, location:)
  @left = left
  @operator = operator
  @right = right
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2002
2003
2004
# File 'lib/syntax_tree/node.rb', line 2002

def comments
  @comments
end

#leftObject (readonly)

untyped

the left-hand side of the expression



1993
1994
1995
# File 'lib/syntax_tree/node.rb', line 1993

def left
  @left
end

#operatorObject (readonly)

Symbol

the operator used between the two expressions



1996
1997
1998
# File 'lib/syntax_tree/node.rb', line 1996

def operator
  @operator
end

#rightObject (readonly)

untyped

the right-hand side of the expression



1999
2000
2001
# File 'lib/syntax_tree/node.rb', line 1999

def right
  @right
end

Instance Method Details

#===(other) ⇒ Object



2067
2068
2069
2070
# File 'lib/syntax_tree/node.rb', line 2067

def ===(other)
  other.is_a?(Binary) && left === other.left &&
    operator === other.operator && right === other.right
end

#accept(visitor) ⇒ Object



2012
2013
2014
# File 'lib/syntax_tree/node.rb', line 2012

def accept(visitor)
  visitor.visit_binary(self)
end

#child_nodesObject Also known as: deconstruct



2016
2017
2018
# File 'lib/syntax_tree/node.rb', line 2016

def child_nodes
  [left, right]
end

#copy(left: nil, operator: nil, right: nil, location: nil) ⇒ Object



2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
# File 'lib/syntax_tree/node.rb', line 2020

def copy(left: nil, operator: nil, right: nil, location: nil)
  node =
    Binary.new(
      left: left || self.left,
      operator: operator || self.operator,
      right: right || self.right,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



2035
2036
2037
2038
2039
2040
2041
2042
2043
# File 'lib/syntax_tree/node.rb', line 2035

def deconstruct_keys(_keys)
  {
    left: left,
    operator: operator,
    right: right,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
# File 'lib/syntax_tree/node.rb', line 2045

def format(q)
  power = operator == :**

  q.group do
    q.group { q.format(left) }
    q.text(" ") unless power

    if operator == :<<
      q.text("<< ")
      q.format(right)
    else
      q.group do
        q.text(operator.name)
        q.indent do
          power ? q.breakable_empty : q.breakable_space
          q.format(right)
        end
      end
    end
  end
end