diff --git a/kv/src/ast.rs b/kv/src/ast.rs index 5fe6c9f..32d1d53 100644 --- a/kv/src/ast.rs +++ b/kv/src/ast.rs @@ -28,7 +28,7 @@ pub enum Form { Symbol(String), Cell(RefCell>), Pair(Rc
,Rc), - PrimComb(String, fn(Rc, Rc) -> PossibleTailCall), + PrimComb(String, fn(Rc, Rc, Cont) -> Cursor), DeriComb { se: Rc, de: Option, params: String, body: Rc }, } @@ -108,57 +108,59 @@ impl fmt::Display for Form { } } -struct Cursor { e: Rc, f: Rc, next: Cont } -enum Cont { +pub struct Cursor { f: Rc, c: Cont } +pub enum Cont { ID, - Call { p: Rc, ne: Rc, nc: Box }, + Eval { e: Rc, nc: Box }, + Call { p: Rc, e: Rc, nc: Box }, + //MidPrim { f: fn(Rc, Rc, Cont) -> Cursor, cd: Rc, nc: Box }, } - pub fn eval(e: Rc, f: Rc) -> Rc { - let mut cursor = Cursor { e, f, next: Cont::ID }; + let mut cursor = Cursor { f, c: Cont::Eval { e, nc: Box::new(Cont::ID) } }; loop { - let Cursor { e, f, mut next } = cursor; - if let Form::Pair(ref c, ref p) = *f { - cursor = Cursor { e: Rc::clone(&e), f: Rc::clone(c), next: Cont::Call { p: Rc::clone(p), ne: e, nc: Box::new(next) } } - } else { - let mut v = if let Form::Symbol(ref s) = *f { - let mut t = Rc::clone(&e); - while s != t.car().unwrap().car().unwrap().sym().unwrap() { - t = t.cdr().unwrap(); - } - t.car().unwrap().cdr().unwrap() - } else { - f - }; - // inner nc popping loop with mutable v? - loop { - match next { - Cont::ID => return v, - Cont::Call { p, ne, nc } => { - match *v { - Form::PrimComb(ref _n, ref f) => match f(ne, p) { - PossibleTailCall::Result(r) => { - v = r; - next = *nc; - }, - PossibleTailCall::TailCall(ne, nf) => { - cursor = Cursor { e: ne, f: nf, next: *nc }; - break; - }, - }, - 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(&ne), new_e); - } - new_e = assoc(params, p, new_e); - cursor = Cursor { e: new_e, f: Rc::clone(body), next: *nc }; - break; - }, - _ => panic!("Tried to call not a Prim/DeriComb {:?}", v), + let Cursor { f, c } = cursor; + match c { + Cont::ID => return f, + 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 }) } } + }, + Form::Symbol(ref s) => { + let mut t = Rc::clone(&e); + while s != t.car().unwrap().car().unwrap().sym().unwrap() { + t = t.cdr().unwrap(); } - } + cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc }; + }, + _ => { + cursor = Cursor { f: f, c: *nc }; + }, + } + }, + Cont::Call { p, e, nc } => { + match *f { + Form::PrimComb(ref _n, ref f) => { + cursor = f(e, p, *nc); + }, + //Form::PrimComb(ref _n, ref f) => match f(e, p) { + //PossibleTailCall::Result(r) => { + //cursor = Cursor { f: r, c: *nc }; + //}, + //PossibleTailCall::TailCall(ne, nf) => { + //cursor = Cursor { f: nf, c: Cont::Eval { e: ne, nc: nc } }; + //}, + //}, + 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 } }; + }, + _ => panic!("Tried to call not a Prim/DeriComb {:?}", f), } } } @@ -182,182 +184,209 @@ fn assoc_vec(kvs: Vec<(&str, Rc)>) -> Rc { pub fn root_env() -> Rc { assoc_vec(vec![ - ("eval", Rc::new(Form::PrimComb("eval".to_owned(), |e, p| { + ("eval", Rc::new(Form::PrimComb("eval".to_owned(), |e, p, c| { let b = eval(Rc::clone(&e), p.car().unwrap()); let e = eval(e, p.cdr().unwrap().car().unwrap()); - PossibleTailCall::TailCall(e, b) + + //PossibleTailCall::TailCall(e, b) + Cursor { f: b, c: Cont::Eval { e: e, nc: Box::new(c) } } }))), // (vau de params body) - ("vau", Rc::new(Form::PrimComb("vau".to_owned(), |e, p| { + ("vau", Rc::new(Form::PrimComb("vau".to_owned(), |e, p, c| { 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(); - PossibleTailCall::Result(Rc::new(Form::DeriComb { se: e, de, params, body })) + //PossibleTailCall::Result(Rc::new(Form::DeriComb { se: e, de, params, body })) + Cursor { f: Rc::new(Form::DeriComb { se: e, de, params, body }), c: c } }))), - ("=", Rc::new(Form::PrimComb("=".to_owned(), |e, p| { + ("=", Rc::new(Form::PrimComb("=".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()); let b = eval(e, p.cdr().unwrap().car().unwrap()); - PossibleTailCall::Result(Rc::new(Form::Bool(a == b))) + //PossibleTailCall::Result(Rc::new(Form::Bool(a == b))) + Cursor { f: Rc::new(Form::Bool(a == b)), c: c } }))), - ("<", Rc::new(Form::PrimComb("<".to_owned(), |e, p| { + ("<", Rc::new(Form::PrimComb("<".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()); let b = eval(e, p.cdr().unwrap().car().unwrap()); - PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() < b.int().unwrap()))) + //PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() < b.int().unwrap()))) + Cursor { f: Rc::new(Form::Bool(a.int().unwrap() < b.int().unwrap())), c: c } }))), - (">", Rc::new(Form::PrimComb(">".to_owned(), |e, p| { + (">", Rc::new(Form::PrimComb(">".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()); let b = eval(e, p.cdr().unwrap().car().unwrap()); - PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() > b.int().unwrap()))) + //PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() > b.int().unwrap()))) + Cursor { f: Rc::new(Form::Bool(a.int().unwrap() > b.int().unwrap())), c: c } }))), - ("<=", Rc::new(Form::PrimComb("<=".to_owned(), |e, p| { + ("<=", Rc::new(Form::PrimComb("<=".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()); let b = eval(e, p.cdr().unwrap().car().unwrap()); - PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() <= b.int().unwrap()))) + //PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() <= b.int().unwrap()))) + Cursor { f: Rc::new(Form::Bool(a.int().unwrap() <= b.int().unwrap())), c: c } }))), - (">=", Rc::new(Form::PrimComb(">=".to_owned(), |e, p| { + (">=", Rc::new(Form::PrimComb(">=".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()); let b = eval(e, p.cdr().unwrap().car().unwrap()); - PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() >= b.int().unwrap()))) + //PossibleTailCall::Result(Rc::new(Form::Bool(a.int().unwrap() >= b.int().unwrap()))) + Cursor { f: Rc::new(Form::Bool(a.int().unwrap() >= b.int().unwrap())), c: c } }))), - ("if", Rc::new(Form::PrimComb("if".to_owned(), |e, p| { + ("if", Rc::new(Form::PrimComb("if".to_owned(), |e, p, c| { if eval(Rc::clone(&e), p.car().unwrap()).truthy() { - PossibleTailCall::TailCall(e, p.cdr().unwrap().car().unwrap()) + Cursor { f: p.cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) } } + //PossibleTailCall::TailCall(e, p.cdr().unwrap().car().unwrap()) } else { - PossibleTailCall::TailCall(e, p.cdr().unwrap().cdr().unwrap().car().unwrap()) + Cursor { f: p.cdr().unwrap().cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) } } + //PossibleTailCall::TailCall(e, p.cdr().unwrap().cdr().unwrap().car().unwrap()) } }))), - ("cell", Rc::new(Form::PrimComb("cell".to_owned(), |e, p| { + ("cell", Rc::new(Form::PrimComb("cell".to_owned(), |e, p, c| { let x = eval(Rc::clone(&e), p.car().unwrap()); - PossibleTailCall::Result(Rc::new(Form::Cell(RefCell::new(x)))) + //PossibleTailCall::Result(Rc::new(Form::Cell(RefCell::new(x)))) + Cursor { f: Rc::new(Form::Cell(RefCell::new(x))), c: c } }))), - ("set", Rc::new(Form::PrimComb("set".to_owned(), |e, p| { + ("set", Rc::new(Form::PrimComb("set".to_owned(), |e, p, c| { match &*eval(Rc::clone(&e), p.car().unwrap()) { - Form::Cell(c) => PossibleTailCall::Result(c.replace(eval(Rc::clone(&e), p.cdr().unwrap().car().unwrap()))), + //Form::Cell(c) => PossibleTailCall::Result(c.replace(eval(Rc::clone(&e), p.cdr().unwrap().car().unwrap()))), + Form::Cell(cell) => Cursor { f: cell.replace(eval(Rc::clone(&e), p.cdr().unwrap().car().unwrap())), c: c }, _ => panic!("set on not cell"), } }))), - ("get", Rc::new(Form::PrimComb("get".to_owned(), |e, p| { + ("get", Rc::new(Form::PrimComb("get".to_owned(), |e, p, c| { match &*eval(Rc::clone(&e), p.car().unwrap()) { - Form::Cell(c) => PossibleTailCall::Result(Rc::clone(&c.borrow())), + //Form::Cell(c) => PossibleTailCall::Result(Rc::clone(&c.borrow())), + Form::Cell(cell) => Cursor { f: Rc::clone(&cell.borrow()), c: c }, _ => panic!("get on not cell"), } }))), - ("cons", Rc::new(Form::PrimComb("cons".to_owned(), |e, p| { + ("cons", Rc::new(Form::PrimComb("cons".to_owned(), |e, p, c| { let h = eval(Rc::clone(&e), p.car().unwrap()); let t = eval(e, p.cdr().unwrap().car().unwrap()); - PossibleTailCall::Result(Rc::new(Form::Pair(h, t))) + //PossibleTailCall::Result(Rc::new(Form::Pair(h, t))) + Cursor { f: Rc::new(Form::Pair(h, t)), c: c } }))), - ("car", Rc::new(Form::PrimComb("car".to_owned(), |e, p| { - PossibleTailCall::Result(eval(Rc::clone(&e), p.car().unwrap()).car().unwrap()) + ("car", Rc::new(Form::PrimComb("car".to_owned(), |e, p, c| { + //PossibleTailCall::Result(eval(Rc::clone(&e), p.car().unwrap()).car().unwrap()) + Cursor { f: eval(Rc::clone(&e), p.car().unwrap()).car().unwrap(), c: c } }))), - ("cdr", Rc::new(Form::PrimComb("cdr".to_owned(), |e, p| { - PossibleTailCall::Result(eval(Rc::clone(&e), p.car().unwrap()).cdr().unwrap()) + ("cdr", Rc::new(Form::PrimComb("cdr".to_owned(), |e, p, c| { + //PossibleTailCall::Result(eval(Rc::clone(&e), p.car().unwrap()).cdr().unwrap()) + Cursor { f: eval(Rc::clone(&e), p.car().unwrap()).cdr().unwrap(), c: c } }))), - ("quote", Rc::new(Form::PrimComb("quote".to_owned(), |_e, p| { - PossibleTailCall::Result(p.car().unwrap()) + ("quote", Rc::new(Form::PrimComb("quote".to_owned(), |_e, p, c| { + //PossibleTailCall::Result(p.car().unwrap()) + Cursor { f: p.car().unwrap(), c: c } }))), - ("debug", Rc::new(Form::PrimComb("debug".to_owned(), |e, p| { - //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()) - }))), - ("assert", Rc::new(Form::PrimComb("assert".to_owned(), |e, p| { + //("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 } }; + //}))), + ("assert", Rc::new(Form::PrimComb("assert".to_owned(), |e, p, c| { let thing = eval(Rc::clone(&e), p.car().unwrap()); if !thing.truthy() { println!("Assert failed: {:?}", thing); } assert!(thing.truthy()); - PossibleTailCall::TailCall(e, p.cdr().unwrap().car().unwrap()) + //PossibleTailCall::TailCall(e, p.cdr().unwrap().car().unwrap()) + Cursor { f: p.cdr().unwrap().car().unwrap(), c: Cont::Eval { e: e, nc: Box::new(c) } } }))), - ("+", Rc::new(Form::PrimComb("+".to_owned(), |e, p| { + ("+", Rc::new(Form::PrimComb("+".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a + b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a + b))) + Cursor { f: Rc::new(Form::Int(a + b)), c: c } }))), - ("-", Rc::new(Form::PrimComb("-".to_owned(), |e, p| { + ("-", Rc::new(Form::PrimComb("-".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a - b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a - b))) + Cursor { f: Rc::new(Form::Int(a - b)), c: c } }))), - ("*", Rc::new(Form::PrimComb("*".to_owned(), |e, p| { + ("*", Rc::new(Form::PrimComb("*".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a * b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a * b))) + Cursor { f: Rc::new(Form::Int(a * b)), c: c } }))), - ("/", Rc::new(Form::PrimComb("/".to_owned(), |e, p| { + ("/", Rc::new(Form::PrimComb("/".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a / b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a / b))) + Cursor { f: Rc::new(Form::Int(a / b)), c: c } }))), - ("%", Rc::new(Form::PrimComb("%".to_owned(), |e, p| { + ("%", Rc::new(Form::PrimComb("%".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a % b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a % b))) + Cursor { f: Rc::new(Form::Int(a % b)), c: c } }))), - ("&", Rc::new(Form::PrimComb("&".to_owned(), |e, p| { + ("&", Rc::new(Form::PrimComb("&".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a & b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a & b))) + Cursor { f: Rc::new(Form::Int(a & b)), c: c } }))), - ("|", Rc::new(Form::PrimComb("|".to_owned(), |e, p| { + ("|", Rc::new(Form::PrimComb("|".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a | b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a | b))) + Cursor { f: Rc::new(Form::Int(a | b)), c: c } }))), - ("^", Rc::new(Form::PrimComb("^".to_owned(), |e, p| { + ("^", Rc::new(Form::PrimComb("^".to_owned(), |e, p, c| { let a = eval(Rc::clone(&e), p.car().unwrap()).int().unwrap(); let b = eval(e, p.cdr().unwrap().car().unwrap()).int().unwrap(); - PossibleTailCall::Result(Rc::new(Form::Int(a ^ b))) + //PossibleTailCall::Result(Rc::new(Form::Int(a ^ b))) + Cursor { f: Rc::new(Form::Int(a ^ b)), c: c } }))), - ("comb?", Rc::new(Form::PrimComb("comb?".to_owned(), |e, p| { - PossibleTailCall::Result(Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { + ("comb?", Rc::new(Form::PrimComb("comb?".to_owned(), |e, p, c| { + Cursor { f: Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { Form::PrimComb(_n, _f) => true, Form::DeriComb { .. } => true, _ => false, - }))) + })), c: c } }))), - ("cell?", Rc::new(Form::PrimComb("cell?".to_owned(), |e, p| { - PossibleTailCall::Result(Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { + ("cell?", Rc::new(Form::PrimComb("cell?".to_owned(), |e, p, c| { + Cursor { f: Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { Form::Cell(_c) => true, _ => false, - }))) + })), c: c } }))), - ("pair?", Rc::new(Form::PrimComb("pair?".to_owned(), |e, p| { - PossibleTailCall::Result(Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { + ("pair?", Rc::new(Form::PrimComb("pair?".to_owned(), |e, p, c| { + Cursor { f: Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { Form::Pair(_a,_b) => true, _ => false, - }))) + })), c: c } }))), - ("symbol?", Rc::new(Form::PrimComb("symbol?".to_owned(), |e, p| { - PossibleTailCall::Result(Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { + ("symbol?", Rc::new(Form::PrimComb("symbol?".to_owned(), |e, p, c| { + Cursor { f: Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { Form::Symbol(_) => true, _ => false, - }))) + })), c: c } }))), - ("int?", Rc::new(Form::PrimComb("int?".to_owned(), |e, p| { - PossibleTailCall::Result(Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { + ("int?", Rc::new(Form::PrimComb("int?".to_owned(), |e, p, c| { + Cursor { f: Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { Form::Int(_) => true, _ => false, - }))) + })), c: c } }))), // maybe bool? but also could be derived. Nil def - ("bool?", Rc::new(Form::PrimComb("bool?".to_owned(), |e, p| { - PossibleTailCall::Result(Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { + ("bool?", Rc::new(Form::PrimComb("bool?".to_owned(), |e, p, c| { + Cursor { f: Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { Form::Bool(_) => true, _ => false, - }))) + })), c: c } }))), - ("nil?", Rc::new(Form::PrimComb("nil?".to_owned(), |e, p| { - PossibleTailCall::Result(Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { + ("nil?", Rc::new(Form::PrimComb("nil?".to_owned(), |e, p, c| { + Cursor { f: Rc::new(Form::Bool(match &*eval(e, p.car().unwrap()) { Form::Nil => true, _ => false, - }))) + })), c: c } }))), // consts