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

Constructor Details

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

Returns a new instance of Binary.



1873
1874
1875
1876
1877
1878
1879
# File 'lib/syntax_tree/node.rb', line 1873

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



1871
1872
1873
# File 'lib/syntax_tree/node.rb', line 1871

def comments
  @comments
end

#leftObject (readonly)

untyped

the left-hand side of the expression



1862
1863
1864
# File 'lib/syntax_tree/node.rb', line 1862

def left
  @left
end

#operatorObject (readonly)

Symbol

the operator used between the two expressions



1865
1866
1867
# File 'lib/syntax_tree/node.rb', line 1865

def operator
  @operator
end

#rightObject (readonly)

untyped

the right-hand side of the expression



1868
1869
1870
# File 'lib/syntax_tree/node.rb', line 1868

def right
  @right
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1881
1882
1883
# File 'lib/syntax_tree/node.rb', line 1881

def child_nodes
  [left, right]
end

#deconstruct_keys(keys) ⇒ Object



1887
1888
1889
1890
1891
1892
1893
1894
1895
# File 'lib/syntax_tree/node.rb', line 1887

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

#format(q) ⇒ Object



1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
# File 'lib/syntax_tree/node.rb', line 1897

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



1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
# File 'lib/syntax_tree/node.rb', line 1915

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



1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
# File 'lib/syntax_tree/node.rb', line 1932

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