simplify

Changeset

2:85ccbdf10533
2006-07-09 Paul Boddie raw files shortlog changelog graph Added AugAssign, introduced index attributes to LoadTemp/StoreTemp/ReleaseTemp.
simplified.py (file) simplify.py (file) tests/augassign.py (file) tests/logical.py (file)
     1.1 --- a/simplified.py	Sat Jul 08 22:59:43 2006 +0200
     1.2 +++ b/simplified.py	Sun Jul 09 13:45:27 2006 +0200
     1.3 @@ -42,6 +42,8 @@
     1.4      def __repr__(self):
     1.5          if hasattr(self, "name"):
     1.6              return "%s '%s' (at %x)" % (self.__class__, self.name, id(self))
     1.7 +        if hasattr(self, "index"):
     1.8 +            return "%s (%d) (at %x)" % (self.__class__, self.index, id(self))
     1.9          elif hasattr(self, "value"):
    1.10              return "%s %s (at %x)" % (self.__class__, repr(self.value), id(self))
    1.11          elif hasattr(self, "ref"):
     2.1 --- a/simplify.py	Sat Jul 08 22:59:43 2006 +0200
     2.2 +++ b/simplify.py	Sun Jul 09 13:45:27 2006 +0200
     2.3 @@ -30,16 +30,16 @@
     2.4      """
     2.5      A simplifying visitor for AST nodes.
     2.6  
     2.7 -    Covered: AssAttr, AssList, AssName, AssTuple, Assign, Break, CallFunc,
     2.8 -             Class, Const, Continue, Dict, Discard, For, Function, Getattr,
     2.9 -             Global, If, Keyword, Lambda, List, Module, Name, Pass, Return,
    2.10 -             Stmt, TryExcept, TryFinally, Tuple, While.
    2.11 +    Covered: AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break,
    2.12 +             CallFunc, Class, Const, Continue, Dict, Discard, For, Function,
    2.13 +             Getattr, Global, If, Keyword, Lambda, List, Module, Name, Pass,
    2.14 +             Return, Stmt, TryExcept, TryFinally, Tuple, While.
    2.15  
    2.16 -    Missing: Add, And, Assert, AugAssign, Backquote, Bitand, Bitor, Bitxor,
    2.17 -             Compare, Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import,
    2.18 -             Invert, LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul,
    2.19 -             Not, Or, Power, Print, Printnl, Raise, RightShift, Slice,
    2.20 -             Sliceobj, Sub, Subscript, UnaryAdd, UnarySub, Yield.
    2.21 +    Missing: Add, And, Assert, Backquote, Bitand, Bitor, Bitxor, Compare,
    2.22 +             Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import, Invert,
    2.23 +             LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or,
    2.24 +             Power, Print, Printnl, Raise, RightShift, Slice, Sliceobj, Sub,
    2.25 +             Subscript, UnaryAdd, UnarySub, Yield.
    2.26      """
    2.27  
    2.28      def __init__(self):
    2.29 @@ -185,6 +185,40 @@
    2.30  
    2.31      # Assignments.
    2.32  
    2.33 +    augassign_methods = {
    2.34 +        "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__"
    2.35 +        }
    2.36 +
    2.37 +    def visitAugAssign(self, augassign):
    2.38 +        result = Assign(augassign)
    2.39 +        expr = self.dispatch(augassign.expr)
    2.40 +
    2.41 +        # Simple augmented assignment: name += expr
    2.42 +
    2.43 +        if isinstance(augassign.node, compiler.ast.Name):
    2.44 +            node = self.dispatch(augassign.node)
    2.45 +            get_incremented = StoreTemp(
    2.46 +                expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr])
    2.47 +                )
    2.48 +            store = StoreName(expr=LoadTemp(), name=augassign.node.name)
    2.49 +            result.code = [get_incremented, store, ReleaseTemp()]
    2.50 +
    2.51 +        # Complicated augmented assignment: expr.attr += expr
    2.52 +
    2.53 +        elif isinstance(augassign.node, compiler.ast.Getattr):
    2.54 +            store_expr = StoreTemp(index=1, expr=self.dispatch(augassign.node.expr))
    2.55 +            node_attr = LoadAttr(expr=LoadTemp(index=1), name=augassign.node.attrname)
    2.56 +            get_incremented = StoreTemp(
    2.57 +                expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr])
    2.58 +                )
    2.59 +            store = StoreAttr(expr=LoadTemp(), lvalue=LoadTemp(index=1), name=augassign.node.attrname)
    2.60 +            result.code = [store_expr, get_incremented, store, ReleaseTemp(index=1), ReleaseTemp()]
    2.61 +
    2.62 +        else:
    2.63 +            raise NotImplementedError, augassign.node.__class__
    2.64 +
    2.65 +        return result
    2.66 +
    2.67      def visitAssign(self, assign):
    2.68          result = Assign(assign)
    2.69          store = StoreTemp(expr=self.dispatch(assign.expr))
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/augassign.py	Sun Jul 09 13:45:27 2006 +0200
     3.3 @@ -0,0 +1,3 @@
     3.4 +a += b
     3.5 +a.b += c
     3.6 +a.b().c += d
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tests/logical.py	Sun Jul 09 13:45:27 2006 +0200
     4.3 @@ -0,0 +1,1 @@
     4.4 +a = x and y or b and not c