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

Maps on the child.



1319
1320
1321
1322
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1319

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.



1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1330

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.



1308
1309
1310
1311
1312
1313
1314
1315
1316
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1308

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