1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 08:20:27 +00:00

Add sorting to pretty printer.

This commit is contained in:
Calvin Rose 2018-03-28 21:16:12 -04:00
parent 39d6bd573a
commit e21a69920f
2 changed files with 50 additions and 13 deletions

View File

@ -63,6 +63,9 @@
(defn array? [x] (= (type x) :array)) (defn array? [x] (= (type x) :array))
(defn tuple? [x] (= (type x) :tuple)) (defn tuple? [x] (= (type x) :tuple))
(defn boolean? [x] (= (type x) :boolean)) (defn boolean? [x] (= (type x) :boolean))
(defn indexed? [x]
(def t (type x))
(if (= t :array) true (= t :tuple)))
(defn function? [x] (defn function? [x]
(def t (type x)) (def t (type x))
(if (= t :function) true (= t :cfunction))) (if (= t :function) true (= t :cfunction)))
@ -573,6 +576,36 @@ the predicate, and abort on first failiure."
(:= key (next c key)))) (:= key (next c key))))
(if (table? (get colls 0)) container (table-to-struct container))) (if (table? (get colls 0)) container (table-to-struct container)))
(defn keys
"Get the keys of an associative data structure."
[x]
(def arr @[])
(var k (next x nil))
(while k
(array-push arr k)
(:= k (next x k)))
arr)
(defn values
"Get the values of an associative data structure."
[x]
(def arr @[])
(var k (next x nil))
(while k
(array-push arr (get x k))
(:= k (next x k)))
arr)
(defn pairs
"Get the values of an associative data structure."
[x]
(def arr @[])
(var k (next x nil))
(while k
(array-push arr (tuple k (get x k)))
(:= k (next x k)))
arr)
### ###
### ###
### Pretty Printer ### Pretty Printer
@ -622,28 +655,25 @@ to call on any table. Does not print table prototype information."
(buffer-push-string buf indent)))) (buffer-push-string buf indent))))
(defn pp-dict-nested [y] (defn pp-dict-nested [y]
(var k (next y nil))
(buffer-push-string indent " ") (buffer-push-string indent " ")
(def ps (sort (pairs y)))
(for [i 0 (length ps)]
(def [k v] (get ps i))
(buffer-push-string buf indent) (buffer-push-string buf indent)
(while k
(def v (get y k))
(recur k) (recur k)
(buffer-push-string buf " ") (buffer-push-string buf " ")
(recur v) (recur v))
(:= k (next y k))
(when k (buffer-push-string buf indent)))
(buffer-popn indent 2) (buffer-popn indent 2)
(buffer-push-string buf indent)) (buffer-push-string buf indent))
(defn pp-dict-simple [y] (defn pp-dict-simple [y]
(var k (next y nil)) (def ps (sort (pairs y)))
(while k (for [i 0 (length ps)]
(def v (get y k)) (def [k v] (get ps i))
(if (pos? i) (buffer-push-string buf " "))
(recur k) (recur k)
(buffer-push-string buf " ") (buffer-push-string buf " ")
(recur v) (recur v)))
(:= k (next y k))
(when k (buffer-push-string buf " "))))
(defn pp-dict [y] (defn pp-dict [y]
((if (> 4 (length y)) pp-dict-simple pp-dict-nested) y)) ((if (> 4 (length y)) pp-dict-simple pp-dict-nested) y))
@ -756,6 +786,12 @@ to call on any table. Does not print table prototype information."
(:= current (macroexpand1 current))) (:= current (macroexpand1 current)))
current) current)
###
###
### Evaluation and Compilation
###
###
(defn make-env [parent] (defn make-env [parent]
(def parent (if parent parent _env)) (def parent (if parent parent _env))
(def newenv (setproto @{} parent)) (def newenv (setproto @{} parent))

View File

@ -138,6 +138,7 @@ int dst_compare(Dst x, Dst y) {
return dst_unwrap_integer(x) > dst_unwrap_integer(y) ? 1 : -1; return dst_unwrap_integer(x) > dst_unwrap_integer(y) ? 1 : -1;
} }
case DST_STRING: case DST_STRING:
case DST_SYMBOL:
return dst_string_compare(dst_unwrap_string(x), dst_unwrap_string(y)); return dst_string_compare(dst_unwrap_string(x), dst_unwrap_string(y));
case DST_TUPLE: case DST_TUPLE:
return dst_tuple_compare(dst_unwrap_tuple(x), dst_unwrap_tuple(y)); return dst_tuple_compare(dst_unwrap_tuple(x), dst_unwrap_tuple(y));