Skip to content
Snippets Groups Projects
Commit 17513e4c authored by Per Lindgren's avatar Per Lindgren
Browse files

borrow scope wip4

parent 50af83f9
No related branches found
No related tags found
No related merge requests found
fn g(b: &mut bool) {
let c = b;
*c = true;
}
fn f(b: &mut bool) {
let c = b;
g(c);
}
fn main() {
let mut b = false;
f(&mut b);
}
fn g(b: &mut i32) {
let c = b;
*c = 1;
}
fn f(b: &mut i32) {
let c = b;
g(c);
*c = 2;
}
fn main() {
let mut b = 0;
f(&mut b);
}
......@@ -52,10 +52,16 @@ impl Mem {
}
fn get_mut(&mut self, scope: Scope, id: String) -> Option<&mut Val> {
println!("--- get_mut {:?} in scope # {}", id, scope);
let nr_scopes = self.0.len();
self.0
.iter_mut()
.enumerate()
.find_map(|(i, hm)| match hm.get_mut(id.as_str()) {
.find_map(|(i, hm)| match nr_scopes - i <= scope {
true => {
// search in this scope
println!("i {}, hm {:?} ", i, hm);
match hm.get_mut(id.as_str()) {
Some((true, v, _)) => Some(v), // a mut reference
Some((_, v, _)) => {
match v {
......@@ -69,6 +75,9 @@ impl Mem {
}
}
_ => None,
}
}
false => None, // skip this scope
})
}
......@@ -83,7 +92,7 @@ impl Mem {
// println!("before mem {:?}", self);
match self.get_mut(scope, id.clone()) {
Some(v_ref) => {
println!("found");
println!("update v_ref {:?} = val {:?}", v_ref, val);
*v_ref = val;
}
None => {
......@@ -211,25 +220,25 @@ fn eval_expr(e: &Expr, m: &mut Mem, fn_env: &FnEnv) -> Val {
}
}
fn eval_left_expr(e: &Expr, m: &Mem, fn_env: &FnEnv) -> Id {
fn eval_left_expr(e: &Expr, m: &Mem, fn_env: &FnEnv, scope: Scope) -> (Scope, Id) {
println!("eval_left_expr {}", e);
match e {
Expr::Id(id) => id.to_owned(),
Expr::Id(id) => (scope, id.to_owned()),
Expr::DeRef(e) => {
println!("eval_left deref e {:?}", e);
let ev = eval_left_expr(e, m, fn_env);
println!("eval_left {:?}", ev);
let (sc, ev) = eval_left_expr(e, m, fn_env, scope);
println!("eval_left {:?}, sc {}", ev, sc);
match m.get(ev) {
Some((_, Val::Ref(s, id))) => {
println!("Ref id {} in scope {}", id, s);
id.clone()
(*s, id.clone())
}
Some((_, Val::RefMut(s, id))) => {
println!("RefMut id {} in scope {}", id, s);
id.clone()
(*s, id.clone())
}
_ => panic!("deref failed"),
}
......@@ -271,10 +280,12 @@ fn eval_stmts(stmts: &Stmts, m: &mut Mem, fn_env: &FnEnv) -> Val {
m.update(m.get_scope(), id.to_owned(), v)
}
_ => {
let lv = eval_left_expr(ev, m, fn_env);
println!("lv {:?}", lv);
let cur_scope = m.get_scope();
println!("cur_scope {}", cur_scope);
let (scope, id) = eval_left_expr(ev, m, fn_env, cur_scope);
println!("scope {} id {:?}", scope, id);
let v = eval_expr(e, m, fn_env);
m.update(m.get_scope(), lv, v)
m.update(scope, id, v)
}
};
Val::Unit
......
......@@ -118,3 +118,23 @@ fn borrow() {
fn borrow2() {
eval_prog("examples/borrow2.rs");
}
#[test]
fn borrow4() {
eval_prog("examples/borrow4.rs");
}
#[test]
fn borrow5() {
eval_prog("examples/borrow5.rs");
}
#[test]
fn borrow6() {
eval_prog("examples/borrow6.rs");
}
#[test]
fn borrow7() {
eval_prog("examples/borrow7.rs");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment