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.



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

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



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

def comments
  @comments
end

#predicateObject (readonly)

Node

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [predicate, statements]
end

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



11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
# File 'lib/syntax_tree/node.rb', line 11979

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



11993
11994
11995
11996
11997
11998
11999
12000
# File 'lib/syntax_tree/node.rb', line 11993

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

#format(q) ⇒ Object



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

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

#modifier?Boolean

Returns:

  • (Boolean)


12011
12012
12013
# File 'lib/syntax_tree/node.rb', line 12011

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