From db3b56d79876df9b3b214a8d50392464b8929db3 Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 22 Apr 2022 21:14:53 +0200 Subject: [PATCH] Double Melo causes variable deletion --- ablescript/src/interpret.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index d0885d0..f666a55 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -276,9 +276,16 @@ impl ExecEnv { Stmt::HopBack => { return Ok(HaltStatus::Hopback(stmt.span.clone())); } - Stmt::Melo(ident) => { - *self.get_var_mut(ident)? = Variable::Melo; - } + Stmt::Melo(ident) => match self.get_var_mut(ident)? { + var @ Variable::Ref(_) => *var = Variable::Melo, + Variable::Melo => { + for s in &mut self.stack { + if s.variables.remove(&ident.item).is_some() { + break; + } + } + } + }, Stmt::Rlyeh => { // Maybe print a creepy error message or something // here at some point. ~~Alex @@ -366,7 +373,8 @@ impl ExecEnv { .iter() .map(|arg| { if let Expr::Variable(name) = &arg.item { - self.get_var_rc_mut(&Spanned::new(name.to_owned(), arg.span.clone())).cloned() + self.get_var_rc_mut(&Spanned::new(name.to_owned(), arg.span.clone())) + .cloned() } else { self.eval_expr(arg).map(ValueRef::new) } @@ -536,8 +544,7 @@ impl ExecEnv { } } - /// Get a mutable reference to a variable. Throw an error if the - /// variable is inaccessible or banned. + /// Get a mutable reference to a variable. fn get_var_mut(&mut self, name: &Spanned) -> Result<&mut Variable, Error> { // This function has a lot of duplicated code with `get_var`, // which I feel like is a bad sign...