aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/prelude.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lisp/prelude.rs')
-rw-r--r--src/lisp/prelude.rs46
1 files changed, 39 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)