mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 01:37:19 +00:00
Add pretty print to build in.
This commit is contained in:
parent
a673b7e326
commit
f3b2c29580
@ -77,49 +77,56 @@
|
|||||||
(tuple 'varset! sym (tuple '+ sym 1))
|
(tuple 'varset! sym (tuple '+ sym 1))
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(defn pp-seq [pp buf a indent start end]
|
(def pp (do
|
||||||
|
|
||||||
|
(defn pp-seq [pp seen buf a start end]
|
||||||
|
(if (get seen a)
|
||||||
|
(buffer-push-string buf "<cycle>")
|
||||||
|
(do
|
||||||
|
(put seen a true)
|
||||||
(def len (length a))
|
(def len (length a))
|
||||||
(buffer-push-string buf start)
|
(buffer-push-string buf start)
|
||||||
(for [i 0 len]
|
(for [i 0 len]
|
||||||
(when (not= i 0) (buffer-push-string buf " "))
|
(when (not= i 0) (buffer-push-string buf " "))
|
||||||
(pp buf (get a i) indent))
|
(pp seen buf (get a i)))
|
||||||
(buffer-push-string buf end)
|
(buffer-push-string buf end)
|
||||||
|
))
|
||||||
buf)
|
buf)
|
||||||
|
|
||||||
(defn push-line [buf indent]
|
(defn pp-dict [pp seen buf a start end]
|
||||||
(buffer-push-string buf "\n")
|
(if (get seen a)
|
||||||
(for [i 0 indent] (buffer-push-string buf " "))
|
(buffer-push-string buf "<cycle>")
|
||||||
)
|
(do
|
||||||
|
(put seen a true)
|
||||||
(defn pp-dict [pp buf a indent start end]
|
|
||||||
(var k (next a nil))
|
(var k (next a nil))
|
||||||
(buffer-push-string buf start)
|
(buffer-push-string buf start)
|
||||||
(def newindent (+ 2 indent))
|
|
||||||
(push-line buf newindent)
|
|
||||||
(while k
|
(while k
|
||||||
(def v (get a k))
|
(def v (get a k))
|
||||||
(pp buf k)
|
(pp seen buf k)
|
||||||
(buffer-push-string buf " ")
|
(buffer-push-string buf " ")
|
||||||
(pp buf v)
|
(pp seen buf v)
|
||||||
(varset! k (next a k))
|
(varset! k (next a k))
|
||||||
(push-line buf (if k newindent indent))
|
(when k (buffer-push-string buf " "))
|
||||||
)
|
)
|
||||||
(buffer-push-string buf end)
|
(buffer-push-string buf end)
|
||||||
|
))
|
||||||
buf)
|
buf)
|
||||||
|
|
||||||
(def _printers {
|
(def _printers {
|
||||||
:array (fn [pp buf x i] (pp-seq pp buf x i "[" "]"))
|
:array (fn [pp seen buf x] (pp-seq pp seen buf x "[" "]"))
|
||||||
:tuple (fn [pp buf x i] (pp-seq pp buf x i "(" ")"))
|
:tuple (fn [pp seen buf x] (pp-seq pp seen buf x "(" ")"))
|
||||||
:table (fn [pp buf x i] (pp-dict pp buf x i "@{" "}"))
|
:table (fn [pp seen buf x] (pp-dict pp seen buf x "@{" "}"))
|
||||||
:struct (fn [pp buf x i] (pp-dict pp buf x i "{" "}"))
|
:struct (fn [pp seen buf x] (pp-dict pp seen buf x "{" "}"))
|
||||||
})
|
})
|
||||||
|
|
||||||
(defn _default_printer [_ buf x] (buffer-push-string buf (string x)) buf)
|
(defn _default_printer [pp seen buf x]
|
||||||
|
(buffer-push-string buf (string x))
|
||||||
|
buf)
|
||||||
|
|
||||||
(defn pp1 [buf x indent]
|
(defn pp1 [seen buf x]
|
||||||
(def pmaybe (get _printers (type x)))
|
(def pmaybe (get _printers (type x)))
|
||||||
(def p (if pmaybe pmaybe _default_printer))
|
(def p (if pmaybe pmaybe _default_printer))
|
||||||
(p pp1 buf x indent))
|
(p pp1 seen buf x))
|
||||||
|
|
||||||
(defn pp [x] (print (pp1 (buffer) x 0)))
|
|
||||||
|
|
||||||
|
(fn [x] (print (pp1 @{} @"" x)))
|
||||||
|
))
|
||||||
|
@ -746,6 +746,15 @@ static DstSlot dstc_tablector(DstFopts opts, DstAst *ast, Dst x, DstCFunction cf
|
|||||||
return dstc_call(opts, ast, dstc_toslotskv(c, x), dstc_cslot(dst_wrap_cfunction(cfun)));
|
return dstc_call(opts, ast, dstc_toslotskv(c, x), dstc_cslot(dst_wrap_cfunction(cfun)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static DstSlot dstc_bufferctor(DstFopts opts, DstAst *ast, Dst x) {
|
||||||
|
DstCompiler *c = opts.compiler;
|
||||||
|
DstBuffer *b = dst_unwrap_buffer(x);
|
||||||
|
Dst onearg = dst_stringv(b->data, b->count);
|
||||||
|
return dstc_call(opts, ast,
|
||||||
|
dstc_toslots(c, &onearg, 1),
|
||||||
|
dstc_cslot(dst_wrap_cfunction(dst_core_buffer)));
|
||||||
|
}
|
||||||
|
|
||||||
/* Compile a symbol */
|
/* Compile a symbol */
|
||||||
DstSlot dstc_symbol(DstFopts opts, DstAst *ast, const uint8_t *sym) {
|
DstSlot dstc_symbol(DstFopts opts, DstAst *ast, const uint8_t *sym) {
|
||||||
if (dst_string_length(sym) && sym[0] != ':') {
|
if (dst_string_length(sym) && sym[0] != ':') {
|
||||||
@ -846,6 +855,9 @@ recur:
|
|||||||
case DST_TABLE:
|
case DST_TABLE:
|
||||||
ret = dstc_tablector(opts, ast, x, dst_core_table);
|
ret = dstc_tablector(opts, ast, x, dst_core_table);
|
||||||
break;
|
break;
|
||||||
|
case DST_BUFFER:
|
||||||
|
ret = dstc_bufferctor(opts, ast, x);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (dstc_iserr(&opts)) {
|
if (dstc_iserr(&opts)) {
|
||||||
return dstc_cslot(dst_wrap_nil());
|
return dstc_cslot(dst_wrap_nil());
|
||||||
@ -1009,3 +1021,4 @@ int dst_lib_compile(DstArgs args) {
|
|||||||
dst_env_def(env, "compile", dst_wrap_cfunction(dst_compile_cfun));
|
dst_env_def(env, "compile", dst_wrap_cfunction(dst_compile_cfun));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -232,12 +232,12 @@ int dst_core_next(DstArgs args) {
|
|||||||
if (dst_checktype(ds, DST_TABLE)) {
|
if (dst_checktype(ds, DST_TABLE)) {
|
||||||
DstTable *t = dst_unwrap_table(ds);
|
DstTable *t = dst_unwrap_table(ds);
|
||||||
kv = dst_checktype(args.v[1], DST_NIL)
|
kv = dst_checktype(args.v[1], DST_NIL)
|
||||||
? t->data
|
? NULL
|
||||||
: dst_table_find(t, args.v[1]);
|
: dst_table_find(t, args.v[1]);
|
||||||
} else if (dst_checktype(ds, DST_STRUCT)) {
|
} else if (dst_checktype(ds, DST_STRUCT)) {
|
||||||
const DstKV *st = dst_unwrap_struct(ds);
|
const DstKV *st = dst_unwrap_struct(ds);
|
||||||
kv = dst_checktype(args.v[1], DST_NIL)
|
kv = dst_checktype(args.v[1], DST_NIL)
|
||||||
? st
|
? NULL
|
||||||
: dst_struct_find(st, args.v[1]);
|
: dst_struct_find(st, args.v[1]);
|
||||||
} else {
|
} else {
|
||||||
return dst_throw(args, "expected table/struct");
|
return dst_throw(args, "expected table/struct");
|
||||||
|
Loading…
Reference in New Issue
Block a user