From 81488c3ea8d66680f739cee087ad9aa7619f5e0e Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 18 Apr 2021 11:59:44 +0530 Subject: clippy lint; add `get-pixel` primitive --- src/lisp/prelude.rs | 46 +++++++++++++++++++++++++++++++++++++++------- src/lisp/std.lisp | 15 +++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) (limited to 'src/lisp') diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index d6e1874..7cb46ff 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -42,8 +42,8 @@ macro_rules! type_match { let mut temp_vec = vec![]; $( temp_vec.push(matches!(&$args[$range], $kind)); - )+ - temp_vec.iter().all(|&t| t) + )+ + temp_vec.iter().all(|&t| t) } }; ($args:expr, $($range:expr => $kind:pat),+) => { @@ -54,8 +54,8 @@ macro_rules! type_match { for arg in arg_range { temp_vec.push(matches!(arg, $kind)); } - )+ - temp_vec.iter().all(|&t| t) + )+ + temp_vec.iter().all(|&t| t) } } } @@ -139,6 +139,17 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(1), "integer", |args, _| { + if type_match!(args, 0 => LispExpr::Number(_)) { + Ok(LispExpr::Number(match args[0].unwrap_number() { + LispNumber::Float(f) => LispNumber::Integer(f as i64), + s => s, + })) + } else { + Err(EvalError::TypeMismatch.into()) + } + }); + primitive!(env, Arity::Atleast(1), "begin", |args, _| { Ok(args.iter().last().unwrap().clone()) }); @@ -394,9 +405,9 @@ pub fn new_env() -> Result { primitive!(env, Arity::Exact(3), "set-pixel!", |args, app| { if type_match!( - args, 0 => LispExpr::Number(LispNumber::Integer(_)), - 1 => LispExpr::Number(LispNumber::Integer(_)), - 2 => LispExpr::BoolLit(_) + args, 0 => LispExpr::Number(LispNumber::Integer(_)), + 1 => LispExpr::Number(LispNumber::Integer(_)), + 2 => LispExpr::BoolLit(_) ) { let x = args[0].unwrap_number(); let y = args[1].unwrap_number(); @@ -418,6 +429,27 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(2), "get-pixel", |args, app| { + if type_match!( + args, + 0 => LispExpr::Number(LispNumber::Integer(_)), + 1 => LispExpr::Number(LispNumber::Integer(_)) + ) { + let x = args[0].unwrap_number(); + let y = args[1].unwrap_number(); + let get_loc: MapPoint = (x.unwrap_integer(), y.unwrap_integer()) + .try_into() + .map_err(|_| -> LispError { EvalError::InvalidCoordinates((x, y)).into() })?; + if !app.pixmap.contains(get_loc) { + Err(EvalError::InvalidCoordinates((x, y)).into()) + } else { + Ok(LispExpr::BoolLit(app.pixmap.get(get_loc))) + } + } else { + Err(EvalError::TypeMismatch.into()) + } + }); + primitive!(env, Arity::Exact(0), "commit", |_, app| { app.commit_operation(); Ok(LispExpr::Unit) diff --git a/src/lisp/std.lisp b/src/lisp/std.lisp index 6c5beed..a256125 100644 --- a/src/lisp/std.lisp +++ b/src/lisp/std.lisp @@ -40,6 +40,13 @@ (cons (func (car ls)) (map func (cdr ls))))) +(define (zip l1 l2) + (if (or (null? l1) + (null? l2)) + '() + (cons (cons (car l1) (car l2)) + (zip (cdr l1) (cdr l2))))) + (define (filter pred ls) (if (null? ls) '() @@ -57,3 +64,11 @@ (define (sum ls) (fold 0 + ls)) (define (product ls) (fold 1 * ls)) + +(define (reverse ls) + (begin + (define (rev-helper p acc) + (if (null? p) + acc + (rev-helper (cdr p) (cons (car p) acc)))) + (rev-helper ls '()))) -- cgit v1.2.3