simplify

Changeset

10:e51ec15e7979
2006-07-15 paulb raw files shortlog changelog graph Added Subscript node support (including augmented subscript assignment) along with support for slice/subscript deletion flags and special methods.
simplify.py (file) tests/augassign.py (file) tests/subscript.py (file)
     1.1 --- a/simplify.py	Sat Jul 15 00:47:24 2006 +0200
     1.2 +++ b/simplify.py	Sat Jul 15 01:21:58 2006 +0200
     1.3 @@ -323,6 +323,23 @@
     1.4              result.code = [store_expr, store_lower, store_upper, get_incremented, store,
     1.5                  ReleaseTemp(index="expr"), ReleaseTemp(index="lower"), ReleaseTemp(index="upper"), ReleaseTemp()]
     1.6  
     1.7 +        # Complicated augassign using subscripts: lvalue[subs] += expr
     1.8 +
     1.9 +        elif isinstance(augassign.node, compiler.ast.Subscript):
    1.10 +
    1.11 +            # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr))
    1.12 +
    1.13 +            subscript = augassign.node
    1.14 +            store_expr = StoreTemp(index="expr", expr=self.dispatch(subscript.expr))
    1.15 +            subs = self._visitSubscriptSubs(subscript.subs)
    1.16 +            store_subs = StoreTemp(index="subs", expr=subs)
    1.17 +            node_subscript = self._visitSubscript(LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_APPLY")
    1.18 +            get_incremented = StoreTemp(
    1.19 +                expr=Invoke(expr=LoadAttr(expr=node_subscript, name=self.augassign_methods[augassign.op]), args=[expr])
    1.20 +                )
    1.21 +            store = self._visitSubscript(LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_ASSIGN", LoadTemp())
    1.22 +            result.code = [store_expr, store_subs, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp(index="subs"), ReleaseTemp()]
    1.23 +
    1.24          else:
    1.25              raise NotImplementedError, augassign.node.__class__
    1.26  
    1.27 @@ -372,6 +389,9 @@
    1.28          elif flags == "OP_APPLY":
    1.29              args = []
    1.30              result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__"))
    1.31 +        elif flags == "OP_DELETE":
    1.32 +            args = []
    1.33 +            result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__"))
    1.34          else:
    1.35              raise NotImplementedError, flags
    1.36  
    1.37 @@ -387,6 +407,37 @@
    1.38          value = self._visitAssNameOrAttr(slice, in_sequence)
    1.39          return self._visitSlice(self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), self.dispatch_or_none(slice.upper), slice.flags, value)
    1.40  
    1.41 +    def _visitSubscript(self, expr, subs, flags, value=None):
    1.42 +        if flags == "OP_ASSIGN":
    1.43 +            args = [value]
    1.44 +            result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__"))
    1.45 +        elif flags == "OP_APPLY":
    1.46 +            args = []
    1.47 +            result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__"))
    1.48 +        elif flags == "OP_DELETE":
    1.49 +            args = []
    1.50 +            result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__"))
    1.51 +        else:
    1.52 +            raise NotImplementedError, flags
    1.53 +
    1.54 +        # Add the dimensions.
    1.55 +
    1.56 +        args.insert(0, subs)
    1.57 +
    1.58 +        result.args = args
    1.59 +        return result
    1.60 +
    1.61 +    def _visitSubscriptSubs(self, subs):
    1.62 +        if len(subs) == 1:
    1.63 +            return self.dispatch(subs[0])
    1.64 +        else:
    1.65 +            return Invoke(expr=LoadName(name="Tuple"), args=self.dispatches(subs))
    1.66 +
    1.67 +    def visitSubscript(self, subscript, in_sequence=0):
    1.68 +        value = self._visitAssNameOrAttr(subscript, in_sequence)
    1.69 +        subs = self._visitSubscriptSubs(subscript.subs)
    1.70 +        return self._visitSubscript(self.dispatch(subscript.expr), subs, subscript.flags, value)
    1.71 +
    1.72      # Invocation and subprogram transformations.
    1.73  
    1.74      def _visitFunction(self, function, subprogram):
     2.1 --- a/tests/augassign.py	Sat Jul 15 00:47:24 2006 +0200
     2.2 +++ b/tests/augassign.py	Sat Jul 15 01:21:58 2006 +0200
     2.3 @@ -3,3 +3,5 @@
     2.4  a.b().c += d
     2.5  a[b:c] += d
     2.6  a.b[c:] += d
     2.7 +a[b] += c
     2.8 +a[b,c] += d
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/subscript.py	Sat Jul 15 01:21:58 2006 +0200
     3.3 @@ -0,0 +1,3 @@
     3.4 +a[x]
     3.5 +p[q] = s
     3.6 +p[q,r] = s