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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of If.



5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
# File 'lib/syntax_tree/node.rb', line 5221

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



5219
5220
5221
# File 'lib/syntax_tree/node.rb', line 5219

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



5216
5217
5218
# File 'lib/syntax_tree/node.rb', line 5216

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



5210
5211
5212
# File 'lib/syntax_tree/node.rb', line 5210

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5213
5214
5215
# File 'lib/syntax_tree/node.rb', line 5213

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5235
5236
5237
# File 'lib/syntax_tree/node.rb', line 5235

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

#child_nodesObject Also known as: deconstruct



5239
5240
5241
# File 'lib/syntax_tree/node.rb', line 5239

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



5245
5246
5247
5248
5249
5250
5251
5252
5253
# File 'lib/syntax_tree/node.rb', line 5245

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

#format(q) ⇒ Object



5255
5256
5257
# File 'lib/syntax_tree/node.rb', line 5255

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