summaryrefslogtreecommitdiff
path: root/lisp/src/ast.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/src/ast.rs')
-rw-r--r--lisp/src/ast.rs59
1 files changed, 58 insertions, 1 deletions
diff --git a/lisp/src/ast.rs b/lisp/src/ast.rs
index 8cac963..929383f 100644
--- a/lisp/src/ast.rs
+++ b/lisp/src/ast.rs
@@ -1,7 +1,64 @@
-#[derive(Clone, Debug, PartialEq, Eq)]
+use std::fmt;
+
+#[derive(Clone, PartialEq, Eq)]
pub enum Val {
Atom(String),
List(Vec<Self>),
I64(i64),
String(String),
}
+
+impl fmt::Debug for Val {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Val::Atom(v) => write!(f, "{v}"),
+ Val::List(vs) => {
+ f.write_str("(")?;
+ for (i, v) in vs.iter().enumerate() {
+ if i != 0 {
+ f.write_str(" ")?;
+ }
+ v.fmt(f)?;
+ }
+ f.write_str(")")?;
+ Ok(())
+ }
+ Val::I64(v) => v.fmt(f),
+ Val::String(s) => s.fmt(f),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn print_string() {
+ assert_eq!(
+ format!("{:?}", Val::String("Hello, World!".to_string())),
+ "\"Hello, World!\"".to_string()
+ );
+ }
+ #[test]
+ fn print_atom() {
+ assert_eq!(
+ format!("{:?}", Val::Atom("boo-guff".to_string())),
+ "boo-guff".to_string()
+ );
+ }
+ #[test]
+ fn print_i64() {
+ assert_eq!(
+ format!("{:?}", Val::I64(1234)),
+ "1234".to_string()
+ );
+ }
+ #[test]
+ fn print_list() {
+ assert_eq!(
+ format!("{:?}", Val::List(vec!(Val::Atom("a".to_string()), Val::Atom("b".to_string())))),
+ "(a b)".to_string()
+ );
+ }
+}