Class: SyntaxTree::IfOp

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

Overview

IfOp represents a ternary clause.

predicate ? truthy : falsy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate:, truthy:, falsy:, location:, comments: []) ⇒ IfOp

Returns a new instance of IfOp.



6594
6595
6596
6597
6598
6599
6600
# File 'lib/syntax_tree.rb', line 6594

def initialize(predicate:, truthy:, falsy:, location:, comments: [])
  @predicate = predicate
  @truthy = truthy
  @falsy = falsy
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6592
6593
6594
# File 'lib/syntax_tree.rb', line 6592

def comments
  @comments
end

#falsyObject (readonly)

untyped

the expression to be executed if the predicate is falsy



6586
6587
6588
# File 'lib/syntax_tree.rb', line 6586

def falsy
  @falsy
end

#locationObject (readonly)

Location

the location of this node



6589
6590
6591
# File 'lib/syntax_tree.rb', line 6589

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



6580
6581
6582
# File 'lib/syntax_tree.rb', line 6580

def predicate
  @predicate
end

#truthyObject (readonly)

untyped

the expression to be executed if the predicate is truthy



6583
6584
6585
# File 'lib/syntax_tree.rb', line 6583

def truthy
  @truthy
end

Instance Method Details

#child_nodesObject



6602
6603
6604
# File 'lib/syntax_tree.rb', line 6602

def child_nodes
  [predicate, truthy, falsy]
end

#format(q) ⇒ Object



6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
# File 'lib/syntax_tree.rb', line 6606

def format(q)
  q.group do
    q.if_break do
      q.text("if ")
      q.nest("if ".length) { q.format(predicate) }

      q.indent do
        q.breakable
        q.format(truthy)
      end

      q.breakable
      q.text("else")

      q.indent do
        q.breakable
        q.format(falsy)
      end

      q.breakable
      q.text("end")
    end.if_flat do
      q.format(predicate)
      q.text(" ?")

      q.breakable
      q.format(truthy)
      q.text(" :")

      q.breakable
      q.format(falsy)
    end
  end
end

#pretty_print(q) ⇒ Object



6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
# File 'lib/syntax_tree.rb', line 6641

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

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(truthy)

    q.breakable
    q.pp(falsy)

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

#to_json(*opts) ⇒ Object



6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
# File 'lib/syntax_tree.rb', line 6658

def to_json(*opts)
  {
    type: :ifop,
    pred: predicate,
    tthy: truthy,
    flsy: falsy,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end