Commit b5860e73 authored by Stefan Behnel's avatar Stefan Behnel

clean up compiled code in Visitor.py

parent 311e2182
...@@ -5,9 +5,9 @@ cdef class BasicVisitor: ...@@ -5,9 +5,9 @@ cdef class BasicVisitor:
cdef class TreeVisitor(BasicVisitor): cdef class TreeVisitor(BasicVisitor):
cdef public list access_path cdef public list access_path
cpdef visitchild(self, child, parent, attrname, idx) cpdef visitchild(self, child, parent, attrname, idx)
cpdef visitchildren(self, parent, attrs=*)
cdef class VisitorTransform(TreeVisitor): cdef class VisitorTransform(TreeVisitor):
cdef object _super_visitchildren
cpdef visitchildren(self, parent, attrs=*) cpdef visitchildren(self, parent, attrs=*)
cpdef recurse_to_children(self, node) cpdef recurse_to_children(self, node)
......
...@@ -187,14 +187,13 @@ class TreeVisitor(BasicVisitor): ...@@ -187,14 +187,13 @@ class TreeVisitor(BasicVisitor):
or a list of return values (in the case of multiple children or a list of return values (in the case of multiple children
in an attribute)). in an attribute)).
""" """
if parent is None: return None if parent is None: return None
result = {} result = {}
for attr in parent.child_attrs: for attr in parent.child_attrs:
if attrs is not None and attr not in attrs: continue if attrs is not None and attr not in attrs: continue
child = getattr(parent, attr) child = getattr(parent, attr)
if child is not None: if child is not None:
if isinstance(child, list): if type(child) is list:
childretval = [self.visitchild(x, parent, attr, idx) for idx, x in enumerate(child)] childretval = [self.visitchild(x, parent, attr, idx) for idx, x in enumerate(child)]
else: else:
childretval = self.visitchild(child, parent, attr, None) childretval = self.visitchild(child, parent, attr, None)
...@@ -223,22 +222,18 @@ class VisitorTransform(TreeVisitor): ...@@ -223,22 +222,18 @@ class VisitorTransform(TreeVisitor):
was not, an exception will be raised. (Typically you want to ensure that you was not, an exception will be raised. (Typically you want to ensure that you
are within a StatListNode or similar before doing this.) are within a StatListNode or similar before doing this.)
""" """
def __init__(self):
super(VisitorTransform, self).__init__()
self._super_visitchildren = super(VisitorTransform, self).visitchildren
def visitchildren(self, parent, attrs=None): def visitchildren(self, parent, attrs=None):
result = cython.declare(dict) result = cython.declare(dict)
result = self._super_visitchildren(parent, attrs) result = TreeVisitor.visitchildren(self, parent, attrs)
for attr, newnode in result.iteritems(): for attr, newnode in result.iteritems():
if not isinstance(newnode, list): if not type(newnode) is list:
setattr(parent, attr, newnode) setattr(parent, attr, newnode)
else: else:
# Flatten the list one level and remove any None # Flatten the list one level and remove any None
newlist = [] newlist = []
for x in newnode: for x in newnode:
if x is not None: if x is not None:
if isinstance(x, list): if type(x) is list:
newlist += x newlist += x
else: else:
newlist.append(x) newlist.append(x)
......
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