Commit e92a6012 authored by Robert Bradshaw's avatar Robert Bradshaw

Visitor dispatch optimization

parent b0b1ba40
...@@ -16,12 +16,11 @@ class BasicVisitor(object): ...@@ -16,12 +16,11 @@ class BasicVisitor(object):
self.dispatch_table = {} self.dispatch_table = {}
def visit(self, obj): def visit(self, obj):
pattern = "visit_%s"
cls = obj.__class__ cls = obj.__class__
mname = pattern % cls.__name__ m = self.dispatch_table.get(cls.__name__)
m = self.dispatch_table.get(mname)
if m is None: if m is None:
# Must resolve, try entire hierarchy # Must resolve, try entire hierarchy
pattern = "visit_%s"
mro = inspect.getmro(cls) mro = inspect.getmro(cls)
for cls in mro: for cls in mro:
m = getattr(self, pattern % cls.__name__, None) m = getattr(self, pattern % cls.__name__, None)
...@@ -33,7 +32,7 @@ class BasicVisitor(object): ...@@ -33,7 +32,7 @@ class BasicVisitor(object):
print self.access_path[-1][0].pos print self.access_path[-1][0].pos
print self.access_path[-1][0].__dict__ print self.access_path[-1][0].__dict__
raise RuntimeError("Visitor does not accept object: %s" % obj) raise RuntimeError("Visitor does not accept object: %s" % obj)
self.dispatch_table[mname] = m self.dispatch_table[cls.__name__] = m
return m(obj) return m(obj)
class TreeVisitor(BasicVisitor): class TreeVisitor(BasicVisitor):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment