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 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/lisp/eval.rs') 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()), })) } _ => { -- cgit v1.2.3