aboutsummaryrefslogtreecommitdiff
path: root/src/lisp
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-18 07:29:44 +0100
committerAkshay <[email protected]>2021-04-18 07:29:44 +0100
commit81488c3ea8d66680f739cee087ad9aa7619f5e0e (patch)
tree5ef0edb9401ee05a1999db2088d69daf0e0bb6ee /src/lisp
parent73514f48554dcd860f1c1c675d53c8d3ae6aa800 (diff)
clippy lint; add `get-pixel` primitive
Diffstat (limited to 'src/lisp')
-rw-r--r--src/lisp/prelude.rs46
-rw-r--r--src/lisp/std.lisp15
2 files changed, 54 insertions, 7 deletions
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 {
42 let mut temp_vec = vec![]; 42 let mut temp_vec = vec![];
43 $( 43 $(
44 temp_vec.push(matches!(&$args[$range], $kind)); 44 temp_vec.push(matches!(&$args[$range], $kind));
45 )+ 45 )+
46 temp_vec.iter().all(|&t| t) 46 temp_vec.iter().all(|&t| t)
47 } 47 }
48 }; 48 };
49 ($args:expr, $($range:expr => $kind:pat),+) => { 49 ($args:expr, $($range:expr => $kind:pat),+) => {
@@ -54,8 +54,8 @@ macro_rules! type_match {
54 for arg in arg_range { 54 for arg in arg_range {
55 temp_vec.push(matches!(arg, $kind)); 55 temp_vec.push(matches!(arg, $kind));
56 } 56 }
57 )+ 57 )+
58 temp_vec.iter().all(|&t| t) 58 temp_vec.iter().all(|&t| t)
59 } 59 }
60 } 60 }
61} 61}
@@ -139,6 +139,17 @@ pub fn new_env() -> Result<Environment, LispError> {
139 } 139 }
140 }); 140 });
141 141
142 primitive!(env, Arity::Exact(1), "integer", |args, _| {
143 if type_match!(args, 0 => LispExpr::Number(_)) {
144 Ok(LispExpr::Number(match args[0].unwrap_number() {
145 LispNumber::Float(f) => LispNumber::Integer(f as i64),
146 s => s,
147 }))
148 } else {
149 Err(EvalError::TypeMismatch.into())
150 }
151 });
152
142 primitive!(env, Arity::Atleast(1), "begin", |args, _| { 153 primitive!(env, Arity::Atleast(1), "begin", |args, _| {
143 Ok(args.iter().last().unwrap().clone()) 154 Ok(args.iter().last().unwrap().clone())
144 }); 155 });
@@ -394,9 +405,9 @@ pub fn new_env() -> Result<Environment, LispError> {
394 405
395 primitive!(env, Arity::Exact(3), "set-pixel!", |args, app| { 406 primitive!(env, Arity::Exact(3), "set-pixel!", |args, app| {
396 if type_match!( 407 if type_match!(
397 args, 0 => LispExpr::Number(LispNumber::Integer(_)), 408 args, 0 => LispExpr::Number(LispNumber::Integer(_)),
398 1 => LispExpr::Number(LispNumber::Integer(_)), 409 1 => LispExpr::Number(LispNumber::Integer(_)),
399 2 => LispExpr::BoolLit(_) 410 2 => LispExpr::BoolLit(_)
400 ) { 411 ) {
401 let x = args[0].unwrap_number(); 412 let x = args[0].unwrap_number();
402 let y = args[1].unwrap_number(); 413 let y = args[1].unwrap_number();
@@ -418,6 +429,27 @@ pub fn new_env() -> Result<Environment, LispError> {
418 } 429 }
419 }); 430 });
420 431
432 primitive!(env, Arity::Exact(2), "get-pixel", |args, app| {
433 if type_match!(
434 args,
435 0 => LispExpr::Number(LispNumber::Integer(_)),
436 1 => LispExpr::Number(LispNumber::Integer(_))
437 ) {
438 let x = args[0].unwrap_number();
439 let y = args[1].unwrap_number();
440 let get_loc: MapPoint = (x.unwrap_integer(), y.unwrap_integer())
441 .try_into()
442 .map_err(|_| -> LispError { EvalError::InvalidCoordinates((x, y)).into() })?;
443 if !app.pixmap.contains(get_loc) {
444 Err(EvalError::InvalidCoordinates((x, y)).into())
445 } else {
446 Ok(LispExpr::BoolLit(app.pixmap.get(get_loc)))
447 }
448 } else {
449 Err(EvalError::TypeMismatch.into())
450 }
451 });
452
421 primitive!(env, Arity::Exact(0), "commit", |_, app| { 453 primitive!(env, Arity::Exact(0), "commit", |_, app| {
422 app.commit_operation(); 454 app.commit_operation();
423 Ok(LispExpr::Unit) 455 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 @@
40 (cons (func (car ls)) 40 (cons (func (car ls))
41 (map func (cdr ls))))) 41 (map func (cdr ls)))))
42 42
43(define (zip l1 l2)
44 (if (or (null? l1)
45 (null? l2))
46 '()
47 (cons (cons (car l1) (car l2))
48 (zip (cdr l1) (cdr l2)))))
49
43(define (filter pred ls) 50(define (filter pred ls)
44 (if (null? ls) 51 (if (null? ls)
45 '() 52 '()
@@ -57,3 +64,11 @@
57 64
58(define (sum ls) (fold 0 + ls)) 65(define (sum ls) (fold 0 + ls))
59(define (product ls) (fold 1 * ls)) 66(define (product ls) (fold 1 * ls))
67
68(define (reverse ls)
69 (begin
70 (define (rev-helper p acc)
71 (if (null? p)
72 acc
73 (rev-helper (cdr p) (cons (car p) acc))))
74 (rev-helper ls '())))