1
0
mirror of https://github.com/janet-lang/janet synced 2024-09-29 23:40:40 +00:00
janet/libs/pp.gst

60 lines
1.3 KiB
Plaintext
Raw Normal View History

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