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.



11928
11929
11930
11931
11932
11933
# File 'lib/syntax_tree/node.rb', line 11928

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



11926
11927
11928
# File 'lib/syntax_tree/node.rb', line 11926

def comments
  @comments
end

#predicateObject (readonly)

Node

the expression to be checked



11920
11921
11922
# File 'lib/syntax_tree/node.rb', line 11920

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11923
11924
11925
# File 'lib/syntax_tree/node.rb', line 11923

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



11935
11936
11937
# File 'lib/syntax_tree/node.rb', line 11935

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

#child_nodesObject Also known as: deconstruct



11939
11940
11941
# File 'lib/syntax_tree/node.rb', line 11939

def child_nodes
  [predicate, statements]
end

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



11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
# File 'lib/syntax_tree/node.rb', line 11943

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



11957
11958
11959
11960
11961
11962
11963
11964
# File 'lib/syntax_tree/node.rb', line 11957

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

#format(q) ⇒ Object



11966
11967
11968
# File 'lib/syntax_tree/node.rb', line 11966

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

#modifier?Boolean

Returns:

  • (Boolean)


11975
11976
11977
# File 'lib/syntax_tree/node.rb', line 11975

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