| 1 | (import [hy.models [HyInteger :as Int HyKeyword :as Keyword |
| 2 | HyString :as Str HySymbol :as Sym]]) |
| 3 | (import [mal_types [Atom]]) |
| 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 dict) (+ "{" (.join " " (map (fn [k] (+ (pr-str k _r) " " |
| 22 | (pr-str (get obj k) _r))) |
| 23 | obj)) "}") |
| 24 | (instance? Atom obj) (+ "(atom " (pr-str obj.val _r) ")") |
| 25 | True (str obj)))) |