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.



12583
12584
12585
12586
12587
12588
# File 'lib/syntax_tree.rb', line 12583

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



12581
12582
12583
# File 'lib/syntax_tree.rb', line 12581

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12578
12579
12580
# File 'lib/syntax_tree.rb', line 12578

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



12572
12573
12574
# File 'lib/syntax_tree.rb', line 12572

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



12575
12576
12577
# File 'lib/syntax_tree.rb', line 12575

def statements
  @statements
end

Instance Method Details

#child_nodesObject



12590
12591
12592
# File 'lib/syntax_tree.rb', line 12590

def child_nodes
  [predicate, statements]
end

#format(q) ⇒ Object



12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
# File 'lib/syntax_tree.rb', line 12594

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



12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
# File 'lib/syntax_tree.rb', line 12609

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



12623
12624
12625
12626
12627
12628
12629
12630
12631
# File 'lib/syntax_tree.rb', line 12623

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