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

Returns a new instance of WhileNode.



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

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



11765
11766
11767
# File 'lib/syntax_tree/node.rb', line 11765

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



11759
11760
11761
# File 'lib/syntax_tree/node.rb', line 11759

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11762
11763
11764
# File 'lib/syntax_tree/node.rb', line 11762

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



11809
11810
11811
11812
# File 'lib/syntax_tree/node.rb', line 11809

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [predicate, statements]
end

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



11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
# File 'lib/syntax_tree/node.rb', line 11782

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



11796
11797
11798
11799
11800
11801
11802
11803
# File 'lib/syntax_tree/node.rb', line 11796

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

#format(q) ⇒ Object



11805
11806
11807
# File 'lib/syntax_tree/node.rb', line 11805

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

#modifier?Boolean

Returns:

  • (Boolean)


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

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