/* 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 . */ 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>; // Standardized function signature for stdlib functions pub type InternalOperation = fn(&Box, &mut Box, &mut Box) -> Box; 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, // list of argument string tokens arg_syms: Vec } /* 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 * If Strict, is a list of type tags denoting argument type. */ pub enum Args { // signed: -1 denotes infinite args Lazy(i128), Strict(Vec) } // 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 } impl Function { /* call * routine is called by eval when a function call is detected */ pub fn call( &self, args: &Box, vars: &mut Box, funcs: &mut Box ) -> Result, String> { let n_args: &Box; let outer_owner: Box; 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 ) ) } n_args = &outer_owner; } else { n_args = args; } match &self.args { Args::Lazy(num) => { if *num < 0 { } if !(*num == (n_args.len() - 1)) { return Err(format!("expected {} args in call to {}", num, self.name)) } }, Args::Strict(arg_types) => { let mut idx: usize = 0; let passes = n_args.circuit(&mut |c: &Ctr| { if idx >= arg_types.len() { return false; } if let Ctr::None = c { return false; } let ret = arg_types[idx] == c.to_type(); if ret { idx += 1; } return ret; }); if passes && idx < (arg_types.len() - 1) { return Err(format!( "{} too little arguments in call to {}", arg_types.len() - (idx + 1), self.name )); } 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 )); } } } } 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) } } } } pub fn declare( ft: &mut Box, f: Box ) -> Option { 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() ); } } ft.insert(f.name.clone(), f); None }