# HG changeset patch # User paulb@localhost.localdomain # Date 1171486666 -3600 # Node ID 68958ffc9bbac071e39e5c6fc4b6836dbaf23230 # Parent 98912a726919fc774e511f64c6e75a8679c86063 Changed comparison operations to use the same infrastructure as normal binary operations, modifying that infrastructure slightly. Introduced a special AST-style node for improved viewer referencing of the operations. diff -r 98912a726919 -r 68958ffc9bba simplified.py --- a/simplified.py Wed Feb 14 18:33:08 2007 +0100 +++ b/simplified.py Wed Feb 14 21:57:46 2007 +0100 @@ -635,6 +635,14 @@ def __init__(self, attribute): self.types = [attribute] +class Op: + + "A replacement AST node representing an operation in a Compare construct." + + def __init__(self, name, expr): + self.name = name + self.expr = expr + # Configuration setting. Class = SingleInstanceClass diff -r 98912a726919 -r 68958ffc9bba simplify.py --- a/simplify.py Wed Feb 14 18:33:08 2007 +0100 +++ b/simplify.py Wed Feb 14 21:57:46 2007 +0100 @@ -491,8 +491,14 @@ return result comparison_methods = { - "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__", - ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None + "==" : ("__eq__", "__ne__"), + "!=" : ("__ne__", "__eq__"), + "<" : ("__lt__", "__gt__"), + "<=" : ("__le__", "__ge__"), + ">=" : ("__ge__", "__le__"), + ">" : ("__gt__", "__lt__"), + "is" : None, + "is not" : None } def visitCompare(self, compare): @@ -529,24 +535,22 @@ for op in compare.ops: op_name, node = op + + # Make a new AST-style node to wrap the operation program nodes. + + new_op = Op(op_name, node) expr = self.dispatch(node) # Identify the operation and produce the appropriate method call. - method_name = self.comparison_methods[op_name] - if method_name: - invocation = InvokeFunction( - compare, - expr=LoadAttr( - expr=previous, - name=method_name), - args=[expr], - star=None, - dstar=None) + method_names = self.comparison_methods[op_name] + if method_names: + first_name, alternative_name = method_names + invocation = self._visitBinaryOp(new_op, previous, expr, first_name, alternative_name) elif op_name == "is": invocation = InvokeFunction( - compare, + new_op, 1, expr=LoadName(name="__is__"), args=[previous, expr], star=None, @@ -554,8 +558,9 @@ elif op_name == "is not": invocation = Not( + new_op, 1, expr=InvokeFunction( - compare, + new_op, expr=LoadName(name="__is__"), args=[previous, expr], star=None, @@ -565,7 +570,7 @@ raise NotImplementedError, op_name nodes.append(StoreTemp(expr=invocation)) - compare._ops.append(invocation) + compare._ops.append(new_op) # Return from the subprogram where the test is not satisfied. @@ -1423,6 +1428,9 @@ # Convenience methods. def _visitBinary(self, binary, left_name, right_name): + return self._visitBinaryOp(binary, self.dispatch(binary.left), self.dispatch(binary.right), left_name, right_name) + + def _visitBinaryOp(self, binary, left, right, left_name, right_name): """ Emulate the current mechanisms by producing nodes as follows: @@ -1444,8 +1452,8 @@ ReturnFromBlock( expr=InvokeFunction( binary, - expr=LoadAttr(expr=self.dispatch(binary.left), name=left_name), - args=[self.dispatch(binary.right)], + expr=LoadAttr(expr=left, name=left_name), + args=[right], star=None, dstar=None) ) @@ -1459,8 +1467,8 @@ ReturnFromBlock( expr=InvokeFunction( binary, - expr=LoadAttr(expr=self.dispatch(binary.right), name=right_name), - args=[self.dispatch(binary.left)], + expr=LoadAttr(expr=right, name=right_name), + args=[left], star=None, dstar=None) ) diff -r 98912a726919 -r 68958ffc9bba viewer.py --- a/viewer.py Wed Feb 14 18:33:08 2007 +0100 +++ b/viewer.py Wed Feb 14 21:57:46 2007 +0100 @@ -632,14 +632,14 @@ def visitCompare(self, node): self.stream.write("\n") self.dispatch(node.expr) - for (op_name, expr), _op in map(None, node.ops, node._ops): + for op in node._ops: self.stream.write("\n") - self.stream.write(op_name) + self.stream.write(op.name) self._popup_start() - self._op(op_name, _op) + self._op(op) self._popup_end() self.stream.write("\n") - self.dispatch(expr) + self.dispatch(op.expr) self.stream.write("\n") def visitConst(self, node): @@ -905,12 +905,17 @@ def _popup_end(self): self.stream.write("\n") - def _op(self, op_name, op): - if op is not None: - if isinstance(op, Not): - self._invocations(op.expr) - else: - self._invocations(op) + 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) + else: + _node = node._node + if isinstance(_node, Not): + _node = _node.expr + self._invocations_list(_node) + self.stream.write("
\n") def _invocations(self, node): self.stream.write("
\n")