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



10058
10059
10060
10061
10062
10063
# File 'lib/syntax_tree/node.rb', line 10058

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



10056
10057
10058
# File 'lib/syntax_tree/node.rb', line 10056

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



10050
10051
10052
# File 'lib/syntax_tree/node.rb', line 10050

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



10053
10054
10055
# File 'lib/syntax_tree/node.rb', line 10053

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



10065
10066
10067
# File 'lib/syntax_tree/node.rb', line 10065

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

#child_nodesObject Also known as: deconstruct



10069
10070
10071
# File 'lib/syntax_tree/node.rb', line 10069

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



10075
10076
10077
10078
10079
10080
10081
10082
# File 'lib/syntax_tree/node.rb', line 10075

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

#format(q) ⇒ Object



10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
# File 'lib/syntax_tree/node.rb', line 10084

def format(q)
  if statements.empty?
    keyword = "while "

    q.group do
      q.text(keyword)
      q.nest(keyword.length) { q.format(predicate) }
      q.breakable_force
      q.text("end")
    end
  else
    LoopFormatter.new("while", self, statements).format(q)
  end
end