Reorganize naming scheme
This commit is contained in:
31
ki/src/grammar.lalrpop
Normal file
31
ki/src/grammar.lalrpop
Normal file
@@ -0,0 +1,31 @@
|
||||
use std::str::FromStr;
|
||||
use std::rc::Rc;
|
||||
use ki::Form;
|
||||
|
||||
grammar;
|
||||
|
||||
pub Term: Rc<Form> = {
|
||||
NUM => Rc::new(Form::Int(i32::from_str(<>).unwrap())),
|
||||
SYM => Rc::new(Form::Symbol(<>.to_owned())),
|
||||
"(" <ListInside?> ")" => <>.unwrap_or(Rc::new(Form::Nil)),
|
||||
"'" <Term> => Rc::new(Form::Pair(Rc::new(Form::Symbol("quote".to_owned())), Rc::new(Form::Pair(<>, Rc::new(Form::Nil))))),
|
||||
"!" <h: Term> <t: Term> => {
|
||||
h.append(t).unwrap()
|
||||
},
|
||||
};
|
||||
ListInside: Rc<Form> = {
|
||||
<Term> => Rc::new(Form::Pair(<>, Rc::new(Form::Nil))),
|
||||
<h: Term> <t: ListInside> => Rc::new(Form::Pair(h, t)),
|
||||
<a: Term> "." <d: Term> => Rc::new(Form::Pair(a, d)),
|
||||
}
|
||||
match {
|
||||
"(",
|
||||
")",
|
||||
".",
|
||||
"'",
|
||||
"!",
|
||||
r"[0-9]+" => NUM,
|
||||
r"[a-zA-Z+*/_=?%&|^<>-][\w+*/=_?%&|^<>-]*" => SYM,
|
||||
r"(;[^\n]*\n)|\s+" => { }
|
||||
}
|
||||
|
||||
426
ki/src/lib.rs
Normal file
426
ki/src/lib.rs
Normal file
@@ -0,0 +1,426 @@
|
||||
use std::fmt;
|
||||
use std::boxed::Box;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::From;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Form {
|
||||
Nil,
|
||||
Int(i32),
|
||||
Bool(bool),
|
||||
Symbol(String),
|
||||
Cell(RefCell<Rc<Form>>),
|
||||
Pair(Rc<Form>,Rc<Form>),
|
||||
PrimComb { eval_limit: i32, ins: PrimCombI },
|
||||
DeriComb { se: Rc<Form>, de: Option<String>, params: String, body: Rc<Form> },
|
||||
ContComb(Cont),
|
||||
}
|
||||
|
||||
// todo, strings not symbols?
|
||||
impl From<String> 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<i32> for Form { fn from(item: i32) -> Self { Form::Int(item) } }
|
||||
impl From<bool> for Form { fn from(item: bool) -> Self { Form::Bool(item) } }
|
||||
impl<A: Into<Form>, B: Into<Form>> From<(A, B)> for Form {
|
||||
fn from(item: (A, B)) -> Self {
|
||||
Form::Pair(Rc::new(item.0.into()), Rc::new(item.1.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Form {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Form::Nil => write!(f, "nil"),
|
||||
Form::Int(i) => write!(f, "{i}"),
|
||||
Form::Bool(b) => write!(f, "{b}"),
|
||||
Form::Symbol(s) => write!(f, "{s}"),
|
||||
Form::Cell(c) => write!(f, "@{}", c.borrow()),
|
||||
Form::Pair(car, cdr) => {
|
||||
write!(f, "({}", car)?;
|
||||
let mut traverse: Rc<Form> = Rc::clone(cdr);
|
||||
loop {
|
||||
match &*traverse {
|
||||
Form::Pair(ref carp, ref cdrp) => {
|
||||
write!(f, " {}", carp)?;
|
||||
traverse = Rc::clone(cdrp);
|
||||
},
|
||||
Form::Nil => {
|
||||
write!(f, ")")?;
|
||||
return Ok(());
|
||||
},
|
||||
x => {
|
||||
write!(f, ". {x})")?;
|
||||
return Ok(());
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
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)
|
||||
},
|
||||
Form::ContComb(_) => write!(f, "<cont>"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Form {
|
||||
fn truthy(&self) -> bool {
|
||||
match self {
|
||||
Form::Bool(b) => *b,
|
||||
Form::Nil => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
fn int(&self) -> Option<i32> {
|
||||
match self {
|
||||
Form::Int(i) => Some(*i),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn append(&self, x: Rc<Form>) -> Option<Rc<Form>> {
|
||||
match self {
|
||||
Form::Pair(car, cdr) => cdr.append(x).map(|x| Rc::new(Form::Pair(Rc::clone(car), x))),
|
||||
Form::Nil => Some(Rc::new(Form::Pair(x, Rc::new(Form::Nil)))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn sym(&self) -> Option<&str> {
|
||||
match self {
|
||||
Form::Symbol(s) => Some(s),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn pair(&self) -> Option<(Rc<Form>,Rc<Form>)> {
|
||||
match self {
|
||||
Form::Pair(car, cdr) => Some((Rc::clone(car),Rc::clone(cdr))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn car(&self) -> Option<Rc<Form>> {
|
||||
match self {
|
||||
Form::Pair(car, _cdr) => Some(Rc::clone(car)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn cdr(&self) -> Option<Rc<Form>> {
|
||||
match self {
|
||||
Form::Pair(_car, cdr) => Some(Rc::clone(cdr)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn is_nil(&self) -> bool {
|
||||
match self {
|
||||
Form::Nil => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
fn call(&self, p: Rc<Self>, e: Rc<Self>, nc: Box<Cont>, metac: Cont) -> Cursor {
|
||||
match self {
|
||||
Form::PrimComb{eval_limit, ins} => {
|
||||
Cursor { f: Rc::new(Form::Nil), c: Cont::PramEval { eval_limit: *eval_limit, to_eval: p, collected: None, e, ins: *ins, nc: nc }, metac }
|
||||
}
|
||||
Form::DeriComb {se, de, params, 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(¶ms, p, new_e);
|
||||
Cursor { f: Rc::clone(body), c: Cont::Eval { e: new_e, nc: nc }, metac }
|
||||
}
|
||||
Form::ContComb(c) => {
|
||||
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 {:?}", self, nc);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn impl_prim(ins: PrimCombI, e: Rc<Self>, ps: Vec<Rc<Self>>, c: Cont, metac: Cont) -> Cursor {
|
||||
match ins {
|
||||
PrimCombI::Eval => Cursor { f: Rc::clone(&ps[0]), c: Cont::Eval { e: Rc::clone(&ps[1]), nc: Box::new(c) }, metac },
|
||||
PrimCombI::Vau => {
|
||||
let de = ps[0].sym().map(|s| s.to_owned());
|
||||
let params = ps[1].sym().unwrap().to_owned();
|
||||
let body = Rc::clone(&ps[2]);
|
||||
|
||||
Cursor { f: Rc::new(Form::DeriComb { se: e, de, params, body }), c, metac }
|
||||
},
|
||||
PrimCombI::If => if ps[0].truthy() {
|
||||
Cursor { f: Rc::clone(&ps[1]), c: Cont::Eval { e: e, nc: Box::new(c) }, metac }
|
||||
} else {
|
||||
Cursor { f: Rc::clone(&ps[2]), c: Cont::Eval { e: e, nc: Box::new(c) }, metac }
|
||||
},
|
||||
|
||||
PrimCombI::Reset => Cursor { f: Rc::clone(&ps[0]),
|
||||
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: Rc::clone(&ps[0]),
|
||||
c: Cont::Call { p: Rc::new(Form::Pair(Rc::new(Form::ContComb(c)),
|
||||
Rc::new(Form::Nil))),
|
||||
e: e,
|
||||
nc: Box::new(Cont::MetaRet) },
|
||||
// I think this is unnecessary and can just be "metac },"
|
||||
// because reset puts this metac in
|
||||
metac: Cont::CatchRet { nc: Box::new(metac.clone()), restore_meta: Box::new(metac) } },
|
||||
PrimCombI::Assert => {
|
||||
let thing = Rc::clone(&ps[0]);
|
||||
if !thing.truthy() {
|
||||
println!("Assert failed: {:?}", thing);
|
||||
}
|
||||
assert!(thing.truthy());
|
||||
Cursor { f: Rc::clone(&ps[1]), c: Cont::Eval { e: e, nc: Box::new(c) }, metac }
|
||||
},
|
||||
|
||||
PrimCombI::Cell => Cursor { f: Rc::new(Form::Cell(RefCell::new(Rc::clone(&ps[0])))), c, metac },
|
||||
PrimCombI::Set => match &*ps[0] {
|
||||
Form::Cell(cell) => Cursor { f: cell.replace(Rc::clone(&ps[1])), c, metac },
|
||||
_ => panic!("set on not cell"),
|
||||
},
|
||||
PrimCombI::Get => match &*ps[0] {
|
||||
Form::Cell(cell) => Cursor { f: Rc::clone(&cell.borrow()), c, metac },
|
||||
_ => panic!("get on not cell"),
|
||||
},
|
||||
|
||||
PrimCombI::Cons => Cursor { f: Rc::new(Form::Pair(Rc::clone(&ps[0]), Rc::clone(&ps[1]))), c, metac },
|
||||
PrimCombI::Car => Cursor { f: ps[0].car().unwrap(), c, metac },
|
||||
PrimCombI::Cdr => Cursor { f: ps[0].cdr().unwrap(), c, metac },
|
||||
PrimCombI::Quote => Cursor { f: Rc::clone(&ps[0]), c, metac },
|
||||
|
||||
PrimCombI::Eq => Cursor { f: Rc::new(Form::Bool(ps[0] == ps[1])), c, metac },
|
||||
PrimCombI::Lt => Cursor { f: Rc::new(Form::Bool(ps[0].int().unwrap() < ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::LEq => Cursor { f: Rc::new(Form::Bool(ps[0].int().unwrap() <= ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::Gt => Cursor { f: Rc::new(Form::Bool(ps[0].int().unwrap() > ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::GEq => Cursor { f: Rc::new(Form::Bool(ps[0].int().unwrap() >= ps[1].int().unwrap())), c, metac },
|
||||
|
||||
PrimCombI::Plus => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() + ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::Minus => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() - ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::Mult => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() * ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::Div => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() / ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::Mod => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() % ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::And => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() & ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::Or => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() | ps[1].int().unwrap())), c, metac },
|
||||
PrimCombI::Xor => Cursor { f: Rc::new(Form::Int(ps[0].int().unwrap() ^ ps[1].int().unwrap())), c, metac },
|
||||
|
||||
PrimCombI::CombP => Cursor { f: Rc::new(Form::Bool(match &*ps[0] {
|
||||
Form::PrimComb { .. } => true,
|
||||
Form::DeriComb { .. } => true,
|
||||
_ => false,
|
||||
})), c, metac },
|
||||
PrimCombI::CellP => Cursor { f: Rc::new(Form::Bool(match &*ps[0] {
|
||||
Form::Cell(_c) => true,
|
||||
_ => false,
|
||||
})), c, metac },
|
||||
PrimCombI::PairP => Cursor { f: Rc::new(Form::Bool(match &*ps[0] {
|
||||
Form::Pair(_a,_b) => true,
|
||||
_ => false,
|
||||
})), c, metac },
|
||||
PrimCombI::SymbolP => Cursor { f: Rc::new(Form::Bool(match &*ps[0] {
|
||||
Form::Symbol(_) => true,
|
||||
_ => false,
|
||||
})), c, metac },
|
||||
PrimCombI::IntP => Cursor { f: Rc::new(Form::Bool(match &*ps[0] {
|
||||
Form::Int(_) => true,
|
||||
_ => false,
|
||||
})), c, metac },
|
||||
PrimCombI::BoolP => Cursor { f: Rc::new(Form::Bool(match &*ps[0] {
|
||||
Form::Bool(_) => true,
|
||||
_ => false,
|
||||
})), c, metac },
|
||||
PrimCombI::NilP => Cursor { f: Rc::new(Form::Bool(ps[0].is_nil())), c, metac },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn assoc(k: &str, v: Rc<Form>, l: Rc<Form>) -> Rc<Form> {
|
||||
Rc::new(Form::Pair(
|
||||
Rc::new(Form::Pair(
|
||||
Rc::new(Form::Symbol(k.to_owned())),
|
||||
v)),
|
||||
l))
|
||||
}
|
||||
pub fn assoc_vec(kvs: Vec<(&str, Rc<Form>)>) -> Rc<Form> {
|
||||
let mut to_ret = Rc::new(Form::Nil);
|
||||
for (k, v) in kvs {
|
||||
to_ret = assoc(k, v, to_ret);
|
||||
}
|
||||
to_ret
|
||||
}
|
||||
|
||||
// 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<Form> {
|
||||
assoc_vec(vec![
|
||||
("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 { eval_limit: 0, ins: PrimCombI::Reset })),
|
||||
("shift", Rc::new(Form::PrimComb { eval_limit: -1, ins: PrimCombI::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 })),
|
||||
|
||||
("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 })),
|
||||
|
||||
("=", 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 })),
|
||||
|
||||
("+", 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 })),
|
||||
|
||||
("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 })),
|
||||
|
||||
("true", Rc::new(Form::Bool(true))),
|
||||
("false", Rc::new(Form::Bool(false))),
|
||||
("nil", Rc::new(Form::Nil)),
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Cont {
|
||||
Exit,
|
||||
MetaRet,
|
||||
CatchRet { nc: Box<Cont>, restore_meta: Box<Cont> },
|
||||
Eval { e: Rc<Form>, nc: Box<Cont> },
|
||||
Call { p: Rc<Form>, e: Rc<Form>, nc: Box<Cont> },
|
||||
PramEval { eval_limit: i32, to_eval: Rc<Form>, collected: Option<Vec<Rc<Form>>>, e: Rc<Form>, ins: PrimCombI, nc: Box<Cont> },
|
||||
}
|
||||
impl Clone for Cont {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Cont::Exit => Cont::Exit,
|
||||
Cont::MetaRet => Cont::MetaRet,
|
||||
Cont::CatchRet { nc, restore_meta } => Cont::CatchRet { nc: nc.clone(), restore_meta: restore_meta.clone() },
|
||||
Cont::Eval { e, nc } => Cont::Eval { e: Rc::clone(e), nc: nc.clone() },
|
||||
Cont::Call { p, e, nc } => Cont::Call { p: Rc::clone(p), e: Rc::clone(e), nc: nc.clone() },
|
||||
Cont::PramEval { eval_limit, to_eval, collected, e, ins, nc} => Cont::PramEval { eval_limit: *eval_limit, to_eval: Rc::clone(to_eval),
|
||||
collected: collected.as_ref().map(|x| x.iter().map(|x| Rc::clone(x)).collect()),
|
||||
e: Rc::clone(e), ins: ins.clone(), nc: nc.clone() },
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct Cursor { pub f: Rc<Form>, pub c: Cont, pub metac: Cont }
|
||||
|
||||
pub fn eval(e: Rc<Form>, f: Rc<Form>) -> Rc<Form> {
|
||||
let mut cursor = Cursor { f, c: Cont::Eval { e, nc: Box::new(Cont::MetaRet) }, metac: Cont::Exit };
|
||||
loop {
|
||||
let Cursor { f, c, metac } = cursor;
|
||||
match c {
|
||||
Cont::Exit => {
|
||||
return f;
|
||||
},
|
||||
Cont::MetaRet => {
|
||||
cursor = Cursor { f: f, c: metac.clone(), metac: metac };
|
||||
},
|
||||
Cont::CatchRet { nc, restore_meta } => {
|
||||
cursor = Cursor { f: f, c: *nc, metac: *restore_meta };
|
||||
},
|
||||
Cont::Eval { e, nc } => {
|
||||
if let Some((comb, p)) = f.pair() {
|
||||
cursor = Cursor { f: comb, c: Cont::Eval { e: Rc::clone(&e), nc: Box::new(Cont::Call { p, e, nc }) }, metac }
|
||||
} else if let Some(s) = f.sym() {
|
||||
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, metac };
|
||||
} else {
|
||||
cursor = Cursor { f: Rc::clone(&f), c: *nc, metac };
|
||||
}
|
||||
},
|
||||
Cont::PramEval { eval_limit, to_eval, collected, e, ins, nc } => {
|
||||
let mut next_collected = if let Some(mut collected) = collected {
|
||||
collected.push(f); collected
|
||||
} else { vec![] };
|
||||
if eval_limit == 0 || to_eval.is_nil() {
|
||||
let mut traverse = to_eval;
|
||||
while !traverse.is_nil() {
|
||||
next_collected.push(traverse.car().unwrap());
|
||||
traverse = traverse.cdr().unwrap();
|
||||
}
|
||||
cursor = Form::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, ins, nc }) }, metac };
|
||||
}
|
||||
},
|
||||
Cont::Call { p, e, nc } => {
|
||||
cursor = f.call(p, e, nc, metac);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
21
ki/src/main.rs
Normal file
21
ki/src/main.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
#[macro_use] extern crate lalrpop_util;
|
||||
lalrpop_mod!(pub grammar);
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use ki::{root_env,eval};
|
||||
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
fn main() {
|
||||
//let input = "(= 17 ((vau d p (+ (eval (car p) d) 13)) (+ 1 3)))";
|
||||
//let input = "(+ 1 3)";
|
||||
let input = "(= (+ 1 3) (* 2 2))";
|
||||
let parsed_input = grammar::TermParser::new().parse(input).unwrap();
|
||||
//println!("Parsed input is {} - {:?}", parsed_input, parsed_input);
|
||||
let root = root_env();
|
||||
let result = eval(Rc::clone(&root), Rc::clone(&parsed_input));
|
||||
println!("Result is {} - {:?}", result, result);
|
||||
}
|
||||
|
||||
572
ki/src/test.rs
Normal file
572
ki/src/test.rs
Normal file
@@ -0,0 +1,572 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::grammar;
|
||||
use ki::{root_env,Form,eval};
|
||||
|
||||
#[test]
|
||||
fn parse_test() {
|
||||
let g = grammar::TermParser::new();
|
||||
for test in [
|
||||
"22", "(22)", "(((22)))",
|
||||
"(22 )", "()", "( )", "( 44)", "(44 )",
|
||||
"(22 44 (1) 33 (4 5 (6) 6))", "hello",
|
||||
"-", "+", "(+ 1 ;hi
|
||||
3)", "'13", "hello-world", "_",
|
||||
] {
|
||||
assert!(g.parse(test).is_ok());
|
||||
}
|
||||
assert!(g.parse("((22)").is_err());
|
||||
}
|
||||
|
||||
fn eval_test<T: Into<Form>>(gram: &grammar::TermParser, e: &Rc<Form>, code: &str, expected: T) {
|
||||
println!("Doing test {}", code);
|
||||
let parsed = gram.parse(code).unwrap();
|
||||
let basic_result = eval(Rc::clone(e), Rc::clone(&parsed));
|
||||
assert_eq!(*basic_result, expected.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, "(+ 2 (car (cons 4 '(1 2))))", 6);
|
||||
eval_test(&g, &e, "(= 17 ((vau d p (+ (eval (car p) d) 13)) (+ 1 3)))", true);
|
||||
eval_test(&g, &e, "(if (= 2 2) (+ 1 2) (+ 3 4))", 3);
|
||||
eval_test(&g, &e, "(quote a)", "a");
|
||||
eval_test(&g, &e, "'a", "a");
|
||||
eval_test(&g, &e, "'(1 . a)", (1, "a"));
|
||||
eval_test(&g, &e, "'(1 a)", (1, ("a", Form::Nil)));
|
||||
eval_test(&g, &e, "true", true);
|
||||
eval_test(&g, &e, "false", false);
|
||||
eval_test(&g, &e, "nil", Form::Nil);
|
||||
|
||||
eval_test(&g, &e, "(+ 1 2)", 3);
|
||||
eval_test(&g, &e, "(- 1 2)", -1);
|
||||
eval_test(&g, &e, "(* 1 2)", 2);
|
||||
eval_test(&g, &e, "(/ 4 2)", 2);
|
||||
eval_test(&g, &e, "(% 3 2)", 1);
|
||||
eval_test(&g, &e, "(& 3 2)", 2);
|
||||
eval_test(&g, &e, "(| 2 1)", 3);
|
||||
eval_test(&g, &e, "(^ 2 1)", 3);
|
||||
eval_test(&g, &e, "(^ 3 1)", 2);
|
||||
|
||||
eval_test(&g, &e, "(< 3 1)", false);
|
||||
eval_test(&g, &e, "(<= 3 1)", false);
|
||||
eval_test(&g, &e, "(> 3 1)", true);
|
||||
eval_test(&g, &e, "(>= 3 1)", true);
|
||||
|
||||
eval_test(&g, &e, "(comb? +)", true);
|
||||
eval_test(&g, &e, "(comb? (vau d p 1))", true);
|
||||
eval_test(&g, &e, "(comb? 1)", false);
|
||||
eval_test(&g, &e, "(pair? '(a))", true);
|
||||
//eval_test(&g, &e, "(pair? '())", true);
|
||||
eval_test(&g, &e, "(nil? nil)", true);
|
||||
eval_test(&g, &e, "(nil? 1)", false);
|
||||
eval_test(&g, &e, "(pair? 1)", false);
|
||||
eval_test(&g, &e, "(symbol? 'a)", true);
|
||||
eval_test(&g, &e, "(symbol? 1)", false);
|
||||
eval_test(&g, &e, "(int? 1)", true);
|
||||
eval_test(&g, &e, "(int? true)", false);
|
||||
eval_test(&g, &e, "(bool? true)", true);
|
||||
eval_test(&g, &e, "(bool? 1)", false);
|
||||
|
||||
eval_test(&g, &e, "!(bool?) 1", false);
|
||||
eval_test(&g, &e, "!(bool?) true", true);
|
||||
|
||||
eval_test(&g, &e, "((vau root_env _ (eval 'a (cons (cons 'a 2) root_env))))", 2);
|
||||
eval_test(&g, &e, "'name-dash", "name-dash");
|
||||
|
||||
eval_test(&g, &e, "(get (cell 1))", 1);
|
||||
eval_test(&g, &e, "(set (cell 1) 2)", 1);
|
||||
eval_test(&g, &e, "(cell? (cell 1))", true);
|
||||
eval_test(&g, &e, "((vau de p (eval (quote (cons (set a 2) (get a))) (cons (cons (quote a) (cell 1)) de))))", (1,2));
|
||||
|
||||
eval_test(&g, &e, "(reset 1)", 1);
|
||||
eval_test(&g, &e, "(reset (+ 1 2))", 3);
|
||||
eval_test(&g, &e, "(reset (+ 1 (shift (vau de p 2))))", 2);
|
||||
eval_test(&g, &e, "(reset (+ 1 (shift (vau de p ((car p) 2)))))", 3);
|
||||
eval_test(&g, &e, "(reset (+ 1 (shift (vau de p ((car p) ((car p) 2)) ))))", 4);
|
||||
eval_test(&g, &e, "(reset (+ 1 (shift (vau de p (+ ((car p) 3) ((car p) ((car p) 2))) ))))", 8);
|
||||
eval_test(&g, &e, "((reset (+ 1 (shift (vau de p (car p))))) 2)", 3);
|
||||
}
|
||||
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
static LET: Lazy<String> = Lazy::new(|| {
|
||||
"!((vau root_env p (eval (car p)
|
||||
(cons (cons 'let1
|
||||
(vau de p (eval (car (cdr (cdr p))) (cons (cons (car p) (eval (car (cdr p)) de)) de)))
|
||||
) root_env))))".to_owned()
|
||||
});
|
||||
|
||||
|
||||
#[test]
|
||||
fn fib_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (let1 x 10 (+ x 7))", *LET), 17);
|
||||
let def_fib = "
|
||||
!(let1 fib (vau de p
|
||||
!(let1 self (eval (car p) de))
|
||||
!(let1 n (eval (car (cdr p)) de))
|
||||
!(if (= 0 n) 0)
|
||||
!(if (= 1 n) 1)
|
||||
(+ (self self (- n 1)) (self self (- n 2)))
|
||||
))";
|
||||
eval_test(&g, &e, &format!("{} {} (fib fib 6)", *LET, def_fib), 8);
|
||||
}
|
||||
#[test]
|
||||
fn fact_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
let def_fact = "
|
||||
!(let1 fact (vau de p
|
||||
!(let1 self (eval (car p) de))
|
||||
!(let1 n (eval (car (cdr p)) de))
|
||||
!(if (= 0 n) 1)
|
||||
(* n (self self (- n 1)))
|
||||
))";
|
||||
eval_test(&g, &e, &format!("{} {} (fact fact 6)", *LET, def_fact), 720);
|
||||
}
|
||||
static VAPPLY: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 vapply (vau de p
|
||||
!(let1 f (eval (car p) de))
|
||||
!(let1 ip (eval (car (cdr p)) de))
|
||||
!(let1 nde (eval (car (cdr (cdr p))) de))
|
||||
(eval (cons f ip) nde)
|
||||
))", *LET)
|
||||
});
|
||||
#[test]
|
||||
fn vapply_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
// need the vapply to keep env in check because otherwise the env keeps growing
|
||||
// and the Rc::drop will overflow the stack lol
|
||||
let def_badid = format!("
|
||||
{}
|
||||
!(let1 badid (vau de p
|
||||
!(let1 inner (vau ide ip
|
||||
!(let1 self (car ip))
|
||||
!(let1 n (car (cdr ip)))
|
||||
!(let1 acc (car (cdr (cdr ip))))
|
||||
!(if (= 0 n) acc)
|
||||
(vapply self (cons self (cons (- n 1) (cons (+ acc 1) nil))) de)
|
||||
))
|
||||
(vapply inner (cons inner (cons (eval (car p) de) (cons 0 nil))) de)
|
||||
))", *VAPPLY);
|
||||
// Won't work unless tail calls work
|
||||
// so no PE?
|
||||
eval_test(&g, &e, &format!("{} (badid 1000)", def_badid), 1000);
|
||||
}
|
||||
|
||||
static VMAP: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 vmap (vau de p
|
||||
!(let1 vmap_inner (vau ide ip
|
||||
!(let1 self (car ip))
|
||||
!(let1 f (car (cdr ip)))
|
||||
!(let1 l (car (cdr (cdr ip))))
|
||||
!(if (= nil l) l)
|
||||
(cons (vapply f (cons (car l) nil) de) (vapply self (cons self (cons f (cons (cdr l) nil))) de))
|
||||
))
|
||||
(vapply vmap_inner (cons vmap_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
|
||||
))", *VAPPLY)
|
||||
});
|
||||
#[test]
|
||||
fn vmap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
// Maybe define in terms of a right fold?
|
||||
//eval_test(&g, &e, &format!("{} (vmap (vau de p (+ 1 (car p))) '(1 2 3))", *VMAP), (2, (3, (4, Form::Nil))));
|
||||
eval_test(&g, &e, &format!("{} (vmap (vau de p (+ 1 (car p))) '(1))", *VMAP), (2, Form::Nil));
|
||||
}
|
||||
|
||||
static WRAP: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 wrap (vau de p
|
||||
!(let1 f (eval (car p) de))
|
||||
(vau ide p (vapply f (vmap (vau _ xp (eval (car xp) ide)) p) ide))
|
||||
))", *VMAP)
|
||||
});
|
||||
#[test]
|
||||
fn wrap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
// Make sure (wrap (vau ...)) and internal style are optimized the same
|
||||
eval_test(&g, &e, &format!("{} ((wrap (vau _ p (+ (car p) 1))) (+ 1 2))", *WRAP), 4);
|
||||
}
|
||||
|
||||
static UNWRAP: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 unwrap (vau de p
|
||||
!(let1 f (eval (car p) de))
|
||||
(vau ide p (vapply f (vmap (vau _ xp (cons quote (cons (car xp) nil))) p) ide))
|
||||
))", *WRAP)
|
||||
});
|
||||
#[test]
|
||||
fn unwrap_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
// Can't represent prims in tests :( - they do work though, uncommenting and checking the
|
||||
// failed assert verifies
|
||||
//eval_test(&g, &e, &format!("{} ((unwrap (vau de p (car p))) (+ 1 2))", def_unwrap), ("quote", (("+", (1, (2, Form::Nil))), Form::Nil)));
|
||||
//eval_test(&g, &e, &format!("{} ((unwrap (vau de p (eval (car p) de))) (+ 1 2))", def_unwrap), (("+", (1, (2, Form::Nil))), Form::Nil));
|
||||
eval_test(&g, &e, &format!("{} ((unwrap (vau de p (eval (eval (car p) de) de))) (+ 1 2))", *UNWRAP), 3);
|
||||
eval_test(&g, &e, &format!("{} ((unwrap (vau de p (+ (eval (eval (car p) de) de) 1))) (+ 1 2))", *UNWRAP), 4);
|
||||
}
|
||||
|
||||
static LAPPLY: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 lapply (vau de p
|
||||
!(let1 f (eval (car p) de))
|
||||
!(let1 ip (eval (car (cdr p)) de))
|
||||
!(let1 nde (eval (car (cdr (cdr p))) de))
|
||||
(eval (cons (unwrap f) ip) nde)
|
||||
))", *UNWRAP)
|
||||
});
|
||||
#[test]
|
||||
fn lapply_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
// Should this allow envs at all? It technically can, but I feel like it kinda goes against the
|
||||
// sensible deriviation
|
||||
let def_lbadid = format!("
|
||||
{}
|
||||
!(let1 lbadid (vau de p
|
||||
!(let1 inner (wrap (vau ide ip
|
||||
!(let1 self (car ip))
|
||||
!(let1 n (car (cdr ip)))
|
||||
!(let1 acc (car (cdr (cdr ip))))
|
||||
!(if (= 0 n) acc)
|
||||
(lapply self (cons self (cons (- n 1) (cons (+ acc 1) nil))) de)
|
||||
)))
|
||||
(lapply inner (cons inner (cons (eval (car p) de) (cons 0 nil))) de)
|
||||
))", *LAPPLY);
|
||||
// Won't work unless tail calls work
|
||||
// takes a while though
|
||||
eval_test(&g, &e, &format!("{} (lbadid 1000)", def_lbadid), 1000);
|
||||
}
|
||||
|
||||
static VFOLDL: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 vfoldl (vau de p
|
||||
!(let1 vfoldl_inner (vau ide ip
|
||||
!(let1 self (car ip))
|
||||
!(let1 f (car (cdr ip)))
|
||||
!(let1 a (car (cdr (cdr ip))))
|
||||
!(let1 l (car (cdr (cdr (cdr ip)))))
|
||||
!(if (= nil l) a)
|
||||
(vapply self (cons self (cons f (cons (vapply f (cons a (cons (car l) nil)) de) (cons (cdr l) nil)))) de)
|
||||
))
|
||||
(vapply vfoldl_inner (cons vfoldl_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) (cons (eval (car (cdr (cdr p))) de) nil)))) de)
|
||||
))", *LAPPLY)
|
||||
});
|
||||
#[test]
|
||||
fn vfoldl_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (vfoldl (vau de p (+ (car p) (car (cdr p)))) 0 '(1 2 3))", *VFOLDL), 6);
|
||||
}
|
||||
static ZIPD: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 zipd (vau de p
|
||||
!(let1 zipd_inner (vau ide ip
|
||||
!(let1 self (car ip))
|
||||
!(let1 a (car (cdr ip)))
|
||||
!(let1 b (car (cdr (cdr ip))))
|
||||
!(if (= nil a) a)
|
||||
!(if (= nil b) b)
|
||||
(cons (cons (car a) (car b)) (vapply self (cons self (cons (cdr a) (cons (cdr b) nil))) de))
|
||||
))
|
||||
(vapply zipd_inner (cons zipd_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
|
||||
))", *VFOLDL)
|
||||
});
|
||||
#[test]
|
||||
fn zipd_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (zipd '(1 2 3) '(4 5 6))", *ZIPD), ((1,4), ((2,5), ((3,6), Form::Nil))));
|
||||
}
|
||||
static CONCAT: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 concat (vau de p
|
||||
!(let1 concat_inner (vau ide ip
|
||||
!(let1 self (car ip))
|
||||
!(let1 a (car (cdr ip)))
|
||||
!(let1 b (car (cdr (cdr ip))))
|
||||
!(if (= nil a) b)
|
||||
(cons (car a) (vapply self (cons self (cons (cdr a) (cons b nil))) de))
|
||||
))
|
||||
(vapply concat_inner (cons concat_inner (cons (eval (car p) de) (cons (eval (car (cdr p)) de) nil))) de)
|
||||
))", *ZIPD)
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn concat_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (concat '(1 2 3) '(4 5 6))", *CONCAT), (1, (2, (3, (4, (5, (6, Form::Nil)))))));
|
||||
}
|
||||
|
||||
static BVAU: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 match_params (wrap (vau 0 p
|
||||
!(let1 self (car p))
|
||||
!(let1 p_ls (car (cdr p)))
|
||||
!(let1 dp (car (cdr (cdr p))))
|
||||
!(let1 e (car (cdr (cdr (cdr p)))))
|
||||
!(if (= nil p_ls) (assert (= nil dp) e))
|
||||
!(if (symbol? p_ls) (cons (cons p_ls dp) e))
|
||||
(self self (cdr p_ls) (cdr dp) (self self (car p_ls) (car dp) e))
|
||||
)))
|
||||
!(let1 bvau (vau se p
|
||||
(if (= nil (cdr (cdr p)))
|
||||
; No de case
|
||||
!(let1 p_ls (car p))
|
||||
!(let1 b_v (car (cdr p)))
|
||||
(vau 0 dp
|
||||
(eval b_v (match_params match_params p_ls dp se))
|
||||
)
|
||||
|
||||
; de case
|
||||
!(let1 de_s (car p))
|
||||
!(let1 p_ls (car (cdr p)))
|
||||
!(let1 b_v (car (cdr (cdr p))))
|
||||
(vau dde dp
|
||||
(eval b_v (match_params match_params p_ls dp (cons (cons de_s dde) se)))
|
||||
)
|
||||
)
|
||||
))", *CONCAT)
|
||||
});
|
||||
#[test]
|
||||
fn bvau_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} ((bvau _ (a b c) (+ a (- b c))) 10 2 3)", *BVAU), 9);
|
||||
eval_test(&g, &e, &format!("{} ((bvau (a b c) (+ a (- b c))) 10 2 3)", *BVAU), 9);
|
||||
|
||||
eval_test(&g, &e, &format!("{} ((bvau (a b . c) c) 10 2 3)", *BVAU), (3, Form::Nil));
|
||||
eval_test(&g, &e, &format!("{} ((bvau (a b . c) c) 10 2)", *BVAU), Form::Nil);
|
||||
eval_test(&g, &e, &format!("{} ((bvau (a b . c) c) 10 2 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
|
||||
eval_test(&g, &e, &format!("{} ((bvau c c) 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
|
||||
eval_test(&g, &e, &format!("{} ((bvau c c))", *BVAU), Form::Nil);
|
||||
eval_test(&g, &e, &format!("{} ((bvau ((a b) . c) c) (10 2) 3 4 5)", *BVAU), (3, (4, (5, Form::Nil))));
|
||||
eval_test(&g, &e, &format!("{} ((bvau ((a b) . c) a) (10 2) 3 4 5)", *BVAU), 10);
|
||||
eval_test(&g, &e, &format!("{} ((bvau ((a b) . c) b) (10 2) 3 4 5)", *BVAU), 2);
|
||||
|
||||
eval_test(&g, &e, &format!("{} ((wrap (bvau _ (a b c) (+ a (- b c)))) (+ 10 1) (+ 2 2) (+ 5 3))", *BVAU), 7);
|
||||
eval_test(&g, &e, &format!("{} ((wrap (bvau (a b c) (+ a (- b c)))) (+ 10 1) (+ 2 2) (+ 5 3))", *BVAU), 7);
|
||||
}
|
||||
|
||||
static LAMBDA: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 lambda (vau de p
|
||||
(wrap (vapply bvau p de))
|
||||
))", *BVAU)
|
||||
});
|
||||
#[test]
|
||||
fn lambda_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} ((lambda (a b c) (+ a (- b c))) (+ 10 1) (+ 2 2) (+ 5 3))", *LAMBDA), 7);
|
||||
eval_test(&g, &e, &format!("{} ((lambda (a b . c) c) 10 2 3)", *LAMBDA), (3, Form::Nil));
|
||||
eval_test(&g, &e, &format!("{} ((lambda (a b . c) c) 10 2)", *LAMBDA), Form::Nil);
|
||||
eval_test(&g, &e, &format!("{} ((lambda (a b . c) c) 10 2 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
|
||||
eval_test(&g, &e, &format!("{} ((lambda c c) 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
|
||||
eval_test(&g, &e, &format!("{} ((lambda c c))", *LAMBDA), Form::Nil);
|
||||
eval_test(&g, &e, &format!("{} ((lambda ((a b) . c) c) '(10 2) 3 4 5)", *LAMBDA), (3, (4, (5, Form::Nil))));
|
||||
eval_test(&g, &e, &format!("{} ((lambda ((a b) . c) a) '(10 2) 3 4 5)", *LAMBDA), 10);
|
||||
eval_test(&g, &e, &format!("{} ((lambda ((a b) . c) b) '(10 2) 3 4 5)", *LAMBDA), 2);
|
||||
eval_test(&g, &e, &format!("{} ((lambda ((a b . c) d) b) '(10 2 3 4) 3)", *LAMBDA), 2);
|
||||
eval_test(&g, &e, &format!("{} ((lambda ((a b . c) d) c) '(10 2 3 4) 3)", *LAMBDA), (3, (4, Form::Nil)));
|
||||
// should fail
|
||||
//eval_test(&g, &e, &format!("{} ((lambda (a b c) c) 10 2 3 4)", *LAMBDA), 3);
|
||||
}
|
||||
|
||||
static LET2: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 let1 (bvau dp (s v b)
|
||||
(eval b (match_params match_params s (eval v dp) dp))
|
||||
))
|
||||
", *LAMBDA)
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn let2_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
|
||||
eval_test( &g, &e, &format!("{} (let1 x (+ 10 1) (+ x 1))", *LET2), 12);
|
||||
eval_test( &g, &e, &format!("{} (let1 x '(10 1) x)", *LET2), (10, (1, Form::Nil)));
|
||||
eval_test( &g, &e, &format!("{} (let1 (a b) '(10 1) a)", *LET2), 10);
|
||||
eval_test( &g, &e, &format!("{} (let1 (a b) '(10 1) b)", *LET2), 1);
|
||||
eval_test( &g, &e, &format!("{} (let1 (a b . c) '(10 1) c)", *LET2), Form::Nil);
|
||||
eval_test( &g, &e, &format!("{} (let1 (a b . c) '(10 1 2 3) c)", *LET2), (2, (3, Form::Nil)));
|
||||
eval_test( &g, &e, &format!("{} (let1 ((a . b) . c) '((10 1) 2 3) a)", *LET2), 10);
|
||||
eval_test( &g, &e, &format!("{} (let1 ((a . b) . c) '((10 1) 2 3) b)", *LET2), (1, Form::Nil));
|
||||
// should fail
|
||||
//eval_test(&g, &e, &format!("{} (let1 (a b c) '(10 2 3 4) a)", *LET2), 10);
|
||||
}
|
||||
|
||||
static LIST: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 list (lambda args args))
|
||||
", *LET2)
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn list_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (list 1 2 (+ 3 4))", *LIST), (1, (2, (7, Form::Nil))));
|
||||
}
|
||||
|
||||
static Y: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 Y (lambda (f3)
|
||||
((lambda (x1) (x1 x1))
|
||||
(lambda (x2) (f3 (wrap (vau app_env y (lapply (x2 x2) y app_env)))))))
|
||||
)
|
||||
", *LIST)
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn y_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
|
||||
eval_test(&g, &e, &format!("{} ((Y (lambda (recurse) (lambda (n) (if (= 0 n) 1 (* n (recurse (- n 1))))))) 5)", *Y), 120);
|
||||
|
||||
}
|
||||
|
||||
static RLAMBDA: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 rlambda (bvau se (n p b)
|
||||
(eval (list Y (list lambda (list n) (list lambda p b))) se)
|
||||
))
|
||||
", *Y)
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn rlambda_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} ((rlambda recurse (n) (if (= 0 n) 1 (* n (recurse (- n 1))))) 5)", *RLAMBDA), 120);
|
||||
}
|
||||
static AND_OR: Lazy<String> = Lazy::new(|| {
|
||||
// need to extend for varidac
|
||||
format!("
|
||||
{}
|
||||
!(let1 and (bvau se (a b)
|
||||
!(let1 ae (eval a se))
|
||||
(if ae (eval b se) ae)
|
||||
))
|
||||
!(let1 or (bvau se (a b)
|
||||
!(let1 ae (eval a se))
|
||||
(if ae ae (eval b se))
|
||||
))
|
||||
", *RLAMBDA)
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn and_or_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (and true true)", *AND_OR), true);
|
||||
eval_test(&g, &e, &format!("{} (and false true)", *AND_OR), false);
|
||||
eval_test(&g, &e, &format!("{} (and true false)", *AND_OR), false);
|
||||
eval_test(&g, &e, &format!("{} (and false false)", *AND_OR), false);
|
||||
|
||||
eval_test(&g, &e, &format!("{} (or true true)", *AND_OR), true);
|
||||
eval_test(&g, &e, &format!("{} (or false true)", *AND_OR), true);
|
||||
eval_test(&g, &e, &format!("{} (or true false)", *AND_OR), true);
|
||||
eval_test(&g, &e, &format!("{} (or false false)", *AND_OR), false);
|
||||
}
|
||||
static LEN: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 len (lambda (l)
|
||||
!(let1 len_helper (rlambda len_helper (l a)
|
||||
(if (pair? l) (len_helper (cdr l) (+ 1 a))
|
||||
a)
|
||||
))
|
||||
(len_helper l 0)
|
||||
))
|
||||
", *AND_OR)
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn len_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (len '())", *LEN), 0);
|
||||
eval_test(&g, &e, &format!("{} (len '(1))", *LEN), 1);
|
||||
eval_test(&g, &e, &format!("{} (len '(1 2))", *LEN), 2);
|
||||
eval_test(&g, &e, &format!("{} (len '(1 2 3))", *LEN), 3);
|
||||
}
|
||||
static MATCH: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 match (bvau de (x . cases)
|
||||
!(let1 evaluate_case (rlambda evaluate_case (access c)
|
||||
!(if (symbol? c) (list true (lambda (b) (list let1 c access b))))
|
||||
!(if (and (pair? c) (= 'unquote (car c))) (list (list = access (car (cdr c))) (lambda (b) b)))
|
||||
!(if (and (pair? c) (= 'quote (car c))) (list (list = access c) (lambda (b) b)))
|
||||
!(if (pair? c)
|
||||
!(let1 tests (list and (list pair? access) (list = (len c) (list len access))))
|
||||
!(let1 (tests body_func) ((rlambda recurse (c tests access body_func) (if (pair? c)
|
||||
!(let1 (inner_test inner_body_func) (evaluate_case (list car access) (car c)))
|
||||
(recurse (cdr c)
|
||||
(list and tests inner_test)
|
||||
(list cdr access)
|
||||
(lambda (b) (body_func (inner_body_func b))))
|
||||
; else
|
||||
(list tests body_func)
|
||||
))
|
||||
c tests access (lambda (b) b)))
|
||||
(list tests body_func))
|
||||
(list (list = access c) (lambda (b) b))
|
||||
))
|
||||
!(let1 helper (rlambda helper (x_sym cases) (if (= nil cases) (list assert false)
|
||||
(let1 (test body_func) (evaluate_case x_sym (car cases))
|
||||
(concat (list if test (body_func (car (cdr cases)))) (list (helper x_sym (cdr (cdr cases)))))))))
|
||||
|
||||
(eval (list let1 '___MATCH_SYM x (helper '___MATCH_SYM cases)) de)
|
||||
;!(let1 expanded (list let1 '___MATCH_SYM x (helper '___MATCH_SYM cases)))
|
||||
;(debug expanded (eval expanded de))
|
||||
))
|
||||
", *LEN)
|
||||
});
|
||||
#[test]
|
||||
fn match_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (match (+ 1 2) 1 2 2 3 3 4 _ 0)", *MATCH), 4);
|
||||
eval_test(&g, &e, &format!("{} (match '(1 2) 1 2 2 3 3 4 _ 0)", *MATCH), 0);
|
||||
eval_test(&g, &e, &format!("{} (match '(1 2) 1 2 2 3 (a b) (+ a (+ 2 b)) _ 0)", *MATCH), 5);
|
||||
eval_test(&g, &e, &format!("{} (match '(1 2) 1 2 2 3 '(1 2) 7 _ 0)", *MATCH), 7);
|
||||
eval_test(&g, &e, &format!("{} (let1 a 70 (match (+ 60 10) (unquote a) 100 2 3 _ 0))", *MATCH), 100);
|
||||
}
|
||||
static RBTREE: Lazy<String> = Lazy::new(|| {
|
||||
format!("
|
||||
{}
|
||||
!(let1 empty (list 'B nil nil nil))
|
||||
!(let1 E empty)
|
||||
!(let1 EE (list 'BB nil nil nil))
|
||||
|
||||
!(let1 generic-foldl (rlambda generic-foldl (f z t) (match t
|
||||
(unquote E) z
|
||||
|
||||
(c a x b) !(let1 new_left_result (generic-foldl f z a))
|
||||
!(let1 folded (f new_left_result x))
|
||||
(generic-foldl f folded b))))
|
||||
|
||||
!(let1 blacken (lambda (t) (match t
|
||||
('R a x b) (list 'B a x b)
|
||||
t t)))
|
||||
!(let1 balance (lambda (t) (match t
|
||||
; figures 1 and 2
|
||||
('B ('R ('R a x b) y c) z d) (list 'R (list 'B a x b) y (list 'B c z d))
|
||||
('B ('R a x ('R b y c)) z d) (list 'R (list 'B a x b) y (list 'B c z d))
|
||||
('B a x ('R ('R b y c) z d)) (list 'R (list 'B a x b) y (list 'B c z d))
|
||||
('B a x ('R b y ('R c z d))) (list 'R (list 'B a x b) y (list 'B c z d))
|
||||
; figure 8, double black cases
|
||||
('BB ('R a x ('R b y c)) z d) (list 'B (list 'B a x b) y (list 'B c z d))
|
||||
('BB a x ('R ('R b y c) z d)) (list 'B (list 'B a x b) y (list 'B c z d))
|
||||
; already balenced
|
||||
t t)))
|
||||
|
||||
!(let1 map-insert !(let1 ins (rlambda ins (t k v) (match t
|
||||
(unquote E) (list 'R t (list k v) t)
|
||||
(c a x b) !(if (< k (car x)) (balance (list c (ins a k v) x b)))
|
||||
!(if (= k (car x)) (list c a (list k v) b))
|
||||
(balance (list c a x (ins b k v))))))
|
||||
(lambda (t k v) (blacken (ins t k v))))
|
||||
|
||||
!(let1 map-empty empty)
|
||||
|
||||
!(let1 make-test-tree (rlambda make-test-tree (n t) (if (<= n 0) t
|
||||
(make-test-tree (- n 1) (map-insert t n (= 0 (% n 10)))))))
|
||||
!(let1 reduce-test-tree (lambda (tree) (generic-foldl (lambda (a x) (if (car (cdr x)) (+ a 1) a)) 0 tree)))
|
||||
", *MATCH)
|
||||
});
|
||||
#[test]
|
||||
fn rbtree_eval_test() { let g = grammar::TermParser::new(); let e = root_env();
|
||||
eval_test(&g, &e, &format!("{} (reduce-test-tree (make-test-tree 10 map-empty))", *RBTREE), 1);
|
||||
//eval_test(&g, &e, &format!("{} (reduce-test-tree (make-test-tree 20 map-empty))", *RBTREE), 2);
|
||||
}
|
||||
Reference in New Issue
Block a user