WIP commit:
- Big organizational updates - Class of errors brought down to borrow checker level
This commit is contained in:
parent
6dcb804d34
commit
b680e3ca9a
6 changed files with 162 additions and 138 deletions
|
|
@ -46,7 +46,7 @@ impl<'a> FTable<'a> {
|
|||
self.0.get(&id)
|
||||
}
|
||||
|
||||
pub fn remove(&self, id: String) {
|
||||
pub fn remove(&mut self, id: String) {
|
||||
self.0.remove(&id);
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ impl<'a> FTable<'a> {
|
|||
|
||||
|
||||
// Standardized function signature for stdlib functions
|
||||
//pub type InternalOperation = impl Fn(Box<Seg>, Box<VTable>, Box<FTable>) -> Box<Ctr>;
|
||||
//pub type InternalOperation = impl Fn(&Seg, &mut VTable, &mut FTable) -> Ctr;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ExternalOperation<'a> {
|
||||
|
|
@ -88,70 +88,28 @@ pub enum Args {
|
|||
Strict(Vec<Type>),
|
||||
}
|
||||
|
||||
pub struct Function<'a> {
|
||||
pub function: Operation<'a>,
|
||||
pub name: String,
|
||||
pub args: Args,
|
||||
|
||||
// dont fail on undefined symbol (passed to eval)
|
||||
pub loose_syms: bool,
|
||||
|
||||
// dont evaluate args at all. leave that to the function
|
||||
pub eval_lazy: bool,
|
||||
}
|
||||
|
||||
impl<'a> Function<'a> {
|
||||
/* call
|
||||
* routine is called by eval when a function call is detected
|
||||
*/
|
||||
pub fn func_call(
|
||||
&self,
|
||||
args: &'a Seg<'a>,
|
||||
vars: &'a mut VTable<'a>,
|
||||
funcs: &'a mut FTable<'a>,
|
||||
) -> Result<Box<Ctr<'a>>, String> {
|
||||
let mut evaluated_args = args;
|
||||
if !self.eval_lazy {
|
||||
match eval(args, vars, funcs, self.loose_syms) {
|
||||
Ok(arg_data) => {
|
||||
if let Ctr::Seg(ast) = *arg_data {
|
||||
evaluated_args = *
|
||||
} else {
|
||||
return Err("Panicking: eval returned not a list for function args.".to_string());
|
||||
}
|
||||
}
|
||||
Err(s) => {
|
||||
return Err(format!(
|
||||
"error evaluating args to {}: {}",
|
||||
self.name, s
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match self.args {
|
||||
impl Args {
|
||||
fn validate_inputs(&self, args: &Seg) -> Result<(), String> {
|
||||
match self {
|
||||
Args::Lazy(ref num) => {
|
||||
let called_arg_count = evaluated_args.len() as i128;
|
||||
let called_arg_count = args.len() as i128;
|
||||
if *num == 0 {
|
||||
if let Ctr::None = *evaluated_args.car {
|
||||
if let Ctr::None = *args.car {
|
||||
//pass
|
||||
} else {
|
||||
return Err(format!(
|
||||
"expected 0 args in call to {}. Got one or more.",
|
||||
self.name,
|
||||
));
|
||||
return Err("Expected 0 args. Got one or more.".to_string());
|
||||
}
|
||||
} else if *num > -1 && (*num != called_arg_count) {
|
||||
return Err(format!(
|
||||
"expected {} args in call to {}. Got {}.",
|
||||
num, self.name, called_arg_count
|
||||
"Expected {} args. Got {}.",
|
||||
num, called_arg_count
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Args::Strict(ref arg_types) => {
|
||||
let mut idx: usize = 0;
|
||||
let passes = evaluated_args.circuit(&mut |c: &Ctr| -> bool {
|
||||
let passes = args.circuit(&mut |c: &Ctr| -> bool {
|
||||
if idx >= arg_types.len() {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -169,32 +127,75 @@ impl<'a> Function<'a> {
|
|||
|
||||
if passes && idx < (arg_types.len() - 1) {
|
||||
return Err(format!(
|
||||
"{} too little arguments in call to {}",
|
||||
arg_types.len() - (idx + 1),
|
||||
self.name
|
||||
"{} too few arguments",
|
||||
arg_types.len() - (idx + 1)
|
||||
));
|
||||
}
|
||||
|
||||
if !passes {
|
||||
if idx < (arg_types.len() - 1) {
|
||||
return Err(format!(
|
||||
"argument {} in call to {} is of wrong type (expected {})",
|
||||
"argument {} is of wrong type (expected {})",
|
||||
idx + 1,
|
||||
self.name,
|
||||
arg_types[idx].to_string()
|
||||
));
|
||||
}
|
||||
|
||||
if idx == (arg_types.len() - 1) {
|
||||
return Err(format!(
|
||||
"too many arguments in call to {}",
|
||||
self.name
|
||||
));
|
||||
return Err("too many arguments".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Function<'a> {
|
||||
pub function: Operation<'a>,
|
||||
pub name: String,
|
||||
pub args: Args,
|
||||
|
||||
// dont fail on undefined symbol (passed to eval)
|
||||
pub loose_syms: bool,
|
||||
|
||||
// dont evaluate args at all. leave that to the function
|
||||
pub eval_lazy: bool,
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> Function<'a> {
|
||||
/* call
|
||||
* routine is called by eval when a function call is detected
|
||||
*/
|
||||
pub fn func_call(
|
||||
&self,
|
||||
args: &'b Seg<'b>,
|
||||
vars: &'a mut VTable<'a>,
|
||||
funcs: &'a mut FTable<'a>,
|
||||
) -> Result<Box<Ctr<'c>>, String> {
|
||||
// put args in simplest desired form
|
||||
let evaluated_args;
|
||||
match eval(args, vars, funcs, self.loose_syms, self.eval_lazy) {
|
||||
Ok(arg_data) => {
|
||||
if let Ctr::Seg(ast) = *arg_data {
|
||||
evaluated_args = *
|
||||
} else {
|
||||
return Err("Panicking: eval returned not a list for function args.".to_string());
|
||||
}
|
||||
}
|
||||
Err(s) => {
|
||||
return Err(format!(
|
||||
"error evaluating args to {}: {}",
|
||||
self.name, s
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(msg) = self.args.validate_inputs(evaluated_args) {
|
||||
return Err(format!("failure to call {}: {}", self.name, msg));
|
||||
}
|
||||
|
||||
/* corecursive with eval.
|
||||
* essentially calls eval on each body in the function.
|
||||
* result of the final body is returned.
|
||||
|
|
@ -211,7 +212,7 @@ impl<'a> Function<'a> {
|
|||
let iterate = &*(f.ast);
|
||||
loop {
|
||||
if let Ctr::Seg(ref data) = *iterate.car {
|
||||
match eval(data, vars, funcs, self.loose_syms) {
|
||||
match eval(data, vars, funcs, self.loose_syms, true) {
|
||||
Ok(ctr) => result = ctr,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue