Class: Ikra::AST::IfNode

Inherits:
TreeNode show all
Defined in:
lib/ast/nodes.rb,
lib/ast/printer.rb,
lib/ast/visitor.rb

Constant Summary

Constants inherited from TreeNode

TreeNode::TYPE_INFO_VARS

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Instance Method Summary collapse

Methods inherited from TreeNode

#==, #enclosing_class, #find_behavior_node, #get_type, #is_begin_node?, #merge_union_type, #register_type_change, #replace, #replace_child, #symbol_table

Methods inherited from Node

#==, #eql?, #hash

Constructor Details

#initialize(condition:, true_body_stmts:, false_body_stmts: nil) ⇒ IfNode

Returns a new instance of IfNode.



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/ast/nodes.rb', line 548

def initialize(condition:, true_body_stmts:, false_body_stmts: nil)
    if true_body_stmts == nil
        # Handle empty `true` block
        true_body_stmts = BeginNode.new
    end

    if false_body_stmts == nil
        # Handle empty `false` block
        false_body_stmts = BeginNode.new
    end

    @condition = condition
    @true_body_stmts = true_body_stmts
    @false_body_stmts = false_body_stmts

    condition.parent = self
    true_body_stmts.parent = self 
    false_body_stmts.parent = self
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



544
545
546
# File 'lib/ast/nodes.rb', line 544

def condition
  @condition
end

#false_body_stmtsObject (readonly)

Returns the value of attribute false_body_stmts.



546
547
548
# File 'lib/ast/nodes.rb', line 546

def false_body_stmts
  @false_body_stmts
end

#true_body_stmtsObject (readonly)

Returns the value of attribute true_body_stmts.



545
546
547
# File 'lib/ast/nodes.rb', line 545

def true_body_stmts
  @true_body_stmts
end

Instance Method Details

#accept(visitor) ⇒ Object



164
165
166
# File 'lib/ast/visitor.rb', line 164

def accept(visitor)
    return visitor.visit_if_node(self)
end

#cloneObject



568
569
570
571
572
573
# File 'lib/ast/nodes.rb', line 568

def clone
    return IfNode.new(
        condition: @condition.clone,
        true_body_stmts: @true_body_stmts.clone,
        false_body_stmts: @false_body_stmts.clone)
end

#to_sObject



156
157
158
159
160
161
162
# File 'lib/ast/printer.rb', line 156

def to_s
    if false_body_stmts != nil
        return "[IfNode: #{condition.to_s}, #{true_body_stmts.to_s}, #{false_body_stmts.to_s}]"
    else
        return "[IfNode: #{condition.to_s}, #{true_body_stmts.to_s}]"
    end
end