Commit 2ab4744f authored by Stefan Behnel's avatar Stefan Behnel

Minor code simplifications in visitor implementation.

parent 743df03d
......@@ -12,10 +12,12 @@ cdef class TreeVisitor:
cdef _visitchild(self, child, parent, attrname, idx)
cdef dict _visitchildren(self, parent, attrs)
cpdef visitchildren(self, parent, attrs=*)
cdef _raise_compiler_error(self, child, e)
cdef class VisitorTransform(TreeVisitor):
cdef dict _process_children(self, parent, attrs=*)
cpdef visitchildren(self, parent, attrs=*)
cdef list _flatten_list(self, list orig_list)
cdef class CythonTransform(VisitorTransform):
cdef public context
......
......@@ -78,7 +78,7 @@ class TreeVisitor(object):
self.dispatch_table = {}
self.access_path = []
def dump_node(self, node, indent=0):
def dump_node(self, node):
ignored = list(node.child_attrs or []) + [
u'child_attrs', u'pos', u'gil_message', u'cpp_message', u'subexprs']
values = []
......@@ -90,7 +90,6 @@ class TreeVisitor(object):
source = os.path.basename(source.get_description())
values.append(u'%s:%s:%s' % (source, pos[1], pos[2]))
attribute_names = dir(node)
attribute_names.sort()
for attr in attribute_names:
if attr in ignored:
continue
......@@ -156,7 +155,6 @@ class TreeVisitor(object):
cls = type(obj)
pattern = "visit_%s"
mro = inspect.getmro(cls)
handler_method = None
for mro_cls in mro:
handler_method = getattr(self, pattern % mro_cls.__name__, None)
if handler_method is not None:
......@@ -255,19 +253,22 @@ class VisitorTransform(TreeVisitor):
# fast cdef entry point for calls from Cython subclasses
result = self._visitchildren(parent, attrs)
for attr, newnode in result.items():
if type(newnode) is not list:
if type(newnode) is list:
newnode = self._flatten_list(newnode)
setattr(parent, attr, newnode)
else:
return result
@cython.final
def _flatten_list(self, orig_list):
# Flatten the list one level and remove any None
newlist = []
for x in newnode:
for x in orig_list:
if x is not None:
if type(x) is list:
newlist += x
newlist.extend(x)
else:
newlist.append(x)
setattr(parent, attr, newlist)
return result
return newlist
def recurse_to_children(self, node):
self._process_children(node)
......@@ -276,6 +277,7 @@ class VisitorTransform(TreeVisitor):
def __call__(self, root):
return self._visit(root)
class CythonTransform(VisitorTransform):
"""
Certain common conventions and utilities for Cython transforms.
......
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