diff --git a/lykiadb-server/src/engine/interpreter.rs b/lykiadb-server/src/engine/interpreter.rs index dadf2fa..a4dc46a 100644 --- a/lykiadb-server/src/engine/interpreter.rs +++ b/lykiadb-server/src/engine/interpreter.rs @@ -250,7 +250,7 @@ impl Interpreter { } fn look_up_variable(&mut self, name: &str, expr: &Expr) -> Result { - let distance = self.current_program.clone().unwrap().get_distance(expr); + let distance = self.current_program.as_ref().unwrap().get_distance(expr); if let Some(unwrapped) = distance { EnvironmentFrame::read_at( &self.env, @@ -285,7 +285,7 @@ impl Interpreter { statements: &Vec, env_opt: Arc, ) -> Result { - let previous = std::mem::replace(&mut self.env, env_opt.clone()); + let previous = std::mem::replace(&mut self.env, env_opt); let mut ret = Ok(RV::Undefined); @@ -356,7 +356,7 @@ impl VisitorMut for Interpreter { Ok(RV::Bool(self.visit_expr(right)?.as_bool())) } Expr::Assignment { dst, expr, .. } => { - let distance = self.current_program.clone().unwrap().get_distance(e); + let distance = self.current_program.as_ref().unwrap().get_distance(e); let evaluated = self.visit_expr(expr)?; let result = if let Some(distance_unv) = distance { EnvironmentFrame::assign_at( @@ -589,7 +589,7 @@ impl VisitorMut for Interpreter { Stmt::Declaration { dst, expr, .. } => { let evaluated = self.visit_expr(expr)?; self.env - .define(self.interner.get_or_intern(&dst.name), evaluated.clone()); + .define(self.interner.get_or_intern(&dst.name), evaluated); } Stmt::Block { body: stmts, .. } => { return self.execute_block( diff --git a/lykiadb-server/src/value/environment.rs b/lykiadb-server/src/value/environment.rs index f43bae2..3f78af8 100644 --- a/lykiadb-server/src/value/environment.rs +++ b/lykiadb-server/src/value/environment.rs @@ -22,7 +22,7 @@ pub enum EnvironmentError { Other { message: String }, } -macro_rules! env_ancestor { +macro_rules! to_ancestor { ($init:expr, $distance:expr) => {{ let mut env = $init; for _ in 0..$distance { @@ -74,7 +74,13 @@ impl EnvironmentFrame { key_sym: SymbolU32, value: RV, ) -> Result { - env_ancestor!(env, distance).assign(key, key_sym, value) + if distance == 0 { + return env.assign(key, key_sym, value); + } + if distance == 1 && env.parent.is_some() { + return env.parent.as_ref().unwrap().assign(key, key_sym, value); + } + to_ancestor!(env, distance).assign(key, key_sym, value) } pub fn read(&self, key: &str, key_sym: &SymbolU32) -> Result { @@ -99,7 +105,13 @@ impl EnvironmentFrame { key: &str, key_sym: &SymbolU32, ) -> Result { - env_ancestor!(env, distance) + if distance == 0 { + return env.read(key, key_sym); + } + if distance == 1 && env.parent.is_some() { + return env.parent.as_ref().unwrap().read(key, key_sym); + } + to_ancestor!(env, distance) .map .read() .unwrap()