fix: panic when deleting a variable

after exiting the scope
This commit is contained in:
Artemy 2022-08-09 10:40:38 +03:00
parent e7b4a02ab7
commit a3cb87e8b4

View file

@ -129,7 +129,7 @@ impl Interpreter {
}, },
"delete" => match value { "delete" => match value {
Value::String(value) => { Value::String(value) => {
self.delete(value); self.delete(value, true);
} }
_ => { _ => {
self.error("Unsupported data type for the `delete` argument, must be a string"); self.error("Unsupported data type for the `delete` argument, must be a string");
@ -321,7 +321,7 @@ impl Interpreter {
fn delete_last_scope(&mut self) { fn delete_last_scope(&mut self) {
let vars = self.scopes[self.scope].clone(); let vars = self.scopes[self.scope].clone();
for name in vars { for name in vars {
self.delete(&name); self.delete(&name, false);
} }
self.scopes.remove(self.scope); self.scopes.remove(self.scope);
} }
@ -359,15 +359,17 @@ impl Interpreter {
} }
} }
fn delete(&mut self, var_name: &String) { fn delete(&mut self, var_name: &String, panic: bool) {
if self.var_exists(var_name) { if self.var_exists(var_name) {
self.vars.remove(var_name); self.vars.remove(var_name);
} else { } else {
self.error(&format!( if panic {
"The variable {} does not exist and cannot be deleted", self.error(&format!(
var_name "The variable {} does not exist and cannot be deleted",
)); var_name
panic!(); ));
panic!();
}
} }
} }