Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-03-01 11:38:02 -08:00
parent ecbc47d4fe
commit bc09cb07b1
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
17 changed files with 236 additions and 217 deletions

View file

@ -16,7 +16,7 @@
*/
use crate::eval::eval;
use crate::segment::{Seg, Ctr, Type};
use crate::segment::{Ctr, Seg, Type};
use std::collections::HashMap;
use std::rc::Rc;
pub struct SymTable(HashMap<String, Symbol>);
@ -41,7 +41,7 @@ pub struct UserFn {
pub enum ValueType {
Internal(Rc<dyn Fn(&Seg, &mut SymTable) -> Result<Ctr, String>>),
FuncForm(UserFn),
VarForm(Box<Ctr>)
VarForm(Box<Ctr>),
}
/* Function Args
@ -53,7 +53,7 @@ pub enum Args {
Lazy(u128),
Strict(Vec<Type>),
Infinite,
None
None,
}
#[derive(Clone)]
@ -83,7 +83,12 @@ impl SymTable {
self.0.remove(arg)
}
pub fn call_symbol(&mut self, name: &String, args: &Seg, call_func: bool) -> Result<Box<Ctr>, String> {
pub fn call_symbol(
&mut self,
name: &String,
args: &Seg,
call_func: bool,
) -> Result<Box<Ctr>, String> {
let symbol = match self.remove(name) {
Some(s) => s,
None => return Err(format!("undefined symbol: {}", name)),
@ -125,21 +130,21 @@ impl Args {
Args::None => {
if args.len() == 1 {
if let Ctr::None = *args.car {
return Ok(())
return Ok(());
} else {
return Err("expected no args".to_string())
return Err("expected no args".to_string());
}
} else {
return Err("expected no args".to_string())
return Err("expected no args".to_string());
}
},
}
Args::Infinite => {
if !args.is_empty() {
return Ok(())
return Ok(());
} else {
return Err("expected args but none were provided".to_string())
return Err("expected args but none were provided".to_string());
}
},
}
Args::Lazy(ref num) => {
let called_arg_count = args.len();
@ -150,15 +155,9 @@ impl Args {
return Err("expected 0 args. Got one or more.".to_string());
}
} else if *num != called_arg_count {
return Err(format!(
"expected {} args. Got {}.",
num, called_arg_count
));
return Err(format!("expected {} args. Got {}.", num, called_arg_count));
} else if let Ctr::None = *args.car {
return Err(format!(
"expected {} args. Got 0.",
num,
));
return Err(format!("expected {} args. Got 0.", num,));
}
}
@ -181,15 +180,12 @@ impl Args {
});
if passes && idx < (arg_types.len() - 1) {
return Err(format!(
"{} too few arguments",
arg_types.len() - (idx + 1)
));
return Err(format!("{} too few arguments", arg_types.len() - (idx + 1)));
}
if !passes {
if mismatch {
return Err(format!("arg {} expected to be {}", idx+1, arg_types[idx]));
return Err(format!("arg {} expected to be {}", idx + 1, arg_types[idx]));
}
if idx > (arg_types.len() - 1) {
return Err("too many arguments".to_string());
@ -209,11 +205,7 @@ impl Symbol {
/* call
* routine is called by eval when a symbol is expanded
*/
pub fn call(
&self,
args: &Seg,
syms: &mut SymTable
) -> Result<Box<Ctr>, String> {
pub fn call(&self, args: &Seg, syms: &mut SymTable) -> Result<Box<Ctr>, String> {
let evaluated_args: &Seg;
let outer_scope_seg_storage: Seg;
let outer_scope_eval: Box<Ctr>;
@ -251,13 +243,15 @@ impl Symbol {
// Prep var table for function execution
for n in 0..f.arg_syms.len() {
if let Some(old) = syms.insert(f.arg_syms[n].clone(), Symbol{
if let Some(old) = syms.insert(
f.arg_syms[n].clone(),
Symbol {
name: f.arg_syms[n].clone(),
value: ValueType::VarForm(Box::new(evaluated_args[n].clone())),
args: Args::None,
conditional_branches: false,
})
{
},
) {
holding_table.insert(f.arg_syms[n].clone(), old);
}
}
@ -280,7 +274,7 @@ impl Symbol {
} else {
result = ctr;
}
},
}
Err(e) => return Err(e),
}
}