aboutsummaryrefslogtreecommitdiff
path: root/haskell/Printer.hs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-12-23 20:35:48 -0700
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:52 -0600
commitb76aa73bc76a28d7c6bb3c5a43acc9afd9ec42c8 (patch)
tree4b57f91dcf1df0e079a4251a1cab78fe0188dfb4 /haskell/Printer.hs
parenta816262a057ecc4bd1fd07750d21cab81490f336 (diff)
downloadmal-b76aa73bc76a28d7c6bb3c5a43acc9afd9ec42c8.tar.gz
mal-b76aa73bc76a28d7c6bb3c5a43acc9afd9ec42c8.zip
Haskell: steps 0-3
Diffstat (limited to 'haskell/Printer.hs')
-rw-r--r--haskell/Printer.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/haskell/Printer.hs b/haskell/Printer.hs
new file mode 100644
index 0000000..94bc5ed
--- /dev/null
+++ b/haskell/Printer.hs
@@ -0,0 +1,36 @@
+module Printer
+( _pr_str )
+where
+
+import qualified Data.Map as Map
+
+import Types
+
+_pr_list :: [MalVal] -> String
+_pr_list = unwords . map _pr_str
+
+_flatTuples ((a,b):xs) = MalString a : b : _flatTuples xs
+_flatTuples _ = []
+
+unescape chr = case chr of
+ '\n' -> "\\n"
+ '\\' -> "\\\\"
+ '"' -> "\\\""
+ c -> [c]
+
+_pr_str :: MalVal -> String
+_pr_str (MalString ('\x029e':str)) = ":" ++ str
+_pr_str (MalString str) = "\"" ++ concatMap unescape str ++ "\""
+_pr_str (MalSymbol name) = name
+_pr_str (MalKeyword name) = ":" ++ name
+_pr_str (MalNumber num) = show num
+_pr_str (MalTrue) = "true"
+_pr_str (MalFalse) = "false"
+_pr_str (Nil) = "nil"
+_pr_str (MalList items) = "(" ++ _pr_list items ++ ")"
+_pr_str (MalVector items) = "[" ++ _pr_list items ++ "]"
+_pr_str (MalHashMap m) = "{" ++ _pr_list (_flatTuples $ Map.assocs m) ++ "}"
+_pr_str (MalFunc f) = "#<function>"
+
+instance Show MalVal where show = _pr_str
+