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.



13062
13063
13064
13065
13066
13067
# File 'lib/syntax_tree.rb', line 13062

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



13060
13061
13062
# File 'lib/syntax_tree.rb', line 13060

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13057
13058
13059
# File 'lib/syntax_tree.rb', line 13057

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



13051
13052
13053
# File 'lib/syntax_tree.rb', line 13051

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



13054
13055
13056
# File 'lib/syntax_tree.rb', line 13054

def statements
  @statements
end

Instance Method Details

#child_nodesObject



13069
13070
13071
# File 'lib/syntax_tree.rb', line 13069

def child_nodes
  [predicate, statements]
end

#format(q) ⇒ Object



13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
# File 'lib/syntax_tree.rb', line 13073

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



13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
# File 'lib/syntax_tree.rb', line 13088

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



13102
13103
13104
13105
13106
13107
13108
13109
13110
# File 'lib/syntax_tree.rb', line 13102

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