Class: SyntaxTree::WhileNode

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

Overview

While represents a while loop.

while predicate
end

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(predicate:, statements:, location:) ⇒ WhileNode



11776
11777
11778
11779
11780
11781
# File 'lib/syntax_tree/node.rb', line 11776

def initialize(predicate:, statements:, location:)
  @predicate = predicate
  @statements = statements
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11774
11775
11776
# File 'lib/syntax_tree/node.rb', line 11774

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



11768
11769
11770
# File 'lib/syntax_tree/node.rb', line 11768

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11771
11772
11773
# File 'lib/syntax_tree/node.rb', line 11771

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



11818
11819
11820
11821
# File 'lib/syntax_tree/node.rb', line 11818

def ===(other)
  other.is_a?(WhileNode) && predicate === other.predicate &&
    statements === other.statements
end

#accept(visitor) ⇒ Object



11783
11784
11785
# File 'lib/syntax_tree/node.rb', line 11783

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

#child_nodesObject Also known as: deconstruct



11787
11788
11789
# File 'lib/syntax_tree/node.rb', line 11787

def child_nodes
  [predicate, statements]
end

#copy(predicate: nil, statements: nil, location: nil) ⇒ Object



11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
# File 'lib/syntax_tree/node.rb', line 11791

def copy(predicate: nil, statements: nil, location: nil)
  node =
    WhileNode.new(
      predicate: predicate || self.predicate,
      statements: statements || self.statements,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



11805
11806
11807
11808
11809
11810
11811
11812
# File 'lib/syntax_tree/node.rb', line 11805

def deconstruct_keys(_keys)
  {
    predicate: predicate,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



11814
11815
11816
# File 'lib/syntax_tree/node.rb', line 11814

def format(q)
  LoopFormatter.new("while", self).format(q)
end

#modifier?Boolean



11823
11824
11825
# File 'lib/syntax_tree/node.rb', line 11823

def modifier?
  predicate.location.start_char > statements.location.start_char
end