From 3b858eda98d188510a461351371d7dbf705875bc Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Tue, 6 Jun 2023 01:10:01 -0400 Subject: [PATCH] Move primitives to enum - defunctionalized --- kv/src/ast.rs | 573 +++++++++++++++++-------------------------------- kv/src/main.rs | 8 +- 2 files changed, 209 insertions(+), 372 deletions(-) diff --git a/kv/src/ast.rs b/kv/src/ast.rs index e7180e6..e30f92e 100644 --- a/kv/src/ast.rs +++ b/kv/src/ast.rs @@ -4,12 +4,11 @@ use std::rc::Rc; use std::cell::RefCell; use std::convert::From; -impl From for Form { fn from(item: i32) -> Self { Form::Int(item) } } -impl From for Form { fn from(item: bool) -> Self { Form::Bool(item) } } // todo, strings not symbols? impl From for Form { fn from(item: String) -> Self { Form::Symbol(item) } } -impl From<&str> for Form { fn from(item: &str) -> Self { Form::Symbol(item.to_owned()) } } - +impl From<&str> for Form { fn from(item: &str) -> Self { Form::Symbol(item.to_owned()) } } +impl From for Form { fn from(item: i32) -> Self { Form::Int(item) } } +impl From for Form { fn from(item: bool) -> Self { Form::Bool(item) } } impl, B: Into
> From<(A, B)> for Form { fn from(item: (A, B)) -> Self { Form::Pair(Rc::new(item.0.into()), Rc::new(item.1.into())) @@ -90,7 +89,7 @@ impl fmt::Display for Form { } } }, - Form::PrimComb(name, _f) => write!(f, "<{name}>"), + Form::PrimComb { eval_limit, ins } => write!(f, "<{eval_limit}> - {ins:?}"), Form::DeriComb { se, de, params, body } => { write!(f, "<{} {} {}>", de.as_ref().unwrap_or(&"".to_string()), params, body) }, @@ -107,7 +106,7 @@ pub enum Form { Symbol(String), Cell(RefCell>), Pair(Rc,Rc), - PrimComb(String, fn(Rc, Rc, Cont, Cont) -> Cursor), + PrimComb { eval_limit: i32, ins: PrimCombI }, DeriComb { se: Rc, de: Option, params: String, body: Rc }, ContComb(Cont), } @@ -118,81 +117,47 @@ pub enum Cont { CatchRet { nc: Box, restore_meta: Box }, Eval { e: Rc, nc: Box }, Call { p: Rc, e: Rc, nc: Box }, - PramEval { eval_limit: i32, to_eval: Rc, collected: Option>, e: Rc, pf: fn(Rc, Rc, Cont, Cont) -> Cursor, nc: Box }, + PramEval { eval_limit: i32, to_eval: Rc, collected: Option>, e: Rc, ins: PrimCombI, nc: Box }, } -#[derive(Debug, Eq, PartialEq, Clone)] -enum Ins { - Const(Rc), - EnvGet(i32), - Eq, - Lt, - Leq, - Gt, - Geq, - Add, - Sub, - Mul, - Div, - Mod, - And, - Or, - Xor, - CombP, - CellP, - IntP, - BoolP, - SymbolP, - PairP, - NilP, - Exit, - MetaRet, - CatchRet, - Bad(&'static str), -} -pub struct Cursor { f: Rc, c: Cont, metac: Cont, new_bytecode: Option } +pub struct Cursor { f: Rc, c: Cont, metac: Cont } pub fn eval(e: Rc, f: Rc) -> Rc { - let mut cursor = Cursor { f, c: Cont::Eval { e, nc: Box::new(Cont::MetaRet) }, metac: Cont::Exit, new_bytecode: None }; + let mut cursor = Cursor { f, c: Cont::Eval { e, nc: Box::new(Cont::MetaRet) }, metac: Cont::Exit }; loop { - let Cursor { f, c, metac, new_bytecode } = cursor; - if let Some(bc) = new_bytecode { - //println!("{:?}", bc); - } + let Cursor { f, c, metac } = cursor; match c { Cont::Exit => { - println!("{:?}", Ins::Exit); return f; }, Cont::MetaRet => { - cursor = Cursor { f: f, c: metac.clone(), metac: metac, new_bytecode: Some(Ins::MetaRet) }; + cursor = Cursor { f: f, c: metac.clone(), metac: metac }; }, Cont::CatchRet { nc, restore_meta } => { - cursor = Cursor { f: f, c: *nc, metac: *restore_meta, new_bytecode: Some(Ins::CatchRet) }; + cursor = Cursor { f: f, c: *nc, metac: *restore_meta }; }, - Cont::PramEval { eval_limit, to_eval, collected, e, pf, nc } => { + Cont::PramEval { eval_limit, to_eval, collected, e, ins, nc } => { let next_collected = if let Some(collected) = collected { Rc::new(collected.append(f).unwrap()) } else { Rc::new(Form::Nil) }; if eval_limit == 0 || to_eval.is_nil() { - // hmm, it should probs reverse collected let mut traverse = to_eval; let mut next_collected = next_collected; while !traverse.is_nil() { next_collected = Rc::new(next_collected.append(traverse.car().unwrap()).unwrap()); traverse = traverse.cdr().unwrap(); } - cursor = pf(e, next_collected, *nc, metac); + cursor = impl_prim(ins, e, next_collected, *nc, metac); } else { cursor = Cursor { f: to_eval.car().unwrap(), c: Cont::Eval { e: Rc::clone(&e), nc: Box::new(Cont::PramEval { eval_limit: eval_limit - 1, to_eval: to_eval.cdr().unwrap(), collected: Some(next_collected), - e, pf, nc }) }, metac, new_bytecode: None }; + e, ins, nc }) }, metac }; } }, Cont::Eval { e, nc } => { match *f { Form::Pair(ref comb, ref p) => { - cursor = Cursor { f: Rc::clone(comb), c: Cont::Eval { e: Rc::clone(&e), nc: Box::new(Cont::Call { p: Rc::clone(p), e: e, nc: nc }) }, metac, new_bytecode: None } + cursor = Cursor { f: Rc::clone(comb), c: Cont::Eval { e: Rc::clone(&e), nc: Box::new(Cont::Call { p: Rc::clone(p), e: e, nc: nc }) }, metac } }, Form::Symbol(ref s) => { let mut t = Rc::clone(&e); @@ -201,30 +166,28 @@ pub fn eval(e: Rc, f: Rc) -> Rc { t = t.cdr().unwrap(); dist += 1; } - // Hmm, we need to know if this is from this call or not for if we should EnvGet - //cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc, metac, new_bytecode: Some(Ins::EnvGet(dist)) }; - cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc, metac, new_bytecode: Some(Ins::Bad("don't know if envget or const or nothing (think called prim vs passed prim vs parameter), and how to maintain that info")) }; + cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc, metac }; }, _ => { - cursor = Cursor { f: Rc::clone(&f), c: *nc, metac, new_bytecode: Some(Ins::Const(f)) }; + cursor = Cursor { f: Rc::clone(&f), c: *nc, metac }; }, } }, Cont::Call { p, e, nc } => { match *f { - Form::PrimComb(ref _n, ref f) => { - cursor = f(e, p, *nc, metac); - }, + Form::PrimComb { eval_limit, ins } => { + cursor = Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit, to_eval: p, collected: None, e, ins, nc: nc }, metac }; + } Form::DeriComb{ref se, ref de, ref params, ref body } => { let mut new_e = Rc::clone(se); if let Some(de) = de { new_e = assoc(de, Rc::clone(&e), new_e); } new_e = assoc(params, p, new_e); - cursor = Cursor { f: Rc::clone(body), c: Cont::Eval { e: new_e, nc: nc }, metac, new_bytecode: Some(Ins::Bad("deri-comb call")) }; + cursor = Cursor { f: Rc::clone(body), c: Cont::Eval { e: new_e, nc: nc }, metac }; }, Form::ContComb(ref c) => { - cursor = Cursor { f: p.car().unwrap(), c: Cont::Eval { e, nc: Box::new(c.clone()) }, metac: Cont::CatchRet { nc: nc, restore_meta: Box::new(metac) }, new_bytecode: Some(Ins::Bad("cont-comb call")) }; + cursor = Cursor { f: p.car().unwrap(), c: Cont::Eval { e, nc: Box::new(c.clone()) }, metac: Cont::CatchRet { nc: nc, restore_meta: Box::new(metac) } }; }, _ => panic!("Tried to call not a Prim/DeriComb/ContComb {:?}, nc was {:?}", f, nc), } @@ -233,14 +196,14 @@ pub fn eval(e: Rc, f: Rc) -> Rc { } } -fn assoc(k: &str, v: Rc, l: Rc) -> Rc { +pub fn assoc(k: &str, v: Rc, l: Rc) -> Rc { Rc::new(Form::Pair( Rc::new(Form::Pair( Rc::new(Form::Symbol(k.to_owned())), v)), l)) } -fn assoc_vec(kvs: Vec<(&str, Rc)>) -> Rc { +pub fn assoc_vec(kvs: Vec<(&str, Rc)>) -> Rc { let mut to_ret = Rc::new(Form::Nil); for (k, v) in kvs { to_ret = assoc(k, v, to_ret); @@ -248,325 +211,193 @@ fn assoc_vec(kvs: Vec<(&str, Rc)>) -> Rc { to_ret } +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum PrimCombI { + Eval, + Vau, + If, + + Reset, + Shift, + + Cell, + Set, + Get, + + Cons, + Car, + Cdr, + Quote, + Assert, + + Eq, + Lt, + LEq, + Gt, + GEq, + + Plus, + Minus, + Mult, + Div, + Mod, + And, + Or, + Xor, + + CombP, + CellP, + PairP, + SymbolP, + IntP, + BoolP, + NilP, +} + +pub fn impl_prim(ins: PrimCombI, e: Rc, ps: Rc, c: Cont, metac: Cont) -> Cursor { + match ins { + PrimCombI::Eval => Cursor { f: ps.car().unwrap(), c: Cont::Eval { e: ps.cdr().unwrap().car().unwrap(), nc: Box::new(c) }, metac }, + PrimCombI::Vau => { + let de = ps.car().unwrap().sym().map(|s| s.to_owned()); + let params = ps.cdr().unwrap().car().unwrap().sym().unwrap().to_owned(); + let body = ps.cdr().unwrap().cdr().unwrap().car().unwrap(); + + Cursor { f: Rc::new(Form::DeriComb { se: e, de, params, body }), c: c, metac: metac } + }, + PrimCombI::If => if ps.car().unwrap().truthy() { + Cursor { f: ps.cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) }, metac } + } else { + Cursor { f: ps.cdr().unwrap().cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) }, metac } + }, + + PrimCombI::Reset => Cursor { f: ps.car().unwrap(), + c: Cont::Eval { e: e, nc: Box::new(Cont::MetaRet) }, + metac: Cont::CatchRet { nc: Box::new(c), restore_meta: Box::new(metac) } }, + PrimCombI::Shift => Cursor { f: ps.car().unwrap(), + c: Cont::Call { p: Rc::new(Form::Pair(Rc::new(Form::ContComb(c)), + Rc::new(Form::Nil))), + e: e, + nc: Box::new(Cont::MetaRet) }, + metac: Cont::CatchRet { nc: Box::new(metac.clone()), restore_meta: Box::new(metac) } }, + + PrimCombI::Cell => Cursor { f: Rc::new(Form::Cell(RefCell::new(ps.car().unwrap()))), c, metac }, + PrimCombI::Set => match &*ps.car().unwrap() { + Form::Cell(cell) => Cursor { f: cell.replace(ps.cdr().unwrap().car().unwrap()), c: c, metac }, + _ => panic!("set on not cell"), + }, + PrimCombI::Get => match &*ps.car().unwrap() { + Form::Cell(cell) => Cursor { f: Rc::clone(&cell.borrow()), c: c, metac }, + _ => panic!("get on not cell"), + }, + + PrimCombI::Cons => Cursor { f: Rc::new(Form::Pair(ps.car().unwrap(), ps.cdr().unwrap().car().unwrap())), c: c, metac }, + PrimCombI::Car => Cursor { f: ps.car().unwrap().car().unwrap(), c: c, metac }, + PrimCombI::Cdr => Cursor { f: ps.car().unwrap().cdr().unwrap(), c: c, metac }, + PrimCombI::Quote => Cursor { f: ps.car().unwrap(), c: c, metac: metac }, + PrimCombI::Assert => { + let thing = ps.car().unwrap(); + if !thing.truthy() { + println!("Assert failed: {:?}", thing); + } + assert!(thing.truthy()); + Cursor { f: ps.cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) }, metac } + }, + + PrimCombI::Eq => Cursor { f: Rc::new(Form::Bool(ps.car().unwrap() == ps.cdr().unwrap().car().unwrap())), c, metac }, + PrimCombI::Lt => Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() < ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::LEq => Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() <= ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::Gt => Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() > ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::GEq => Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() >= ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + + PrimCombI::Plus => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() + ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::Minus => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() - ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::Mult => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() * ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::Div => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() / ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::Mod => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() % ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::And => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() & ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::Or => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() | ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + PrimCombI::Xor => Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() ^ ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac }, + + PrimCombI::CombP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { + Form::PrimComb { eval_limit, ins } => true, + Form::DeriComb { .. } => true, + _ => false, + })), c, metac }, + PrimCombI::CellP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { + Form::Cell(_c) => true, + _ => false, + })), c, metac }, + PrimCombI::PairP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { + Form::Pair(_a,_b) => true, + _ => false, + })), c, metac }, + PrimCombI::SymbolP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { + Form::Symbol(_) => true, + _ => false, + })), c, metac }, + PrimCombI::IntP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { + Form::Int(_) => true, + _ => false, + })), c, metac }, + PrimCombI::BoolP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { + Form::Bool(_) => true, + _ => false, + })), c, metac }, + PrimCombI::NilP => Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().is_nil())), c, metac }, + } +} + + +// Have eval?/maybe Cont?/maybe Cursor? parameterized on value type? +// Parameterized on prim implementation? +// Should opt impl use same prim implementation but trace values through accessors/constructors? +// with some special handling of If/Vau/etc? + pub fn root_env() -> Rc { assoc_vec(vec![ - ("eval", Rc::new(Form::PrimComb("eval".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: ps.car().unwrap(), c: Cont::Eval { e: ps.cdr().unwrap().car().unwrap(), nc: Box::new(c) }, metac, new_bytecode: Some(Ins::Bad("EvalSetEnv")) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), + ("eval", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Eval })), + ("vau", Rc::new(Form::PrimComb { eval_limit: 0, ins: PrimCombI::Vau })), + ("if", Rc::new(Form::PrimComb { eval_limit: 1, ins: PrimCombI::If })), - ("reset", Rc::new(Form::PrimComb("reset".to_owned(), |e, p, c, metac| { - Cursor { f: p.car().unwrap(), - c: Cont::Eval { e: e, nc: Box::new(Cont::MetaRet) }, - metac: Cont::CatchRet { nc: Box::new(c), restore_meta: Box::new(metac) }, new_bytecode: Some(Ins::Bad("reset")) } - }))), - ("shift", Rc::new(Form::PrimComb("shift".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { + ("reset", Rc::new(Form::PrimComb { eval_limit: 0, ins: PrimCombI::Reset })), + ("shift", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Shift })), - let c_comb_p = Rc::new(Form::Pair(Rc::new(Form::ContComb(c)), - Rc::new(Form::Nil))); - Cursor { f: ps.car().unwrap(), - c: Cont::Call { p: c_comb_p, - e: e, - nc: Box::new(Cont::MetaRet) }, - metac: Cont::CatchRet { nc: Box::new(metac.clone()), restore_meta: Box::new(metac) }, new_bytecode: Some(Ins::Bad("shift")) } + ("cell", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Cell })), + ("set", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Set })), + ("get", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Get })), - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), + ("cons", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Cons })), + ("car", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Car })), + ("cdr", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Cdr })), + ("quote", Rc::new(Form::PrimComb { eval_limit: 0, ins: PrimCombI::Quote })), + ("assert", Rc::new(Form::PrimComb { eval_limit: 1, ins: PrimCombI::Assert })), - // (vau de params body) - ("vau", Rc::new(Form::PrimComb("vau".to_owned(), |e, p, c, metac| { - let de = p.car().unwrap().sym().map(|s| s.to_owned()); - let params = p.cdr().unwrap().car().unwrap().sym().unwrap().to_owned(); - let body = p.cdr().unwrap().cdr().unwrap().car().unwrap(); + ("=", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Eq })), + ("<", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Lt })), + ("<=", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::LEq })), + (">", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Gt })), + (">=", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::GEq })), - Cursor { f: Rc::new(Form::DeriComb { se: e, de, params, body }), c: c, metac: metac, new_bytecode: Some(Ins::Bad("vau")) } - }))), - ("=", Rc::new(Form::PrimComb("=".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(ps.car().unwrap() == ps.cdr().unwrap().car().unwrap())), c, metac, new_bytecode: Some(Ins::Eq)} - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("<", Rc::new(Form::PrimComb("<".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() < ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Lt) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("<=", Rc::new(Form::PrimComb("<=".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() <= ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Leq) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - (">", Rc::new(Form::PrimComb(">".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() > ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Gt) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - (">=", Rc::new(Form::PrimComb(">=".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().int().unwrap() >= ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Geq) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), + ("+", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Plus })), + ("-", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Minus })), + ("*", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Mult })), + ("/", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Div })), + ("%", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Mod })), + ("&", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::And })), + ("|", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Or })), + ("^", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::Xor })), - ("if", Rc::new(Form::PrimComb("if".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: 1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - if ps.car().unwrap().truthy() { - Cursor { f: ps.cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) }, metac, new_bytecode: Some(Ins::Bad("EnsBranchPositive")) } - } else { - Cursor { f: ps.cdr().unwrap().cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) }, metac, new_bytecode: Some(Ins::Bad("EnsBranchFalse")) } - } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), + ("comb?", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::CombP })), + ("cell?", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::CellP })), + ("pair?", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::PairP })), + ("symbol?", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::SymbolP })), + ("int?", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::IntP })), + ("bool?", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::BoolP })), + ("nil?", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::NilP })), - ("cell", Rc::new(Form::PrimComb("cell".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Cell(RefCell::new(ps.car().unwrap()))), c, metac, new_bytecode: Some(Ins::Bad("cell")) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("set", Rc::new(Form::PrimComb("set".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - match &*ps.car().unwrap() { - Form::Cell(cell) => Cursor { f: cell.replace(ps.cdr().unwrap().car().unwrap()), c: c, metac, new_bytecode: Some(Ins::Bad("set")) }, - _ => panic!("set on not cell"), - } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("get", Rc::new(Form::PrimComb("get".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - match &*ps.car().unwrap() { - Form::Cell(cell) => Cursor { f: Rc::clone(&cell.borrow()), c: c, metac, new_bytecode: Some(Ins::Bad("get")) }, - _ => panic!("get on not cell"), - } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - - ("cons", Rc::new(Form::PrimComb("cons".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Pair(ps.car().unwrap(), ps.cdr().unwrap().car().unwrap())), c: c, metac, new_bytecode: Some(Ins::Bad("cons")) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("car", Rc::new(Form::PrimComb("car".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: ps.car().unwrap().car().unwrap(), c: c, metac, new_bytecode: Some(Ins::Bad("car")) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("cdr", Rc::new(Form::PrimComb("cdr".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: ps.car().unwrap().cdr().unwrap(), c: c, metac, new_bytecode: Some(Ins::Bad("cdr")) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("quote", Rc::new(Form::PrimComb("quote".to_owned(), |_e, p, c, metac| { - Cursor { f: p.car().unwrap(), c: c, metac: metac, new_bytecode: Some(Ins::Bad("quote - emit as const?")) } - }))), - - //("debug", Rc::new(Form::PrimComb("debug".to_owned(), |e, p, c| { - ////println!("Debug: {:?}", eval(Rc::clone(&e), p.car().unwrap())); - //println!("Debug: {}", eval(Rc::clone(&e), p.car().unwrap())); - ////PossibleTailCall::TailCall(e, p.cdr().unwrap().car().unwrap()) - //Cursor { f: p.cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: c }, new_bytecode: None }; - //}))), - ("assert", Rc::new(Form::PrimComb("assert".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: 1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - let thing = ps.car().unwrap(); - if !thing.truthy() { - println!("Assert failed: {:?}", thing); - } - assert!(thing.truthy()); - Cursor { f: ps.cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) }, metac, new_bytecode: Some(Ins::Bad("assert")) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - - ("+", Rc::new(Form::PrimComb("+".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() + ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Add)} - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("-", Rc::new(Form::PrimComb("-".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() - ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Sub)} - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("*", Rc::new(Form::PrimComb("*".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() * ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Mul)} - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("/", Rc::new(Form::PrimComb("/".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() / ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Div)} - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("%", Rc::new(Form::PrimComb("%".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() % ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Mod)} - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("&", Rc::new(Form::PrimComb("&".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() & ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::And) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("|", Rc::new(Form::PrimComb("|".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() | ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Or) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("^", Rc::new(Form::PrimComb("^".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Int(ps.car().unwrap().int().unwrap() ^ ps.cdr().unwrap().car().unwrap().int().unwrap())), c, metac, new_bytecode: Some(Ins::Xor) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - - ("comb?", Rc::new(Form::PrimComb("comb?".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { - Form::PrimComb(_n, _f) => true, - Form::DeriComb { .. } => true, - _ => false, - })), c, metac, new_bytecode: Some(Ins::CombP) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("cell?", Rc::new(Form::PrimComb("cell?".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { - Form::Cell(_c) => true, - _ => false, - })), c, metac, new_bytecode: Some(Ins::CellP) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("pair?", Rc::new(Form::PrimComb("pair?".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { - Form::Pair(_a,_b) => true, - _ => false, - })), c, metac, new_bytecode: Some(Ins::PairP) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("symbol?", Rc::new(Form::PrimComb("symbol?".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { - Form::Symbol(_) => true, - _ => false, - })), c, metac, new_bytecode: Some(Ins::SymbolP) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("int?", Rc::new(Form::PrimComb("int?".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { - Form::Int(_) => true, - _ => false, - })), c, metac, new_bytecode: Some(Ins::IntP) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("bool?", Rc::new(Form::PrimComb("bool?".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() { - Form::Bool(_) => true, - _ => false, - })), c, metac, new_bytecode: Some(Ins::BoolP) } - }, - nc: Box::new(c), - } - , metac, new_bytecode: None } - }))), - ("nil?", Rc::new(Form::PrimComb("nil?".to_owned(), |e, p, c, metac| { - Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: -1, to_eval: p, collected: None, e, pf: |e, ps, c, metac| { - Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().is_nil())), c, metac, new_bytecode: None } - }, - nc: Box::new(c), - } - , metac, new_bytecode: Some(Ins::NilP) } - }))), - - // consts - ("true", Rc::new(Form::Bool(true))), - ("false", Rc::new(Form::Bool(false))), - ("nil", Rc::new(Form::Nil)), + ("true", Rc::new(Form::Bool(true))), + ("false", Rc::new(Form::Bool(false))), + ("nil", Rc::new(Form::Nil)), ]) } diff --git a/kv/src/main.rs b/kv/src/main.rs index f803d16..a4e960d 100644 --- a/kv/src/main.rs +++ b/kv/src/main.rs @@ -5,6 +5,8 @@ use std::rc::Rc; mod ast; use crate::ast::{eval,root_env}; +//mod opt; +//use crate::opt::opt_eval; mod test; fn main() { @@ -13,7 +15,11 @@ fn main() { let input = "(= (+ 1 3) (* 2 2))"; let parsed_input = Rc::new(grammar::TermParser::new().parse(input).unwrap()); //println!("Parsed input is {} - {:?}", parsed_input, parsed_input); - let result = eval(root_env(), parsed_input); + let root = root_env(); + let result = eval(Rc::clone(&root), Rc::clone(&parsed_input)); + //let opt_result = opt_eval(root, parsed_input); println!("Result is {} - {:?}", result, result); + //println!("Opt Result is {} - {:?}", opt_result, opt_result); + //assert!(opt_result.congruent(result)); }