def compileSlice(doc): assertResult(doc[0], "slice") assertResult(doc[1], "slice from") assertResult(doc[2], "slice to") assertResult(doc[3], "slice with step") return ast.Subscript(topy(doc[0]), 'OP_APPLY', [ast.Sliceobj([topy(x) for x in doc[1:4]])])
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)))])
def _do_SliceExpression(self, node): def transform(node): if node is None: return ast.Const(None) return self.transform(node) start = transform(node.SliceStart) stop = transform(node.SliceStop) step = transform(node.SliceStep) return ast.Sliceobj([start, stop, step])
def continuationOp(self, expr, parts): "__continue__" assertResult(expr, "call or subscript") res = topy(expr) for part in parts: kind = part[-1] args = part[:-1] if kind == 'call': funcArgs = [] funcKws = {} for a in args: if isinstance(a, rootops.PyOp) and a.__syntax__.token == '=': if isinstance(a[0], Symbol): funcKws[str(a[0])] = a[1] else: raise CompileError("invalid keyword arg %r" % a[0]) else: funcArgs.append(a) star = part.get('star', None) dstar = part.get('dstar', None) res = compileFunctionCall(res, funcArgs, funcKws, star, dstar) elif kind == 'subscript': for key in args: assertResult(key, "subscript with") res = ast.Subscript(res, 'OP_APPLY', map(topy, args)) elif kind == 'slice': start, end, step = args assertResult(start, "slice from") assertResult(end, "slice to") assertResult(step, "slice with step") res = ast.Subscript(res, 'OP_APPLY', [ast.Sliceobj(map(topy, (start, end, step)))]) return res
def continuationOp(self, expr, *parts): "" # The language knows it as the blank operator assertResult(expr, "call or subscript") res = topy(expr) for part in parts: kind = part[-1] args = part[:-1] if kind == 'call': funcArgs = [] funcKws = {} for a in args: if isPyOp(a, "="): if isinstance(a[0], Symbol) and a[0].namespace == "": funcKws[a[0].name] = a[1] else: raise CompileError("invalid keyword arg %r" % a[0]) else: funcArgs.append(a) star = part.get('star') dstar = part.get('dstar') res = compileFunctionCall(res, funcArgs, funcKws, star, dstar) elif kind == 'subscript': for key in args: assertResult(key, "subscript with") res = ast.Subscript(res, 'OP_APPLY', map(topy, args)) elif kind == 'slice': start, end, step = args assertResult(start, "slice from") assertResult(end, "slice to") assertResult(step, "slice with step") res = ast.Subscript(res, 'OP_APPLY', [ast.Sliceobj(map(topy, (start, end, step)))]) return res
def visitSlice(self, node): n = ast.Subscript(node.expr, compiler.consts.OP_APPLY, [ast.Sliceobj(node.asList()[2:])]) self.visit(n)
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)
def testSliceobj(self): node = ast.Sliceobj([ast.Const(1), ast.Const(2), ast.Const(3)]) source = ModuleSourceCodeGenerator(node).getSourceCode() self.assertEqual(source, 'slice(1, 2, 3)')
def BUILD_SLICE(decompiler, size): return ast.Sliceobj(decompiler.pop_items(size))