This commit is contained in:
Aidan Hahn 2022-01-16 22:02:40 -08:00
parent f805290a4b
commit be73b0b828
No known key found for this signature in database
GPG key ID: 327711E983899316
17 changed files with 588 additions and 675 deletions

View file

@ -15,11 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use std::rc::Rc;
use std::cell::RefCell;
use crate::segment::{Ast, Ctr, new_ast};
use crate::func::{FTable, func_call};
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;
/* iterates over a syntax tree
* returns a NEW LIST of values
@ -29,7 +29,7 @@ pub fn eval(
ast: Ast,
vars: Rc<RefCell<VTable>>,
funcs: Rc<RefCell<FTable>>,
sym_loose: bool
sym_loose: bool,
) -> Result<Ctr, String> {
let mut car = ast.borrow().clone().car;
let mut cdr = ast.borrow().clone().cdr;
@ -49,19 +49,24 @@ pub fn eval(
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())
return func_call(func.clone(), ast.clone(), vars.clone(), funcs.clone());
} else if !sym_loose {
return Err(format!("Couldnt find definition of {}.", tok))
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())
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))
return Err(format!("Couldnt find definition of {}.", tok));
}
},
_ => return Err(format!("Arguments to function not a list!"))
}
_ => return Err(format!("Arguments to function not a list!")),
}
}
@ -72,9 +77,9 @@ pub fn eval(
Ctr::Seg(ref inner) => {
match eval(inner.clone(), vars.clone(), funcs.clone(), sym_loose) {
Ok(res) => (*iter).borrow_mut().car = res,
Err(e) => return Err(format!("Evaluation error: {}", e))
Err(e) => return Err(format!("Evaluation error: {}", e)),
}
},
}
// if SYMBOL: unwrap naively
Ctr::Symbol(ref tok) => {
@ -83,9 +88,9 @@ pub fn eval(
} else if sym_loose {
(*iter).borrow_mut().car = Ctr::Symbol(tok.to_string());
} else {
return Err(format!("Undefined variable: {}", tok))
return Err(format!("Undefined variable: {}", tok));
}
},
}
// if OTHER: clone and set
_ => {
@ -101,11 +106,11 @@ pub fn eval(
} else if sym_loose {
(*iter).borrow_mut().car = Ctr::Symbol(tok.to_string());
} else {
return Err(format!("Undefined variable: {}", tok))
return Err(format!("Undefined variable: {}", tok));
}
none = true;
},
}
// if LIST:
// - iter.cdr = new_ast(None, None)
@ -135,5 +140,5 @@ pub fn eval(
}
}
return Ok(Ctr::Seg(ret))
return Ok(Ctr::Seg(ret));
}