[Libre-soc-bugs] [Bug 1094] insndb instruction database visitor-walker is needed

bugzilla-daemon at libre-soc.org bugzilla-daemon at libre-soc.org
Sat Jun 10 11:16:29 BST 2023


--- Comment #89 from Luke Kenneth Casson Leighton <lkcl at lkcl.net> ---
(In reply to Dmitry Selyutin from comment #87)
> About methods named as classes. If can you propose something that relies on
> _objects_, not _names_, I'm fine with that. 

okaaay, i grasp this now.  some clues: i wondered if it was possible
to just assume that dataclasses are iterable, because if they are
then Node.subnodes may not even be necessary!


drat, that won't work (unless first doing "getattr(node, 'astuple')",
plus __iter__ is unlikely to work on objects.

* bool is a singleton type so could be special-cased

    field = field_value if isinstance(field, bool) else...

* list/tuple/str/int likewise...

no it really is looking like this is the right thing to do here:

  field = field_value if \
          type(field) in [bool,list,tuple,str,int,float,complex,bytes]) else

wait... no... not list/tuple and not field_value

  field = field if \
          type(field) in [bool,str,int,float,complex,bytes]) else ...

and for list/tuple to be walked separately... ahhh *that's* why python ast.py
did "elif isinstance(field, list):"


so the order of "field = ...." should be something like:

* bool first (as a singleton type) yields... *field* not field_value
* type(field) in [str,int,foat,complex,bytes] also yield field
* if class-type is a dataclass, use the dataclass.fields trick
* otherwise just iterate on it.

something like this:

class Dataclass(metaclass=DataclassMeta):
    # https://www.google.com/search?q=python+are+lambda+functions+hashable
    # lambda is a singleton, so there is no danger
    def walk(clsself, match=lambda subnode:True):
        def field_type(field):            return field.type
        def field_value(field):           return getattr(clsself, field.name)

        if type(clsself) in [bool,str,int,float,complex,bytes]: yield clsself
        # this will work because "class is considered a subclass of itself"
        elif issubclass(clsself, dataclass):
            # i prefer the "getattr(field, ...., field.__call__)" thing be used
            field = (field_type if isinstance(clsself, type) else field_value)
            yield from filter(match, map(field, _dataclasses.fields(clsself)))
            yield from clsself # assumes that the object is iterable somehow

something like that?

combined with the proxy-idea i *think* that code becomes fully-independent
of the class types...

it's almost there :)

You are receiving this mail because:
You are on the CC list for the bug.

More information about the libre-soc-bugs mailing list