aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/expr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lisp/expr.rs')
-rw-r--r--src/lisp/expr.rs46
1 files changed, 36 insertions, 10 deletions
diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs
index d2066e7..692f951 100644
--- a/src/lisp/expr.rs
+++ b/src/lisp/expr.rs
@@ -1,12 +1,18 @@
1use std::{cmp::PartialEq, convert::TryFrom, fmt}; 1use std::{
2 cmp::PartialEq,
3 convert::{From, TryFrom},
4 fmt,
5};
2 6
3use crate::{ 7use crate::{
4 app::AppState, 8 app::AppState,
9 bitmap::Axis,
10 guide::Guide,
5 lisp::{ 11 lisp::{
6 error::{EvalError, LispError}, 12 error::{EvalError, LispError},
7 eval::lookup, 13 eval::lookup,
8 number::LispNumber, 14 number::LispNumber,
9 EnvList, 15 Environment,
10 }, 16 },
11}; 17};
12 18
@@ -113,7 +119,7 @@ impl LispExpr {
113 } 119 }
114 } 120 }
115 121
116 pub fn compare(&self, other: &Self, envs: &EnvList) -> Result<BoolLit, LispError> { 122 pub fn compare(&self, other: &Self, envs: &[Environment]) -> Result<BoolLit, LispError> {
117 match (self, other) { 123 match (self, other) {
118 (LispExpr::Unit, LispExpr::Unit) => Ok(true), 124 (LispExpr::Unit, LispExpr::Unit) => Ok(true),
119 (LispExpr::Number(s), LispExpr::Number(o)) => Ok(s == o), 125 (LispExpr::Number(s), LispExpr::Number(o)) => Ok(s == o),
@@ -121,8 +127,8 @@ impl LispExpr {
121 .iter() 127 .iter()
122 .zip(o) 128 .zip(o)
123 .all(|(a, b)| matches!(a.compare(b, envs), Ok(true)))), 129 .all(|(a, b)| matches!(a.compare(b, envs), Ok(true)))),
124 (LispExpr::List(s), LispExpr::Unit) => Ok(s.len() == 0), 130 (LispExpr::List(s), LispExpr::Unit) => Ok(s.is_empty()),
125 (LispExpr::Unit, LispExpr::List(s)) => Ok(s.len() == 0), 131 (LispExpr::Unit, LispExpr::List(s)) => Ok(s.is_empty()),
126 (LispExpr::StringLit(s), LispExpr::StringLit(o)) => Ok(s == o), 132 (LispExpr::StringLit(s), LispExpr::StringLit(o)) => Ok(s == o),
127 (LispExpr::Char(s), LispExpr::Char(o)) => Ok(s == o), 133 (LispExpr::Char(s), LispExpr::Char(o)) => Ok(s == o),
128 (LispExpr::BoolLit(s), LispExpr::BoolLit(o)) => Ok(s == o), 134 (LispExpr::BoolLit(s), LispExpr::BoolLit(o)) => Ok(s == o),
@@ -152,7 +158,7 @@ impl LispExpr {
152 // have these be code gen'd somehow 158 // have these be code gen'd somehow
153 pub fn unwrap_number(&self) -> LispNumber { 159 pub fn unwrap_number(&self) -> LispNumber {
154 match &self { 160 match &self {
155 LispExpr::Number(p) => p.clone(), 161 LispExpr::Number(p) => *p,
156 _ => panic!("attempt to call `unwrap_number` on invalid type"), 162 _ => panic!("attempt to call `unwrap_number` on invalid type"),
157 } 163 }
158 } 164 }
@@ -200,10 +206,7 @@ impl LispExpr {
200 } 206 }
201 207
202 pub fn cast_bool(&self) -> bool { 208 pub fn cast_bool(&self) -> bool {
203 match &self { 209 !matches!(self, LispExpr::BoolLit(false))
204 LispExpr::BoolLit(false) => false,
205 _ => true,
206 }
207 } 210 }
208} 211}
209 212
@@ -347,3 +350,26 @@ impl TryFrom<LispExpr> for BoolLit {
347 } 350 }
348 } 351 }
349} 352}
353
354// conversion implementations
355
356impl From<Axis> for LispExpr {
357 fn from(axis: Axis) -> LispExpr {
358 LispExpr::Quote(
359 Box::new(LispExpr::Ident(match axis {
360 Axis::X => "X".into(),
361 Axis::Y => "Y".into(),
362 })),
363 1,
364 )
365 }
366}
367
368impl From<Guide> for LispExpr {
369 fn from(guide: Guide) -> LispExpr {
370 LispExpr::List(vec![
371 guide.axis.into(),
372 LispExpr::Number(LispNumber::Integer(guide.offset as i64)),
373 ])
374 }
375}