Skip to content

Commit

Permalink
Fix scoping in For statement
Browse files Browse the repository at this point in the history
derarion committed Jul 12, 2024
1 parent cb9f9e0 commit e405556
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions momonga/src/eval.rs
Original file line number Diff line number Diff line change
@@ -125,18 +125,17 @@ fn eval_for_stmt<'a>(for_stmt: &'a ForStmt, env: Rc<RefCell<Env<'a>>>) -> 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<RefCell<Env<'a>>>) -> 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<RefCell<Env<'a>>>) -> EvalSt
default => default,
};

eval_for_stmt_afterthought(afterthought, Rc::clone(&env_block))?;
eval_for_stmt_afterthought(afterthought, Rc::clone(&env))?;
}
result
}

0 comments on commit e405556

Please sign in to comment.