2020-11-17 11:30:49 +03:00
|
|
|
(import ./types :as t)
|
|
|
|
|
2020-11-05 13:56:47 +03:00
|
|
|
(defn escape
|
|
|
|
[a-str]
|
|
|
|
(->> (buffer a-str)
|
|
|
|
(peg/replace-all "\\" "\\\\")
|
|
|
|
(peg/replace-all "\"" "\\\"")
|
|
|
|
(peg/replace-all "\n" "\\n")
|
|
|
|
string))
|
|
|
|
|
|
|
|
(defn code*
|
|
|
|
[ast buf print_readably]
|
2020-11-17 11:30:49 +03:00
|
|
|
(cond
|
|
|
|
(or (t/boolean?* ast)
|
|
|
|
(t/nil?* ast)
|
|
|
|
(t/keyword?* ast)
|
|
|
|
(t/symbol?* ast))
|
|
|
|
(buffer/push-string buf (t/get-value ast))
|
|
|
|
##
|
|
|
|
(t/number?* ast)
|
|
|
|
(buffer/push-string buf (string (t/get-value ast)))
|
|
|
|
##
|
|
|
|
(t/string?* ast)
|
2020-11-05 13:56:47 +03:00
|
|
|
(if print_readably
|
|
|
|
(buffer/push-string buf (string "\""
|
2020-11-17 11:30:49 +03:00
|
|
|
(escape (t/get-value ast))
|
2020-11-05 13:56:47 +03:00
|
|
|
"\""))
|
2020-11-17 11:30:49 +03:00
|
|
|
(buffer/push-string buf (t/get-value ast)))
|
|
|
|
##
|
|
|
|
(t/list?* ast)
|
2020-11-05 13:56:47 +03:00
|
|
|
(do
|
|
|
|
(buffer/push-string buf "(")
|
|
|
|
(var remove false)
|
2020-11-17 11:30:49 +03:00
|
|
|
(each elt (t/get-value ast)
|
2020-11-05 13:56:47 +03:00
|
|
|
(code* elt buf print_readably)
|
|
|
|
(buffer/push-string buf " ")
|
|
|
|
(set remove true))
|
|
|
|
(when remove
|
|
|
|
(buffer/popn buf 1))
|
|
|
|
(buffer/push-string buf ")"))
|
2020-11-17 11:30:49 +03:00
|
|
|
##
|
|
|
|
(t/hash-map?* ast)
|
2020-11-05 13:56:47 +03:00
|
|
|
(do
|
|
|
|
(buffer/push-string buf "{")
|
|
|
|
(var remove false)
|
2020-11-17 11:30:49 +03:00
|
|
|
(eachp [k v] (t/get-value ast)
|
2020-11-13 17:37:05 +03:00
|
|
|
(code* k buf print_readably)
|
|
|
|
(buffer/push-string buf " ")
|
|
|
|
(code* v buf print_readably)
|
2020-11-05 13:56:47 +03:00
|
|
|
(buffer/push-string buf " ")
|
|
|
|
(set remove true))
|
|
|
|
(when remove
|
|
|
|
(buffer/popn buf 1))
|
|
|
|
(buffer/push-string buf "}"))
|
2020-11-17 11:30:49 +03:00
|
|
|
##
|
|
|
|
(t/vector?* ast)
|
2020-11-05 13:56:47 +03:00
|
|
|
(do
|
|
|
|
(buffer/push-string buf "[")
|
|
|
|
(var remove false)
|
2020-11-17 11:30:49 +03:00
|
|
|
(each elt (t/get-value ast)
|
2020-11-05 13:56:47 +03:00
|
|
|
(code* elt buf print_readably)
|
|
|
|
(buffer/push-string buf " ")
|
|
|
|
(set remove true))
|
|
|
|
(when remove
|
|
|
|
(buffer/popn buf 1))
|
2020-11-08 16:24:51 +03:00
|
|
|
(buffer/push-string buf "]"))
|
2020-11-17 11:30:49 +03:00
|
|
|
## XXX: what about macro?
|
|
|
|
(t/fn?* ast)
|
2020-11-10 13:25:38 +03:00
|
|
|
(buffer/push-string buf "#<function>")
|
2020-11-17 11:30:49 +03:00
|
|
|
##
|
|
|
|
(t/atom?* ast)
|
2020-11-10 13:25:38 +03:00
|
|
|
(do
|
|
|
|
(buffer/push-string buf "(atom ")
|
2020-11-17 11:30:49 +03:00
|
|
|
(code* (t/get-value ast) buf print_readably)
|
2020-11-13 17:37:05 +03:00
|
|
|
(buffer/push-string buf ")"))
|
2020-11-17 11:30:49 +03:00
|
|
|
##
|
|
|
|
(t/exception?* ast)
|
2020-11-13 17:37:05 +03:00
|
|
|
(do
|
2020-11-15 08:20:20 +03:00
|
|
|
(buffer/push-string buf "Error: ")
|
2020-11-17 11:30:49 +03:00
|
|
|
(code* (t/get-value ast) buf print_readably))))
|
2020-11-05 13:56:47 +03:00
|
|
|
|
|
|
|
(comment
|
|
|
|
|
|
|
|
(let [buf @""]
|
2020-11-17 11:30:49 +03:00
|
|
|
(code* (make-number 1) buf false))
|
2020-11-05 13:56:47 +03:00
|
|
|
# => @"1"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
(defn pr_str
|
|
|
|
[ast print_readably]
|
|
|
|
(let [buf @""]
|
|
|
|
(code* ast buf print_readably)
|
|
|
|
buf))
|
|
|
|
|
|
|
|
(comment
|
|
|
|
|
2020-11-17 11:30:49 +03:00
|
|
|
(pr_str (make-number 1) false)
|
2020-11-05 13:56:47 +03:00
|
|
|
# => @"1"
|
|
|
|
|
|
|
|
)
|