2017-05-06 21:46:28 +00:00
|
|
|
(do
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-04-24 22:09:23 +00:00
|
|
|
(: pp nil)
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-05-07 20:48:35 +00:00
|
|
|
"Pretty print an array or tuple"
|
2017-04-27 13:44:52 +00:00
|
|
|
(: print-seq (fn [start end a seen]
|
|
|
|
(: seen (if seen seen {}))
|
2017-05-04 15:34:24 +00:00
|
|
|
(if (get seen a) (get seen a)
|
2017-04-27 13:44:52 +00:00
|
|
|
(do
|
2017-05-07 22:20:11 +00:00
|
|
|
(set! seen a "<cycle>")
|
2017-04-27 13:44:52 +00:00
|
|
|
(: parts [])
|
|
|
|
(: len (length a))
|
|
|
|
(: i 0)
|
|
|
|
(while (< i len)
|
|
|
|
(push! parts (pp (get a i) seen))
|
|
|
|
(push! parts " ")
|
|
|
|
(: i (+ 1 i)))
|
|
|
|
(if (> len 0) (pop! parts))
|
|
|
|
(push! parts end)
|
|
|
|
(: ret (apply string start parts))
|
2017-05-04 15:34:24 +00:00
|
|
|
(set! seen a ret)
|
2017-04-27 13:44:52 +00:00
|
|
|
ret))))
|
2017-04-24 17:12:55 +00:00
|
|
|
|
2017-05-07 20:48:35 +00:00
|
|
|
"Pretty print an object or struct"
|
2017-04-27 13:44:52 +00:00
|
|
|
(: print-struct (fn [start end s seen]
|
|
|
|
(: seen (if seen seen {}))
|
|
|
|
(if (get seen s) (get seen s)
|
|
|
|
(do
|
2017-05-07 22:20:11 +00:00
|
|
|
(set! seen s "<cycle>")
|
2017-04-27 13:44:52 +00:00
|
|
|
(: parts [])
|
|
|
|
(: key (next s))
|
|
|
|
(while (not (= key nil))
|
|
|
|
(push! parts (pp key seen))
|
|
|
|
(push! parts " ")
|
|
|
|
(push! parts (pp (get s key) seen))
|
|
|
|
(push! parts " ")
|
|
|
|
(: key (next s key)))
|
|
|
|
(if (> (length parts) 0) (pop! parts))
|
|
|
|
(push! parts end)
|
|
|
|
(: ret (apply string start parts))
|
|
|
|
(set! seen s ret)
|
|
|
|
ret))))
|
|
|
|
|
2017-05-07 20:48:35 +00:00
|
|
|
"Type handlers"
|
2017-04-24 22:09:23 +00:00
|
|
|
(: handlers {
|
2017-04-27 13:44:52 +00:00
|
|
|
"array" (fn [a seen] (print-seq "[" "]" a seen))
|
|
|
|
"tuple" (fn [a seen] (print-seq "(" ")" a seen))
|
|
|
|
"table" (fn [s seen] (print-struct "{" "}" s seen))
|
|
|
|
"struct" (fn [s seen] (print-struct "#{" "}" s seen))
|
2017-04-19 13:02:12 +00:00
|
|
|
})
|
2017-04-24 22:09:23 +00:00
|
|
|
|
2017-05-07 20:48:35 +00:00
|
|
|
"Define pretty print"
|
2017-04-27 13:44:52 +00:00
|
|
|
(: pp (fn [x seen]
|
2017-05-06 21:46:28 +00:00
|
|
|
(: handler (get handlers (type x)))
|
|
|
|
(: handler (if handler handler tostring))
|
|
|
|
(handler x seen)))
|
2017-04-24 22:09:23 +00:00
|
|
|
|
2017-05-07 20:48:35 +00:00
|
|
|
"Export pretty print"
|
2017-05-07 22:20:11 +00:00
|
|
|
(export! "pp" pp)
|
|
|
|
|
|
|
|
(: arr [1 2 3 4])
|
|
|
|
(push! arr arr)
|
|
|
|
(print (pp arr))
|
2017-05-06 21:46:28 +00:00
|
|
|
|
|
|
|
)
|