Module: HDLRuby::Low::OneChildMutable

Included in:
Cast, Unary
Defined in:
lib/HDLRuby/hruby_low_mutable.rb

Overview

Module for mutable expressions with one child.

Instance Method Summary collapse

Instance Method Details

#map_nodes!(&ruby_block) ⇒ Object Also known as: map_expressions!

Maps on the child.



1412
1413
1414
1415
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1412

def map_nodes!(&ruby_block)
    @child = ruby_block.call(@child)
    @child.parent = self unless @child.parent
end

#replace_expressions!(node2rep) ⇒ Object

Replaces sub expressions using +node2rep+ table indicating the node to replace and the corresponding replacement. Returns the actually replaced nodes and their corresponding replacement.

NOTE: the replacement is duplicated.



1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1425

def replace_expressions!(node2rep)
    # First recurse on the child.
    res = self.child.replace_expressions!(node2rep)
    # Is there a replacement to do?
    rep = node2rep[self.child]
    if rep then
        # Yes, do it.
        rep = rep.clone
        node = self.child
        # node.set_parent!(nil)
        self.set_child!(rep)
        # And register the replacement.
        res[node] = rep
    end
    return res
end

#set_child!(child) ⇒ Object

Sets the child.



1401
1402
1403
1404
1405
1406
1407
1408
1409
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1401

def set_child!(child)
    # Check and set the child.
    unless child.is_a?(Expression)
        raise AnyError,"Invalid class for an expression: #{child.class}"
    end
    @child = child
    # And set its parent.
    child.parent = self
end