diff options
Diffstat (limited to 'src/lisp/expr.rs')
-rw-r--r-- | src/lisp/expr.rs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs index 31476c9..aeaa2d8 100644 --- a/src/lisp/expr.rs +++ b/src/lisp/expr.rs | |||
@@ -62,6 +62,7 @@ pub enum LispExpr { | |||
62 | Unit, | 62 | Unit, |
63 | Number(LispNumber), | 63 | Number(LispNumber), |
64 | List(Vec<LispExpr>), | 64 | List(Vec<LispExpr>), |
65 | DottedList(Vec<LispExpr>), | ||
65 | StringLit(String), | 66 | StringLit(String), |
66 | BoolLit(bool), | 67 | BoolLit(bool), |
67 | Ident(Ident), | 68 | Ident(Ident), |
@@ -157,7 +158,14 @@ impl LispExpr { | |||
157 | pub fn unwrap_list(&self) -> Vec<LispExpr> { | 158 | pub fn unwrap_list(&self) -> Vec<LispExpr> { |
158 | match &self { | 159 | match &self { |
159 | LispExpr::List(p) => p.clone(), | 160 | LispExpr::List(p) => p.clone(), |
160 | _ => panic!("attempt to call `unwrap_number` on invalid type"), | 161 | _ => panic!("attempt to call `unwrap_list` on invalid type"), |
162 | } | ||
163 | } | ||
164 | |||
165 | pub fn unwrap_dotted_list(&self) -> Vec<LispExpr> { | ||
166 | match &self { | ||
167 | LispExpr::DottedList(p) => p.clone(), | ||
168 | _ => panic!("attempt to call `unwrap_dotted_list` on invalid type"), | ||
161 | } | 169 | } |
162 | } | 170 | } |
163 | 171 | ||
@@ -209,6 +217,13 @@ impl fmt::Display for LispExpr { | |||
209 | &l.iter().map(|expr| format!("{}", expr)).collect::<Vec<_>>()[..].join(" ") | 217 | &l.iter().map(|expr| format!("{}", expr)).collect::<Vec<_>>()[..].join(" ") |
210 | )?; | 218 | )?; |
211 | } | 219 | } |
220 | LispExpr::DottedList(l) => { | ||
221 | write!(f, "(")?; | ||
222 | for item in &l[..l.len() - 1] { | ||
223 | write!(f, "{} ", item)?; | ||
224 | } | ||
225 | write!(f, ". {})", &l[l.len() - 1])?; | ||
226 | } | ||
212 | LispExpr::StringLit(s) => write!(f, "{:?}", s)?, | 227 | LispExpr::StringLit(s) => write!(f, "{:?}", s)?, |
213 | LispExpr::Char(c) => write!(f, "{}", c)?, | 228 | LispExpr::Char(c) => write!(f, "{}", c)?, |
214 | LispExpr::BoolLit(b) => { | 229 | LispExpr::BoolLit(b) => { |
@@ -237,7 +252,14 @@ impl fmt::Debug for LispExpr { | |||
237 | match self { | 252 | match self { |
238 | LispExpr::Unit => f.debug_tuple("Unit").finish(), | 253 | LispExpr::Unit => f.debug_tuple("Unit").finish(), |
239 | LispExpr::Number(n) => write!(f, "Number({:?})", n), | 254 | LispExpr::Number(n) => write!(f, "Number({:?})", n), |
240 | LispExpr::List(l) => f.debug_list().entries(l.iter()).finish(), | 255 | LispExpr::List(l) => { |
256 | write!(f, "List ")?; | ||
257 | f.debug_list().entries(l.iter()).finish() | ||
258 | }, | ||
259 | LispExpr::DottedList(l) => { | ||
260 | write!(f, "DottedList ")?; | ||
261 | f.debug_list().entries(l.iter()).finish() | ||
262 | }, | ||
241 | LispExpr::StringLit(s) => write!(f, "String({:?})", s), | 263 | LispExpr::StringLit(s) => write!(f, "String({:?})", s), |
242 | LispExpr::Char(c) => write!(f, "Char({:?})", c), | 264 | LispExpr::Char(c) => write!(f, "Char({:?})", c), |
243 | LispExpr::BoolLit(b) => write!(f, "Bool({:?})", b), | 265 | LispExpr::BoolLit(b) => write!(f, "Bool({:?})", b), |