示例#1
0
 def _do_TupleExpression(self, node, context):
     if context == OP_APPLY:
         nodes = map(self.transform, node.Items)
         return ast.Tuple(nodes)
     else:
         transform = lambda node: self.transform(node, context)
         nodes = map(transform, node.Items)
         return ast.AssTuple(nodes)
示例#2
0
def Assign(left, right):
    names = []
    if isinstance(left, ast.Name):
        # Single assignment on left
        return ast.Assign([ast.AssName(left.name, 'OP_ASSIGN')], right)
    elif isinstance(left, ast.Tuple):
        # List of things - make sure they are Name nodes
        names = []
        for child in left.getChildren():
            if not isinstance(child, ast.Name):
                raise SyntaxError("that assignment not supported")
            names.append(child.name)
        ass_list = [ast.AssName(name, 'OP_ASSIGN') for name in names]
        return ast.Assign([ast.AssTuple(ass_list)], right)
    else:
        raise SyntaxError("Can't do that yet")
示例#3
0
def compilePlace(place, opname='OP_ASSIGN'):
    if isinstance(place, Symbol):
        return ast.AssName(str(place), opname)

    elif isinstance(place, rootops.PyOp):
        token = place.__syntax__.token

        if token == ".":
            # {{{ assign to field
            expr = topy(place[0])
            field = place[1]
            if isinstance(field, Symbol):
                return ast.AssAttr(expr, str(field), opname)
            else:
                raise CompileError("Cannot assign to %s" % place)
            # }}}

        elif token == "__continue__":
            # {{{ assign to slice or subscript
            last = place[1][-1]

            # expr = the continuationOp not including the last part
            if len(place[1]) == 1:
                expr = place[0]
            else:
                expr = basel.getContinuationOp()(place[0], place[1][:-1])
            
            kind = last[-1]
            if kind == 'subscript':
                # {{{ assign to subscript
                assertResult(expr, "subscript")

                keys = last[:-1]
                for key in keys:
                    assertResult(key, "subscript with")

                return ast.Subscript(topy(expr), opname, map(topy, keys))
                # }}}

            elif kind ==  "slice":
                # {{{ assign to slice
                assertResult(expr, "slice")

                start,end,step = last[:-1]
                assertResult(start, "slice from ")
                assertResult(end, "slice to")
                assertResult(step, "slice with step")

                return ast.Subscript(topy(expr), opname,
                                     [ast.Sliceobj(map(topy, (start,end,step)))])
                # }}}
                
            else:
                raise CompileError("Cannot asign to %s " % place)
            # }}}

        elif token == ",":
            return ast.AssTuple([compilePlace(p, opname) for p in list(place)])

        elif token == "[" and place[1] == 'list':
            return ast.AssList([compilePlace(p, opname) for p in place.elems])

        elif token == "(":
            return compilePlace(place[1], opname)

        else:
            raise CompileError("Cannot asign to %s " % place)

    else:
        raise CompileError("Cannot assign to %s" % place)
示例#4
0
def compilePlace(place, opname='OP_ASSIGN'):

    # {{{ def compileSubscriptPlace(objx, keyxs):
    def compileSubscriptPlace(objx, keyxs):
         assertResult(objx, "subscript")

         for key in keyxs:
             assertResult(key, "subscript with")

         return ast.Subscript(topy(objx), opname, map(topy, keyxs))
    # }}}

    # {{{ def compileSlicePlace(objx, fromx, tox, stepx):
    def compileSlicePlace(objx, fromx, tox, stepx):
         assertResult(objx, "slice")
         
         assertResult(fromx, "slice from ")
         assertResult(tox, "slice to")
         assertResult(stepx, "slice with step")
         
         return ast.Subscript(topy(objx), opname,
                              [ast.Sliceobj(map(topy, (fromx, tox, stepx)))])
    # }}}
    
    if isinstance(place, Symbol):
        if place.namespace != "":
            raise CompileError, "cannot assign to symbol with namespace: %s" % place
        return ast.AssName(place.name, opname)

    elif isDoc(place, rootops.sliceOp):
        return compileSlicePlace(place[0], place[1], place[2], place[3])

    elif isDoc(place, rootops.subscriptOp):
        return compileSubscriptPlace(place[0], place[1:])

    elif isPyOp(place):
        token = place.tag.name

        if token == ".":
            # {{{ assign to field
            expr = topy(place[0])
            field = place[1]
            if isinstance(field, Symbol) and field.namespace == "":
                return ast.AssAttr(expr, field.name, opname)
            else:
                raise CompileError("Cannot assign to %s" % place)
            # }}}

        elif token == "": # continuation op
            # {{{ assign to slice or subscript
            last = place[-1]

            # expr = the continuationOp not including the last part
            if len(place) == 2:
                expr = place[0]
            else:
                expr = Doc(Symbol(rootops.base_ns, ""),
                           place[:-1])
            
            kind = last[-1]
            if kind == 'subscript':
                return compileSubscriptPlace(expr, last[:-1])

            elif kind ==  "slice":
                start,end,step = last[:-1]
                return compileSlicePlace(expr, start, end, step)
                
            else:
                raise CompileError("Cannot asign to %s " % place)
            # }}}

        elif token == ",":
            return ast.AssTuple([compilePlace(p, opname) for p in list(place)])

        elif token == "[" and place[1] == 'list':
            return ast.AssList([compilePlace(p, opname) for p in place])

        elif token == "(":
            return compilePlace(place[1], opname)

        else:
            raise CompileError("Cannot asign to %s " % place)

    else:
        raise CompileError("Cannot assign to %s" % place)
示例#5
0
 def _do_DelStatement(self, node):
     if len(node.Expressions) == 1:
         return self.transform_delete(node.Expressions[0])
     nodes = map(self.transform_delete, node.Expressions)
     return ast.AssTuple(nodes)
示例#6
0
 def UNPACK_SEQUENCE(decompiler, count):
     ass_tuple = ast.AssTuple([])
     ass_tuple.count = count
     return ass_tuple