Move entire Form out of eval into Trait

This commit is contained in:
2023-07-04 10:16:38 -04:00
parent d6cf6a6b64
commit b03122c7b8
2 changed files with 216 additions and 166 deletions

View File

@@ -15,246 +15,103 @@ impl<A: Into<Form>, B: Into<Form>> From<(A, B)> for Form {
}
}
impl Form {
pub fn truthy(&self) -> bool {
pub trait FormT: std::fmt::Debug {
fn nil() -> Rc<Self>;
fn truthy(&self) -> bool;
fn int(&self) -> Option<i32>;
fn sym(&self) -> Option<&str>;
fn pair(&self) -> Option<(Rc<Self>,Rc<Self>)>;
fn car(&self) -> Option<Rc<Self>>;
fn cdr(&self) -> Option<Rc<Self>>;
fn prim_comb(&self) -> Option<(i32, PrimCombI)>;
fn deri_comb(&self) -> Option<(Rc<Self>, Option<String>, String, Rc<Self>)>;
fn cont_comb(&self) -> Option<Cont<Self>>;
fn is_nil(&self) -> bool;
fn append(&self, x: Rc<Self>) -> Option<Rc<Self>>;
fn assoc(k: &str, v: Rc<Self>, l: Rc<Self>) -> Rc<Self>;
fn impl_prim(ins: PrimCombI, e: Rc<Self>, ps: Rc<Self>, c: Cont<Self>, metac: Cont<Self>) -> Cursor<Self>;
}
impl FormT for Form {
fn prim_comb(&self) -> Option<(i32, PrimCombI)> {
match self {
Form::PrimComb{ eval_limit, ins } => Some((*eval_limit,*ins)),
_ => None,
}
}
fn deri_comb(&self) -> Option<(Rc<Self>, Option<String>, String, Rc<Self>)> {
match self {
Form::DeriComb{ se, de, params, body } => Some((Rc::clone(se), de.clone(), params.clone(), Rc::clone(body))),
_ => None,
}
}
fn cont_comb(&self) -> Option<Cont<Self>> {
match self {
Form::ContComb(c) => Some(c.clone()),
_ => None,
}
}
fn nil() -> Rc<Self> {
Rc::new(Form::Nil)
}
fn truthy(&self) -> bool {
match self {
Form::Bool(b) => *b,
Form::Nil => false,
_ => true,
}
}
pub fn int(&self) -> Option<i32> {
fn int(&self) -> Option<i32> {
match self {
Form::Int(i) => Some(*i),
_ => None,
}
}
pub fn sym(&self) -> Option<&str> {
fn sym(&self) -> Option<&str> {
match self {
Form::Symbol(s) => Some(s),
_ => None,
}
}
pub fn car(&self) -> Option<Rc<Form>> {
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,
}
}
pub fn cdr(&self) -> Option<Rc<Form>> {
fn cdr(&self) -> Option<Rc<Form>> {
match self {
Form::Pair(_car, cdr) => Some(Rc::clone(cdr)),
_ => None,
}
}
pub fn is_nil(&self) -> bool {
fn is_nil(&self) -> bool {
match self {
Form::Nil => true,
_ => false,
}
}
pub fn append(&self, x: Rc<Form>) -> Option<Form> {
fn append(&self, x: Rc<Form>) -> Option<Rc<Form>> {
match self {
Form::Pair(car, cdr) => cdr.append(x).map(|x| Form::Pair(Rc::clone(car), Rc::new(x))),
Form::Nil => Some(Form::Pair(x, Rc::new(Form::Nil))),
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,
}
}
}
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>"),
}
}
}
#[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),
}
#[derive(Debug, Eq, PartialEq, Clone)]
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<Rc<Form>>, e: Rc<Form>, ins: PrimCombI, nc: Box<Cont> },
}
pub struct Cursor { f: Rc<Form>, c: Cont, 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::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() {
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 = 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::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 }
},
Form::Symbol(ref s) => {
let mut t = Rc::clone(&e);
let mut dist = 0;
while s != t.car().unwrap().car().unwrap().sym().unwrap() {
t = t.cdr().unwrap();
dist += 1;
}
cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc, metac };
},
_ => {
cursor = Cursor { f: Rc::clone(&f), c: *nc, metac };
},
}
},
Cont::Call { p, e, nc } => {
match *f {
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 };
},
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) } };
},
_ => panic!("Tried to call not a Prim/DeriComb/ContComb {:?}, nc was {:?}", f, nc),
}
},
}
}
}
pub fn assoc(k: &str, v: Rc<Form>, l: Rc<Form>) -> Rc<Form> {
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
}
#[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<Form>, ps: Rc<Form>, c: Cont, metac: Cont) -> Cursor {
fn impl_prim(ins: PrimCombI, e: Rc<Form>, ps: Rc<Form>, c: Cont<Form>, metac: Cont<Form>) -> Cursor<Form> {
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 => {
@@ -346,6 +203,199 @@ pub fn impl_prim(ins: PrimCombI, e: Rc<Form>, ps: Rc<Form>, c: Cont, metac: Cont
PrimCombI::NilP => Cursor { f: Rc::new(Form::Bool(ps.car().unwrap().is_nil())), c, metac },
}
}
}
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>"),
}
}
}
#[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<Form>),
}
//#[derive(Debug, Eq, PartialEq, Clone)]
#[derive(Debug, Eq, PartialEq)]
pub enum Cont<F: FormT + ?Sized> {
Exit,
MetaRet,
CatchRet { nc: Box<Cont<F>>, restore_meta: Box<Cont<F>> },
Eval { e: Rc<F>, nc: Box<Cont<F>> },
Call { p: Rc<F>, e: Rc<F>, nc: Box<Cont<F>> },
PramEval { eval_limit: i32, to_eval: Rc<F>, collected: Option<Rc<F>>, e: Rc<F>, ins: PrimCombI, nc: Box<Cont<F>> },
}
impl<F: FormT> Clone for Cont<F> {
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| Rc::clone(x)), e: Rc::clone(e), ins: ins.clone(), nc: nc.clone() },
}
}
}
pub struct Cursor<F: FormT + ?Sized> { f: Rc<F>, c: Cont<F>, metac: Cont<F> }
pub fn eval<F: FormT>(e: Rc<F>, f: Rc<F>) -> Rc<F> {
let mut cursor = Cursor::<F> { 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::PramEval { eval_limit, to_eval, collected, e, ins, nc } => {
let next_collected = if let Some(collected) = collected {
collected.append(f).unwrap()
} else { F::nil() };
if eval_limit == 0 || to_eval.is_nil() {
let mut traverse = to_eval;
let mut next_collected = next_collected;
while !traverse.is_nil() {
next_collected = next_collected.append(traverse.car().unwrap()).unwrap();
traverse = traverse.cdr().unwrap();
}
cursor = F::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::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);
let mut dist = 0;
while s != t.car().unwrap().car().unwrap().sym().unwrap() {
t = t.cdr().unwrap();
dist += 1;
}
cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc, metac };
} else {
cursor = Cursor { f: Rc::clone(&f), c: *nc, metac };
}
},
Cont::Call { p, e, nc } => {
if let Some((eval_limit, ins)) = f.prim_comb() {
cursor = Cursor { f: F::nil(), c: Cont::PramEval { eval_limit, to_eval: p, collected: None, e, ins, nc: nc }, metac };
} else if let Some((se, de, params, body)) = f.deri_comb() {
let mut new_e = se;
if let Some(de) = de {
new_e = F::assoc(&de, Rc::clone(&e), new_e);
}
new_e = F::assoc(&params, p, new_e);
cursor = Cursor { f: body, c: Cont::Eval { e: new_e, nc: nc }, metac };
} else if let Some(c) = f.cont_comb() {
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) } };
} else {
panic!("Tried to call not a Prim/DeriComb/ContComb {:?}, nc was {:?}", f, nc);
}
},
}
}
}
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 = Form::assoc(k, v, to_ret);
}
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,
}
// Have eval?/maybe Cont?/maybe Cursor? parameterized on value type?

View File

@@ -1,22 +1,22 @@
use std::str::FromStr;
use std::rc::Rc;
use crate::ast::Form;
use crate::ast::{Form,FormT};
grammar;
pub Term: Form = {
NUM => Form::Int(i32::from_str(<>).unwrap()),
SYM => Form::Symbol(<>.to_owned()),
"(" <ListInside?> ")" => <>.unwrap_or(Form::Nil),
"'" <Term> => Form::Pair(Rc::new(Form::Symbol("quote".to_owned())), Rc::new(Form::Pair(Rc::new(<>), Rc::new(Form::Nil)))),
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(Rc::new(t)).unwrap()
h.append(t).unwrap()
},
};
ListInside: Form = {
<Term> => Form::Pair(Rc::new(<>), Rc::new(Form::Nil)),
<h: Term> <t: ListInside> => Form::Pair(Rc::new(h), Rc::new(t)),
<a: Term> "." <d: Term> => Form::Pair(Rc::new(a), Rc::new(d)),
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 {
"(",