From 1a152b5cdaf3e636a5f495e81895d35d0f841c44 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 24 Mar 2021 13:17:54 +0530 Subject: implement boolean primitives; `begin` form --- src/lisp/env.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lisp/env.rs b/src/lisp/env.rs index 6338f91..c5ff6d3 100644 --- a/src/lisp/env.rs +++ b/src/lisp/env.rs @@ -67,7 +67,7 @@ pub fn with_prelude() -> Environment { } } }); - primitive!(env, Some(1), "and", |args, _| { + primitive!(env, Some(2), "and", |args, _| { if args .iter() .any(|arg| matches!(arg, LispExpr::BoolLit(false))) @@ -77,6 +77,35 @@ pub fn with_prelude() -> Environment { Ok(LispExpr::BoolLit(true)) } }); + primitive!(env, Some(2), "or", |args, _| { + if args + .iter() + .any(|arg| matches!(arg, LispExpr::BoolLit(true))) + { + Ok(LispExpr::BoolLit(true)) + } else { + Ok(LispExpr::BoolLit(false)) + } + }); + primitive!(env, Some(1), "not", |args, _| { + match args { + [val] => { + if matches!(val, LispExpr::BoolLit(false)) { + Ok(LispExpr::BoolLit(true)) + } else { + Ok(LispExpr::BoolLit(false)) + } + } + _ => Err(LispError::EvalError), + } + }); + primitive!(env, None, "begin", |args, _| { + if args.is_empty() { + Err(LispError::EvalError) + } else { + Ok(args.into_iter().last().unwrap().clone()) + } + }); env } -- cgit v1.2.3