1.1 --- a/simplify.py Sat Jul 15 15:29:26 2006 +0200
1.2 +++ b/simplify.py Sat Jul 15 19:23:25 2006 +0200
1.3 @@ -31,15 +31,16 @@
1.4 A simplifying visitor for AST nodes.
1.5
1.6 Covered: And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break,
1.7 - CallFunc, Class, Const, Continue, Dict, Discard, For, From,
1.8 - Function, Getattr, Global, If, Import, Invert, Keyword, Lambda,
1.9 - List, Module, Name, Not, Or, Pass, Raise, Return, Stmt, TryExcept,
1.10 - TryFinally, Tuple, While, UnaryAdd, UnarySub.
1.11 + CallFunc, Class, Compare, Const, Continue, Dict, Discard, For,
1.12 + From, Function, Getattr, Global, If, Import, Invert, Keyword,
1.13 + Lambda, List, Module, Name, Not, Or, Pass, Raise, Return, Slice,
1.14 + Stmt, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd,
1.15 + UnarySub.
1.16
1.17 - Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Compare, Decorators,
1.18 - Div, Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor,
1.19 - ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift,
1.20 - Slice, Sliceobj, Sub, Subscript, Yield.
1.21 + Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Div,
1.22 + Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor,
1.23 + ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift, Sliceobj,
1.24 + Sub, Yield.
1.25 """
1.26
1.27 def __init__(self):
1.28 @@ -191,7 +192,55 @@
1.29 result.args = args
1.30 return result
1.31
1.32 - # Logical operators.
1.33 + # Logical and comparison operations.
1.34 +
1.35 + comparison_methods = {
1.36 + "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__",
1.37 + ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None
1.38 + }
1.39 +
1.40 + def visitCompare(self, compare):
1.41 +
1.42 + # Make a subprogram for the expression and record it outside the main tree.
1.43 +
1.44 + subprogram = Subprogram(name=hex(id(compare)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None)
1.45 + self.current_subprograms.append(subprogram)
1.46 +
1.47 + # In the subprogram, make instructions which invoke a method on the
1.48 + # first operand of each operand pair and, if appropriate, return with
1.49 + # the value from that method.
1.50 +
1.51 + nodes = []
1.52 + last = compare.ops[-1]
1.53 + previous = self.dispatch(compare.expr)
1.54 + for op in compare.ops:
1.55 + op_name, node = op
1.56 + expr = self.dispatch(node)
1.57 + method_name = self.comparison_methods[op_name]
1.58 + if method_name:
1.59 + invocation = Invoke(expr=LoadAttr(expr=previous, name=method_name), params=[expr], star=None, dstar=None)
1.60 + elif op_name == "is":
1.61 + invocation = Invoke(expr=LoadName(name="__is__"), params=[previous, expr], star=None, dstar=None)
1.62 + elif op_name == "is not":
1.63 + invocation = Not(expr=Invoke(expr=LoadName(name="__is__"), params=[previous, expr], star=None, dstar=None))
1.64 + else:
1.65 + raise NotImplementedError, op_name
1.66 + nodes.append(StoreTemp(expr=invocation))
1.67 + if op is not last:
1.68 + nodes.append(Conditional(test=Not(expr=LoadTemp()), body=[Return(expr=LoadTemp())]))
1.69 + nodes.append(ReleaseTemp())
1.70 + else:
1.71 + nodes.append(Return(expr=LoadTemp()))
1.72 + subprogram.code = nodes
1.73 +
1.74 + self.current_subprograms.pop()
1.75 + self.subprograms.append(subprogram)
1.76 +
1.77 + # Make an invocation of the subprogram.
1.78 +
1.79 + result = Invoke(compare, same_frame=1, star=None, dstar=None, args=[])
1.80 + result.expr = LoadRef(ref=subprogram)
1.81 + return result
1.82
1.83 def visitAnd(self, and_):
1.84
1.85 @@ -210,9 +259,8 @@
1.86 expr = self.dispatch(node)
1.87 if node is not last:
1.88 nodes.append(StoreTemp(expr=expr))
1.89 - invocation = Not(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"),
1.90 - params=[], star=None, dstar=None))
1.91 - nodes.append(Conditional(test=invocation, body=[Return(expr=LoadTemp())]))
1.92 + invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), params=[], star=None, dstar=None)
1.93 + nodes.append(Conditional(test=Not(expr=invocation), body=[Return(expr=LoadTemp())]))
1.94 nodes.append(ReleaseTemp())
1.95 else:
1.96 nodes.append(Return(expr=expr))