significant refactor and simplification

This commit is contained in:
Ava Hahn 2023-02-17 21:00:07 -08:00
parent ca4c557d95
commit 7555a90328
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
8 changed files with 501 additions and 422 deletions

View file

@ -15,22 +15,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::sym::{SYM_TABLE, Symbol, ValueType};
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(
pub fn eval (
ast: &Seg,
sym_loose: bool,
call_lazy: bool,
expect_all_symbols_defined: bool,
simplify_function_branches: bool,
) -> Result<Box<Ctr>, String> {
// data to return
let mut ret = Box::from(Ctr::None);
let mut first = true;
// to be assigned from cloned/evaled data
let mut car;
@ -40,60 +39,69 @@ pub fn eval(
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) = &**arg_car {
binding_for_vtable_get = vars.get(tok.clone());
if let Some(ref val) = binding_for_vtable_get {
arg_car = &val;
}
}
// 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(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!")),
}
}
}
let table_handle = SYM_TABLE.lock().unwrap();
// iterate over ast and build out ret
let mut none = false;
while !none {
match &**arg_car {
let mut prefetched_function: Option<&Symbol> = None;
while let Ctr::Symbol(ref tok) = **arg_car {
prefetched_function = table_handle.get(tok);
if let Some(sym_ref) = prefetched_function {
if let ValueType::VarForm(ref value) = sym_ref.value {
arg_car = value;
} else {
break;
}
} else if !expect_all_symbols_defined {
return Err(format!("evaluation error: undefined symbol {}", tok))
} else {
break
}
}
match **arg_car {
Ctr::Seg(ref inner) => {
match eval(inner, vars, funcs, sym_loose, call_lazy) {
match eval(inner, expect_all_symbols_defined, simplify_function_branches) {
Ok(res) => car = res,
Err(e) => return Err(format!("Evaluation error: {}", e)),
Err(e) => return Err(format!("evaluation error: {}", e)),
}
}
Ctr::Symbol(ref tok) => {
binding_for_vtable_get = vars.get(tok.clone());
if let Some(ref val) = binding_for_vtable_get {
car = val.clone();
} else if sym_loose {
car = arg_car.clone()
// im tired please simplify this
Ctr::Symbol(_) => {
if simplify_function_branches && first {
if let Some(func) = prefetched_function {
if let Ctr::Seg(ref candidates) = **arg_cdr {
let fc: Result<Box<Ctr>, String>;
match eval(candidates, expect_all_symbols_defined, false) {
Ok(res) => {
match *res {
Ctr::Seg(ref args) => {
fc = func.call(args);
},
_ => {
fc = func.call(&Seg::from_mono(res.clone()))
}
}
match fc {
Ok(datum) => car = datum,
Err(e) => return Err(format!("call to {} failed: {}", func.name, e))
}
}
Err(e) => return Err(format!("evaluation error: {}", e))
}
} else {
match func.call(&Seg::new()) {
Ok(res) => car = res,
Err(e) => return Err(format!("call to {} failed: {}", func.name, e))
}
}
} else {
car = arg_car.clone();
}
} else {
return Err(format!("Undefined variable: {}", tok.clone()));
car = arg_car.clone();
}
}
@ -102,16 +110,25 @@ pub fn eval(
}
}
match &**arg_cdr {
Ctr::Symbol(ref tok) => {
if let Some(val) = vars.get(tok.clone()) {
cdr = val.clone();
} else if sym_loose {
cdr = ast.cdr.clone()
// weird tree but okay
while let Ctr::Symbol(ref tok) = **arg_cdr {
prefetched_function = table_handle.get(tok);
if let Some(sym_ref) = prefetched_function {
if let ValueType::VarForm(ref value) = sym_ref.value {
arg_cdr = value;
} else {
return Err(format!("Undefined variable: {}", tok.clone()));
break;
}
} else if !expect_all_symbols_defined {
return Err(format!("evaluation error: undefined symbol {}", tok))
} else {
break
}
}
match **arg_cdr {
Ctr::Symbol(_) => {
cdr = arg_cdr.clone();
none = true;
}
@ -125,7 +142,6 @@ pub fn eval(
arg_cdr = &next.cdr
}
// if OTHER: clone and set, and then end
_ => {
cdr = ast.cdr.clone();
none = true;
@ -137,7 +153,10 @@ pub fn eval(
none = true;
}
}
first = false;
}
return Ok(ret);
Ok(ret)
}