mirror of
https://github.com/janet-lang/janet
synced 2024-12-26 00:10:27 +00:00
60 lines
1.7 KiB
Plaintext
60 lines
1.7 KiB
Plaintext
# Pretty print
|
|
|
|
# Declare pretty print
|
|
(: pp nil)
|
|
|
|
# Pretty print an array or tuple
|
|
(: print-seq (fn [start end a seen]
|
|
(: seen (if seen seen {}))
|
|
(if (get seen s) (get seen s)
|
|
(do
|
|
(: 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))
|
|
(set! seen s ret)
|
|
ret))))
|
|
|
|
# Pretty print an object or struct
|
|
(: print-struct (fn [start end s seen]
|
|
(: seen (if seen seen {}))
|
|
(if (get seen s) (get seen s)
|
|
(do
|
|
(: 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))))
|
|
|
|
# Type handlers
|
|
(: handlers {
|
|
"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))
|
|
})
|
|
|
|
# Define pretty print
|
|
(: pp (fn [x seen]
|
|
(: h (get handlers (type x)))
|
|
((if h h tostring) x seen)))
|
|
|
|
(print (pp [1 {4 5 6 7} 2 3]))
|
|
|
|
# Module export pattern - last expression in file is value of module
|
|
pp
|