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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9741
9742
9743
# File 'lib/syntax_tree/node.rb', line 9741

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9750
9751
9752
# File 'lib/syntax_tree/node.rb', line 9750

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

#child_nodesObject Also known as: deconstruct



9754
9755
9756
# File 'lib/syntax_tree/node.rb', line 9754

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



9760
9761
9762
9763
9764
9765
9766
9767
# File 'lib/syntax_tree/node.rb', line 9760

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

#format(q) ⇒ Object



9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
# File 'lib/syntax_tree/node.rb', line 9769

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