1.1 --- a/simplified.py Thu Feb 15 20:32:37 2007 +0100
1.2 +++ b/simplified.py Fri Feb 16 00:13:09 2007 +0100
1.3 @@ -165,6 +165,7 @@
1.4
1.5 self.original = original
1.6 self.defining = defining
1.7 + self.copies = []
1.8
1.9 if self.original is not None and defining:
1.10 self.original._node = self
1.11 @@ -282,6 +283,14 @@
1.12 self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream)
1.13 self._pprint(indent, "", "--------", stream=stream)
1.14
1.15 + # Node discovery functions.
1.16 +
1.17 + def active(self):
1.18 +
1.19 + "Return the active copies of this node or a list containing this node."
1.20 +
1.21 + return self.copies or [self]
1.22 +
1.23 # Node manipulation functions.
1.24
1.25 def copy(self, new_name=None):
1.26 @@ -307,12 +316,9 @@
1.27 node = self.__class__(**common)
1.28 node.defining = self.defining
1.29
1.30 - # Add links to copied nodes from original AST nodes.
1.31 + # Add links to copies from originals.
1.32
1.33 - if node.original is not None and node.defining:
1.34 - if not hasattr(node.original, "_nodes"):
1.35 - node.original._nodes = []
1.36 - node.original._nodes.append(node)
1.37 + self.copies.append(node)
1.38
1.39 # Copy attributes of different types.
1.40
2.1 --- a/simplify.py Thu Feb 15 20:32:37 2007 +0100
2.2 +++ b/simplify.py Fri Feb 16 00:13:09 2007 +0100
2.3 @@ -838,10 +838,12 @@
2.4 (dstar)
2.5 """
2.6
2.7 - # NOTE: Making the actual subprogram defining, too.
2.8 + subprogram = Subprogram(function, name=function.name, module=self.module, structures=self.current_structures[:],
2.9 + internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function)
2.10
2.11 - subprogram = Subprogram(function, 1, name=function.name, module=self.module, structures=self.current_structures[:],
2.12 - internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function)
2.13 + # Make nice annotations for the viewer.
2.14 +
2.15 + function._subprogram = subprogram
2.16
2.17 self.current_subprograms.append(subprogram)
2.18 within_class = self.within_class
2.19 @@ -952,9 +954,15 @@
2.20
2.21 # Make a subprogram for the function and record it outside the main
2.22 # tree.
2.23 - # NOTE: Making the actual subprogram defining, too.
2.24 +
2.25 + subprogram = Subprogram(lambda_, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_)
2.26 +
2.27 + # Make nice annotations for the viewer.
2.28
2.29 - subprogram = Subprogram(lambda_, 1, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_)
2.30 + function._subprogram = subprogram
2.31 +
2.32 + # Process the lambda contents.
2.33 +
2.34 self.current_subprograms.append(subprogram)
2.35 subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))]
2.36 self.current_subprograms.pop()
3.1 --- a/viewer.py Thu Feb 15 20:32:37 2007 +0100
3.2 +++ b/viewer.py Fri Feb 16 00:13:09 2007 +0100
3.3 @@ -166,7 +166,7 @@
3.4 self.stream.write("%s\n" % node.op)
3.5 self._popup_start()
3.6 self.stream.write("<div class='invocations'>\n")
3.7 - self._invocations_list(node._op_call)
3.8 + self._invocations_list(node._op_call.active())
3.9 self.stream.write("</div>\n")
3.10 self._popup_end()
3.11 self.stream.write("</span>\n")
3.12 @@ -180,7 +180,7 @@
3.13
3.14 def visitClass(self, node):
3.15 definition = node._node
3.16 - definitions = getattr(node, "_nodes", [definition])
3.17 + definitions = definition.active()
3.18 structure = definition.expr.ref
3.19 self.stream.write("<div class='class' id='%s'>\n" % self._url(structure.full_name()))
3.20 self.stream.write("<div>\n")
3.21 @@ -235,14 +235,14 @@
3.22 self.stream.write("<span class='iterator'>\n")
3.23 self._keyword("for")
3.24 self._popup_start()
3.25 - self._invocations(node._next_call)
3.26 + self._invocations(node._next_call.active())
3.27 self._popup_end()
3.28 self.stream.write("</span>\n")
3.29 self.dispatch(node.assign)
3.30 self.stream.write("<span class='iterator'>\n")
3.31 self._keyword("in")
3.32 self._popup_start()
3.33 - self._invocations(node._iter_call)
3.34 + self._invocations(node._iter_call.active())
3.35 self._popup_end()
3.36 self.stream.write("</span>\n")
3.37 self.dispatch(node.list)
3.38 @@ -267,7 +267,7 @@
3.39 self.stream.write("<span class='name'>\n")
3.40 self.stream.write(node.modname)
3.41 self._popup_start()
3.42 - self._types([node._modname])
3.43 + self._types(node._modname.active())
3.44 self._popup_end()
3.45 self.stream.write("</span>\n")
3.46 self._keyword("import")
3.47 @@ -289,9 +289,9 @@
3.48
3.49 def visitFunction(self, node):
3.50 definition = node._node
3.51 - definitions = [n for n in getattr(node, "_nodes", [definition]) if not isinstance(n, Subprogram)]
3.52 - subprogram = definition.expr.ref
3.53 - subprograms = [n for n in getattr(node, "_nodes", [subprogram]) if isinstance(n, Subprogram)]
3.54 + definitions = [n for n in definition.active() if not isinstance(n, Subprogram)]
3.55 + subprogram = node._subprogram
3.56 + subprograms = subprogram.active()
3.57 self.stream.write("<div class='function' id='%s'>\n" % self._url(subprogram.full_name()))
3.58 self.stream.write("<div>\n")
3.59 self._keyword("def")
3.60 @@ -329,7 +329,7 @@
3.61 self.stream.write("<div class='if'>\n")
3.62 first = 1
3.63 conditional = node._node
3.64 - conditionals = getattr(node, "_nodes", [conditional])
3.65 + conditionals = conditional.active()
3.66 for compare, stmt in node.tests:
3.67 self.stream.write("<div>\n")
3.68 self.stream.write("<span class='conditional'>\n")
3.69 @@ -338,7 +338,7 @@
3.70 else:
3.71 self._keyword("elif")
3.72 self._popup_start()
3.73 - self._invocations(conditional.test)
3.74 + self._invocations([c.test for c in conditionals])
3.75 self._popup_end()
3.76 self.stream.write("</span>\n")
3.77 self.dispatch(compare)
3.78 @@ -425,7 +425,7 @@
3.79
3.80 def visitReturn(self, node):
3.81 value = node._node
3.82 - values = getattr(node, "_nodes", [value])
3.83 + values = value.active()
3.84 self.stream.write("<div class='return'>\n")
3.85 self.stream.write("<span class='returns'>\n")
3.86 self._keyword("return")
3.87 @@ -497,7 +497,7 @@
3.88 self.stream.write("<span class='conditional'>\n")
3.89 self._keyword("while")
3.90 self._popup_start()
3.91 - self._invocations(node._test_call)
3.92 + self._invocations(node._test_call.active())
3.93 self._popup_end()
3.94 self.stream.write("</span>\n")
3.95 self.dispatch(node.test)
3.96 @@ -525,8 +525,8 @@
3.97 self.stream.write(symbol)
3.98 self._popup_start()
3.99 self.stream.write("<div class='invocations'>\n")
3.100 - self._invocations_list(node._left_call)
3.101 - self._invocations_list(node._right_call)
3.102 + self._invocations_list(node._left_call.active())
3.103 + self._invocations_list(node._right_call.active())
3.104 self.stream.write("</div>\n")
3.105 self._popup_end()
3.106 self.stream.write("</span>\n")
3.107 @@ -539,7 +539,7 @@
3.108 self.stream.write(symbol)
3.109 self._popup_start()
3.110 self.stream.write("<div class='invocations'>\n")
3.111 - self._invocations_list(node._unary_call)
3.112 + self._invocations_list(node._unary_call.active())
3.113 self.stream.write("</div>\n")
3.114 self._popup_end()
3.115 self.stream.write("</span>\n")
3.116 @@ -563,7 +563,7 @@
3.117
3.118 def visitAssAttr(self, node):
3.119 target = node._node
3.120 - targets = getattr(node, "_nodes", [target])
3.121 + targets = target.active()
3.122 self.stream.write("<span class='assattr'>\n")
3.123 self.dispatch(node.expr)
3.124 self.stream.write("<span class='attr'>\n")
3.125 @@ -584,7 +584,7 @@
3.126
3.127 def visitAssName(self, node):
3.128 target = node._node
3.129 - targets = getattr(node, "_nodes", [target])
3.130 + targets = target.active()
3.131 self._name_start(target.name)
3.132 self._popup_start()
3.133 self._scopes(targets)
3.134 @@ -601,13 +601,13 @@
3.135
3.136 def visitCallFunc(self, node):
3.137 target = node._node
3.138 - targets = getattr(node, "_nodes", [target])
3.139 + targets = target.active()
3.140 self.stream.write("<span class='callfunc'>\n")
3.141 self.dispatch(node.node)
3.142 self.stream.write("<span class='call'>\n")
3.143 self.stream.write("(")
3.144 self._popup_start()
3.145 - self._invocations(target)
3.146 + self._invocations(targets)
3.147 self._popup_end()
3.148 self.stream.write("</span>\n")
3.149 first = 1
3.150 @@ -660,7 +660,7 @@
3.151
3.152 def visitGetattr(self, node):
3.153 target = node._node
3.154 - targets = getattr(node, "_nodes", [target])
3.155 + targets = target.active()
3.156 self.stream.write("<span class='getattr'>\n")
3.157 self.dispatch(node.expr)
3.158 self.stream.write("<span class='attr'>\n")
3.159 @@ -681,9 +681,9 @@
3.160
3.161 def visitLambda(self, node):
3.162 definition = node._node
3.163 - definitions = [n for n in getattr(node, "_nodes", [definition]) if not isinstance(n, Subprogram)]
3.164 - subprogram = definition.expr.ref
3.165 - subprograms = [n for n in getattr(node, "_nodes", [subprogram]) if isinstance(n, Subprogram)]
3.166 + definitions = [n for n in definition.active() if not isinstance(n, Subprogram)]
3.167 + subprogram = node._subprogram
3.168 + subprograms = subprogram.active()
3.169 self.stream.write("<span class='lambda'>\n")
3.170 self._keyword("lambda")
3.171 self._parameters(subprogram, subprograms)
3.172 @@ -700,7 +700,7 @@
3.173
3.174 def visitName(self, node):
3.175 target = node._node
3.176 - targets = getattr(node, "_nodes", [target])
3.177 + targets = target.active()
3.178 self._name_start(target.name)
3.179 self._popup_start()
3.180 self._scopes(targets)
3.181 @@ -908,8 +908,8 @@
3.182 def _op(self, node):
3.183 self.stream.write("<div class='invocations'>\n")
3.184 if hasattr(node, "_left_call") and hasattr(node, "_right_call"):
3.185 - self._invocations_list(node._left_call)
3.186 - self._invocations_list(node._right_call)
3.187 + self._invocations_list(node._left_call.active())
3.188 + self._invocations_list(node._right_call.active())
3.189 else:
3.190 _node = node._node
3.191 if isinstance(_node, Not):
3.192 @@ -917,24 +917,27 @@
3.193 self._invocations_list(_node)
3.194 self.stream.write("</div>\n")
3.195
3.196 - def _invocations(self, node):
3.197 + def _invocations(self, nodes):
3.198 self.stream.write("<div class='invocations'>\n")
3.199 - self._invocations_list(node)
3.200 + self._invocations_list(nodes)
3.201 self.stream.write("</div>\n")
3.202
3.203 - def _invocations_list(self, node):
3.204 - if hasattr(node, "invocations"):
3.205 - for invocation in node.invocations:
3.206 - fn = getattr(invocation, "copy_of", invocation).full_name()
3.207 - module = invocation.module.name
3.208 - name = invocation.name
3.209 - structures = [x.name for x in invocation.structures]
3.210 - qualified_name = ".".join([module] + structures + [name])
3.211 - self.stream.write("<div class='invocation'>")
3.212 - self.stream.write("<a href='%s.html#%s'>" % (self._url(module), self._url(fn)))
3.213 - self.stream.write(self._text(qualified_name))
3.214 - self.stream.write("</a>")
3.215 - self.stream.write("</div>\n")
3.216 + def _invocations_list(self, nodes):
3.217 + invocations = []
3.218 + for node in nodes:
3.219 + if hasattr(node, "invocations"):
3.220 + invocations += node.invocations
3.221 + for invocation in unique(invocations):
3.222 + fn = getattr(invocation, "copy_of", invocation).full_name()
3.223 + module = invocation.module.name
3.224 + name = invocation.name
3.225 + structures = [x.name for x in invocation.structures]
3.226 + qualified_name = ".".join([module] + structures + [name])
3.227 + self.stream.write("<div class='invocation'>")
3.228 + self.stream.write("<a href='%s.html#%s'>" % (self._url(module), self._url(fn)))
3.229 + self.stream.write(self._text(qualified_name))
3.230 + self.stream.write("</a>")
3.231 + self.stream.write("</div>\n")
3.232
3.233 def _types(self, nodes):
3.234 self.stream.write("<div class='types'>\n")
3.235 @@ -972,6 +975,7 @@
3.236 self.stream.write("</div>\n")
3.237
3.238 def _types_list(self, types):
3.239 + types = unique(types)
3.240 for type in types:
3.241 fn = type.type.full_name()
3.242 self.stream.write("<div class='type'>")
3.243 @@ -1050,9 +1054,14 @@
3.244 def flatten(lists):
3.245 result = []
3.246 for l in lists:
3.247 - for attr in l:
3.248 - if attr not in result:
3.249 - result.append(attr)
3.250 + result += unique(l)
3.251 + return result
3.252 +
3.253 +def unique(l):
3.254 + result = []
3.255 + for i in l:
3.256 + if i not in result:
3.257 + result.append(i)
3.258 return result
3.259
3.260 # Convenience functions.