From: Luke Kenneth Casson Leighton Date: Sat, 20 Apr 2019 09:24:46 +0000 (+0100) Subject: investigating Record flattening X-Git-Tag: ls180-24jan2020~1217 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=600416d77350932a740c2fa26e25661714555d5e;p=ieee754fpu.git investigating Record flattening --- diff --git a/src/add/singlepipe.py b/src/add/singlepipe.py index 011019fc..ca01e858 100644 --- a/src/add/singlepipe.py +++ b/src/add/singlepipe.py @@ -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)