WIP commit:

* Fix up project structures
* combine vars and funcs table
* make a place for old code that may be useful to reference
* singleton pattern for sym table

Commentary:
When this change is finally finished I promise to use feature branches
from here on out
This commit is contained in:
Ava Hahn 2023-02-15 23:27:00 -08:00
parent b680e3ca9a
commit ca4c557d95
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
32 changed files with 1092 additions and 616 deletions

View file

@ -15,130 +15,129 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::func::{func_call, FTable};
use crate::segment::{new_ast, Ast, Ctr};
use crate::vars::VTable;
use std::cell::RefCell;
use std::rc::Rc;
use crate::segment::{Seg, Ctr};
use crate::sym::SYM_TABLE;
/* iterates over a syntax tree
* returns a NEW LIST of values
* representing the simplest possible form of the input
*/
pub fn eval(
ast: Ast,
vars: Rc<RefCell<VTable>>,
funcs: Rc<RefCell<FTable>>,
ast: &Seg,
sym_loose: bool,
) -> Result<Ctr, String> {
let mut car = ast.borrow().clone().car;
let mut cdr = ast.borrow().clone().cdr;
let ret = new_ast(Ctr::None, Ctr::None);
let mut iter = ret.clone();
call_lazy: bool,
) -> Result<Box<Ctr>, String> {
// data to return
let mut ret = Box::from(Ctr::None);
// 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(ref tok) = car {
if let Some(val) = vars.borrow().get(tok) {
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 cdr.clone() {
Ctr::Seg(ast) => {
if let Some(func) = funcs.borrow().get(tok) {
return func_call(func.clone(), ast.clone(), vars.clone(), funcs.clone());
} 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.borrow().get(tok) {
return func_call(
func.clone(),
new_ast(Ctr::None, Ctr::None),
vars.clone(),
funcs.clone(),
);
} else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok));
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.clone(), vars.clone(), funcs.clone(), sym_loose) {
Ok(res) => (*iter).borrow_mut().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.borrow().get(&tok.clone()) {
(*iter).borrow_mut().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).borrow_mut().car = Ctr::Symbol(tok.to_string());
car = arg_car.clone()
} else {
return Err(format!("Undefined variable: {}", tok));
return Err(format!("Undefined variable: {}", tok.clone()));
}
}
// if OTHER: clone and set
_ => {
(*iter).borrow_mut().car = car.clone();
car = arg_car.clone();
}
}
match cdr {
// if SYMBOL: unwrap naively, then end
match &**arg_cdr {
Ctr::Symbol(ref tok) => {
if let Some(val) = vars.borrow().get(&tok.clone()) {
(*iter).borrow_mut().car = (**val).clone();
if let Some(val) = vars.get(tok.clone()) {
cdr = val.clone();
} else if sym_loose {
(*iter).borrow_mut().car = Ctr::Symbol(tok.to_string());
cdr = ast.cdr.clone()
} else {
return Err(format!("Undefined variable: {}", tok));
return Err(format!("Undefined variable: {}", tok.clone()));
}
none = true;
}
// if LIST:
// - iter.cdr = new_ast(None, None)
// - iter = iter.cdr
// - car = cdr.car
// - cdr = cdr.cdr
// - LOOP
Ctr::Seg(next) => {
let n = new_ast(Ctr::None, Ctr::None);
iter.borrow_mut().cdr = Ctr::Seg(n.clone());
iter = n;
car = next.borrow().clone().car;
cdr = next.borrow().clone().cdr;
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).borrow_mut().cdr = cdr.clone();
cdr = ast.cdr.clone();
none = true;
}
}
if let Ctr::None = car {
if let Ctr::None = cdr {
if let Ctr::None = **arg_car {
if let Ctr::None = **arg_cdr {
none = true;
}
}
}
return Ok(Ctr::Seg(ret));
return Ok(ret);
}