1.1 --- a/simplify.py Wed Feb 14 18:33:08 2007 +0100
1.2 +++ b/simplify.py Wed Feb 14 21:57:46 2007 +0100
1.3 @@ -491,8 +491,14 @@
1.4 return result
1.5
1.6 comparison_methods = {
1.7 - "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__",
1.8 - ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None
1.9 + "==" : ("__eq__", "__ne__"),
1.10 + "!=" : ("__ne__", "__eq__"),
1.11 + "<" : ("__lt__", "__gt__"),
1.12 + "<=" : ("__le__", "__ge__"),
1.13 + ">=" : ("__ge__", "__le__"),
1.14 + ">" : ("__gt__", "__lt__"),
1.15 + "is" : None,
1.16 + "is not" : None
1.17 }
1.18
1.19 def visitCompare(self, compare):
1.20 @@ -529,24 +535,22 @@
1.21
1.22 for op in compare.ops:
1.23 op_name, node = op
1.24 +
1.25 + # Make a new AST-style node to wrap the operation program nodes.
1.26 +
1.27 + new_op = Op(op_name, node)
1.28 expr = self.dispatch(node)
1.29
1.30 # Identify the operation and produce the appropriate method call.
1.31
1.32 - method_name = self.comparison_methods[op_name]
1.33 - if method_name:
1.34 - invocation = InvokeFunction(
1.35 - compare,
1.36 - expr=LoadAttr(
1.37 - expr=previous,
1.38 - name=method_name),
1.39 - args=[expr],
1.40 - star=None,
1.41 - dstar=None)
1.42 + method_names = self.comparison_methods[op_name]
1.43 + if method_names:
1.44 + first_name, alternative_name = method_names
1.45 + invocation = self._visitBinaryOp(new_op, previous, expr, first_name, alternative_name)
1.46
1.47 elif op_name == "is":
1.48 invocation = InvokeFunction(
1.49 - compare,
1.50 + new_op, 1,
1.51 expr=LoadName(name="__is__"),
1.52 args=[previous, expr],
1.53 star=None,
1.54 @@ -554,8 +558,9 @@
1.55
1.56 elif op_name == "is not":
1.57 invocation = Not(
1.58 + new_op, 1,
1.59 expr=InvokeFunction(
1.60 - compare,
1.61 + new_op,
1.62 expr=LoadName(name="__is__"),
1.63 args=[previous, expr],
1.64 star=None,
1.65 @@ -565,7 +570,7 @@
1.66 raise NotImplementedError, op_name
1.67
1.68 nodes.append(StoreTemp(expr=invocation))
1.69 - compare._ops.append(invocation)
1.70 + compare._ops.append(new_op)
1.71
1.72 # Return from the subprogram where the test is not satisfied.
1.73
1.74 @@ -1423,6 +1428,9 @@
1.75 # Convenience methods.
1.76
1.77 def _visitBinary(self, binary, left_name, right_name):
1.78 + return self._visitBinaryOp(binary, self.dispatch(binary.left), self.dispatch(binary.right), left_name, right_name)
1.79 +
1.80 + def _visitBinaryOp(self, binary, left, right, left_name, right_name):
1.81
1.82 """
1.83 Emulate the current mechanisms by producing nodes as follows:
1.84 @@ -1444,8 +1452,8 @@
1.85 ReturnFromBlock(
1.86 expr=InvokeFunction(
1.87 binary,
1.88 - expr=LoadAttr(expr=self.dispatch(binary.left), name=left_name),
1.89 - args=[self.dispatch(binary.right)],
1.90 + expr=LoadAttr(expr=left, name=left_name),
1.91 + args=[right],
1.92 star=None,
1.93 dstar=None)
1.94 )
1.95 @@ -1459,8 +1467,8 @@
1.96 ReturnFromBlock(
1.97 expr=InvokeFunction(
1.98 binary,
1.99 - expr=LoadAttr(expr=self.dispatch(binary.right), name=right_name),
1.100 - args=[self.dispatch(binary.left)],
1.101 + expr=LoadAttr(expr=right, name=right_name),
1.102 + args=[left],
1.103 star=None,
1.104 dstar=None)
1.105 )