1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 14:52:16 +03:00
mal/impls/hy/printer.hy
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

26 lines
1015 B
Hy

(import [hy.models [HyInteger :as Int HyKeyword :as Keyword
HyString :as Str HySymbol :as Sym]])
(import [mal_types [Atom]])
(defn escape [s]
(-> (str s) (.replace "\\" "\\\\")
(.replace "\"" "\\\"")
(.replace "\n" "\\n")))
(defn pr-str [obj &optional [print-readably True]]
(setv _r print-readably
t (type obj))
(Str
(if
(none? obj) "nil"
(= t bool) (if obj "true" "false")
(= t Keyword) (+ ":" (name obj))
(= t Str) (if _r (+ "\"" (escape obj) "\"") obj)
(= t tuple) (+ "(" (.join " " (map (fn [x] (pr-str x _r)) obj)) ")")
(= t list) (+ "[" (.join " " (map (fn [x] (pr-str x _r)) obj)) "]")
(= t dict) (+ "{" (.join " " (map (fn [k] (+ (pr-str k _r) " "
(pr-str (get obj k) _r)))
obj)) "}")
(instance? Atom obj) (+ "(atom " (pr-str obj.val _r) ")")
True (str obj))))