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.



9621
9622
9623
9624
9625
9626
# File 'lib/syntax_tree/node.rb', line 9621

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



9619
9620
9621
# File 'lib/syntax_tree/node.rb', line 9619

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



9613
9614
9615
# File 'lib/syntax_tree/node.rb', line 9613

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



9616
9617
9618
# File 'lib/syntax_tree/node.rb', line 9616

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9628
9629
9630
# File 'lib/syntax_tree/node.rb', line 9628

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

#child_nodesObject Also known as: deconstruct



9632
9633
9634
# File 'lib/syntax_tree/node.rb', line 9632

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



9638
9639
9640
9641
9642
9643
9644
9645
# File 'lib/syntax_tree/node.rb', line 9638

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

#format(q) ⇒ Object



9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
# File 'lib/syntax_tree/node.rb', line 9647

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