From 305bf638f823a41f391936712eef302bc6733d00 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 23 Mar 2021 18:56:25 +0530 Subject: expose functions to lisp interface, add primitives with macros --- src/lisp/expr.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/lisp/expr.rs') diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs index 4676a3e..adacc1c 100644 --- a/src/lisp/expr.rs +++ b/src/lisp/expr.rs @@ -11,7 +11,7 @@ pub enum LispExpr { StringLit(String), BoolLit(bool), Ident(String), - PrimitiveFunc(fn(&[LispExpr], Option<&mut AppState>) -> Result), + PrimitiveFunc(PrimitiveFunc), Function(LispFunction), // none of these depths should be zero @@ -21,6 +21,23 @@ pub enum LispExpr { Quote(Box, u32), } +#[derive(Clone)] +pub struct PrimitiveFunc { + pub arity: Option, + pub closure: fn(&[LispExpr], &mut AppState) -> Result, +} + +impl PrimitiveFunc { + pub fn call(&self, args: &[LispExpr], app: &mut AppState) -> Result { + if let Some(arity) = self.arity { + if args.len() < arity { + return Err(LispError::EvalError); + } + } + (self.closure)(args, app) + } +} + impl LispExpr { pub fn comma(self, n: u32) -> LispExpr { match self { -- cgit v1.2.3