aboutsummaryrefslogtreecommitdiff
path: root/src/lisp
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-11 11:01:32 +0100
committerAkshay <[email protected]>2021-04-12 11:02:49 +0100
commitabcf2b32777ffb934788e3219cacc2bbc048b6a3 (patch)
treed310c7d2c69531a4583dac7ab34705f6ae5c12c7 /src/lisp
parentb0685c1638044b85dc7e8b07555a7b639b54d69a (diff)
add `selection-start` and `selection-end` primitives
Diffstat (limited to 'src/lisp')
-rw-r--r--src/lisp/eval.rs13
-rw-r--r--src/lisp/expr.rs6
-rw-r--r--src/lisp/number.rs7
-rw-r--r--src/lisp/prelude.rs38
4 files changed, 56 insertions, 8 deletions
diff --git a/src/lisp/eval.rs b/src/lisp/eval.rs
index 0cf267c..b404662 100644
--- a/src/lisp/eval.rs
+++ b/src/lisp/eval.rs
@@ -217,12 +217,13 @@ where
217 217
218 pub fn eval_for(&mut self, args: &[LispExpr]) -> Result<LispExpr, LispError> { 218 pub fn eval_for(&mut self, args: &[LispExpr]) -> Result<LispExpr, LispError> {
219 let arity = Arity::Exact(2); 219 let arity = Arity::Exact(2);
220 let valid_binding_stmt = |expr: &LispExpr| 220 let valid_binding_stmt = |expr: &LispExpr| {
221 matches!( 221 matches!(
222 expr, 222 expr,
223 LispExpr::List(v) 223 LispExpr::List(v)
224 if v.len() == 2 224 if v.len() == 2
225 && matches!(v[0], LispExpr::Ident(_))); 225 && matches!(v[0], LispExpr::Ident(_)))
226 };
226 227
227 if !arity.check(args) { 228 if !arity.check(args) {
228 Err(arity.to_error()) 229 Err(arity.to_error())
@@ -258,8 +259,8 @@ where
258 } 259 }
259 _ => { 260 _ => {
260 error!("invalid for loop args"); 261 error!("invalid for loop args");
261 Err(EvalError::BadForm.into()) 262 Err(EvalError::BadForm.into())
262 }, 263 }
263 } 264 }
264 } 265 }
265 } 266 }
diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs
index 692f951..d086ecf 100644
--- a/src/lisp/expr.rs
+++ b/src/lisp/expr.rs
@@ -310,6 +310,12 @@ impl TryFrom<LispExpr> for LispNumber {
310 } 310 }
311} 311}
312 312
313impl From<i64> for LispExpr {
314 fn from(num: i64) -> Self {
315 LispExpr::Number(num.into())
316 }
317}
318
313impl<'a> TryFrom<&'a LispExpr> for &'a LispNumber { 319impl<'a> TryFrom<&'a LispExpr> for &'a LispNumber {
314 type Error = LispError; 320 type Error = LispError;
315 fn try_from(value: &'a LispExpr) -> Result<Self, Self::Error> { 321 fn try_from(value: &'a LispExpr) -> Result<Self, Self::Error> {
diff --git a/src/lisp/number.rs b/src/lisp/number.rs
index 4824e21..06c6baa 100644
--- a/src/lisp/number.rs
+++ b/src/lisp/number.rs
@@ -1,5 +1,6 @@
1use std::{ 1use std::{
2 cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}, 2 cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
3 convert::From,
3 fmt, 4 fmt,
4 ops::{Add, Mul, Sub}, 5 ops::{Add, Mul, Sub},
5}; 6};
@@ -113,3 +114,9 @@ impl fmt::Display for LispNumber {
113 } 114 }
114 } 115 }
115} 116}
117
118impl From<i64> for LispNumber {
119 fn from(target: i64) -> Self {
120 LispNumber::Integer(target)
121 }
122}
diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs
index e24ade5..1d37a32 100644
--- a/src/lisp/prelude.rs
+++ b/src/lisp/prelude.rs
@@ -1,6 +1,6 @@
1use crate::{ 1use crate::{
2 bitmap::MapPoint, 2 bitmap::MapPoint,
3 brush::Brush, 3 brush::{Brush, RectSelectBrush},
4 grid::GridKind, 4 grid::GridKind,
5 guide::Guide, 5 guide::Guide,
6 lisp::{ 6 lisp::{
@@ -11,7 +11,7 @@ use crate::{
11 }, 11 },
12 primitive, 12 primitive,
13 undo::PaintRecord, 13 undo::PaintRecord,
14 utils::load_script, 14 utils::{load_script, rect_coords},
15}; 15};
16 16
17use std::convert::TryInto; 17use std::convert::TryInto;
@@ -437,6 +437,40 @@ pub fn new_env() -> Result<Environment, LispError> {
437 } 437 }
438 }); 438 });
439 439
440 primitive!(env, Arity::Exact(0), "selection-start", |_, app| {
441 if let Brush::RectSelect(RectSelectBrush {
442 start: Some(s),
443 end: Some(e),
444 ..
445 }) = app.brush
446 {
447 let pt = rect_coords(s, e).0;
448 Ok(LispExpr::DottedList(vec![
449 (pt.x as i64).into(),
450 (pt.y as i64).into(),
451 ]))
452 } else {
453 Err(EvalError::CustomInternal("No active selection!").into())
454 }
455 });
456
457 primitive!(env, Arity::Exact(0), "selection-end", |_, app| {
458 if let Brush::RectSelect(RectSelectBrush {
459 start: Some(s),
460 end: Some(e),
461 ..
462 }) = app.brush
463 {
464 let pt = rect_coords(s, e).1;
465 Ok(LispExpr::DottedList(vec![
466 (pt.x as i64).into(),
467 (pt.y as i64).into(),
468 ]))
469 } else {
470 Err(EvalError::CustomInternal("No active selection!").into())
471 }
472 });
473
440 primitive!(env, Arity::Exact(2), "range", |args, _| { 474 primitive!(env, Arity::Exact(2), "range", |args, _| {
441 if type_match!( 475 if type_match!(
442 args, 476 args,