Skip to content

Instantly share code, notes, and snippets.

@bsless
Created August 22, 2021 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsless/c21995e5702c2fa6d954312786da197b to your computer and use it in GitHub Desktop.
Save bsless/c21995e5702c2fa6d954312786da197b to your computer and use it in GitHub Desktop.
(defmacro band
([] true)
([x] `(if (boolean ~x) true false))
([x & next]
`(if ~x (band ~@next) false)))
#?(:clj
(do
(defprotocol IntoEntries
(-parse-entry [this entry])
(-into-entries [this children]))
(deftype EntriesParser
[^int n
^int zero
^int one
^int two
^int ^:unsynchronized-mutable current
^objects children
^objects entries
^objects forms
^java.util.Set entry-set
^boolean naked-keys
^boolean lazy-refs
options
^:unsynchronized-mutable k
^:unsynchronized-mutable ?p
^:unsynchronized-mutable ?v
^:unsynchronized-mutable f
^:unsynchronized-mutable p
^:unsynchronized-mutable ?s
]
IntoEntries
(-parse-entry [this e]
(if (instance? clojure.lang.Sequential e)
(let [^clojure.lang.PersistentVector e e]
(let [c (.count e)
h (.nth e zero)]
(if (-reference? h)
(if (== one c)
(when naked-keys
(set! k h)
(set! ?p nil)
(set! ?v e)
(set! f e))
(when (== two c)
(let [v (.nth e one)]
(when (instance? clojure.lang.IPersistentMap v)
(when naked-keys
(set! k h)
(set! ?p v)
(set! ?v h)
(set! f e))))))
(let [i (unchecked-dec-int c)
last (-> e (.nth i) (schema options) -form)]
(set! k h)
(set! ?p (.nth e one))
(set! ?v (when (> c two) (.nth e two)))
(set! f (.assocN e i last))))))
(if (band naked-keys (-reference? e))
(do
(set! k e)
(set! ?p nil)
(set! ?v e)
(set! f e))
(-fail! ::invalid-ref {:ref e})))
(if (or (nil? ?p) (instance? clojure.lang.IPersistentMap ?p))
(do
(set! p ?p)
(set! ?s ?v))
(do
(set! p nil)
(set! ?s ?p)))
(let [s (cond-> (or ?s (if (-reference? k) f)) lazy-refs (-lazy options))
s' (schema s options)
c [k p s']
vs (-val-schema s' p)
e' (miu/-tagged k vs)]
(clojure.lang.RT/aset children current c)
(clojure.lang.RT/aset entries current e')
(clojure.lang.RT/aset forms current f))
(if (.add entry-set k)
nil
(-fail! ::non-distinct-entry-keys {:keys (set entry-set)}))
(set! current (unchecked-inc-int current))
this)
(-into-entries [this es]
(let [it (.iterator ^clojure.lang.PersistentVector es)]
(loop []
(when (.hasNext it)
(._parse_entry this (.next it))
(recur))))
{:children (clojure.lang.LazilyPersistentVector/createOwning children)
:entries (clojure.lang.LazilyPersistentVector/createOwning entries)
:forms (clojure.lang.LazilyPersistentVector/createOwning forms)}))
(let [i0 (int 0) i1 (int 1) i2 (int 2) lf (float 0.5)]
(defn entries-parser
[^clojure.lang.PersistentVector children naked-keys lazy-refs options]
(let [n (.count children)]
(EntriesParser.
n
i0
i1
i2
0
(clojure.lang.RT/object_array n)
(object-array n)
(object-array n)
(java.util.HashSet. n lf)
naked-keys
lazy-refs
options
nil
nil
nil
nil
nil
nil))))))
;;; usage
(-into-entries (entries-parser entries false false nil) entries)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment