Skip to content

Commit

Permalink
rename internal file. refactor operators. wip
Browse files Browse the repository at this point in the history
  • Loading branch information
inconvergent committed Mar 13, 2024
1 parent 34485d0 commit a761ba6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 79 deletions.
6 changes: 3 additions & 3 deletions bin/test-sh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ r=`echo '{"_id": 1}' | sbcl --script ./jqn-sh.lisp -jm '{:_id}'`; check;
a='((:_ID . 1))'
r=`echo '{"_id": 1}' | sbcl --script ./jqn-sh.lisp -lm '{:_id}'`; check;

a='[{"_id":"65679","things":[{"id":10}]}]
[{"_id":"6AABB"}]'
a='{"_id":"65679","things":[{"id":10}]}
{"_id":"6AABB"}'
r=`echo '{ "_id": "65679", "things": [ { "id": 10 } ] }
{ "_id": "6AABB" }' |\
sbcl --script ./jqn-sh.lisp -m '#{:_id :?@things}'`; check;
sbcl --script ./jqn-sh.lisp -m '{:_id :?@things}'`; check;

echo '## done! all clear!'

25 changes: 6 additions & 19 deletions docs/lqn.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@
; Source file: /data/x/lqn/src/qry-utils.lisp
```

#### LQN:?Q

```
:missing:todo:
; LQN:?Q
; [symbol]
;
; ?Q names a macro:
; Lambda-list: (&REST ARGS)
; Source file: /data/x/lqn/src/qry.lisp
```

#### LQN:@\*

```
Expand Down Expand Up @@ -656,7 +643,7 @@
; Lambda-list: (FN Q &KEY DB)
; Documentation:
; run lqn query on json file, fn
; Source file: /data/x/lqn/src/qry.lisp
; Source file: /data/x/lqn/src/qry-operators.lisp
```

#### LQN:JSNSTR
Expand Down Expand Up @@ -1022,7 +1009,7 @@
; Derived type: (FUNCTION (T &OPTIONAL T) (VALUES CONS &OPTIONAL))
; Documentation:
; compile lqn query
; Source file: /data/x/lqn/src/qry.lisp
; Source file: /data/x/lqn/src/qry-operators.lisp
```

#### LQN:PSH\*
Expand All @@ -1048,7 +1035,7 @@
; Lambda-list: (DAT &REST REST)
; Documentation:
; query data. rest is wrapped in the pipe operator.
; Source file: /data/x/lqn/src/qry.lisp
; Source file: /data/x/lqn/src/qry-operators.lisp
```

#### LQN:QRYD
Expand All @@ -1061,7 +1048,7 @@
; Lambda-list: (DAT Q &KEY DB)
; Documentation:
; run lqn query on dat
; Source file: /data/x/lqn/src/qry.lisp
; Source file: /data/x/lqn/src/qry-operators.lisp
```

#### LQN:QRYDB
Expand All @@ -1074,7 +1061,7 @@
; Lambda-list: (DAT &REST REST)
; Documentation:
; query data. rest is wrapped in the pipe operator.
; Source file: /data/x/lqn/src/qry.lisp
; Source file: /data/x/lqn/src/qry-operators.lisp
```

#### LQN:QRYL
Expand All @@ -1088,7 +1075,7 @@
; Derived type: (FUNCTION (T T &KEY (:DB T)) *)
; Documentation:
; compile lqn query and run on dat
; Source file: /data/x/lqn/src/qry.lisp
; Source file: /data/x/lqn/src/qry-operators.lisp
```

#### LQN:RANGE
Expand Down
6 changes: 3 additions & 3 deletions lqn.asd
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
(:file "docs" :depends-on ("qry-utils"))
(:file "io" :depends-on ("docs"))
(:file "pre-qry" :depends-on ("io" "qry-utils" "docs"))
(:file "qry" :depends-on ("pre-qry"))
(:file "sh" :depends-on ("qry"))
(:file "qry-extra" :depends-on ("qry"))))
(:file "qry-operators" :depends-on ("pre-qry"))
(:file "sh" :depends-on ("qry-operators"))
(:file "qry-extra" :depends-on ("qry-operators"))))

(asdf:defsystem #:lqn/tests
:depends-on (#:lqn #:prove #:uiop #:asdf)
Expand Down
3 changes: 1 addition & 2 deletions src/init.lisp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(in-package :lqn)

(defvar *qmodes* '(:+ :? :- :%))
(defvar *operators*
`(:?map :@ :|| ?rec :*$ :$$ :$* :?filter :?fld :?xpr :?txpr :?mxpr :?srch :?grp))
(defvar *operators* '(:?map :@ :|| ?rec :*$ :$$ :$* :?filter :?fld :?xpr :?txpr :?mxpr :?srch :?grp))
(defvar *opt* '(optimize (speed 3) (safety 1)))
(defvar *fxns* '(:err :wrn :nope :noop :lst :lit :qt :hld :ghv :pnum :inum :cnt
:fmt :out :jsnstr
Expand Down
2 changes: 1 addition & 1 deletion src/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(:nicknames #:cl-lqn)
(:export #:d? #:v?
#:qry #:qrydb #:qryd #:jsnqryf #:qryl #:proc-qry
#:jsnloads #:jsnloadf #:jsnout #:ldnout #:ldnload #:fmt #:out #:jsnstr #:?q #:@* #:@@ #:??
#:jsnloads #:jsnloadf #:jsnout #:ldnout #:ldnload #:fmt #:out #:jsnstr #:@* #:@@ #:??
#:read? #:some? #:none? #:all? #:empty? #:size? #:is?
#:path? #:subdir #:subfiles #:ls #:dir? #:file? #:cwd #:now #:cmd
#:some? #:all? #:none? #:cd #:keys?
Expand Down
108 changes: 59 additions & 49 deletions src/qry.lisp → src/qry-operators.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(3 `(@@ ,@(funcall rec conf d)))
(otherwise (error "@: expected 0-3 arguments. got: ~a." d))))

(defun compile/?map (rec conf d) ; (?map ...) ; do this sequence of expressions on each item
(defun compile/?map (rec conf d) ; #(...) ; do this sequence of expressions on each item
(when (zerop (length d)) (error "?map: missing args."))
(awg (k i kres ires itr par)
(let ((expr (funcall rec (dat/new conf itr) (car d))))
Expand Down Expand Up @@ -117,36 +117,48 @@
(typecase ,par
(null nil) (hash-table (do-ht)) (list (do-vec))
(vector (do-vec)) (simple-vector (do-vec))
(otherwise (error "RT: $$ bad type. expected hash-table or vector.~%got: ~a" ,par)))))))
(otherwise (error "RT: {..} bad type. expected hash-table or vector.~%got: ~a." ,par)))))))

; TODO: vec! does the wrong thing for non-sequence

; rename this to (?@ ) or something?
; change to select from vec of objects? it kinda is alread??
(defun compile/$* (rec conf d) ; #[...] ; sel ; select from vec of hts to vec
(awg (i ires itr dat par)
`(loop with ,ires of-type vector = (mav)
with ,par of-type vector = (vec! ,(gk conf :dat))
for ,itr across ,par for ,i from 0
do (∈ (:par ,par :cnt ,i :itr ,itr)
,(when (car- dat? d) (compile/*add rec conf :+ ires itr))
,@(loop for (m kk expr) in (strip-all d) collect
`(let ((,dat (@@ ,itr ,kk)))
(declare (ignorable ,dat))
(∈ (:key ,kk) ,(compile/*add rec (dat/new conf dat) m ires expr)))))
finally (return ,ires))))
`(let ((,par ,(gk conf :dat)))
(labels ((do-vec (&aux (,ires (mav)))
(loop with ,par of-type vector = (vec! ,par)
for ,itr across ,par for ,i from 0
do (∈ (:par ,par :cnt ,i :itr ,itr)
,(when (car- dat? d) (compile/*add rec conf :+ ires itr))
,@(loop for (m kk expr) in (strip-all d) collect
`(let ((,dat (@@ ,itr ,kk)))
(declare (ignorable ,dat))
(∈ (:key ,kk) ,(compile/*add rec
(dat/new conf dat) m ires expr))))))
,ires))
(typecase ,par ; TODO: support hts
(null nil) (vector (do-vec)) (simple-vector (do-vec)) (list (do-vec))
(otherwise (error "RT: #[..] bad type. expected vector, got: ~a." ,par)))))))

(defun compile/*$ (rec conf d) ; #{...} ; sel ; select from vec of hts to vec of hts
(awg (i ires kvres itr dat par)
`(loop with ,ires of-type vector = (mav)
with ,par of-type vector = (vec! ,(gk conf :dat))
for ,itr of-type hash-table across ,par for ,i from 0
for ,kvres of-type hash-table = ,(if (car- dat? d) `(make$ ,itr) `(make$))
do (∈ (:par ,par :cnt ,i :itr ,itr)
,@(loop for (m kk expr) in (strip-all d)
collect `(let ((,dat (@@ ,itr ,kk)))
(declare (ignorable ,dat))
(∈ (:key ,kk) ,(compile/$add rec (dat/new conf dat) m kvres kk expr))))
(vex ,ires ($nil ,kvres)))
finally (return ,ires))))
`(let ((,par ,(gk conf :dat)))
(labels ((do-vec (&aux (,ires (mav)))
(loop with ,par of-type vector = (vec! ,par)
for ,itr of-type hash-table across ,par for ,i from 0
for ,kvres of-type hash-table = ,(if (car- dat? d) `(make$ ,itr) `(make$))
do (∈ (:par ,par :cnt ,i :itr ,itr)
,@(loop for (m kk expr) in (strip-all d)
collect `(let ((,dat (@@ ,itr ,kk)))
(declare (ignorable ,dat))
(∈ (:key ,kk) ,(compile/$add rec
(dat/new conf dat) m kvres kk expr))))
(vex ,ires ($nil ,kvres))))
,ires))
(typecase ,par (null nil)
(vector (do-vec)) (simple-vector (do-vec)) (list (do-vec))
(otherwise (error "RT: #{..} bad type. expected vector, got: ~a." ,par)))))))

(defun pre/?filter (q &optional (mm :?)) (unless q (warn "?filter: missing args."))
(labels ((unpack- (o) (dsb (m sk) (unpack-mode o mm) `(,m ,(pre/xpr-sel sk :_)))))
Expand All @@ -157,7 +169,7 @@
(defun xpr/get-modes (cd &rest mm)
(loop for (m expr) in (strip-all cd) if (member m mm :test #'eq) collect expr))

(defun compile/?filter (rec conf d) ; [...] ; sel ; filter object by these expressions
(defun compile/?filter (rec conf d) ; [..] ; sel ; filter object by these expressions
(awg (k i kres ires itr par)
`(let ((,par ,(gk conf :dat)))
(labels
Expand Down Expand Up @@ -239,30 +251,29 @@
(awg (dat fn fi)
(labels
((rec (conf d* &aux (d (pre/scan-clause d* nil)))
(cond
((dat? d) (gk conf :dat))
((stringp d) d) ; remember that this order is important
((vectorp d) (rec conf `(?map ,@(coerce d 'list))))
((atom d) d)
((qop? :|| d) (compile/|| #'rec conf (pre/|| (cdr d))))
((qop? :?filter d) (compile/?filter #'rec conf (pre/?filter (cdr d))))
((qop? :$* d) (compile/$* #'rec conf (pre/$$ (cdr d))))
((qop? :*$ d) (compile/*$ #'rec conf (pre/$$ (cdr d))))
((qop? :$$ d) (compile/$$ #'rec conf (pre/$$ (cdr d))))
((qop? :?map d) (compile/?map #'rec conf (pre/?map (cdr d))))
((qop? :?xpr d) (compile/?xpr #'rec conf (cdr d)))
((qop? :@ d) (compile/@ #'rec conf (cdr d)))
((qop? :?fld d) (compile/?fld #'rec conf (cdr d)))
((qop? :?mxpr d) (compile/?mxpr #'rec conf (cdr d)))
((qop? :?txpr d) (compile/?txpr #'rec conf (cdr d)))
((qop? :?srch d) (compile/?srch #'rec conf (cdr d)))
((qop? :?rec d) (compile/?rec #'rec conf (cdr d)))
((qop? :?grp d) (compile/?grp #'rec conf (cdr d)))
((car- lqnfx? d) `(,(psymb 'lqn (car d)) ,@(rec conf (cdr d))))
((consp d) (cons (rec conf (pre/scan-clause (car d))) (rec conf (cdr d))))
(t (error "lqn: unexpected clause: ~a~%in: ~a." d q)))))
`(λ (,dat ,fn ,fi) (q∈ (,dat ,fn ,fi)
,(rec `((:dat . ,dat) ,@conf*) q))))))
(cond ((dat? d) (gk conf :dat))
((stringp d) d) ; remember that this order is important
((vectorp d) (rec conf `(?map ,@(coerce d 'list))))
((atom d) d)
((qop? :|| d) (compile/|| #'rec conf (pre/|| (cdr d))))
((qop? :$* d) (compile/$* #'rec conf (pre/$$ (cdr d))))
((qop? :*$ d) (compile/*$ #'rec conf (pre/$$ (cdr d))))
((qop? :$$ d) (compile/$$ #'rec conf (pre/$$ (cdr d))))
((qop? :?map d) (compile/?map #'rec conf (pre/?map (cdr d))))
((qop? :?filter d) (compile/?filter #'rec conf (pre/?filter (cdr d))))
((qop? :?xpr d) (compile/?xpr #'rec conf (cdr d)))
((qop? :@ d) (compile/@ #'rec conf (cdr d)))
((qop? :?fld d) (compile/?fld #'rec conf (cdr d)))
((qop? :?mxpr d) (compile/?mxpr #'rec conf (cdr d)))
((qop? :?txpr d) (compile/?txpr #'rec conf (cdr d)))
((qop? :?srch d) (compile/?srch #'rec conf (cdr d)))
((qop? :?rec d) (compile/?rec #'rec conf (cdr d)))
((qop? :?grp d) (compile/?grp #'rec conf (cdr d)))
((car- lqnfx? d) `(,(psymb 'lqn (car d)) ,@(rec conf (cdr d))))
((consp d) (cons (rec conf (pre/scan-clause (car d))) (rec conf (cdr d))))
(t (error "lqn: unexpected clause: ~a~%in: ~a." d q)))))
`(λ (,dat ,fn ,fi) (q∈ (,dat ,fn ,fi)
,(rec `((:dat . ,dat) ,@conf*) q))))))

(defun qry/show (q cq)
(format t "
Expand All @@ -277,7 +288,6 @@
`(funcall ,cq ,dat ":internal:" 0)))
(defmacro qry (dat &rest rest) "query data. rest is wrapped in the pipe operator."
`(qryd ,dat (|| ,@rest)))
(abbrev ?q qry)

; TODO: fix arguments/names in qryd, jsnqryf etc.
(defmacro qrydb (dat &rest rest) "query data. rest is wrapped in the pipe operator."
Expand Down
4 changes: 2 additions & 2 deletions test/test-lqn.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@
(is (lqn:jsnqryf *test-data-fn* #[(:%@index (?? _ (= _ 0) _))]) #(0) :test #'equalp)
(is-str (lqn::jsnstr (lqn:jsnqryf *test-data-fn* (|| #[:things] (flatn* _) #[:id])))
"[0,10,11,12,31,32]")
(is-str (lqn::jsnstr (lqn:jsnqryf *test-data-fn* (|| #(#[:things]) (flatn* _ 2) #[:id])))
(is-str (lqn::jsnstr (lqn:jsnqryf *test-data-fn* (|| #[:things] (flatn* _ 2) #[:id])))
"[0,10,11,12,31,32]")
(is-str (lqn::jsnstr (lqn:jsnqryf *test-data-fn*
(|| #(#[:things]) (flatn* _ 2) #[:id]
(|| #[:things] (flatn* _ 2) #[:id]
(?fld (list) acc (cons (1+ _) acc)) (reverse _))))
"[1,11,12,13,32,33]"))

Expand Down

0 comments on commit a761ba6

Please sign in to comment.