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,10 +359,11 @@ 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 {
if panic {
self.error(&format!( self.error(&format!(
"The variable {} does not exist and cannot be deleted", "The variable {} does not exist and cannot be deleted",
var_name var_name
@ -370,6 +371,7 @@ impl Interpreter {
panic!(); panic!();
} }
} }
}
fn get_var(&mut self, var_name: &String) -> Value { fn get_var(&mut self, var_name: &String) -> Value {
let var = self.vars.get(var_name); let var = self.vars.get(var_name);