Big referencing refactor
- RC+RefCell pattern used... everywhere - Ast type implemented - unit tests for func_call - more changes, but this commit scope has grown significantly and I cannot list them all
This commit is contained in:
parent
76b12a8214
commit
3434a49cc1
9 changed files with 446 additions and 391 deletions
179
src/func.rs
179
src/func.rs
|
|
@ -15,23 +15,24 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::boxed::Box;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::TryInto;
|
||||
use std::collections::HashMap;
|
||||
use crate::cell::{Ctr, Cell, Type};
|
||||
use crate::segment::{Ctr, Type, circuit, list_len, list_idx, Ast};
|
||||
use crate::vars::{VTable};
|
||||
use crate::eval::eval;
|
||||
|
||||
pub type FTable = HashMap<String, Box<Function>>;
|
||||
pub type FTable = HashMap<String, Rc<RefCell<Function>>>;
|
||||
|
||||
// Standardized function signature for stdlib functions
|
||||
pub type InternalOperation = fn(&Box<Cell>, &mut Box<VTable>, &mut Box<FTable>) -> Box<Cell>;
|
||||
pub type InternalOperation = fn(Ast, Rc<RefCell<VTable>>, Rc<RefCell<FTable>>) -> Ast;
|
||||
pub struct ExternalOperation {
|
||||
// Un-evaluated abstract syntax tree
|
||||
// TODO: Intermediate evaluation to simplify branches with no argument in them
|
||||
// Simplified branches must not have side effects.
|
||||
// TODO: Apply Memoization?
|
||||
ast: Box<Cell>,
|
||||
ast: Ast,
|
||||
// list of argument string tokens
|
||||
arg_syms: Vec<String>
|
||||
}
|
||||
|
|
@ -67,113 +68,110 @@ pub struct Function {
|
|||
pub eval_lazy: bool
|
||||
}
|
||||
|
||||
impl Function {
|
||||
/* call
|
||||
* routine is called by eval when a function call is detected
|
||||
*/
|
||||
pub fn call(
|
||||
&self,
|
||||
args: &Box<Cell>,
|
||||
vars: &mut Box<VTable>,
|
||||
funcs: &mut Box<FTable>
|
||||
) -> Result<Box<Cell>, String> {
|
||||
let n_args: &Box<Cell>;
|
||||
let outer_owner: Box<Cell>;
|
||||
if !self.eval_lazy {
|
||||
match eval(args, vars, funcs, self.loose_syms) {
|
||||
Ok(box_cell) => outer_owner = box_cell,
|
||||
Err(s) => return Err(
|
||||
format!(
|
||||
"error evaluating args to {}: {}",
|
||||
self.name,
|
||||
s
|
||||
)
|
||||
/* call
|
||||
* routine is called by eval when a function call is detected
|
||||
*/
|
||||
pub fn func_call(
|
||||
function: Rc<RefCell<Function>>,
|
||||
args: Ast,
|
||||
vars: Rc<RefCell<VTable>>,
|
||||
funcs: Rc<RefCell<FTable>>
|
||||
) -> Result<Ast, String> {
|
||||
let called_func = function.borrow_mut();
|
||||
let mut n_args: Ast = args.clone();
|
||||
if !called_func.eval_lazy {
|
||||
match eval(args, vars.clone(), funcs.clone(), called_func.loose_syms) {
|
||||
Ok(rc_seg) => n_args = rc_seg.clone(),
|
||||
Err(s) => return Err(
|
||||
format!(
|
||||
"error evaluating args to {}: {}",
|
||||
called_func.name,
|
||||
s
|
||||
)
|
||||
}
|
||||
n_args = &outer_owner;
|
||||
} else {
|
||||
n_args = args;
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
match &self.args {
|
||||
Args::Lazy(num) => {
|
||||
if *num < 0 {
|
||||
match &called_func.args {
|
||||
Args::Lazy(num) => {
|
||||
if *num < 0 {
|
||||
}
|
||||
|
||||
if !(*num == (list_len(n_args.clone()) as i128 - 1)) {
|
||||
return Err(format!("expected {} args in call to {}", num, called_func.name))
|
||||
}
|
||||
},
|
||||
|
||||
Args::Strict(arg_types) => {
|
||||
let mut idx: usize = 0;
|
||||
let passes = circuit(n_args.clone(), &mut |c: &Ctr| {
|
||||
if idx >= arg_types.len() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !(*num == (n_args.len() - 1)) {
|
||||
return Err(format!("expected {} args in call to {}", num, self.name))
|
||||
if let Ctr::None = c {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
Args::Strict(arg_types) => {
|
||||
let mut idx: usize = 0;
|
||||
let passes = n_args.circuit(&mut |c: &Ctr| {
|
||||
if idx >= arg_types.len() {
|
||||
return false;
|
||||
}
|
||||
let ret = arg_types[idx] == c.to_type();
|
||||
if ret {
|
||||
idx += 1;
|
||||
}
|
||||
return ret;
|
||||
});
|
||||
|
||||
if let Ctr::None = c {
|
||||
return false;
|
||||
}
|
||||
if passes && idx < (arg_types.len() - 1) {
|
||||
return Err(format!(
|
||||
"{} too little arguments in call to {}",
|
||||
arg_types.len() - (idx + 1),
|
||||
called_func.name
|
||||
));
|
||||
}
|
||||
|
||||
let ret = arg_types[idx] == c.to_type();
|
||||
if ret {
|
||||
idx += 1;
|
||||
}
|
||||
return ret;
|
||||
});
|
||||
|
||||
if passes && idx < (arg_types.len() - 1) {
|
||||
if !passes {
|
||||
if idx < (arg_types.len() - 1) {
|
||||
return Err(format!(
|
||||
"{} too little arguments in call to {}",
|
||||
arg_types.len() - (idx + 1),
|
||||
self.name
|
||||
"argument {} in call to {} is of wrong type (expected {})",
|
||||
idx + 1,
|
||||
called_func.name,
|
||||
arg_types[idx].to_str()
|
||||
));
|
||||
}
|
||||
|
||||
if !passes {
|
||||
if idx < (arg_types.len() - 1) {
|
||||
return 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) {
|
||||
return Err(format!(
|
||||
"too many arguments in call to {}",
|
||||
self.name
|
||||
));
|
||||
}
|
||||
if idx == (arg_types.len() - 1) {
|
||||
return Err(format!(
|
||||
"too many arguments in call to {}",
|
||||
called_func.name
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match &self.function {
|
||||
Operation::Internal(f) => Ok((f)(&n_args, vars, funcs)),
|
||||
Operation::External(f) => {
|
||||
// copy var table and add args
|
||||
let mut temp = vars.clone();
|
||||
for n in 0..f.arg_syms.len() {
|
||||
temp.insert(
|
||||
f.arg_syms[n].clone(),
|
||||
Box::new(n_args.index(n))
|
||||
);
|
||||
}
|
||||
eval(&f.ast, &temp, funcs, self.loose_syms)
|
||||
match &called_func.function {
|
||||
Operation::Internal(f) => Ok((f)(n_args, vars, funcs)),
|
||||
Operation::External(f) => {
|
||||
let mut temp = vars.borrow().clone();
|
||||
for n in 0..f.arg_syms.len() {
|
||||
temp.insert(
|
||||
f.arg_syms[n].clone(),
|
||||
Rc::new(list_idx(n_args.clone(), n as u128))
|
||||
);
|
||||
}
|
||||
|
||||
eval(f.ast.clone(), Rc::new(RefCell::new(temp)), funcs, called_func.loose_syms)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn declare(
|
||||
ft: &mut Box<FTable>,
|
||||
f: Box<Function>
|
||||
pub fn func_declare(
|
||||
ft: Rc<RefCell<FTable>>,
|
||||
f: Rc<RefCell<Function>>
|
||||
) -> Option<String> {
|
||||
if let Operation::External(fun) = &f.function {
|
||||
if let Args::Lazy(i) = f.args {
|
||||
let func = f.borrow();
|
||||
let name = func.name.clone();
|
||||
if let Operation::External(fun) = &func.function {
|
||||
if let Args::Lazy(i) = func.args {
|
||||
if fun.arg_syms.len() != i.try_into().unwrap() {
|
||||
return Some(
|
||||
"external function must have lazy args equal to declared arg_syms length"
|
||||
|
|
@ -188,6 +186,7 @@ pub fn declare(
|
|||
}
|
||||
}
|
||||
|
||||
ft.insert(f.name.clone(), f);
|
||||
drop(func);
|
||||
ft.borrow_mut().insert(name, f);
|
||||
None
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue