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

bugzilla-daemon at libre-soc.org bugzilla-daemon at libre-soc.org
Wed Jun 7 21:02:01 BST 2023


--- Comment #60 from Dmitry Selyutin <ghostmansd at gmail.com> ---
Decoupled visitors from walking:


The record filtering is now implemented in terms of concrete visitor. That's
the whole code:

class Visitor:
    def __call__(self, node):
        method = node.__class__.__name__
        method = getattr(self, method, self.Node)
        return method(node=node)

    def Node(self, node):
        for subnode in node.subnodes:
            with self(subnode):
        yield node

class Record(Node):
    def subnodes(self):
        for (name, fields) in self.extras.items():
            yield Extra(name=name, **fields)

class Database(Node):
    def subnodes(self):
        yield from self

Obviously there's more in Record and Database to be visited, just a practical
example. FWIW, here's how the filtering now looks:

class RecordNameVisitor(Visitor):
    def __init__(self, name):
        self.__name = name
        self.__records = set()
        return super().__init__()

    def Record(self, node):
        if node.name == self.__name:
        yield node

    def __iter__(self):
        yield from self.__records

match = RecordNameVisitor(name=args["insn"])
with match(node=db):
    nodes = frozenset(match)

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

More information about the libre-soc-bugs mailing list