simplify

Changeset

4:922988fe3632
2006-07-09 Paul Boddie raw files shortlog changelog graph Added UnaryAdd, UnarySub, Invert.
simplify.py (file) tests/unary.py (file)
     1.1 --- a/simplify.py	Sun Jul 09 14:15:59 2006 +0200
     1.2 +++ b/simplify.py	Sun Jul 09 14:25:16 2006 +0200
     1.3 @@ -32,15 +32,15 @@
     1.4  
     1.5      Covered: AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break,
     1.6               CallFunc, Class, Const, Continue, Dict, Discard, For, From,
     1.7 -             Function, Getattr, Global, If, Import, Keyword, Lambda, List,
     1.8 -             Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, Tuple,
     1.9 -             While.
    1.10 +             Function, Getattr, Global, If, Import, Invert, Keyword, Lambda,
    1.11 +             List, Module, Name, Pass, Return, Stmt, TryExcept, TryFinally,
    1.12 +             Tuple, While, UnaryAdd, UnarySub.
    1.13  
    1.14      Missing: Add, And, Assert, Backquote, Bitand, Bitor, Bitxor, Compare,
    1.15 -             Decorators, Div, Ellipsis, Exec, FloorDiv, Invert, LeftShift,
    1.16 +             Decorators, Div, Ellipsis, Exec, FloorDiv, LeftShift,
    1.17               ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power, Print,
    1.18               Printnl, Raise, RightShift, Slice, Sliceobj, Sub, Subscript,
    1.19 -             UnaryAdd, UnarySub, Yield.
    1.20 +             Yield.
    1.21      """
    1.22  
    1.23      def __init__(self):
    1.24 @@ -203,6 +203,17 @@
    1.25              params=[], star=None, dstar=None))
    1.26          return result
    1.27  
    1.28 +    # Operators.
    1.29 +
    1.30 +    def visitUnaryAdd(self, unaryadd):
    1.31 +        return Invoke(expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[])
    1.32 +
    1.33 +    def visitUnarySub(self, unarysub):
    1.34 +        return Invoke(expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[])
    1.35 +
    1.36 +    def visitInvert(self, invert):
    1.37 +        return Invoke(expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[])
    1.38 +
    1.39      # Assignments.
    1.40  
    1.41      augassign_methods = {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tests/unary.py	Sun Jul 09 14:25:16 2006 +0200
     2.3 @@ -0,0 +1,3 @@
     2.4 +-a
     2.5 ++b
     2.6 +~c