mirror of
https://github.com/janet-lang/janet
synced 2025-06-22 16:34:11 +00:00
Change some function names.
This commit is contained in:
parent
bf2c16ccb0
commit
9806546e1c
20
core/stl.c
20
core/stl.c
@ -367,8 +367,8 @@ int gst_stl_string(Gst *vm) {
|
|||||||
gst_c_return(vm, gst_wrap_string(gst_string_end(vm, str)));
|
gst_c_return(vm, gst_wrap_string(gst_string_end(vm, str)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Associative rawget */
|
/* Associative get */
|
||||||
int gst_stl_rawget(Gst *vm) {
|
int gst_stl_get(Gst *vm) {
|
||||||
GstValue ret;
|
GstValue ret;
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
const char *err;
|
const char *err;
|
||||||
@ -382,8 +382,8 @@ int gst_stl_rawget(Gst *vm) {
|
|||||||
gst_c_return(vm, ret);
|
gst_c_return(vm, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Associative rawset */
|
/* Associative set */
|
||||||
int gst_stl_rawset(Gst *vm) {
|
int gst_stl_set(Gst *vm) {
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
const char *err;
|
const char *err;
|
||||||
count = gst_count_args(vm);
|
count = gst_count_args(vm);
|
||||||
@ -623,17 +623,17 @@ static const GstModuleItem const std_module[] = {
|
|||||||
{"print", gst_stl_print},
|
{"print", gst_stl_print},
|
||||||
{"tostring", gst_stl_tostring},
|
{"tostring", gst_stl_tostring},
|
||||||
{"exit", gst_stl_exit},
|
{"exit", gst_stl_exit},
|
||||||
{"rawget", gst_stl_rawget},
|
{"get", gst_stl_get},
|
||||||
{"rawset", gst_stl_rawset},
|
{"set!", gst_stl_set},
|
||||||
{"next", gst_stl_next},
|
{"next", gst_stl_next},
|
||||||
{"error", gst_stl_error},
|
{"error", gst_stl_error},
|
||||||
{"serialize", gst_stl_serialize},
|
{"serialize", gst_stl_serialize},
|
||||||
{"global", gst_stl_global},
|
{"global", gst_stl_global},
|
||||||
{"setglobal", gst_stl_setglobal},
|
{"setglobal!", gst_stl_setglobal},
|
||||||
{"push", gst_stl_push},
|
{"push!", gst_stl_push},
|
||||||
{"pop", gst_stl_pop},
|
{"pop!", gst_stl_pop},
|
||||||
{"peek", gst_stl_peek},
|
{"peek", gst_stl_peek},
|
||||||
{"ensure", gst_stl_ensure},
|
{"ensure!", gst_stl_ensure},
|
||||||
{"open", gst_stl_open},
|
{"open", gst_stl_open},
|
||||||
{"slurp", gst_stl_slurp},
|
{"slurp", gst_stl_slurp},
|
||||||
{"close", gst_stl_close},
|
{"close", gst_stl_close},
|
||||||
|
86
libs/pp.gst
86
libs/pp.gst
@ -4,56 +4,56 @@
|
|||||||
(: pp nil)
|
(: pp nil)
|
||||||
|
|
||||||
# Pretty print an array or tuple
|
# Pretty print an array or tuple
|
||||||
(: print-seq (fn [start end a]
|
(: print-seq (fn [start end a seen]
|
||||||
(: parts [])
|
(: seen (if seen seen {}))
|
||||||
(: len (length a))
|
(if (get seen s) (get seen s)
|
||||||
(: i 0)
|
(do
|
||||||
(while (< i len)
|
(: parts [])
|
||||||
(push parts (pp (rawget a i)))
|
(: len (length a))
|
||||||
(push parts " ")
|
(: i 0)
|
||||||
(: i (+ 1 i)))
|
(while (< i len)
|
||||||
(if (> len 0) (pop parts))
|
(push! parts (pp (get a i) seen))
|
||||||
(push parts end)
|
(push! parts " ")
|
||||||
(apply string start 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
|
# Pretty print an object or struct
|
||||||
(: print-struct (fn [start end s]
|
(: print-struct (fn [start end s seen]
|
||||||
(: parts [])
|
(: seen (if seen seen {}))
|
||||||
(: key (next s))
|
(if (get seen s) (get seen s)
|
||||||
(while (not (= key nil))
|
(do
|
||||||
(push parts (pp key))
|
(: parts [])
|
||||||
(push parts " ")
|
(: key (next s))
|
||||||
(push parts (pp (rawget s key)))
|
(while (not (= key nil))
|
||||||
(push parts " ")
|
(push! parts (pp key seen))
|
||||||
(: key (next s key)))
|
(push! parts " ")
|
||||||
(if (> (length parts) 0) (pop parts))
|
(push! parts (pp (get s key) seen))
|
||||||
(push parts end)
|
(push! parts " ")
|
||||||
(apply string start parts)))
|
(: key (next s key)))
|
||||||
|
(if (> (length parts) 0) (pop! parts))
|
||||||
# Pretty
|
(push! parts end)
|
||||||
|
(: ret (apply string start parts))
|
||||||
|
(set! seen s ret)
|
||||||
|
ret))))
|
||||||
|
|
||||||
|
# Type handlers
|
||||||
(: handlers {
|
(: handlers {
|
||||||
"real" tostring
|
"array" (fn [a seen] (print-seq "[" "]" a seen))
|
||||||
"integer" tostring
|
"tuple" (fn [a seen] (print-seq "(" ")" a seen))
|
||||||
"nil" tostring
|
"table" (fn [s seen] (print-struct "{" "}" s seen))
|
||||||
"boolean" tostring
|
"struct" (fn [s seen] (print-struct "#{" "}" s seen))
|
||||||
"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
|
# Define pretty print
|
||||||
(: pp (fn [x]
|
(: pp (fn [x seen]
|
||||||
(: h (rawget handlers (type x)))
|
(: h (get handlers (type x)))
|
||||||
(h x)))
|
((if h h tostring) x seen)))
|
||||||
|
|
||||||
(print (pp [1 {4 5 6 7} 2 3]))
|
(print (pp [1 {4 5 6 7} 2 3]))
|
||||||
|
|
||||||
(print "DONE!")
|
# Module export pattern - last expression in file is value of module
|
||||||
|
pp
|
||||||
|
Loading…
x
Reference in New Issue
Block a user