Continue moving stuff around to simplify before opt, and then start sketching out wrapped values and trace - just realized we might need all values to be wrapped tohande side effects that don't depend on input but still have effects (mutation, now ugh and assert)

This commit is contained in:
2023-07-09 00:24:50 -04:00
parent b077daf12e
commit 6edbe7d2c8
3 changed files with 161 additions and 136 deletions

View File

@@ -5,17 +5,13 @@ use std::cell::RefCell;
use std::convert::From;
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 call(&self, p: Rc<Self>, e: Rc<Self>, nc: Box<Cont<Self>>, metac: Cont<Self>) -> Cursor<Self>;
fn is_nil(&self) -> bool;
fn append(&self, x: Rc<Self>) -> Option<Rc<Self>>;
fn impl_prim(ins: PrimCombI, e: Rc<Self>, ps: Rc<Self>, c: Cont<Self>, metac: Cont<Self>) -> Cursor<Self>;
fn call(&self, p: Rc<Self>, e: Rc<Self>, nc: Box<Cont<Self>>, metac: Cont<Self>) -> Cursor<Self>;
fn impl_prim(ins: PrimCombI, e: Rc<Self>, ps: Vec<Rc<Self>>, c: Cont<Self>, metac: Cont<Self>) -> Cursor<Self>;
}
#[derive(Debug, Eq, PartialEq)]
@@ -25,7 +21,7 @@ pub enum Cont<F: FormT + ?Sized> {
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>> },
PramEval { eval_limit: i32, to_eval: Rc<F>, collected: Option<Vec<Rc<F>>>, e: Rc<F>, ins: PrimCombI, nc: Box<Cont<F>> },
}
impl<F: FormT> Clone for Cont<F> {
fn clone(&self) -> Self {
@@ -36,7 +32,7 @@ impl<F: FormT> Clone for Cont<F> {
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)),
collected: collected.as_ref().map(|x| x.iter().map(|x| Rc::clone(x)).collect()),
e: Rc::clone(e), ins: ins.clone(), nc: nc.clone() },
}
}
@@ -71,14 +67,13 @@ pub fn eval<F: FormT>(e: Rc<F>, f: Rc<F>) -> Rc<F> {
}
},
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() };
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;
let mut next_collected = next_collected;
while !traverse.is_nil() {
next_collected = next_collected.append(traverse.car().unwrap()).unwrap();
next_collected.push(traverse.car().unwrap());
traverse = traverse.cdr().unwrap();
}
cursor = F::impl_prim(ins, e, next_collected, *nc, metac);