Skip to content

Commit 87f9a8e

Browse files
committed
Implement deleting tuples
1 parent 3afb5d3 commit 87f9a8e

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

tests/snippets/delete.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ class MyObject: pass
1010

1111
assert not hasattr(foo, 'bar')
1212

13+
x = 1
14+
y = 2
15+
del (x, y)

vm/src/compile.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -456,30 +456,7 @@ impl Compiler {
456456
}
457457
ast::Statement::Delete { targets } => {
458458
for target in targets {
459-
match target {
460-
ast::Expression::Identifier { name } => {
461-
self.emit(Instruction::DeleteName {
462-
name: name.to_string(),
463-
});
464-
}
465-
ast::Expression::Attribute { value, name } => {
466-
self.compile_expression(value)?;
467-
self.emit(Instruction::DeleteAttr {
468-
name: name.to_string(),
469-
});
470-
}
471-
ast::Expression::Subscript { a, b } => {
472-
self.compile_expression(a)?;
473-
self.compile_expression(b)?;
474-
self.emit(Instruction::DeleteSubscript);
475-
}
476-
_ => {
477-
return Err(CompileError {
478-
error: CompileErrorType::Delete(target.name()),
479-
location: self.current_source_location.clone(),
480-
});
481-
}
482-
}
459+
self.compile_delete(target)?;
483460
}
484461
}
485462
ast::Statement::Pass => {
@@ -489,6 +466,39 @@ impl Compiler {
489466
Ok(())
490467
}
491468

469+
fn compile_delete(&mut self, expression: &ast::Expression) -> Result<(), CompileError> {
470+
match expression {
471+
ast::Expression::Identifier { name } => {
472+
self.emit(Instruction::DeleteName {
473+
name: name.to_string(),
474+
});
475+
}
476+
ast::Expression::Attribute { value, name } => {
477+
self.compile_expression(value)?;
478+
self.emit(Instruction::DeleteAttr {
479+
name: name.to_string(),
480+
});
481+
}
482+
ast::Expression::Subscript { a, b } => {
483+
self.compile_expression(a)?;
484+
self.compile_expression(b)?;
485+
self.emit(Instruction::DeleteSubscript);
486+
}
487+
ast::Expression::Tuple { elements } => {
488+
for element in elements {
489+
self.compile_delete(element)?;
490+
}
491+
}
492+
_ => {
493+
return Err(CompileError {
494+
error: CompileErrorType::Delete(expression.name()),
495+
location: self.current_source_location.clone(),
496+
});
497+
}
498+
}
499+
Ok(())
500+
}
501+
492502
fn enter_function(
493503
&mut self,
494504
name: &str,

0 commit comments

Comments
 (0)