]> git.rmz.io Git - my-scheme.git/blobdiff - app/Main.hs
Use `showVal` to print with show rather than the default
[my-scheme.git] / app / Main.hs
index fb8294a4df5b4a0e2ef6c6f28f3574b1a2bb61e8..78f37ea4132604b9ee481dba40f65ab0e5738be3 100644 (file)
@@ -11,7 +11,6 @@ data LispVal = Atom String
              | String String
              | Character Char
              | Bool Bool
-             deriving Show
 
 symbol :: Parser Char
 symbol = oneOf "!#$%&|*+-/:<=>?@^_~"
@@ -94,7 +93,21 @@ parseExpr = parseString
 readExpr :: String -> String
 readExpr input = case parse parseExpr "lisp" input of
     Left err -> "No match: " ++ show err
-    Right val -> "Found value: " ++ show val
+    Right val -> "Found value: " ++ showVal val
+
+instance Show LispVal where show = showVal
+showVal :: LispVal -> String
+showVal (Atom atom)   = atom
+showVal (List list)   = "(" ++ unwordsList list ++ ")"
+showVal (DottedList head tail)   = "(" ++ unwordsList head ++ " . " ++ showVal tail ++ ")"
+showVal (Number n)    = show n
+showVal (String str)  = "\"" ++ str ++ "\""
+showVal (Character c) = "'" ++ [c] ++ "'"
+showVal (Bool True)   = "#t"
+showVal (Bool False)  = "#f"
+
+unwordsList :: [LispVal] -> String
+unwordsList = unwords . map showVal
 
 main :: IO ()
 main = do