Skip to content

Commit

Permalink
fix: Minor tweak for surprising boost
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 16, 2024
1 parent 32548b6 commit 75e3689
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Interpreter {
}

fn look_up_variable(&mut self, name: &str, expr: &Expr) -> Result<RV, HaltReason> {
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,
Expand Down Expand Up @@ -285,7 +285,7 @@ impl Interpreter {
statements: &Vec<Stmt>,
env_opt: Arc<EnvironmentFrame>,
) -> Result<RV, HaltReason> {
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);

Expand Down Expand Up @@ -356,7 +356,7 @@ impl VisitorMut<RV, HaltReason> 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(
Expand Down Expand Up @@ -589,7 +589,7 @@ impl VisitorMut<RV, HaltReason> 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(
Expand Down
18 changes: 15 additions & 3 deletions lykiadb-server/src/value/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -74,7 +74,13 @@ impl EnvironmentFrame {
key_sym: SymbolU32,
value: RV,
) -> Result<bool, HaltReason> {
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<RV, HaltReason> {
Expand All @@ -99,7 +105,13 @@ impl EnvironmentFrame {
key: &str,
key_sym: &SymbolU32,
) -> Result<RV, HaltReason> {
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()
Expand Down

0 comments on commit 75e3689

Please sign in to comment.