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

bc added

parent 5f6af115
Branches
No related tags found
No related merge requests found
...@@ -17,7 +17,15 @@ enum Val { ...@@ -17,7 +17,15 @@ enum Val {
RefMut(Id), RefMut(Id),
} }
type IdVal = HashMap<Id, (bool, Val)>; #[derive(Debug, Clone)]
pub enum Bc {
Fresh, // Not yet accessed
Owned, // accessed as owned, eg let mut a = 4; _ = a + 1;
SharedRef(Vec<Id>), // accessed through shared ref, e.g. let b = &a, _ = *b;
UniqueRef(Id), // accessed through unique ref, e.g. let c = &mut a, _ = *c;
}
type IdVal = HashMap<Id, (bool, Val, Bc)>;
#[derive(Debug)] #[derive(Debug)]
struct Mem(VecDeque<IdVal>); struct Mem(VecDeque<IdVal>);
...@@ -29,7 +37,7 @@ impl Mem { ...@@ -29,7 +37,7 @@ impl Mem {
fn get(&self, id: String) -> Option<&Val> { fn get(&self, id: String) -> Option<&Val> {
self.0.iter().find_map(|hm| match hm.get(id.as_str()) { self.0.iter().find_map(|hm| match hm.get(id.as_str()) {
Some((_, v)) => Some(v), // always ok to go from mut to non mut Some((_, v, _)) => Some(v), // always ok to go from mut to non mut
_ => None, _ => None,
}) })
} }
...@@ -38,8 +46,8 @@ impl Mem { ...@@ -38,8 +46,8 @@ impl Mem {
self.0 self.0
.iter_mut() .iter_mut()
.find_map(|hm| match hm.get_mut(id.as_str()) { .find_map(|hm| match hm.get_mut(id.as_str()) {
Some((true, v)) => Some(v), // a mut reference Some((true, v, _)) => Some(v), // a mut reference
Some((_, v)) => { Some((_, v, _)) => {
match v { match v {
Val::Uninitialized => Some(v), // an uninitialized Val::Uninitialized => Some(v), // an uninitialized
Val::RefMut(_) => { Val::RefMut(_) => {
...@@ -58,7 +66,7 @@ impl Mem { ...@@ -58,7 +66,7 @@ impl Mem {
let hm = self.0.front_mut().unwrap(); let hm = self.0.front_mut().unwrap();
println!("insert id {:?}, in scope {:?}", id, hm); println!("insert id {:?}, in scope {:?}", id, hm);
hm.insert(id, (is_mut, Val::Uninitialized)); hm.insert(id, (is_mut, Val::Uninitialized, Bc::Owned));
} }
fn update(&mut self, id: String, val: Val) { fn update(&mut self, id: String, val: Val) {
...@@ -315,7 +323,7 @@ fn eval_fn(id: &str, args: &Vec<Val>, m: &mut Mem, fn_env: &FnEnv) -> Val { ...@@ -315,7 +323,7 @@ fn eval_fn(id: &str, args: &Vec<Val>, m: &mut Mem, fn_env: &FnEnv) -> Val {
let p_id_arg: Vec<(&(bool, String), &Val)> = p_id.iter().zip(args).collect(); let p_id_arg: Vec<(&(bool, String), &Val)> = p_id.iter().zip(args).collect();
let p_id_arg: IdVal = p_id_arg let p_id_arg: IdVal = p_id_arg
.into_iter() .into_iter()
.map(|(s, v)| (s.1.clone(), (s.0, v.clone()))) .map(|(s, v)| (s.1.clone(), (s.0, v.clone(), Bc::Fresh)))
.collect(); .collect();
println!("args {:?}", args); println!("args {:?}", args);
...@@ -347,9 +355,15 @@ fn test_deref() { ...@@ -347,9 +355,15 @@ fn test_deref() {
let mut m: Mem = Mem::new(); let mut m: Mem = Mem::new();
let mut hm = IdVal::new(); let mut hm = IdVal::new();
hm.insert("a".to_string(), (false, Val::Num(7))); hm.insert("a".to_string(), (false, Val::Num(7), Bc::Fresh));
hm.insert("b".to_string(), (false, Val::Ref("a".to_string()))); hm.insert(
hm.insert("c".to_string(), (false, Val::Ref("b".to_string()))); "b".to_string(),
(false, Val::Ref("a".to_string()), Bc::Fresh),
);
hm.insert(
"c".to_string(),
(false, Val::Ref("b".to_string()), Bc::Fresh),
);
m.push_param_scope(hm); m.push_param_scope(hm);
println!("mem {:?}", m); println!("mem {:?}", m);
let e: Expr = Expr::Id("a".to_string()); let e: Expr = Expr::Id("a".to_string());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment