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;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use crate::cell::{Ctr, Cell, Type};
|
|
|
|
|
use crate::vars::{VTable};
|
|
|
|
|
use crate::eval::eval;
|
|
|
|
|
|
|
|
|
|
// Standardized function signature for stdlib functions
|
|
|
|
|
pub type Operation = fn(Box<Cell>, Box<VTable>, Box<FTable>) -> Box<Cell>;
|
|
|
|
|
pub type FTable = HashMap<String, Function>;
|
|
|
|
|
|
|
|
|
|
/* Function Args
|
|
|
|
|
* If Lazy, is an integer denoting number of args
|
|
|
|
|
* If Strict, is a list of Ctrs (with no wrapped values) denoting arg type.
|
|
|
|
|
*/
|
|
|
|
|
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 times_called: u128,
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
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,
|
|
|
|
|
s
|
|
|
|
|
)
|
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-07 23:16:25 -08:00
|
|
|
let mut passes = false;
|
|
|
|
|
match self.args {
|
|
|
|
|
Args::Lazy(num) => {
|
|
|
|
|
if num < 0 {
|
|
|
|
|
passes = true
|
2021-02-07 11:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
2021-02-07 23:16:25 -08:00
|
|
|
if !(num == (args.len() - 1)) {
|
|
|
|
|
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) => {
|
|
|
|
|
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) {
|
2021-02-07 11:45:26 -08:00
|
|
|
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) {
|
|
|
|
|
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
|
|
|
|
|
));
|
|
|
|
|
}
|
2021-02-07 11:45:26 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 23:16:25 -08:00
|
|
|
|
|
|
|
|
self.times_called += 1;
|
|
|
|
|
return Ok((self.function)(args, vars, funcs));
|
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-07 23:16:25 -08:00
|
|
|
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 get(
|
|
|
|
|
&mut self,
|
|
|
|
|
identifier: String
|
|
|
|
|
) -> Result<Box<Cell>, String> {
|
|
|
|
|
self.get(identifier)
|
|
|
|
|
}
|
2021-02-07 11:45:26 -08:00
|
|
|
}
|