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

#pretty_print, #to_json

Constructor Details

#initialize(predicate:, statements:, location:, comments: []) ⇒ While

Returns a new instance of While.



8944
8945
8946
8947
8948
8949
# File 'lib/syntax_tree/node.rb', line 8944

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



8942
8943
8944
# File 'lib/syntax_tree/node.rb', line 8942

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



8936
8937
8938
# File 'lib/syntax_tree/node.rb', line 8936

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



8939
8940
8941
# File 'lib/syntax_tree/node.rb', line 8939

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



8951
8952
8953
# File 'lib/syntax_tree/node.rb', line 8951

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

#child_nodesObject Also known as: deconstruct



8955
8956
8957
# File 'lib/syntax_tree/node.rb', line 8955

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(keys) ⇒ Object



8961
8962
8963
8964
8965
8966
8967
8968
# File 'lib/syntax_tree/node.rb', line 8961

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

#format(q) ⇒ Object



8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
# File 'lib/syntax_tree/node.rb', line 8970

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