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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(predicate:, statements:, location:) ⇒ WhileNode

Returns a new instance of WhileNode.



11961
11962
11963
11964
11965
11966
# File 'lib/syntax_tree/node.rb', line 11961

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



11959
11960
11961
# File 'lib/syntax_tree/node.rb', line 11959

def comments
  @comments
end

#predicateObject (readonly)

Node

the expression to be checked



11953
11954
11955
# File 'lib/syntax_tree/node.rb', line 11953

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11956
11957
11958
# File 'lib/syntax_tree/node.rb', line 11956

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



12003
12004
12005
12006
# File 'lib/syntax_tree/node.rb', line 12003

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

#accept(visitor) ⇒ Object



11968
11969
11970
# File 'lib/syntax_tree/node.rb', line 11968

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

#child_nodesObject Also known as: deconstruct



11972
11973
11974
# File 'lib/syntax_tree/node.rb', line 11972

def child_nodes
  [predicate, statements]
end

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



11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
# File 'lib/syntax_tree/node.rb', line 11976

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



11990
11991
11992
11993
11994
11995
11996
11997
# File 'lib/syntax_tree/node.rb', line 11990

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

#format(q) ⇒ Object



11999
12000
12001
# File 'lib/syntax_tree/node.rb', line 11999

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

#modifier?Boolean

Returns:

  • (Boolean)


12008
12009
12010
# File 'lib/syntax_tree/node.rb', line 12008

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