[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 14:17:00 BST 2023
https://bugs.libre-soc.org/show_bug.cgi?id=1094
--- Comment #92 from Dmitry Selyutin <ghostmansd at gmail.com> ---
That's what I suggest instead all of these "let's exclude special types",
"let's invent a magic method which has class name", etc.
$ cat interface.py
class Visitor:
@contextlib.contextmanager
def __call__(self, node):
(visitorcls, nodecls) = map(type, (self, node))
mgr = visitor.registry.get(visitorcls, {}).get(nodecls, None)
if mgr is not None:
with mgr(self, node=node) as ctx:
yield ctx
else:
yield node
def visitor(visitorcls, nodecls):
def decorator(fn):
mgr = contextlib.contextmanager(fn)
visitor.registry[visitorcls][nodecls] = mgr
return Visitor.__call__
return decorator
visitor.registry = collections.defaultdict(dict)
$ cat implementation.py
class StubVisitor(Visitor):
pass
@visitor(StubVisitor, str)
def coco(self, node):
print("BY GOD IT'S A STRING!!!", node)
yield node
@visitor(StubVisitor, int)
def jamboo(self, node):
print("DONNERWETTER WE HAVE AN INTEGER!", node)
yield node
stub = StubVisitor()
with StubVisitor().__call__("hi Luke"):
pass
with StubVisitor().__call__(42):
pass
--
You are receiving this mail because:
You are on the CC list for the bug.
More information about the libre-soc-bugs
mailing list