# HG changeset patch # User paulb@localhost.localdomain # Date 1171581189 -3600 # Node ID c7eb79b764e1a97cfb35ad1b4222c07a4aa5e6f4 # Parent 0e071a78202267129a08550fc33154125515eb4c Removed the _nodes annotation. Added _subprogram annotations. Attempted to use active or copied nodes for all special viewer annotations in the viewer. diff -r 0e071a782022 -r c7eb79b764e1 simplified.py --- a/simplified.py Thu Feb 15 20:32:37 2007 +0100 +++ b/simplified.py Fri Feb 16 00:13:09 2007 +0100 @@ -165,6 +165,7 @@ self.original = original self.defining = defining + self.copies = [] if self.original is not None and defining: self.original._node = self @@ -282,6 +283,14 @@ self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream) self._pprint(indent, "", "--------", stream=stream) + # Node discovery functions. + + def active(self): + + "Return the active copies of this node or a list containing this node." + + return self.copies or [self] + # Node manipulation functions. def copy(self, new_name=None): @@ -307,12 +316,9 @@ node = self.__class__(**common) node.defining = self.defining - # Add links to copied nodes from original AST nodes. + # Add links to copies from originals. - if node.original is not None and node.defining: - if not hasattr(node.original, "_nodes"): - node.original._nodes = [] - node.original._nodes.append(node) + self.copies.append(node) # Copy attributes of different types. diff -r 0e071a782022 -r c7eb79b764e1 simplify.py --- a/simplify.py Thu Feb 15 20:32:37 2007 +0100 +++ b/simplify.py Fri Feb 16 00:13:09 2007 +0100 @@ -838,10 +838,12 @@ (dstar) """ - # NOTE: Making the actual subprogram defining, too. + subprogram = Subprogram(function, name=function.name, module=self.module, structures=self.current_structures[:], + internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) - subprogram = Subprogram(function, 1, name=function.name, module=self.module, structures=self.current_structures[:], - internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) + # Make nice annotations for the viewer. + + function._subprogram = subprogram self.current_subprograms.append(subprogram) within_class = self.within_class @@ -952,9 +954,15 @@ # Make a subprogram for the function and record it outside the main # tree. - # NOTE: Making the actual subprogram defining, too. + + subprogram = Subprogram(lambda_, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) + + # Make nice annotations for the viewer. - subprogram = Subprogram(lambda_, 1, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) + function._subprogram = subprogram + + # Process the lambda contents. + self.current_subprograms.append(subprogram) subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] self.current_subprograms.pop() diff -r 0e071a782022 -r c7eb79b764e1 viewer.py --- a/viewer.py Thu Feb 15 20:32:37 2007 +0100 +++ b/viewer.py Fri Feb 16 00:13:09 2007 +0100 @@ -166,7 +166,7 @@ self.stream.write("%s\n" % node.op) self._popup_start() self.stream.write("
\n") - self._invocations_list(node._op_call) + self._invocations_list(node._op_call.active()) self.stream.write("
\n") self._popup_end() self.stream.write("\n") @@ -180,7 +180,7 @@ def visitClass(self, node): definition = node._node - definitions = getattr(node, "_nodes", [definition]) + definitions = definition.active() structure = definition.expr.ref self.stream.write("
\n" % self._url(structure.full_name())) self.stream.write("
\n") @@ -235,14 +235,14 @@ self.stream.write("\n") self._keyword("for") self._popup_start() - self._invocations(node._next_call) + self._invocations(node._next_call.active()) self._popup_end() self.stream.write("\n") self.dispatch(node.assign) self.stream.write("\n") self._keyword("in") self._popup_start() - self._invocations(node._iter_call) + self._invocations(node._iter_call.active()) self._popup_end() self.stream.write("\n") self.dispatch(node.list) @@ -267,7 +267,7 @@ self.stream.write("\n") self.stream.write(node.modname) self._popup_start() - self._types([node._modname]) + self._types(node._modname.active()) self._popup_end() self.stream.write("\n") self._keyword("import") @@ -289,9 +289,9 @@ def visitFunction(self, node): definition = node._node - definitions = [n for n in getattr(node, "_nodes", [definition]) if not isinstance(n, Subprogram)] - subprogram = definition.expr.ref - subprograms = [n for n in getattr(node, "_nodes", [subprogram]) if isinstance(n, Subprogram)] + definitions = [n for n in definition.active() if not isinstance(n, Subprogram)] + subprogram = node._subprogram + subprograms = subprogram.active() self.stream.write("
\n" % self._url(subprogram.full_name())) self.stream.write("
\n") self._keyword("def") @@ -329,7 +329,7 @@ self.stream.write("
\n") first = 1 conditional = node._node - conditionals = getattr(node, "_nodes", [conditional]) + conditionals = conditional.active() for compare, stmt in node.tests: self.stream.write("
\n") self.stream.write("\n") @@ -338,7 +338,7 @@ else: self._keyword("elif") self._popup_start() - self._invocations(conditional.test) + self._invocations([c.test for c in conditionals]) self._popup_end() self.stream.write("\n") self.dispatch(compare) @@ -425,7 +425,7 @@ def visitReturn(self, node): value = node._node - values = getattr(node, "_nodes", [value]) + values = value.active() self.stream.write("
\n") self.stream.write("\n") self._keyword("return") @@ -497,7 +497,7 @@ self.stream.write("\n") self._keyword("while") self._popup_start() - self._invocations(node._test_call) + self._invocations(node._test_call.active()) self._popup_end() self.stream.write("\n") self.dispatch(node.test) @@ -525,8 +525,8 @@ self.stream.write(symbol) self._popup_start() self.stream.write("
\n") - self._invocations_list(node._left_call) - self._invocations_list(node._right_call) + self._invocations_list(node._left_call.active()) + self._invocations_list(node._right_call.active()) self.stream.write("
\n") self._popup_end() self.stream.write("
\n") @@ -539,7 +539,7 @@ self.stream.write(symbol) self._popup_start() self.stream.write("
\n") - self._invocations_list(node._unary_call) + self._invocations_list(node._unary_call.active()) self.stream.write("
\n") self._popup_end() self.stream.write("\n") @@ -563,7 +563,7 @@ def visitAssAttr(self, node): target = node._node - targets = getattr(node, "_nodes", [target]) + targets = target.active() self.stream.write("\n") self.dispatch(node.expr) self.stream.write("\n") @@ -584,7 +584,7 @@ def visitAssName(self, node): target = node._node - targets = getattr(node, "_nodes", [target]) + targets = target.active() self._name_start(target.name) self._popup_start() self._scopes(targets) @@ -601,13 +601,13 @@ def visitCallFunc(self, node): target = node._node - targets = getattr(node, "_nodes", [target]) + targets = target.active() self.stream.write("\n") self.dispatch(node.node) self.stream.write("\n") self.stream.write("(") self._popup_start() - self._invocations(target) + self._invocations(targets) self._popup_end() self.stream.write("\n") first = 1 @@ -660,7 +660,7 @@ def visitGetattr(self, node): target = node._node - targets = getattr(node, "_nodes", [target]) + targets = target.active() self.stream.write("\n") self.dispatch(node.expr) self.stream.write("\n") @@ -681,9 +681,9 @@ def visitLambda(self, node): definition = node._node - definitions = [n for n in getattr(node, "_nodes", [definition]) if not isinstance(n, Subprogram)] - subprogram = definition.expr.ref - subprograms = [n for n in getattr(node, "_nodes", [subprogram]) if isinstance(n, Subprogram)] + definitions = [n for n in definition.active() if not isinstance(n, Subprogram)] + subprogram = node._subprogram + subprograms = subprogram.active() self.stream.write("\n") self._keyword("lambda") self._parameters(subprogram, subprograms) @@ -700,7 +700,7 @@ def visitName(self, node): target = node._node - targets = getattr(node, "_nodes", [target]) + targets = target.active() self._name_start(target.name) self._popup_start() self._scopes(targets) @@ -908,8 +908,8 @@ def _op(self, node): self.stream.write("
\n") if hasattr(node, "_left_call") and hasattr(node, "_right_call"): - self._invocations_list(node._left_call) - self._invocations_list(node._right_call) + self._invocations_list(node._left_call.active()) + self._invocations_list(node._right_call.active()) else: _node = node._node if isinstance(_node, Not): @@ -917,24 +917,27 @@ self._invocations_list(_node) self.stream.write("
\n") - def _invocations(self, node): + def _invocations(self, nodes): self.stream.write("
\n") - self._invocations_list(node) + self._invocations_list(nodes) self.stream.write("
\n") - def _invocations_list(self, node): - if hasattr(node, "invocations"): - for invocation in node.invocations: - fn = getattr(invocation, "copy_of", invocation).full_name() - module = invocation.module.name - name = invocation.name - structures = [x.name for x in invocation.structures] - qualified_name = ".".join([module] + structures + [name]) - self.stream.write("\n") + def _invocations_list(self, nodes): + invocations = [] + for node in nodes: + if hasattr(node, "invocations"): + invocations += node.invocations + for invocation in unique(invocations): + fn = getattr(invocation, "copy_of", invocation).full_name() + module = invocation.module.name + name = invocation.name + structures = [x.name for x in invocation.structures] + qualified_name = ".".join([module] + structures + [name]) + self.stream.write("\n") def _types(self, nodes): self.stream.write("
\n") @@ -972,6 +975,7 @@ self.stream.write("
\n") def _types_list(self, types): + types = unique(types) for type in types: fn = type.type.full_name() self.stream.write("
") @@ -1050,9 +1054,14 @@ def flatten(lists): result = [] for l in lists: - for attr in l: - if attr not in result: - result.append(attr) + result += unique(l) + return result + +def unique(l): + result = [] + for i in l: + if i not in result: + result.append(i) return result # Convenience functions.