Hy: step5
[jackhill/mal.git] / hy / printer.hy
CommitLineData
58ff8110
JM
1(import [hy.models [HyInteger :as Int HyKeyword :as Keyword
2 HyString :as Str HySymbol :as Sym HyDict :as Map]])
3
4
5(defn escape [s]
6 (-> (str s) (.replace "\\" "\\\\")
7 (.replace "\"" "\\\"")
8 (.replace "\n" "\\n")))
9
10(defn pr-str [obj &optional [print-readably True]]
11 (setv _r print-readably
12 t (type obj))
13 (Str
14 (if
15 (none? obj) "nil"
16 (= t bool) (if obj "true" "false")
17 (= t Keyword) (+ ":" (name obj))
18 (= t Str) (if _r (+ "\"" (escape obj) "\"") obj)
19 (= t tuple) (+ "(" (.join " " (map (fn [x] (pr-str x _r)) obj)) ")")
20 (= t list) (+ "[" (.join " " (map (fn [x] (pr-str x _r)) obj)) "]")
21 (= t Map) (+ "{" (.join " " (map (fn [x] (pr-str x _r)) obj)) "}")
22 True (str obj))))