Move call inside form to allow for tracing (might still need a way to get out of the end of tracing)
This commit is contained in:
189
kv/src/ast.rs
189
kv/src/ast.rs
@@ -4,6 +4,40 @@ use std::rc::Rc;
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::convert::From;
|
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 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>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Idea:
|
||||||
|
// Call of DeriComb starts a trace
|
||||||
|
// Every form wrapped
|
||||||
|
// wrapped contains value for this run as well as reference? into trace for code version
|
||||||
|
|
||||||
|
|
||||||
|
#[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>),
|
||||||
|
}
|
||||||
|
|
||||||
// todo, strings not symbols?
|
// todo, strings not symbols?
|
||||||
impl From<String> for Form { fn from(item: String) -> Self { Form::Symbol(item) } }
|
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<&str> for Form { fn from(item: &str) -> Self { Form::Symbol(item.to_owned()) } }
|
||||||
@@ -15,40 +49,63 @@ impl<A: Into<Form>, B: Into<Form>> From<(A, B)> for Form {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FormT: std::fmt::Debug {
|
impl fmt::Display for Form {
|
||||||
fn nil() -> Rc<Self>;
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fn truthy(&self) -> bool;
|
match self {
|
||||||
fn int(&self) -> Option<i32>;
|
Form::Nil => write!(f, "nil"),
|
||||||
fn sym(&self) -> Option<&str>;
|
Form::Int(i) => write!(f, "{i}"),
|
||||||
fn pair(&self) -> Option<(Rc<Self>,Rc<Self>)>;
|
Form::Bool(b) => write!(f, "{b}"),
|
||||||
fn car(&self) -> Option<Rc<Self>>;
|
Form::Symbol(s) => write!(f, "{s}"),
|
||||||
fn cdr(&self) -> Option<Rc<Self>>;
|
Form::Cell(c) => write!(f, "@{}", c.borrow()),
|
||||||
fn prim_comb(&self) -> Option<(i32, PrimCombI)>;
|
Form::Pair(car, cdr) => {
|
||||||
fn deri_comb(&self) -> Option<(Rc<Self>, Option<String>, String, Rc<Self>)>;
|
write!(f, "({}", car)?;
|
||||||
fn cont_comb(&self) -> Option<Cont<Self>>;
|
let mut traverse: Rc<Form> = Rc::clone(cdr);
|
||||||
fn is_nil(&self) -> bool;
|
loop {
|
||||||
fn append(&self, x: Rc<Self>) -> Option<Rc<Self>>;
|
match &*traverse {
|
||||||
fn assoc(k: &str, v: Rc<Self>, l: Rc<Self>) -> Rc<Self>;
|
Form::Pair(ref carp, ref cdrp) => {
|
||||||
fn impl_prim(ins: PrimCombI, e: Rc<Self>, ps: Rc<Self>, c: Cont<Self>, metac: Cont<Self>) -> Cursor<Self>;
|
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 FormT for Form {
|
impl FormT for Form {
|
||||||
fn prim_comb(&self) -> Option<(i32, PrimCombI)> {
|
fn call(&self, p: Rc<Self>, e: Rc<Self>, nc: Box<Cont<Self>>, metac: Cont<Self>) -> Cursor<Self> {
|
||||||
match self {
|
match self {
|
||||||
Form::PrimComb{ eval_limit, ins } => Some((*eval_limit,*ins)),
|
Form::PrimComb{eval_limit, ins} => {
|
||||||
_ => None,
|
Cursor { f: Self::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} => {
|
||||||
fn deri_comb(&self) -> Option<(Rc<Self>, Option<String>, String, Rc<Self>)> {
|
let mut new_e = Rc::clone(se);
|
||||||
match self {
|
if let Some(de) = de {
|
||||||
Form::DeriComb{ se, de, params, body } => Some((Rc::clone(se), de.clone(), params.clone(), Rc::clone(body))),
|
new_e = Self::assoc(&de, Rc::clone(&e), new_e);
|
||||||
_ => None,
|
}
|
||||||
}
|
new_e = Self::assoc(¶ms, p, new_e);
|
||||||
}
|
Cursor { f: Rc::clone(body), c: Cont::Eval { e: new_e, nc: nc }, metac }
|
||||||
fn cont_comb(&self) -> Option<Cont<Self>> {
|
}
|
||||||
match self {
|
Form::ContComb(c) => {
|
||||||
Form::ContComb(c) => Some(c.clone()),
|
Cursor { f: p.car().unwrap(), c: Cont::Eval { e, nc: Box::new(c.clone()) }, metac: Cont::CatchRet { nc: nc, restore_meta: Box::new(metac) } }
|
||||||
_ => None,
|
}
|
||||||
|
_ => {
|
||||||
|
panic!("Tried to call not a Prim/DeriComb/ContComb {:?}, nc was {:?}", self, nc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn nil() -> Rc<Self> {
|
fn nil() -> Rc<Self> {
|
||||||
@@ -176,9 +233,9 @@ impl FormT for Form {
|
|||||||
PrimCombI::Xor => 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() {
|
PrimCombI::CombP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() {
|
||||||
Form::PrimComb { eval_limit, ins } => true,
|
Form::PrimComb { .. } => true,
|
||||||
Form::DeriComb { .. } => true,
|
Form::DeriComb { .. } => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
})), c, metac },
|
})), c, metac },
|
||||||
PrimCombI::CellP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() {
|
PrimCombI::CellP => Cursor { f: Rc::new(Form::Bool(match &*ps.car().unwrap() {
|
||||||
Form::Cell(_c) => true,
|
Form::Cell(_c) => true,
|
||||||
@@ -204,55 +261,6 @@ impl FormT for Form {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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, Clone)]
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum Cont<F: FormT + ?Sized> {
|
pub enum Cont<F: FormT + ?Sized> {
|
||||||
@@ -315,10 +323,8 @@ pub fn eval<F: FormT>(e: Rc<F>, f: Rc<F>) -> Rc<F> {
|
|||||||
cursor = Cursor { f: comb, c: Cont::Eval { e: Rc::clone(&e), nc: Box::new(Cont::Call { p, e, nc }) }, metac }
|
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() {
|
} else if let Some(s) = f.sym() {
|
||||||
let mut t = Rc::clone(&e);
|
let mut t = Rc::clone(&e);
|
||||||
let mut dist = 0;
|
|
||||||
while s != t.car().unwrap().car().unwrap().sym().unwrap() {
|
while s != t.car().unwrap().car().unwrap().sym().unwrap() {
|
||||||
t = t.cdr().unwrap();
|
t = t.cdr().unwrap();
|
||||||
dist += 1;
|
|
||||||
}
|
}
|
||||||
cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc, metac };
|
cursor = Cursor { f: t.car().unwrap().cdr().unwrap(), c: *nc, metac };
|
||||||
} else {
|
} else {
|
||||||
@@ -326,20 +332,7 @@ pub fn eval<F: FormT>(e: Rc<F>, f: Rc<F>) -> Rc<F> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Cont::Call { p, e, nc } => {
|
Cont::Call { p, e, nc } => {
|
||||||
if let Some((eval_limit, ins)) = f.prim_comb() {
|
cursor = f.call(p, e, nc, metac);
|
||||||
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(¶ms, 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);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
mod ast;
|
mod ast;
|
||||||
use crate::ast::{eval,root_env};
|
use crate::ast::{eval,root_env};
|
||||||
//mod opt;
|
|
||||||
//use crate::opt::opt_eval;
|
#[cfg(test)]
|
||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
//let input = "(= 17 ((vau d p (+ (eval (car p) d) 13)) (+ 1 3)))";
|
//let input = "(= 17 ((vau d p (+ (eval (car p) d) 13)) (+ 1 3)))";
|
||||||
//let input = "(+ 1 3)";
|
//let input = "(+ 1 3)";
|
||||||
let input = "(= (+ 1 3) (* 2 2))";
|
let input = "(= (+ 1 3) (* 2 2))";
|
||||||
let parsed_input = Rc::new(grammar::TermParser::new().parse(input).unwrap());
|
let parsed_input = grammar::TermParser::new().parse(input).unwrap();
|
||||||
//println!("Parsed input is {} - {:?}", parsed_input, parsed_input);
|
//println!("Parsed input is {} - {:?}", parsed_input, parsed_input);
|
||||||
let root = root_env();
|
let root = root_env();
|
||||||
let result = eval(Rc::clone(&root), Rc::clone(&parsed_input));
|
let result = eval(Rc::clone(&root), Rc::clone(&parsed_input));
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ fn parse_test() {
|
|||||||
|
|
||||||
fn eval_test<T: Into<Form>>(also_pe: bool, gram: &grammar::TermParser, e: &Rc<Form>, code: &str, expected: T) {
|
fn eval_test<T: Into<Form>>(also_pe: bool, gram: &grammar::TermParser, e: &Rc<Form>, code: &str, expected: T) {
|
||||||
println!("Doing test {}", code);
|
println!("Doing test {}", code);
|
||||||
let parsed = Rc::new(gram.parse(code).unwrap());
|
let parsed = gram.parse(code).unwrap();
|
||||||
let basic_result = eval(Rc::clone(e), Rc::clone(&parsed));
|
let basic_result = eval(Rc::clone(e), Rc::clone(&parsed));
|
||||||
assert_eq!(*basic_result, expected.into());
|
assert_eq!(*basic_result, expected.into());
|
||||||
if also_pe {
|
if also_pe {
|
||||||
|
|||||||
Reference in New Issue
Block a user