aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/expr.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-03-23 13:26:25 +0000
committerAkshay <[email protected]>2021-03-23 13:26:25 +0000
commit305bf638f823a41f391936712eef302bc6733d00 (patch)
tree6a9cc8a41c691bed2027c8debdc4391aab837c89 /src/lisp/expr.rs
parenta0fce05399b3ee284b6c60a409fad74c23432ce8 (diff)
expose functions to lisp interface, add primitives with macros
Diffstat (limited to 'src/lisp/expr.rs')
-rw-r--r--src/lisp/expr.rs19
1 files changed, 18 insertions, 1 deletions
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 {
11 StringLit(String), 11 StringLit(String),
12 BoolLit(bool), 12 BoolLit(bool),
13 Ident(String), 13 Ident(String),
14 PrimitiveFunc(fn(&[LispExpr], Option<&mut AppState>) -> Result<LispExpr, LispError>), 14 PrimitiveFunc(PrimitiveFunc),
15 Function(LispFunction), 15 Function(LispFunction),
16 16
17 // none of these depths should be zero 17 // none of these depths should be zero
@@ -21,6 +21,23 @@ pub enum LispExpr {
21 Quote(Box<LispExpr>, u32), 21 Quote(Box<LispExpr>, u32),
22} 22}
23 23
24#[derive(Clone)]
25pub struct PrimitiveFunc {
26 pub arity: Option<usize>,
27 pub closure: fn(&[LispExpr], &mut AppState) -> Result<LispExpr, LispError>,
28}
29
30impl PrimitiveFunc {
31 pub fn call(&self, args: &[LispExpr], app: &mut AppState) -> Result<LispExpr, LispError> {
32 if let Some(arity) = self.arity {
33 if args.len() < arity {
34 return Err(LispError::EvalError);
35 }
36 }
37 (self.closure)(args, app)
38 }
39}
40
24impl LispExpr { 41impl LispExpr {
25 pub fn comma(self, n: u32) -> LispExpr { 42 pub fn comma(self, n: u32) -> LispExpr {
26 match self { 43 match self {