2021-02-07 11:45:26 -08:00
|
|
|
/* relish: versatile lisp shell
|
|
|
|
|
* Copyright (C) 2021 Aidan Hahn
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use std::boxed::Box;
|
2021-02-14 16:33:17 -08:00
|
|
|
use std::convert::TryInto;
|
2021-02-07 11:45:26 -08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use crate::cell::{Ctr, Cell, Type};
|
|
|
|
|
use crate::vars::{VTable};
|
|
|
|
|
use crate::eval::eval;
|
|
|
|
|
|
2021-02-14 16:33:17 -08:00
|
|
|
pub type FTable = HashMap<String, Box<Function>>;
|
|
|
|
|
|
2021-02-07 11:45:26 -08:00
|
|
|
// Standardized function signature for stdlib functions
|
2021-03-07 19:24:38 -08:00
|
|
|
pub type InternalOperation = fn(&Box<Cell>, &mut Box<VTable>, &mut Box<FTable>) -> Box<Cell>;
|
2021-02-14 16:33:17 -08:00
|
|
|
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)
|
|
|
|
|
}
|
2021-02-07 11:45:26 -08:00
|
|
|
|
|
|
|
|
/* Function Args
|
|
|
|
|
* If Lazy, is an integer denoting number of args
|
2021-03-07 19:24:38 -08:00
|
|
|
* If Strict, is a list of type tags denoting argument type.
|
2021-02-07 11:45:26 -08:00
|
|
|
*/
|
|
|
|
|
pub enum Args {
|
|
|
|
|
// signed: -1 denotes infinite args
|
|
|
|
|
Lazy(i128),
|
|
|
|
|
Strict(Vec<Type>)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// function which does not need args checked
|
|
|
|
|
pub struct Function {
|
|
|
|
|
pub function: Operation,
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 23:16:25 -08:00
|
|
|
impl Function {
|
|
|
|
|
/* call
|
|
|
|
|
* routine is called by eval when a function call is detected
|
|
|
|
|
*/
|
2021-02-14 16:33:17 -08:00
|
|
|
pub fn call(
|
|
|
|
|
&self,
|
2021-03-07 19:24:38 -08:00
|
|
|
args: &Box<Cell>,
|
|
|
|
|
vars: &mut Box<VTable>,
|
|
|
|
|
funcs: &mut Box<FTable>
|
2021-02-07 23:16:25 -08:00
|
|
|
) -> Result<Box<Cell>, String> {
|
2021-03-07 19:24:38 -08:00
|
|
|
let n_args: &Box<Cell>;
|
|
|
|
|
let outer_owner: Box<Cell>;
|
2021-02-07 23:16:25 -08:00
|
|
|
if !self.eval_lazy {
|
|
|
|
|
match eval(args, vars, funcs, self.loose_syms) {
|
2021-03-07 19:24:38 -08:00
|
|
|
Ok(box_cell) => outer_owner = box_cell,
|
2021-02-07 23:16:25 -08:00
|
|
|
Err(s) => return Err(
|
|
|
|
|
format!(
|
|
|
|
|
"error evaluating args to {}: {}",
|
2021-02-14 16:33:17 -08:00
|
|
|
self.name,
|
2021-02-07 23:16:25 -08:00
|
|
|
s
|
|
|
|
|
)
|
2021-02-07 11:45:26 -08:00
|
|
|
)
|
|
|
|
|
}
|
2021-03-07 19:24:38 -08:00
|
|
|
n_args = &outer_owner;
|
|
|
|
|
} else {
|
|
|
|
|
n_args = args;
|
2021-02-07 23:16:25 -08:00
|
|
|
}
|
2021-02-07 11:45:26 -08:00
|
|
|
|
2021-03-07 19:24:38 -08:00
|
|
|
match &self.args {
|
2021-02-07 23:16:25 -08:00
|
|
|
Args::Lazy(num) => {
|
2021-03-07 19:24:38 -08:00
|
|
|
if *num < 0 {
|
2021-02-07 11:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-07 19:24:38 -08:00
|
|
|
if !(*num == (n_args.len() - 1)) {
|
2021-02-07 23:16:25 -08:00
|
|
|
return Err(format!("expected {} args in call to {}", num, self.name))
|
2021-02-07 11:45:26 -08:00
|
|
|
}
|
2021-02-07 23:16:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Args::Strict(arg_types) => {
|
2021-03-07 19:24:38 -08:00
|
|
|
let mut idx: usize = 0;
|
|
|
|
|
let passes = n_args.circuit(&mut |c: &Ctr| {
|
2021-02-07 23:16:25 -08:00
|
|
|
if idx >= arg_types.len() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:33:17 -08:00
|
|
|
if let Ctr::None = c {
|
2021-02-07 23:16:25 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let ret = arg_types[idx] == c.to_type();
|
|
|
|
|
if ret {
|
|
|
|
|
idx += 1;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if passes && idx < (arg_types.len() - 1) {
|
2021-02-14 16:33:17 -08:00
|
|
|
return Err(format!(
|
2021-02-07 23:16:25 -08:00
|
|
|
"{} too little arguments in call to {}",
|
|
|
|
|
arg_types.len() - (idx + 1),
|
|
|
|
|
self.name
|
2021-02-07 11:45:26 -08:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 23:16:25 -08:00
|
|
|
if !passes {
|
|
|
|
|
if idx < (arg_types.len() - 1) {
|
2021-02-14 16:33:17 -08:00
|
|
|
return Err(format!(
|
2021-02-07 23:16:25 -08:00
|
|
|
"argument {} in call to {} is of wrong type (expected {})",
|
|
|
|
|
idx + 1,
|
|
|
|
|
self.name,
|
|
|
|
|
arg_types[idx].to_str()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if idx == (arg_types.len() - 1) {
|
2021-02-14 16:33:17 -08:00
|
|
|
return Err(format!(
|
2021-02-07 23:16:25 -08:00
|
|
|
"too many arguments in call to {}",
|
|
|
|
|
self.name
|
|
|
|
|
));
|
|
|
|
|
}
|
2021-02-07 11:45:26 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 23:16:25 -08:00
|
|
|
|
2021-03-07 19:24:38 -08:00
|
|
|
match &self.function {
|
|
|
|
|
Operation::Internal(f) => Ok((f)(&n_args, vars, funcs)),
|
2021-02-14 16:33:17 -08:00
|
|
|
Operation::External(f) => {
|
|
|
|
|
// copy var table and add args
|
2021-03-07 19:24:38 -08:00
|
|
|
let mut temp = vars.clone();
|
2021-02-14 16:33:17 -08:00
|
|
|
for n in 0..f.arg_syms.len() {
|
|
|
|
|
temp.insert(
|
2021-03-07 19:24:38 -08:00
|
|
|
f.arg_syms[n].clone(),
|
2021-02-14 16:33:17 -08:00
|
|
|
Box::new(n_args.index(n))
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-03-07 19:24:38 -08:00
|
|
|
eval(&f.ast, &temp, funcs, self.loose_syms)
|
2021-02-14 16:33:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 11:45:26 -08:00
|
|
|
}
|
2021-02-07 23:16:25 -08:00
|
|
|
}
|
2021-02-07 11:45:26 -08:00
|
|
|
|
2021-02-14 16:33:17 -08:00
|
|
|
pub fn declare(
|
2021-03-07 19:24:38 -08:00
|
|
|
ft: &mut Box<FTable>,
|
2021-02-14 16:33:17 -08:00
|
|
|
f: Box<Function>
|
|
|
|
|
) -> Option<String> {
|
2021-03-07 19:24:38 -08:00
|
|
|
if let Operation::External(fun) = &f.function {
|
2021-02-14 16:33:17 -08:00
|
|
|
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()
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-02-07 23:16:25 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-07 19:24:38 -08:00
|
|
|
ft.insert(f.name.clone(), f);
|
2021-02-14 16:33:17 -08:00
|
|
|
None
|
|
|
|
|
}
|