in progress commit

- added vars to lib
- fixed adders and getters to both vtable and ftable
- made function operations a dual type (enum)
- prototyped calling of stored external ASTs with
arguments (additional operation type
- stub for eval
- added index function to Cell
This commit is contained in:
Aidan 2021-02-14 16:33:17 -08:00
parent 61e3985592
commit d2f60314f9
No known key found for this signature in database
GPG key ID: 327711E983899316
6 changed files with 162 additions and 54 deletions

View file

@ -16,14 +16,33 @@
*/
use std::boxed::Box;
use std::convert::TryInto;
use std::collections::HashMap;
use crate::cell::{Ctr, Cell, Type};
use crate::vars::{VTable};
use crate::eval::eval;
pub type FTable = HashMap<String, Box<Function>>;
// Standardized function signature for stdlib functions
pub type Operation = fn(Box<Cell>, Box<VTable>, Box<FTable>) -> Box<Cell>;
pub type FTable = HashMap<String, Function>;
pub type InternalOperation = fn(Box<Cell>, Box<VTable>, Box<FTable>) -> Box<Cell>;
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>,
// list of argument string tokens
arg_syms: Vec<String>
}
/* A stored function may either be a pointer to a function
* or a syntax tree to eval with the arguments
*/
pub enum Operation {
Internal(InternalOperation),
External(ExternalOperation)
}
/* Function Args
* If Lazy, is an integer denoting number of args
@ -39,7 +58,6 @@ pub enum Args {
pub struct Function {
pub function: Operation,
pub name: String,
pub times_called: u128,
pub args: Args,
// dont fail on undefined symbol (passed to eval)
@ -53,9 +71,9 @@ impl Function {
/* call
* routine is called by eval when a function call is detected
*/
pub fn call_function (
&mut self,
args: Box<cell>,
pub fn call(
&self,
args: Box<Cell>,
vars: Box<VTable>,
funcs: Box<FTable>
) -> Result<Box<Cell>, String> {
@ -66,33 +84,31 @@ impl Function {
Err(s) => return Err(
format!(
"error evaluating args to {}: {}",
func.name,
self.name,
s
)
)
}
}
let mut passes = false;
match self.args {
Args::Lazy(num) => {
if num < 0 {
passes = true
}
if !(num == (args.len() - 1)) {
if !(num == (n_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| {
let mut passes = args.circuit(|c: Ctr| {
if idx >= arg_types.len() {
return false;
}
if let c = Ctr::None {
if let Ctr::None = c {
return false;
}
@ -104,7 +120,7 @@ impl Function {
});
if passes && idx < (arg_types.len() - 1) {
Err(format!(
return Err(format!(
"{} too little arguments in call to {}",
arg_types.len() - (idx + 1),
self.name
@ -113,7 +129,7 @@ impl Function {
if !passes {
if idx < (arg_types.len() - 1) {
Err(format!(
return Err(format!(
"argument {} in call to {} is of wrong type (expected {})",
idx + 1,
self.name,
@ -122,7 +138,7 @@ impl Function {
}
if idx == (arg_types.len() - 1) {
Err(format!(
return Err(format!(
"too many arguments in call to {}",
self.name
));
@ -131,24 +147,54 @@ impl Function {
}
}
self.times_called += 1;
return Ok((self.function)(args, vars, funcs));
match self.function {
Operation::Internal(f) => Ok((f)(n_args, vars, funcs)),
Operation::External(f) => {
// copy var table and add args
let temp = vars.clone();
for n in 0..f.arg_syms.len() {
temp.insert(
f.arg_syms[n],
Box::new(n_args.index(n))
);
}
eval(f.ast, temp, funcs, self.loose_syms)
}
}
}
}
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);
pub fn declare(
ft: Box<FTable>,
f: Box<Function>
) -> Option<String> {
if let Operation::External(fun) = f.function {
if let Args::Lazy(i) = f.args {
if fun.arg_syms.len() != i.try_into().unwrap() {
return Some(
"external function must have lazy args equal to declared arg_syms length"
.to_string()
);
}
} else {
return Some(
"external function must have lazy args"
.to_string()
);
}
}
pub fn get(
&mut self,
identifier: String
) -> Result<Box<Cell>, String> {
self.get(identifier)
ft.insert(f.name, f);
None
}
pub fn get(
ft: Box<FTable>,
identifier: String
) -> Option<Box<Function>> {
if let Some(f) = ft.get(&identifier) {
Some(*f)
} else {
None
}
}