WIP commit:

- Big organizational updates
- Class of errors brought down to borrow checker level
This commit is contained in:
Ava Hahn 2023-02-15 20:05:48 -08:00
parent 6dcb804d34
commit b680e3ca9a
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
6 changed files with 162 additions and 138 deletions

View file

@ -23,86 +23,98 @@ use crate::vars::VTable;
* returns a NEW LIST of values
* representing the simplest possible form of the input
*/
pub fn eval<'a>(
ast: &'a Seg<'a>,
/* Regarding 'a and 'b
* 'a intends to live beyond the call to eval
* 'b (input seg) only needs to live until this call to seg
*/
pub fn eval<'a, 'b>(
ast: &'b Seg<'b>,
vars: &'a mut VTable<'a>,
funcs: &'a mut FTable<'a>,
sym_loose: bool,
) -> Result<Box<Ctr<'a>>, String> {
let mut ret = Box::new(Ctr::Seg(Seg::new()));
let mut iter: &mut Seg;
if let Ctr::Seg(ref mut s) = *ret {
iter = s;
} else {
return Err("Critfail: no fuckin clue whats up here".to_string())
}
call_lazy: bool,
) -> Result<Box<Ctr<'b>>, String> {
// data to return
let mut ret = Box::from(Ctr::None);
let mut car = &ast.car;
// to be assigned from cloned/evaled data
let mut car;
let mut cdr = Box::from(Ctr::None);
// lets me redirect the input
let mut arg_car = &ast.car;
let mut arg_cdr = &ast.cdr;
// theres probably a better way to do this
let mut binding_for_vtable_get;
// doing an initial variable check here allows us
// to find functions passed in as variables
if let Ctr::Symbol(tok) = **car {
if let Some(val) = vars.get(tok.clone()) {
car = &(val.clone());
if let Ctr::Symbol(tok) = &**arg_car {
binding_for_vtable_get = vars.get(tok.clone());
if let Some(ref val) = binding_for_vtable_get {
arg_car = &val;
}
}
// another check to detect if we may have a function call
if let Ctr::Symbol(ref tok) = **car {
match *ast.cdr {
Ctr::Seg(ref ast) => {
if let Some(func) = funcs.get(tok.clone()) {
return func.func_call(ast, vars, funcs);
} else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok));
// Is ast a function call?
if !call_lazy {
if let Ctr::Symbol(ref tok) = &**arg_car {
match *ast.cdr {
Ctr::Seg(ref ast) => {
if let Some(ref func) = funcs.get(tok.clone()) {
return func.func_call(ast, vars, funcs);
} else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok));
}
}
}
Ctr::None => {
if let Some(func) = funcs.get(tok.clone()) {
return func.func_call(&Seg::new(), vars, funcs);
} else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok.clone()));
Ctr::None => {
if let Some(ref func) = funcs.get(tok.clone()) {
return func.func_call(&Seg::new(), vars, funcs);
} else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok.clone()));
}
}
_ => return Err(format!("Arguments to function not a list!")),
}
_ => return Err(format!("Arguments to function not a list!")),
}
}
// iterate over ast and build out ret
let mut none = false;
while !none {
match **car {
// if LIST: call eval inner on it with first_item=true
match &**arg_car {
Ctr::Seg(ref inner) => {
match eval(inner, vars, funcs, sym_loose) {
Ok(res) => (*iter).car = res,
match eval(inner, vars, funcs, sym_loose, call_lazy) {
Ok(res) => car = res,
Err(e) => return Err(format!("Evaluation error: {}", e)),
}
}
// if SYMBOL: unwrap naively
Ctr::Symbol(ref tok) => {
if let Some(val) = vars.get(tok.clone()) {
iter.car = val.clone();
binding_for_vtable_get = vars.get(tok.clone());
if let Some(ref val) = binding_for_vtable_get {
car = val.clone();
} else if sym_loose {
iter.car = car.clone()
car = arg_car.clone()
} else {
return Err(format!("Undefined variable: {}", tok.clone()));
}
}
// if OTHER: clone and set
_ => {
iter.car = car.clone();
car = arg_car.clone();
}
}
match *ast.cdr {
// if SYMBOL: unwrap naively, then end
match &**arg_cdr {
Ctr::Symbol(ref tok) => {
if let Some(val) = vars.get(tok.clone()) {
iter.cdr = val.clone();
cdr = val.clone();
} else if sym_loose {
iter.cdr = ast.cdr.clone()
cdr = ast.cdr.clone()
} else {
return Err(format!("Undefined variable: {}", tok.clone()));
}
@ -110,26 +122,25 @@ pub fn eval<'a>(
none = true;
}
// if LIST:
// - iter.cdr = new_ast(None, None)
// - iter = iter.cdr
// - car = cdr.car
// - cdr = cdr.cdr
// - LOOP
Ctr::Seg(next) => {
iter.cdr = Box::new(Ctr::Seg(Seg::new()));
ast = &next;
Ctr::Seg(ref next) => {
if let Ctr::None = *ret {
*ret = Ctr::Seg(Seg::from(car, cdr.clone()))
} else if let Ctr::Seg(ref mut s) = *ret {
s.append(Box::from(Ctr::Seg(Seg::from(car, cdr.clone()))))
}
arg_car = &next.car;
arg_cdr = &next.cdr
}
// if OTHER: clone and set, and then end
_ => {
iter.cdr = ast.cdr.clone();
cdr = ast.cdr.clone();
none = true;
}
}
if let Ctr::None = **car {
if let Ctr::None = *ast.cdr {
if let Ctr::None = **arg_car {
if let Ctr::None = **arg_cdr {
none = true;
}
}