aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-15 06:55:26 +0100
committerAkshay <[email protected]>2021-04-15 06:55:26 +0100
commit73514f48554dcd860f1c1c675d53c8d3ae6aa800 (patch)
tree931e81fdc34a80aaeb73709aecac200263953c30
parentabcf2b32777ffb934788e3219cacc2bbc048b6a3 (diff)
change function bodies to be single expresssion
-rw-r--r--src/lisp/eval.rs14
-rw-r--r--src/lisp/expr.rs2
-rw-r--r--src/lisp/prelude.rs3
-rw-r--r--src/lisp/test.lisp4
4 files changed, 12 insertions, 11 deletions
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
71 let nested_env: Environment = 71 let nested_env: Environment =
72 f.params.clone().into_iter().zip(args).collect(); 72 f.params.clone().into_iter().zip(args).collect();
73 self.app.lisp_env.push(nested_env); 73 self.app.lisp_env.push(nested_env);
74 let result = if f.body.is_empty() { 74 let result = self.eval(&f.body);
75 Ok(LispExpr::Unit)
76 } else {
77 self.eval(&LispExpr::List(f.body.clone()))
78 };
79 self.app.lisp_env.pop(); 75 self.app.lisp_env.pop();
80 result 76 result
81 } 77 }
@@ -111,7 +107,7 @@ where
111 } 107 }
112 Ok(LispExpr::Unit) 108 Ok(LispExpr::Unit)
113 } 109 }
114 [LispExpr::List(shorthand), LispExpr::List(body)] => { 110 [LispExpr::List(shorthand), body] => {
115 // (define (func arg) <body>) shorthand 111 // (define (func arg) <body>) shorthand
116 112
117 let id = shorthand[0].unwrap_ident(); 113 let id = shorthand[0].unwrap_ident();
@@ -126,7 +122,7 @@ where
126 .collect::<Result<Vec<Ident>, LispError>>()?; 122 .collect::<Result<Vec<Ident>, LispError>>()?;
127 let value = LispExpr::Function(LispFunction { 123 let value = LispExpr::Function(LispFunction {
128 params, 124 params,
129 body: body.to_vec(), 125 body: Box::new(body.clone()),
130 }); 126 });
131 127
132 let local_env = &mut self.app.lisp_env.last_mut(); 128 let local_env = &mut self.app.lisp_env.last_mut();
@@ -327,10 +323,10 @@ pub fn create_lambda(cdr: &[LispExpr]) -> Result<LispExpr, LispError> {
327 return Err(arity.to_error()); 323 return Err(arity.to_error());
328 } 324 }
329 match cdr { 325 match cdr {
330 [LispExpr::List(params), LispExpr::List(body)] if type_match!(params, (..) => LispExpr::Ident(_)) => { 326 [LispExpr::List(params), body] if type_match!(params, (..) => LispExpr::Ident(_)) => {
331 Ok(LispExpr::Function(LispFunction { 327 Ok(LispExpr::Function(LispFunction {
332 params: params.iter().map(|p| p.unwrap_ident()).collect::<Vec<_>>(), 328 params: params.iter().map(|p| p.unwrap_ident()).collect::<Vec<_>>(),
333 body: body.clone(), 329 body: Box::new(body.clone()),
334 })) 330 }))
335 } 331 }
336 _ => { 332 _ => {
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;
62#[derive(Clone)] 62#[derive(Clone)]
63pub struct LispFunction { 63pub struct LispFunction {
64 pub params: Vec<String>, 64 pub params: Vec<String>,
65 pub body: Vec<LispExpr>, 65 pub body: Box<LispExpr>,
66} 66}
67 67
68#[derive(Clone)] 68#[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<Environment, LispError> {
489 } 489 }
490 }); 490 });
491 491
492 primitive!(env, Arity::Exact(1), "id", |args, _| {
493 Ok(args[0].clone())
494 });
492 Ok(env) 495 Ok(env)
493} 496}
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 @@
14 (for (i '(1 2 3)) (for (j '(1 2 3)) (+ i j))) 14 (for (i '(1 2 3)) (for (j '(1 2 3)) (+ i j)))
15 (list '(2 3 4) '(3 4 5) '(4 5 6))) 15 (list '(2 3 4) '(3 4 5) '(4 5 6)))
16 16
17 17(assert-eq
18 ((lambda (x) x) 2)
19 2)