investigating Record flattening
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 20 Apr 2019 09:24:46 +0000 (10:24 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 20 Apr 2019 09:24:46 +0000 (10:24 +0100)
src/add/singlepipe.py

index 011019fcfd1feafabccc2eb01d84f0538e0b0e3c..ca01e858d4f38e16b9a17cb4fb18a4181bc321b6 100644 (file)
@@ -334,6 +334,25 @@ class Visitor2:
             yield (v, i[k])
         return res
 
+    def _not_quite_working_with_all_unit_tests_record_iter2(self, ao, ai):
+        print ("record_iter2", ao, ai, type(ao), type(ai))
+        if isinstance(ai, Value):
+            if isinstance(ao, Sequence):
+                ao, ai = [ao], [ai]
+            for o, i in zip(ao, ai):
+                yield (o, i)
+            return
+        for idx, (field_name, field_shape, _) in enumerate(ao.layout):
+            if isinstance(field_shape, Layout):
+                val = ai.fields
+            else:
+                val = ai
+            if hasattr(val, field_name): # check for attribute
+                val = getattr(val, field_name)
+            else:
+                val = val[field_name] # dictionary-style specification
+            yield from self.iterator2(ao.fields[field_name], val)
+
     def record_iter2(self, ao, ai):
         for idx, (field_name, field_shape, _) in enumerate(ao.layout):
             if isinstance(field_shape, Layout):
@@ -352,6 +371,7 @@ class Visitor2:
             print ("arrayproxy - p", p, p.name)
             yield from self.iterator2(op, p)
 
+
 class Visitor:
     """ a helper class for iterating single-argument compound data structures.
         similar to Visitor2.
@@ -403,27 +423,12 @@ def eq(o, i):
     return res
 
 
-
 def cat(i):
     """ flattens a compound structure recursively using Cat
-
-        NOTE: this does NOT work:
-            from nmigen.tools import flatten
-            res = list(flatten(i))
-
-        the reason is that flatten is not sophisticated enough,
-        and does not support iteration on Record:
-
-          File "nmigen/tools.py", line 12, in flatten
-            for e in i:
-          File "nmigen/hdl/rec.py", line 98, in __getitem__
-            .format(reference, name, ", ".join(self.fields))) from None
-          NameError: Unnamed record does not have a field '0'.
-                     Did you mean one of: sig1, r2, r3?
     """
-    #from nmigen.tools import flatten
-    #res = list(flatten(i))
-    res = list(Visitor().iterate(i))
+    from nmigen.tools import flatten
+    #res = list(flatten(i)) # works (as of nmigen commit f22106e5) HOWEVER...
+    res = list(Visitor().iterate(i)) # needed because input may be a sequence
     return Cat(*res)