Class: SyntaxTree::While

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

While represents a while loop.

while predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of While.



13148
13149
13150
13151
13152
13153
# File 'lib/syntax_tree.rb', line 13148

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



13146
13147
13148
# File 'lib/syntax_tree.rb', line 13146

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13143
13144
13145
# File 'lib/syntax_tree.rb', line 13143

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



13137
13138
13139
# File 'lib/syntax_tree.rb', line 13137

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



13140
13141
13142
# File 'lib/syntax_tree.rb', line 13140

def statements
  @statements
end

Instance Method Details

#child_nodesObject



13155
13156
13157
# File 'lib/syntax_tree.rb', line 13155

def child_nodes
  [predicate, statements]
end

#format(q) ⇒ Object



13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
# File 'lib/syntax_tree.rb', line 13159

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

#pretty_print(q) ⇒ Object



13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
# File 'lib/syntax_tree.rb', line 13174

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("while")

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



13188
13189
13190
13191
13192
13193
13194
13195
13196
# File 'lib/syntax_tree.rb', line 13188

def to_json(*opts)
  {
    type: :while,
    pred: predicate,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end