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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Binary.



1939
1940
1941
1942
1943
1944
1945
# File 'lib/syntax_tree/node.rb', line 1939

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1937
1938
1939
# File 'lib/syntax_tree/node.rb', line 1937

def comments
  @comments
end

#leftObject (readonly)

untyped

the left-hand side of the expression



1925
1926
1927
# File 'lib/syntax_tree/node.rb', line 1925

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



1934
1935
1936
# File 'lib/syntax_tree/node.rb', line 1934

def location
  @location
end

#operatorObject (readonly)

Symbol

the operator used between the two expressions



1928
1929
1930
# File 'lib/syntax_tree/node.rb', line 1928

def operator
  @operator
end

#rightObject (readonly)

untyped

the right-hand side of the expression



1931
1932
1933
# File 'lib/syntax_tree/node.rb', line 1931

def right
  @right
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1947
1948
1949
# File 'lib/syntax_tree/node.rb', line 1947

def child_nodes
  [left, right]
end

#deconstruct_keys(keys) ⇒ Object



1953
1954
1955
1956
1957
1958
1959
1960
1961
# File 'lib/syntax_tree/node.rb', line 1953

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

#format(q) ⇒ Object



1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
# File 'lib/syntax_tree/node.rb', line 1963

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

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

    q.group do
      q.text(operator)

      q.indent do
        q.breakable(power ? "" : " ")
        q.format(right)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
# File 'lib/syntax_tree/node.rb', line 1981

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("binary")

    q.breakable
    q.pp(left)

    q.breakable
    q.text(operator)

    q.breakable
    q.pp(right)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
# File 'lib/syntax_tree/node.rb', line 1998

def to_json(*opts)
  {
    type: :binary,
    left: left,
    op: operator,
    right: right,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end