diff options
Diffstat (limited to 'src/lisp/expr.rs')
-rw-r--r-- | src/lisp/expr.rs | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs index 8fd794e..059113b 100644 --- a/src/lisp/expr.rs +++ b/src/lisp/expr.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use std::{convert::TryFrom, fmt}; | 1 | use std::{cmp::PartialEq, convert::TryFrom, fmt}; |
2 | 2 | ||
3 | use crate::app::AppState; | 3 | use crate::app::AppState; |
4 | use crate::lisp::{ | 4 | use crate::lisp::{ |
@@ -177,6 +177,47 @@ impl fmt::Display for LispExpr { | |||
177 | } | 177 | } |
178 | } | 178 | } |
179 | 179 | ||
180 | impl fmt::Debug for LispExpr { | ||
181 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
182 | match self { | ||
183 | LispExpr::Unit => f.debug_tuple("Unit").finish(), | ||
184 | LispExpr::Number(n) => write!(f, "Number({:?})", n), | ||
185 | LispExpr::List(l) => f.debug_list().entries(l.iter()).finish(), | ||
186 | LispExpr::StringLit(s) => write!(f, "String({:?})", s), | ||
187 | LispExpr::Char(c) => write!(f, "Char({:?})", c), | ||
188 | LispExpr::BoolLit(b) => write!(f, "Bool({:?})", b), | ||
189 | LispExpr::Ident(s) => write!(f, "Ident({})", s), | ||
190 | LispExpr::PrimitiveFunc(_) => write!(f, "Primitive"), | ||
191 | LispExpr::Function(func) => write!(f, "<#lambda> {}", func.params.join(" ")), | ||
192 | LispExpr::Quasiquote(val, depth) => write!(f, "{}{}", "`".repeat(*depth as usize), val), | ||
193 | LispExpr::Comma(val, depth) => write!(f, "{}{}", ",".repeat(*depth as usize), val), | ||
194 | LispExpr::CommaAt(val, depth) => write!(f, "{}@{}", ",".repeat(*depth as usize), val), | ||
195 | LispExpr::Quote(val, depth) => write!(f, "{}{}", "'".repeat(*depth as usize), val), | ||
196 | } | ||
197 | } | ||
198 | } | ||
199 | |||
200 | impl PartialEq for LispExpr { | ||
201 | fn eq(&self, other: &Self) -> bool { | ||
202 | match (self, other) { | ||
203 | (LispExpr::Unit, LispExpr::Unit) => true, | ||
204 | (LispExpr::Number(s), LispExpr::Number(o)) => s == o, | ||
205 | (LispExpr::List(s), LispExpr::List(o)) => s.iter().zip(o).all(|(a, b)| a == b), | ||
206 | (LispExpr::StringLit(s), LispExpr::StringLit(o)) => s == o, | ||
207 | (LispExpr::Char(s), LispExpr::Char(o)) => s == o, | ||
208 | (LispExpr::BoolLit(s), LispExpr::BoolLit(o)) => s == o, | ||
209 | (LispExpr::Ident(s), LispExpr::Ident(o)) => s == o, | ||
210 | (LispExpr::PrimitiveFunc(_), LispExpr::PrimitiveFunc(_)) => false, | ||
211 | (LispExpr::Function(_), LispExpr::Function(_)) => false, | ||
212 | (LispExpr::Quasiquote(s, sd), LispExpr::Quasiquote(o, od)) => (s, sd) == (o, od), | ||
213 | (LispExpr::Comma(s, sd), LispExpr::Comma(o, od)) => (s, sd) == (o, od), | ||
214 | (LispExpr::CommaAt(s, sd), LispExpr::CommaAt(o, od)) => (s, sd) == (o, od), | ||
215 | (LispExpr::Quote(s, sd), LispExpr::Quote(o, od)) => (s, sd) == (o, od), | ||
216 | _ => false, | ||
217 | } | ||
218 | } | ||
219 | } | ||
220 | |||
180 | impl TryFrom<LispExpr> for LispNumber { | 221 | impl TryFrom<LispExpr> for LispNumber { |
181 | type Error = LispError; | 222 | type Error = LispError; |
182 | fn try_from(value: LispExpr) -> Result<Self, Self::Error> { | 223 | fn try_from(value: LispExpr) -> Result<Self, Self::Error> { |
@@ -197,17 +238,6 @@ impl<'a> TryFrom<&'a LispExpr> for &'a LispNumber { | |||
197 | } | 238 | } |
198 | } | 239 | } |
199 | 240 | ||
200 | //impl TryFrom<LispExpr> for Ident { | ||
201 | // type Error = LispError; | ||
202 | // fn try_from(value: LispExpr) -> Result<Self, Self::Error> { | ||
203 | // match value { | ||
204 | // LispExpr::Ident(i) => Ok(i), | ||
205 | // _ => Err(LispError::Eval(EvalError::TypeMismatch)), | ||
206 | // } | ||
207 | // } | ||
208 | //} | ||
209 | // | ||
210 | // | ||
211 | impl TryFrom<LispExpr> for String { | 241 | impl TryFrom<LispExpr> for String { |
212 | type Error = LispError; | 242 | type Error = LispError; |
213 | fn try_from(value: LispExpr) -> Result<Self, Self::Error> { | 243 | fn try_from(value: LispExpr) -> Result<Self, Self::Error> { |