From 28c1084e8636af29098cdfa6bf707afefe8d75b9 Mon Sep 17 00:00:00 2001 From: ada Date: Wed, 6 Jan 2021 18:36:18 +0100 Subject: [PATCH] adjusting scope for allocs --- lisp/lisp.rs | 38 +++++++++++++++++++++++++++++--------- lisp/new-cs.lisp | 13 +++++++------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lisp/lisp.rs b/lisp/lisp.rs index e61680456..a078f6787 100644 --- a/lisp/lisp.rs +++ b/lisp/lisp.rs @@ -310,15 +310,16 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet { Sym(ref a0sym) if a0sym == "prove" => { let a1 = l[0].clone(); println!("prove {:?}", a1); + println!("allocation {:?}", get_allocations(&env, "Allocations")); + println!("allocation {:?}", get_allocations(&env, "AllocationsInput")); prove(a1.clone(), env.clone()) } - Sym(ref a0sym) if a0sym == "alloc-input" => Ok(MalVal::Nil), - Sym(ref a0sym) if a0sym == "alloc" => { + Sym(ref a0sym) if a0sym == "alloc-input" => { let a1 = l[1].clone(); let value = eval(l[2].clone(), env.clone())?; let result = eval(value.clone(), env.clone())?; let symbol = MalVal::Sym(a1.pr_str(false)); - if let Hash(allocs, _) = get_allocations(&env)? { + if let Hash(allocs, _) = get_allocations(&env, "AllocationsInput")? { let mut new_hm: FnvHashMap = FnvHashMap::default(); for (k, v) in allocs.iter() { new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?); @@ -330,6 +331,27 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet { Hash(Rc::new(new_hm), Rc::new(Nil)), ); }; + println!("allocation {:?}", get_allocations(&env, "AllocationsInput")); + Ok(Nil) + } + Sym(ref a0sym) if a0sym == "alloc" => { + let a1 = l[1].clone(); + let value = eval(l[2].clone(), env.clone())?; + let result = eval(value.clone(), env.clone())?; + let symbol = MalVal::Sym(a1.pr_str(false)); + if let Hash(allocs, _) = get_allocations(&env, "Allocations")? { + let mut new_hm: FnvHashMap = FnvHashMap::default(); + for (k, v) in allocs.iter() { + new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?); + } + new_hm.insert(a1.pr_str(false), result); + env_set( + &env, + Sym("Allocations".to_string()), + Hash(Rc::new(new_hm), Rc::new(Nil)), + ); + }; + println!("allocation {:?}", get_allocations(&env, "Allocations")); Ok(Nil) } //Sym(ref a0sym) if a0sym == "verify" => { @@ -384,14 +406,14 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet { ret } -pub fn get_allocations(env: &Env) -> MalRet { +pub fn get_allocations(env: &Env, key: &str) -> MalRet { let mut alloc_hm: FnvHashMap = FnvHashMap::default(); - match env_find(env, "Allocations") { - Some(e) => match env_get(&e, &Sym("Allocations".to_string())) { + match env_find(env, key) { + Some(e) => match env_get(&e, &Sym(key.to_string())) { Ok(f) => Ok(f), _ => env_set( &env, - Sym("Allocations".to_string()), + Sym(key.to_string()), Hash(Rc::new(alloc_hm), Rc::new(Nil)), ), }, @@ -401,8 +423,6 @@ pub fn get_allocations(env: &Env) -> MalRet { Hash(Rc::new(alloc_hm), Rc::new(Nil)), ), } - - //// TODO check if there is the alloc on the env already } pub fn setup(ast: MalVal, mut env: Env) -> Result, MalErr> { diff --git a/lisp/new-cs.lisp b/lisp/new-cs.lisp index e31372d19..d33ee1508 100644 --- a/lisp/new-cs.lisp +++ b/lisp/new-cs.lisp @@ -1,5 +1,6 @@ (println "new-cs.lisp") -(setup +( + (setup (let* [aux (scalar 3) x (alloc "x" aux) x2 (alloc "x2" (* aux aux)) @@ -24,9 +25,9 @@ (scalar::one cs::one) (scalar::one x3) ) - -) ) -)) -(prove)) -;; (println 'verify (MyCircuit (scalar 27))) \ No newline at end of file + ) + ) +(prove) +) +;; (println 'verify (MyCircuit (scalar 27)))