def SLICE_0(self, instr): 'obj[:]' value = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=None, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Load(), **kw) self.ast_stack.append(subscr)
def DELETE_SLICE_0(self, instr): 'obj[:] = expr' value = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=None, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Del(), **kw) delete = _ast.Delete(targets=[subscr], **kw) self.ast_stack.append(delete)
def STORE_SLICE_0(self, instr): 'obj[:] = expr' value = self.ast_stack.pop() expr = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=None, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw) assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.ast_stack.append(assign)
def DELETE_SLICE_1(self, instr): 'obj[lower:] = expr' lower = self.pop_ast_item() value = self.pop_ast_item() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=lower, step=None, upper=None, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Del(), **kw) delete = _ast.Delete(targets=[subscr], **kw) self.push_ast_item(delete)
def SLICE_2(self, instr): 'obj[:stop]' upper = self.pop_ast_item() value = self.pop_ast_item() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=upper, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Load(), **kw) self.push_ast_item(subscr)
def make_store_delete_slice(i, bytecode, context=None): op = bytecode[i][2] is_delete = op in DELETE_SLICE_OPCODES if context is None: context = _ast.Store() if not is_delete else _ast.Del() lhs_expr = None if op in (STORE_SLICE_0, DELETE_SLICE_0): i, lhs_expr = Statement.make_expr(i - 1, bytecode, context=context) lhs_expr = _ast.Subscript(lhs_expr, _ast.Slice(None, None, None), _ast.Store()) elif op in (STORE_SLICE_1, STORE_SLICE_2, DELETE_SLICE_1, DELETE_SLICE_2): i, index_expr = Statement.make_expr(i - 1, bytecode) i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context) args = [None] * 3 index_index = 0 if op in (STORE_SLICE_1, DELETE_SLICE_1) else 1 args[index_index] = index_expr lhs_expr = _ast.Subscript(arr_expr, _ast.Slice(*args), _ast.Store()) else: i, end_index_expr = Statement.make_expr(i - 1, bytecode) i, start_index_expr = Statement.make_expr(i - 1, bytecode) i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context) lhs_expr = _ast.Subscript(arr_expr, _ast.Slice(start_index_expr, end_index_expr, None), _ast.Store()) if is_delete: return i, _ast.Delete([lhs_expr]) else: i, rhs_expr = Statement.make_expr(i - 1, bytecode) return i, _ast.Assign([lhs_expr], rhs_expr)
def visit_For( self, node ): self.generic_visit( node ) if not ( isinstance( node.iter, _ast.Call ) and isinstance( node.iter.func, _ast.Name ) and node.iter.func.id in ['range', 'xrange'] ): raise VerilogTranslationError( 'For loops are only translatable when using range or xrange!\n' 'Please use "for i in range(...)/xrange(...)".', node.lineno ) call = node.iter if len( call.args ) == 1: start = _ast.Num( n=0 ) stop = call.args[0] step = _ast.Num( n=1 ) elif len( call.args ) == 2: start = call.args[0] stop = call.args[1] step = _ast.Num( n=1 ) # TODO: should be an expression elif len( call.args ) == 3: start = call.args[0] stop = call.args[1] step = call.args[2] else: raise VerilogTranslationError( 'An invalid number of arguments provided to (x)range function!\n', node.lineno ) # Must know if the step is negative or positive in order to set the # correct bound check. This is because of Python's range behavior. try: if hasattr( step, '_object' ): step_val = step._object elif hasattr( step, 'n' ): step_val = step.n assert step_val != 0 except (UnboundLocalError,AssertionError): raise VerilogTranslationError( 'An error occurred when translating a "for loop"!\n' 'The "step" parameter to range must be a constant integer value != 0!', node.lineno ) node.iter = _ast.Slice( lower=start, upper=stop, step=step ) node.iter.lt_gt = '<' if step_val > 0 else '>' return node
def STORE_SLICE_2(self, instr): 'obj[:upper] = expr' upper = self.pop_ast_item() value = self.pop_ast_item() expr = self.pop_ast_item() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=upper, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw) assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.push_ast_item(assign)
def BUILD_SLICE(self, instr): step = None upper = None lower = None if instr.oparg > 2: step = self.ast_stack.pop() if instr.oparg > 1: upper = self.ast_stack.pop() if instr.oparg > 0: lower = self.ast_stack.pop() upper = None if isNone(upper) else upper lower = None if isNone(lower) else lower kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=lower, step=step, upper=upper, **kw) self.ast_stack.append(slice)
def STORE_SLICE_3(self, instr): 'obj[lower:upper] = expr' upper = self.ast_stack.pop() lower = self.ast_stack.pop() value = self.ast_stack.pop() expr = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=lower, step=None, upper=upper, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw) if isinstance(expr, _ast.AugAssign): assign = expr result = cmp_ast(expr.target, subscr) assert result else: assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.ast_stack.append(assign)
def make_slice(i, bytecode): i, step_expr = Statement.make_expr(i - 1, bytecode) i, upper_expr = Statement.make_expr(i - 1, bytecode) i, lower_expr = Statement.make_expr(i - 1, bytecode) return i, _ast.Slice(lower_expr, upper_expr, step_expr)
def Slice(lower=None, upper=None, step=None): return _ast.Slice(lower=lower, upper=upper, step=step)