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/app.rs | 8 +------- src/lisp/prelude.rs | 46 +++++++++++++++++++++++++++++++++++++++------- src/lisp/std.lisp | 15 +++++++++++++++ src/main.rs | 3 ++- 4 files changed, 57 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 2e14dbc..5ba8fb1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -832,13 +832,7 @@ impl<'ctx> AppState<'ctx> { end, active_end, }); - } else if end.is_none() { - self.brush = Brush::RectSelect(RectSelectBrush { - start, - end: contact, - active_end, - }); - } else if active_end { + } else if end.is_none() || active_end { self.brush = Brush::RectSelect(RectSelectBrush { start, end: contact, 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 '()))) diff --git a/src/main.rs b/src/main.rs index d6f0204..225c37c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ #![allow( clippy::upper_case_acronyms, clippy::vec_init_then_push, - clippy::unsound_collection_transmute + clippy::unsound_collection_transmute, + clippy::new_without_default )] mod app; -- cgit v1.2.3