2017-04-19 13:02:12 +00:00
|
|
|
# Pretty print
|
|
|
|
|
2017-04-24 22:09:23 +00:00
|
|
|
# Declare pretty print
|
|
|
|
(: pp nil)
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-04-24 22:09:23 +00:00
|
|
|
# Pretty print an array or tuple
|
|
|
|
(: print-seq (fn [start end a]
|
2017-04-24 17:12:55 +00:00
|
|
|
(: parts [])
|
2017-04-24 22:09:23 +00:00
|
|
|
(: len (length a))
|
2017-04-24 17:12:55 +00:00
|
|
|
(: i 0)
|
2017-04-24 22:09:23 +00:00
|
|
|
(while (< i len)
|
|
|
|
(push parts (pp (rawget a i)))
|
|
|
|
(push parts " ")
|
2017-04-24 17:12:55 +00:00
|
|
|
(: i (+ 1 i)))
|
2017-04-24 22:09:23 +00:00
|
|
|
(if (> len 0) (pop parts))
|
|
|
|
(push parts end)
|
2017-04-26 14:21:03 +00:00
|
|
|
(apply string start parts)))
|
2017-04-24 17:12:55 +00:00
|
|
|
|
2017-04-24 22:09:23 +00:00
|
|
|
# Pretty print an object or struct
|
|
|
|
(: print-struct (fn [start end s]
|
|
|
|
(: parts [])
|
|
|
|
(: key (next s))
|
|
|
|
(while (not (= key nil))
|
|
|
|
(push parts (pp key))
|
|
|
|
(push parts " ")
|
|
|
|
(push parts (pp (rawget s key)))
|
|
|
|
(push parts " ")
|
|
|
|
(: key (next s key)))
|
|
|
|
(if (> (length parts) 0) (pop parts))
|
|
|
|
(push parts end)
|
2017-04-26 14:21:03 +00:00
|
|
|
(apply string start parts)))
|
2017-04-24 22:09:23 +00:00
|
|
|
|
|
|
|
# Pretty
|
|
|
|
|
|
|
|
(: handlers {
|
|
|
|
"real" tostring
|
|
|
|
"integer" tostring
|
2017-04-19 13:02:12 +00:00
|
|
|
"nil" tostring
|
|
|
|
"boolean" tostring
|
|
|
|
"userdata" tostring
|
|
|
|
"cfunction" tostring
|
|
|
|
"function" tostring
|
2017-04-24 22:09:23 +00:00
|
|
|
"string" tostring
|
2017-04-19 13:02:12 +00:00
|
|
|
"buffer" tostring
|
2017-04-24 22:09:23 +00:00
|
|
|
"array" (fn [a] (print-seq "[" "]" a))
|
|
|
|
"tuple" (fn [a] (print-seq "(" ")" a))
|
|
|
|
"object" (fn [s] (print-struct "{" "}" s))
|
|
|
|
"struct" (fn [s] (print-struct "#{" "}" s))
|
2017-04-19 13:02:12 +00:00
|
|
|
"thread" tostring
|
|
|
|
})
|
2017-04-24 22:09:23 +00:00
|
|
|
|
|
|
|
# Define pretty print
|
|
|
|
(: pp (fn [x]
|
|
|
|
(: h (rawget handlers (type x)))
|
|
|
|
(h x)))
|
|
|
|
|
|
|
|
(print (pp [1 {4 5 6 7} 2 3]))
|
|
|
|
|
|
|
|
(print "DONE!")
|