[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 15:06:33 BST 2023


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

--- Comment #96 from Dmitry Selyutin <ghostmansd at gmail.com> ---
class VisitorMeta(type):
    def __init__(cls, name, bases, ns):
        cls.__registry = {}
        return super().__init__(cls)

    def __contains__(self, nodecls):
        return self.__registry.__contains__(nodecls)

    def __getitem__(self, nodecls):
        return self.__registry.__getitem__(nodecls)

    def __setitem__(self, nodecls, call):
        return self.__registry.__setitem__(nodecls, call)

    def __iter__(self):
        yield from self.__registry.items()


class Visitor(metaclass=VisitorMeta):
    @contextlib.contextmanager
    def __call__(self, node):
        (visitorcls, nodecls) = map(type, (self, node))
        if nodecls in visitorcls:
            with visitorcls[nodecls](self, node=node) as ctx:
                yield ctx
        else:
            yield node


class visitormethod:
    def __init__(self, visitorcls, nodecls):
        self.__visitorcls = visitorcls
        self.__nodecls = nodecls
        return super().__init__()

    def __call__(self, call):
        self.__visitorcls[self.__nodecls] = contextlib.contextmanager(call)
        return Visitor.__call__

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


More information about the libre-soc-bugs mailing list