diff --git a/src/borrow.rs b/src/borrow.rs index ad3963944451b2b771d9656df2db997c4343d1cc..29bfa3524f264b4fa2ec7f8ced5df4cdebce7795 100644 --- a/src/borrow.rs +++ b/src/borrow.rs @@ -37,6 +37,10 @@ impl Mem { Mem(VecDeque::new()) } + fn get_scope(&self) -> Scope { + self.0.len() + } + fn get(&self, id: String) -> Option<(Scope, &Val)> { self.0 .iter() @@ -47,10 +51,11 @@ impl Mem { }) } - fn get_mut(&mut self, id: String) -> Option<&mut Val> { + fn get_mut(&mut self, scope: Scope, id: String) -> Option<&mut Val> { self.0 .iter_mut() - .find_map(|hm| match hm.get_mut(id.as_str()) { + .enumerate() + .find_map(|(i, hm)| match hm.get_mut(id.as_str()) { Some((true, v, _)) => Some(v), // a mut reference Some((_, v, _)) => { match v { @@ -74,9 +79,9 @@ impl Mem { hm.insert(id, (is_mut, Val::Uninitialized, Bc::Owned)); } - fn update(&mut self, id: String, val: Val) { + fn update(&mut self, scope: Scope, id: String, val: Val) { // println!("before mem {:?}", self); - match self.get_mut(id.clone()) { + match self.get_mut(scope, id.clone()) { Some(v_ref) => { println!("found"); *v_ref = val; @@ -85,7 +90,7 @@ impl Mem { panic!("variable not found"); } }; - // println!("after mem {:?}", self); + println!("after mem {:?}", self); } fn push_empty_scope(&mut self) { @@ -252,7 +257,7 @@ fn eval_stmts(stmts: &Stmts, m: &mut Mem, fn_env: &FnEnv) -> Val { Some(e) => { let r = eval_expr(e, m, fn_env); println!("r {:?}", r); - m.update(id.to_owned(), r); + m.update(m.get_scope(), id.to_owned(), r); Val::Unit } _ => Val::Unit, @@ -263,13 +268,13 @@ fn eval_stmts(stmts: &Stmts, m: &mut Mem, fn_env: &FnEnv) -> Val { match &**ev { Expr::Id(id) => { let v = eval_expr(e, m, fn_env); - m.update(id.to_owned(), v) + m.update(m.get_scope(), id.to_owned(), v) } _ => { let lv = eval_left_expr(ev, m, fn_env); println!("lv {:?}", lv); let v = eval_expr(e, m, fn_env); - m.update(lv, v) + m.update(m.get_scope(), lv, v) } }; Val::Unit