[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


https://bugs.libre-soc.org/show_bug.cgi?id=1094

--- 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!

https://stackoverflow.com/questions/74393947/make-python-dataclass-iterable
https://github.com/ericvsmith/dataclasses/issues/21

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
  https://stackoverflow.com/a/2172204

    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):"

https://github.com/python/cpython/blob/2.7/Lib/ast.py#L181

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
  (field_type+field_value)
* otherwise just iterate on it.

something like this:

class Dataclass(metaclass=DataclassMeta):
    @walkmethod
    # 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
        #
https://www.programiz.com/python-programming/methods/built-in/issubclass
        # 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)))
        else:
            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