Add vars table

- added vars table
- refactored function and func table to use impl
- added add and delete methods for func table
This commit is contained in:
Aidan 2021-02-07 23:16:25 -08:00
parent af28692175
commit 61e3985592
No known key found for this signature in database
GPG key ID: 327711E983899316
2 changed files with 140 additions and 76 deletions

View file

@ -49,89 +49,106 @@ pub struct Function {
pub eval_lazy: bool
}
/* call_function
* routine is called by eval when a function call is detected
* full list of cells considered to be a function call is passed in
* (that means that args contains the function symbol in 0 index)
*/
pub fn call_function(
args: Box<Cell>,
func: Function,
vars: Box<VTable>,
funcs: Box<FTable>
) -> Result<Box<Cell>, String> {
let mut n_args = args;
if !func.eval_lazy {
match eval(args, vars, funcs, func.loose_syms) {
Ok(box_cell) => n_args = box_cell,
Err(s) => return Err(
format!(
"error evaluating args to {}: {}",
func.name,
s
)
)
}
}
let mut passes = false;
match func.args {
Args::Lazy(num) => {
if num < 0 {
passes = true
}
if !(num == (args.len() - 1)) {
return Err(format!("expected {} args in call to {}", num, func.name))
}
},
Args::Strict(arg_types) => {
let idx: usize = 0;
passes = args.circuit(|c: Ctr| {
if idx >= arg_types.len() {
return false;
}
if let c = Ctr::None {
return false;
}
let ret = arg_types[idx] == c.to_type();
if ret {
idx += 1;
}
return ret;
});
if passes && idx < (arg_types.len() - 1) {
Err(format!(
"{} too little arguments in call to {}",
arg_types.len() - (idx + 1),
func.name
));
}
if !passes {
if idx < (arg_types.len() - 1) {
Err(format!(
"argument {} in call to {} is of wrong type (expected {})",
idx + 1,
impl Function {
/* call
* routine is called by eval when a function call is detected
*/
pub fn call_function (
&mut self,
args: Box<cell>,
vars: Box<VTable>,
funcs: Box<FTable>
) -> Result<Box<Cell>, String> {
let mut n_args = args;
if !self.eval_lazy {
match eval(args, vars, funcs, self.loose_syms) {
Ok(box_cell) => n_args = box_cell,
Err(s) => return Err(
format!(
"error evaluating args to {}: {}",
func.name,
arg_types[idx].to_str()
s
)
)
}
}
let mut passes = false;
match self.args {
Args::Lazy(num) => {
if num < 0 {
passes = true
}
if !(num == (args.len() - 1)) {
return Err(format!("expected {} args in call to {}", num, self.name))
}
},
Args::Strict(arg_types) => {
let idx: usize = 0;
passes = args.circuit(|c: Ctr| {
if idx >= arg_types.len() {
return false;
}
if let c = Ctr::None {
return false;
}
let ret = arg_types[idx] == c.to_type();
if ret {
idx += 1;
}
return ret;
});
if passes && idx < (arg_types.len() - 1) {
Err(format!(
"{} too little arguments in call to {}",
arg_types.len() - (idx + 1),
self.name
));
}
if idx == (arg_types.len() - 1) {
Err(format!(
"too many arguments in call to {}",
func.name
));
if !passes {
if idx < (arg_types.len() - 1) {
Err(format!(
"argument {} in call to {} is of wrong type (expected {})",
idx + 1,
self.name,
arg_types[idx].to_str()
));
}
if idx == (arg_types.len() - 1) {
Err(format!(
"too many arguments in call to {}",
self.name
));
}
}
}
}
self.times_called += 1;
return Ok((self.function)(args, vars, funcs));
}
}
impl FTable {
pub fn declare(
&mut self,
f: Function
) {
// memory leak here? where does Function go? maybe it should be boxed....
self.insert(f.name, f);
}
func.times_called += 1;
return Ok((func.function)(args, vars, funcs));
pub fn get(
&mut self,
identifier: String
) -> Result<Box<Cell>, String> {
self.get(identifier)
}
}