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.



9701
9702
9703
9704
9705
9706
# File 'lib/syntax_tree/node.rb', line 9701

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



9699
9700
9701
# File 'lib/syntax_tree/node.rb', line 9699

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



9693
9694
9695
# File 'lib/syntax_tree/node.rb', line 9693

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



9696
9697
9698
# File 'lib/syntax_tree/node.rb', line 9696

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9708
9709
9710
# File 'lib/syntax_tree/node.rb', line 9708

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

#child_nodesObject Also known as: deconstruct



9712
9713
9714
# File 'lib/syntax_tree/node.rb', line 9712

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



9718
9719
9720
9721
9722
9723
9724
9725
# File 'lib/syntax_tree/node.rb', line 9718

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

#format(q) ⇒ Object



9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
# File 'lib/syntax_tree/node.rb', line 9727

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