Browse Source

added macros.lisp sample

plato 5 years ago
parent
commit
c9ace8c383
4 changed files with 48 additions and 9 deletions
  1. 11 0
      lisp/core.rs
  2. 8 8
      lisp/lisp.rs
  3. 27 0
      lisp/macros.lisp
  4. 2 1
      lisp/run.sh

+ 11 - 0
lisp/core.rs

@@ -189,6 +189,7 @@ fn last(a: MalArgs) -> MalRet {
         _ => error("invalid args to first"),
     }
 }
+
 fn first(a: MalArgs) -> MalRet {
     match a[0].clone() {
         List(ref seq, _) | Vector(ref seq, _) if seq.len() == 0 => Ok(Nil),
@@ -198,6 +199,15 @@ fn first(a: MalArgs) -> MalRet {
     }
 }
 
+fn second(a: MalArgs) -> MalRet {
+    match a[0].clone() {
+        List(ref seq, _) | Vector(ref seq, _) if seq.len() < 2 => Ok(Nil),
+        List(ref seq, _) | Vector(ref seq, _) => Ok(seq[1].clone()),
+        Nil => Ok(Nil),
+        _ => error("invalid args to first"),
+    }
+}
+
 fn rest(a: MalArgs) -> MalRet {
     match a[0].clone() {
         List(ref seq, _) | Vector(ref seq, _) => {
@@ -560,5 +570,6 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("scalar", func(scalar_from)),
         ("square", func(scalar_square)),
         ("cs::one", func(cs_one)),
+        ("second", func(second)),
     ]
 }

+ 8 - 8
lisp/lisp.rs

@@ -100,12 +100,12 @@ fn is_macro_call(ast: &MalVal, env: &Env) -> Option<(MalVal, MalArgs)> {
 fn macroexpand(mut ast: MalVal, env: &Env) -> (bool, MalRet) {
     let mut was_expanded = false;
     while let Some((mf, args)) = is_macro_call(&ast, env) {
-        //println!("macroexpand 1: {:?}", ast);
+        // println!("macroexpand 1: {:?}", ast);
         ast = match mf.apply(args) {
             Err(e) => return (false, Err(e)),
             Ok(a) => a,
         };
-        //println!("macroexpand 2: {:?}", ast);
+        // println!("macroexpand 2: {:?}", ast);
         was_expanded = true;
     }
     (was_expanded, Ok(ast))
@@ -328,7 +328,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                     }
                     Sym(ref a0sym) if a0sym == "prove" => {
                         let a1 = l[1].clone();
-                        ast = eval(a1.clone(), env.clone())?;
+                        eval(a1.clone(), env.clone())?;
                         prove(a1.clone(), env.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc-const" => {
@@ -369,6 +369,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         let a1 = l[1].clone();
                         let value = eval(l[2].clone(), env.clone())?;
                         let result = eval(value.clone(), env.clone())?;
+                        println!("a1 {:?} ", a1);
                         let allocs = get_allocations(&env, "Allocations");
                         let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
                         for (k, v) in allocs.iter() {
@@ -392,7 +393,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         Ok(result.clone())
                     }
                     //Sym(ref a0sym) if a0sym == "verify" => {
-                    Sym(ref a0sym) if a0sym == "enforce" => {
+                    Sym(ref a0sym) if a0sym == "enforce" => {                        
                         // here i'm considering that we always have tuple with only two elements
                         // also it's important to keep in mind for the sake of brevity of this v0
                         // we will not allow calculation or any lisp evaluations inside the enforce
@@ -478,6 +479,9 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                                 vector![vec![Enforce(Rc::new(new_vec.clone()))]],
                             )?;                     
                         }
+
+                        println!("allocs here {:?}", get_allocations_nested(&env, "Allocations"));
+                        println!("enforce here {:?}", get_enforce_allocs_nested(&env));
                         
                         Ok(MalVal::Nil)
                     }
@@ -558,10 +562,6 @@ pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
 
 pub fn get_allocations_nested(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
     let alloc_hm: Rc<FnvHashMap<String, MalVal>> = Rc::new(FnvHashMap::default());
-    
-    if let Some(e) = &env.outer {
-    }
-
     match env_find(env, key) {
         Some(e) => match env_get(&e, &Sym(key.to_string())) {
             Ok(f) => {

+ 27 - 0
lisp/macros.lisp

@@ -0,0 +1,27 @@
+;; testing macros
+
+(def! inc (fn* [a] (i+ a 1)))
+(def! gensym
+  (let* [counter (atom 0)]
+    (fn* []
+      (symbol (str "G__" (swap! counter inc))))))
+
+(defmacro! zk-square (fn* [var] (
+        (let* [v1 (gensym)
+               v2 (gensym)] (
+        `(alloc ~v1 ~var)
+        `(alloc ~v2 (square ~var))
+        `(enforce  
+            (scalar::one ~v1) 
+            (scalar::one ~v1) 
+            (scalar::one ~v2) 
+        )
+        )
+    ))
+))
+(def! param1 (scalar 1))
+(def! param2 (scalar 3))
+(prove 
+  (zk-square param1)
+  (zk-square param2)
+)

+ 2 - 1
lisp/run.sh

@@ -1,3 +1,4 @@
 export RUST_BACKTRACE=full
-cargo run --bin lisp load jubjub-mul.lisp
+cargo run --bin lisp load macros.lisp
+#cargo run --bin lisp load jubjub-mul.lisp
 #cargo run --bin lisp load new-cs.lisp