From a3cb87e8b45e0fe99a38bae11ee298ad75b6b893 Mon Sep 17 00:00:00 2001 From: Artemy Date: Tue, 9 Aug 2022 10:40:38 +0300 Subject: [PATCH] fix: panic when deleting a variable after exiting the scope --- src/interpreter.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index 7d76b0b..e7217c5 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -129,7 +129,7 @@ impl Interpreter { }, "delete" => match value { Value::String(value) => { - self.delete(value); + self.delete(value, true); } _ => { 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) { let vars = self.scopes[self.scope].clone(); for name in vars { - self.delete(&name); + self.delete(&name, false); } 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) { self.vars.remove(var_name); } else { - self.error(&format!( - "The variable {} does not exist and cannot be deleted", - var_name - )); - panic!(); + if panic { + self.error(&format!( + "The variable {} does not exist and cannot be deleted", + var_name + )); + panic!(); + } } }