From e4055560bbdea09d4d9e60ec0896ef365d3f51f9 Mon Sep 17 00:00:00 2001 From: radish19_MBA2020 Date: Sat, 13 Jul 2024 07:06:49 +0900 Subject: [PATCH] Fix scoping in `For` statement --- momonga/src/eval.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/momonga/src/eval.rs b/momonga/src/eval.rs index 169ca35..482633f 100644 --- a/momonga/src/eval.rs +++ b/momonga/src/eval.rs @@ -125,18 +125,17 @@ fn eval_for_stmt<'a>(for_stmt: &'a ForStmt, env: Rc>>) -> EvalSt afterthought, block, } = for_stmt; - let env_block = Rc::new(RefCell::new(Env::new(Some(Rc::clone(&env))))); match init { - Some(ForStmtInit::Var(var_stmt)) => eval_var_stmt(var_stmt, Rc::clone(&env_block))?, - Some(ForStmtInit::Expr(expr_stmt)) => eval_expr_stmt(expr_stmt, env)?, + Some(ForStmtInit::Var(var_stmt)) => eval_var_stmt(var_stmt, Rc::clone(&env))?, + Some(ForStmtInit::Expr(expr_stmt)) => eval_expr_stmt(expr_stmt, Rc::clone(&env))?, _ => todo!(), // TODO: Define how to handle this case }; let mut result = Ok(None); loop { let cond = match cond { - Some(cond) => match *eval_expr(cond, Rc::clone(&env_block))?.borrow() { + Some(cond) => match *eval_expr(cond, Rc::clone(&env))?.borrow() { Value::Bool(bool) => bool, _ => todo!(), // TODO: Define how to handle this case }, @@ -147,9 +146,9 @@ fn eval_for_stmt<'a>(for_stmt: &'a ForStmt, env: Rc>>) -> EvalSt break; } - result = match eval_block_stmt(block, Rc::clone(&env_block)) { + result = match eval_block_stmt(block, Rc::clone(&env)) { Err(JumpStmt::Continue) => { - eval_for_stmt_afterthought(afterthought, Rc::clone(&env_block))?; + eval_for_stmt_afterthought(afterthought, Rc::clone(&env))?; result = Ok(None); continue; } @@ -159,7 +158,7 @@ fn eval_for_stmt<'a>(for_stmt: &'a ForStmt, env: Rc>>) -> EvalSt default => default, }; - eval_for_stmt_afterthought(afterthought, Rc::clone(&env_block))?; + eval_for_stmt_afterthought(afterthought, Rc::clone(&env))?; } result }