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.



11897
11898
11899
11900
11901
11902
# File 'lib/syntax_tree/node.rb', line 11897

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



11895
11896
11897
# File 'lib/syntax_tree/node.rb', line 11895

def comments
  @comments
end

#predicateObject (readonly)

Node

the expression to be checked



11889
11890
11891
# File 'lib/syntax_tree/node.rb', line 11889

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11892
11893
11894
# File 'lib/syntax_tree/node.rb', line 11892

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



11904
11905
11906
# File 'lib/syntax_tree/node.rb', line 11904

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

#child_nodesObject Also known as: deconstruct



11908
11909
11910
# File 'lib/syntax_tree/node.rb', line 11908

def child_nodes
  [predicate, statements]
end

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



11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
# File 'lib/syntax_tree/node.rb', line 11912

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



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

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

#format(q) ⇒ Object



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

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

#modifier?Boolean

Returns:

  • (Boolean)


11944
11945
11946
# File 'lib/syntax_tree/node.rb', line 11944

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