perl: Remove step 0.5.
[jackhill/mal.git] / hy / printer.hy
CommitLineData
58ff8110 1(import [hy.models [HyInteger :as Int HyKeyword :as Keyword
081c3223 2 HyString :as Str HySymbol :as Sym]])
5b86f08c 3(import [mal_types [Atom]])
58ff8110
JM
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)) "]")
081c3223
JM
21 (= t dict) (+ "{" (.join " " (map (fn [k] (+ (pr-str k _r) " "
22 (pr-str (get obj k) _r)))
23 obj)) "}")
5b86f08c 24 (instance? Atom obj) (+ "(atom " (pr-str obj.val _r) ")")
58ff8110 25 True (str obj))))