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.



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

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



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

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



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

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



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

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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