From 73514f48554dcd860f1c1c675d53c8d3ae6aa800 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 15 Apr 2021 11:25:26 +0530 Subject: change function bodies to be single expresssion --- src/lisp/eval.rs | 14 +++++--------- src/lisp/expr.rs | 2 +- src/lisp/prelude.rs | 3 +++ src/lisp/test.lisp | 4 +++- 4 files changed, 12 insertions(+), 11 deletions(-) (limited to 'src/lisp') diff --git a/src/lisp/eval.rs b/src/lisp/eval.rs index b404662..75cb5c9 100644 --- a/src/lisp/eval.rs +++ b/src/lisp/eval.rs @@ -71,11 +71,7 @@ where let nested_env: Environment = f.params.clone().into_iter().zip(args).collect(); self.app.lisp_env.push(nested_env); - let result = if f.body.is_empty() { - Ok(LispExpr::Unit) - } else { - self.eval(&LispExpr::List(f.body.clone())) - }; + let result = self.eval(&f.body); self.app.lisp_env.pop(); result } @@ -111,7 +107,7 @@ where } Ok(LispExpr::Unit) } - [LispExpr::List(shorthand), LispExpr::List(body)] => { + [LispExpr::List(shorthand), body] => { // (define (func arg) ) shorthand let id = shorthand[0].unwrap_ident(); @@ -126,7 +122,7 @@ where .collect::, LispError>>()?; let value = LispExpr::Function(LispFunction { params, - body: body.to_vec(), + body: Box::new(body.clone()), }); let local_env = &mut self.app.lisp_env.last_mut(); @@ -327,10 +323,10 @@ pub fn create_lambda(cdr: &[LispExpr]) -> Result { return Err(arity.to_error()); } match cdr { - [LispExpr::List(params), LispExpr::List(body)] if type_match!(params, (..) => LispExpr::Ident(_)) => { + [LispExpr::List(params), body] if type_match!(params, (..) => LispExpr::Ident(_)) => { Ok(LispExpr::Function(LispFunction { params: params.iter().map(|p| p.unwrap_ident()).collect::>(), - body: body.clone(), + body: Box::new(body.clone()), })) } _ => { diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs index d086ecf..045991e 100644 --- a/src/lisp/expr.rs +++ b/src/lisp/expr.rs @@ -62,7 +62,7 @@ pub type BoolLit = bool; #[derive(Clone)] pub struct LispFunction { pub params: Vec, - pub body: Vec, + pub body: Box, } #[derive(Clone)] diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index 1d37a32..d6e1874 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -489,5 +489,8 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(1), "id", |args, _| { + Ok(args[0].clone()) + }); Ok(env) } diff --git a/src/lisp/test.lisp b/src/lisp/test.lisp index 53b0f59..a167f28 100644 --- a/src/lisp/test.lisp +++ b/src/lisp/test.lisp @@ -14,4 +14,6 @@ (for (i '(1 2 3)) (for (j '(1 2 3)) (+ i j))) (list '(2 3 4) '(3 4 5) '(4 5 6))) - +(assert-eq + ((lambda (x) x) 2) + 2) -- cgit v1.2.3