fhdl: fix series of if/elif/else
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 17 Dec 2011 19:31:42 +0000 (20:31 +0100)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 17 Dec 2011 19:31:42 +0000 (20:31 +0100)
migen/fhdl/structure.py

index 9a03b5991f87acdb65c21e4e5f0f1b083d00a731..92f767a82fc45624f7398d8d8ccbb508265e101f 100644 (file)
@@ -161,13 +161,21 @@ class If:
                self.f = StatementList()
        
        def Else(self, *f):
-               self.f = StatementList(f)
+               _insert_else(self, StatementList(f))
                return self
        
        def Elif(self, cond, *t):
-               self.f = StatementList([If(cond, *t)])
+               _insert_else(self, StatementList([If(cond, *t)]))
                return self
 
+def _insert_else(obj, clause):
+       o = obj
+       while o.f.l:
+               assert(len(o.f.l) == 1)
+               assert(isinstance(o.f.l[0], If))
+               o = o.f.l[0]
+       o.f = clause
+
 def _sl(x):
        if isinstance(x, list):
                return StatementList(x)