Class: SyntaxTree::While

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:, comments: []) ⇒ While

Returns a new instance of While.



9732
9733
9734
9735
9736
9737
# File 'lib/syntax_tree/node.rb', line 9732

def initialize(predicate:, statements:, location:, comments: [])
  @predicate = predicate
  @statements = statements
  @location = location
  @comments = comments
end

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



9730
9731
9732
# File 'lib/syntax_tree/node.rb', line 9730

def comments
  @comments
end

#predicate ⇒ Object (readonly)

[untyped] the expression to be checked



9724
9725
9726
# File 'lib/syntax_tree/node.rb', line 9724

def predicate
  @predicate
end

#statements ⇒ Object (readonly)

[Statements] the expressions to be executed



9727
9728
9729
# File 'lib/syntax_tree/node.rb', line 9727

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9739
9740
9741
# File 'lib/syntax_tree/node.rb', line 9739

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

#child_nodes ⇒ Object Also known as: deconstruct



9743
9744
9745
# File 'lib/syntax_tree/node.rb', line 9743

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



9749
9750
9751
9752
9753
9754
9755
9756
# File 'lib/syntax_tree/node.rb', line 9749

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

#format(q) ⇒ Object



9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
# File 'lib/syntax_tree/node.rb', line 9758

def format(q)
  if statements.empty?
    keyword = "while "

    q.group do
      q.text(keyword)
      q.nest(keyword.length) { q.format(predicate) }
      q.breakable(force: true)
      q.text("end")
    end
  else
    LoopFormatter.new("while", self, statements).format(q)
  end
end