aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/eval.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lisp/eval.rs')
-rw-r--r--src/lisp/eval.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lisp/eval.rs b/src/lisp/eval.rs
index f7f918a..f49efb9 100644
--- a/src/lisp/eval.rs
+++ b/src/lisp/eval.rs
@@ -222,3 +222,51 @@ pub fn unwrap_ident(i: LispExpr) -> String {
222 _ => panic!("unwrap_ident expected string"), 222 _ => panic!("unwrap_ident expected string"),
223 } 223 }
224} 224}
225
226#[cfg(test)]
227mod tests {
228 use super::*;
229
230 use crate::lisp::{expr::LispExpr, lex::Lexer, number::LispNumber, parse::Parser};
231
232 fn run(code: &str, app: &mut AppState) -> LispExpr {
233 let mut parser = Parser::new(Lexer::new(code, 0));
234 eval(&parser.parse_single_expr().unwrap(), app).unwrap()
235 }
236
237 #[test]
238 fn eval_all() {
239 let sdl_context = sdl2::init().unwrap();
240 let ttf_context = sdl2::ttf::init().unwrap();
241 let mut app = AppState::init(100, 100, &sdl_context, &ttf_context, None, None);
242 eval_arithmetic(&mut app);
243 eval_logical(&mut app);
244 }
245
246 fn eval_arithmetic(app: &mut AppState) {
247 assert_eq!(
248 run("(+ 1 2 3)", app),
249 LispExpr::Number(LispNumber::Integer(6))
250 );
251 assert_eq!(
252 run("(+ 1.1 2.2 3.3)", app),
253 LispExpr::Number(LispNumber::Float(6.6))
254 );
255 assert_eq!(
256 run("(* 1 2 3 4 5)", app),
257 LispExpr::Number(LispNumber::Integer(120))
258 );
259 assert_eq!(run("(< 1 2)", app), LispExpr::BoolLit(true));
260 assert_eq!(run("(> 6 5 4 3 2 1)", app), LispExpr::BoolLit(true));
261 assert_eq!(run("(< 1 2 3 4 5 6)", app), LispExpr::BoolLit(true));
262 assert_eq!(run("(>= 5 5 4 3 2 1)", app), LispExpr::BoolLit(true));
263 assert_eq!(run("(<= 2 2 3 4 5 6)", app), LispExpr::BoolLit(true));
264 }
265
266 fn eval_logical(app: &mut AppState) {
267 assert_eq!(run("(and #t #t)", app), LispExpr::BoolLit(true));
268 assert_eq!(run("(or #f #t)", app), LispExpr::BoolLit(true));
269 assert_eq!(run("(not #t)", app), LispExpr::BoolLit(false));
270 assert_eq!(run("(not #f)", app), run("(not (not #t))", app));
271 }
272}