1
0
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:
Calvin Rose 2018-02-03 18:12:07 -05:00
parent a673b7e326
commit f3b2c29580
3 changed files with 57 additions and 37 deletions

View File

@ -77,49 +77,56 @@
(tuple 'varset! sym (tuple '+ sym 1))
)))
(defn pp-seq [pp buf a indent start end]
(def len (length a))
(buffer-push-string buf start)
(for [i 0 len]
(when (not= i 0) (buffer-push-string buf " "))
(pp buf (get a i) indent))
(buffer-push-string buf end)
buf)
(def pp (do
(defn push-line [buf indent]
(buffer-push-string buf "\n")
(for [i 0 indent] (buffer-push-string buf " "))
)
(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))
(buffer-push-string buf start)
(for [i 0 len]
(when (not= i 0) (buffer-push-string buf " "))
(pp seen buf (get a i)))
(buffer-push-string buf end)
))
buf)
(defn pp-dict [pp buf a indent start end]
(var k (next a nil))
(buffer-push-string buf start)
(def newindent (+ 2 indent))
(push-line buf newindent)
(while k
(def v (get a k))
(pp buf k)
(buffer-push-string buf " ")
(pp buf v)
(varset! k (next a k))
(push-line buf (if k newindent indent))
)
(buffer-push-string buf end)
(defn pp-dict [pp seen buf a start end]
(if (get seen a)
(buffer-push-string buf "<cycle>")
(do
(put seen a true)
(var k (next a nil))
(buffer-push-string buf start)
(while k
(def v (get a k))
(pp seen buf k)
(buffer-push-string buf " ")
(pp seen buf v)
(varset! k (next a k))
(when k (buffer-push-string buf " "))
)
(buffer-push-string buf end)
))
buf)
(def _printers {
:array (fn [pp buf x i] (pp-seq pp buf x i "[" "]"))
:tuple (fn [pp buf x i] (pp-seq pp buf x i "(" ")"))
:table (fn [pp buf x i] (pp-dict pp buf x i "@{" "}"))
:struct (fn [pp buf x i] (pp-dict pp buf x i "{" "}"))
:array (fn [pp seen buf x] (pp-seq pp seen buf x "[" "]"))
:tuple (fn [pp seen buf x] (pp-seq pp seen buf x "(" ")"))
:table (fn [pp seen buf x] (pp-dict pp seen buf x "@{" "}"))
: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 p (if pmaybe pmaybe _default_printer))
(p pp1 buf x indent))
(defn pp [x] (print (pp1 (buffer) x 0)))
(p pp1 seen buf x))
(fn [x] (print (pp1 @{} @"" x)))
))

View File

@ -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)));
}
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 */
DstSlot dstc_symbol(DstFopts opts, DstAst *ast, const uint8_t *sym) {
if (dst_string_length(sym) && sym[0] != ':') {
@ -846,6 +855,9 @@ recur:
case DST_TABLE:
ret = dstc_tablector(opts, ast, x, dst_core_table);
break;
case DST_BUFFER:
ret = dstc_bufferctor(opts, ast, x);
break;
}
if (dstc_iserr(&opts)) {
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));
return 0;
}

View File

@ -232,12 +232,12 @@ int dst_core_next(DstArgs args) {
if (dst_checktype(ds, DST_TABLE)) {
DstTable *t = dst_unwrap_table(ds);
kv = dst_checktype(args.v[1], DST_NIL)
? t->data
? NULL
: dst_table_find(t, args.v[1]);
} else if (dst_checktype(ds, DST_STRUCT)) {
const DstKV *st = dst_unwrap_struct(ds);
kv = dst_checktype(args.v[1], DST_NIL)
? st
? NULL
: dst_struct_find(st, args.v[1]);
} else {
return dst_throw(args, "expected table/struct");