MCPcopy Index your code
hub / github.com/RustPython/RustPython / compile_augassign

Method compile_augassign

crates/codegen/src/compile.rs:7238–7311  ·  view source on GitHub ↗
(
        &mut self,
        target: &ast::Expr,
        op: &ast::Operator,
        value: &ast::Expr,
    )

Source from the content-addressed store, hash-verified

7236 }
7237
7238 fn compile_augassign(
7239 &mut self,
7240 target: &ast::Expr,
7241 op: &ast::Operator,
7242 value: &ast::Expr,
7243 ) -> CompileResult<()> {
7244 enum AugAssignKind<'a> {
7245 Name { id: &'a str },
7246 Subscript,
7247 Attr { idx: bytecode::NameIdx },
7248 }
7249
7250 let kind = match &target {
7251 ast::Expr::Name(ast::ExprName { id, .. }) => {
7252 let id = id.as_str();
7253 self.compile_name(id, NameUsage::Load)?;
7254 AugAssignKind::Name { id }
7255 }
7256 ast::Expr::Subscript(ast::ExprSubscript {
7257 value,
7258 slice,
7259 ctx: _,
7260 ..
7261 }) => {
7262 // For augmented assignment, we need to load the value first
7263 // But we can't use compile_subscript directly because we need DUP_TOP2
7264 self.compile_expression(value)?;
7265 self.compile_expression(slice)?;
7266 emit!(self, Instruction::Copy { i: 2 });
7267 emit!(self, Instruction::Copy { i: 2 });
7268 emit!(
7269 self,
7270 Instruction::BinaryOp {
7271 op: BinaryOperator::Subscr
7272 }
7273 );
7274 AugAssignKind::Subscript
7275 }
7276 ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => {
7277 let attr = attr.as_str();
7278 self.compile_expression(value)?;
7279 emit!(self, Instruction::Copy { i: 1 });
7280 let idx = self.name(attr);
7281 self.emit_load_attr(idx);
7282 AugAssignKind::Attr { idx }
7283 }
7284 _ => {
7285 return Err(self.error(CodegenErrorType::Assign(target.python_name())));
7286 }
7287 };
7288
7289 self.compile_expression(value)?;
7290 self.compile_op(op, true);
7291
7292 match kind {
7293 AugAssignKind::Name { id } => {
7294 // stack: RESULT
7295 self.compile_name(id, NameUsage::Store)?;

Callers 1

compile_statementMethod · 0.80

Calls 9

compile_nameMethod · 0.80
compile_expressionMethod · 0.80
emit_load_attrMethod · 0.80
python_nameMethod · 0.80
compile_opMethod · 0.80
ErrClass · 0.50
as_strMethod · 0.45
nameMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected