From f3b2c295808f4ff17660455c7445ec3fd8404f77 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 3 Feb 2018 18:12:07 -0500 Subject: [PATCH] Add pretty print to build in. --- src/compiler/boot.dst | 77 +++++++++++++++++++++++------------------- src/compiler/compile.c | 13 +++++++ src/core/corelib.c | 4 +-- 3 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/compiler/boot.dst b/src/compiler/boot.dst index 16a61675..b4c8a0ae 100644 --- a/src/compiler/boot.dst +++ b/src/compiler/boot.dst @@ -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 "") + (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 "") + (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))) +)) diff --git a/src/compiler/compile.c b/src/compiler/compile.c index a1c53a94..bfac314d 100644 --- a/src/compiler/compile.c +++ b/src/compiler/compile.c @@ -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; } + diff --git a/src/core/corelib.c b/src/core/corelib.c index 48a110bf..b0e02c23 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -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");