fhdl: support nested statement lists
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 5 Jan 2013 13:18:15 +0000 (14:18 +0100)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 5 Jan 2013 13:18:15 +0000 (14:18 +0100)
migen/fhdl/tools.py
migen/fhdl/verilog.py
migen/fhdl/visit.py

index f6d11a090cb176650792f1351fab33127f8963d9..ab9f9d2591933e51f8216020ecd60e0537d83422 100644 (file)
@@ -2,6 +2,14 @@ from migen.fhdl.structure import *
 from migen.fhdl.structure import _Operator, _Slice, _Assign, _ArrayProxy
 from migen.fhdl.visit import NodeVisitor, NodeTransformer
 
+def flat_iteration(l):
+       for element in l:
+               if isinstance(element, (list, tuple)):
+                       for element2 in flat_iteration(element):
+                               yield element2
+               else:
+                       yield element
+
 class _SignalLister(NodeVisitor):
        def __init__(self):
                self.output_list = set()
@@ -35,7 +43,7 @@ def list_targets(node):
 
 def group_by_targets(sl):
        groups = []
-       for statement in sl:
+       for statement in flat_iteration(sl):
                targets = list_targets(statement)
                processed = False
                for g in groups:
index a8f8f6e6d4ed5a02684ee8fd87f0c87ab83fcee5..b26689d50f5b7707df853f587c32a28412c5180d 100644 (file)
@@ -97,7 +97,7 @@ def _printnode(ns, at, level, node):
                else:
                        assignment = " <= "
                return "\t"*level + _printexpr(ns, node.l)[0] + assignment + _printexpr(ns, node.r)[0] + ";\n"
-       elif isinstance(node, list):
+       elif isinstance(node, (list, tuple)):
                return "".join(list(map(partial(_printnode, ns, at, level), node)))
        elif isinstance(node, If):
                r = "\t"*level + "if (" + _printexpr(ns, node.cond)[0] + ") begin\n"
index 3c39700267d909e06f13441fac06840f97a9bc07..059daeef0a405a80e897d8b463d7ed5d7b3dae4c 100644 (file)
@@ -25,7 +25,7 @@ class NodeVisitor:
                        self.visit_Case(node)
                elif isinstance(node, Fragment):
                        self.visit_Fragment(node)
-               elif isinstance(node, list):
+               elif isinstance(node, (list, tuple)):
                        self.visit_statements(node)
                elif isinstance(node, dict):
                        self.visit_clock_domains(node)
@@ -115,7 +115,7 @@ class NodeTransformer:
                        return self.visit_Case(node)
                elif isinstance(node, Fragment):
                        return self.visit_Fragment(node)
-               elif isinstance(node, list):
+               elif isinstance(node, (list, tuple)):
                        return self.visit_statements(node)
                elif isinstance(node, dict):
                        return self.visit_clock_domains(node)
@@ -164,6 +164,7 @@ class NodeTransformer:
                r.sync = self.visit(node.sync)
                return r
        
+       # NOTE: this will always return a list, even if node is a tuple
        def visit_statements(self, node):
                return [self.visit(statement) for statement in node]