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.



9785
9786
9787
9788
9789
9790
# File 'lib/syntax_tree/node.rb', line 9785

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



9783
9784
9785
# File 'lib/syntax_tree/node.rb', line 9783

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



9777
9778
9779
# File 'lib/syntax_tree/node.rb', line 9777

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



9780
9781
9782
# File 'lib/syntax_tree/node.rb', line 9780

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9792
9793
9794
# File 'lib/syntax_tree/node.rb', line 9792

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

#child_nodesObject Also known as: deconstruct



9796
9797
9798
# File 'lib/syntax_tree/node.rb', line 9796

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



9802
9803
9804
9805
9806
9807
9808
9809
# File 'lib/syntax_tree/node.rb', line 9802

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

#format(q) ⇒ Object



9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
# File 'lib/syntax_tree/node.rb', line 9811

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