Class: SyntaxTree::If

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

Overview

If represents the first clause in an if chain.

if 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:, consequent:, location:, comments: []) ⇒ If

Returns a new instance of If.



5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
# File 'lib/syntax_tree/node.rb', line 5566

def initialize(
  predicate:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5564
5565
5566
# File 'lib/syntax_tree/node.rb', line 5564

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



5561
5562
5563
# File 'lib/syntax_tree/node.rb', line 5561

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



5555
5556
5557
# File 'lib/syntax_tree/node.rb', line 5555

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5558
5559
5560
# File 'lib/syntax_tree/node.rb', line 5558

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5580
5581
5582
# File 'lib/syntax_tree/node.rb', line 5580

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

#child_nodesObject Also known as: deconstruct



5584
5585
5586
# File 'lib/syntax_tree/node.rb', line 5584

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



5590
5591
5592
5593
5594
5595
5596
5597
5598
# File 'lib/syntax_tree/node.rb', line 5590

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

#format(q) ⇒ Object



5600
5601
5602
# File 'lib/syntax_tree/node.rb', line 5600

def format(q)
  ConditionalFormatter.new("if", self).format(q)
end