Skip to content

Commit

Permalink
add pipe synonym to ||
Browse files Browse the repository at this point in the history
  • Loading branch information
inconvergent committed May 18, 2024
1 parent 5135375 commit 5d9bfa8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/init.lisp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(in-package :lqn)

(defvar *qmodes* '(:+ :? :- :%))
(defvar *operators* '(:|| :@
(defvar *operators* '(:|| :pipe :@
:?select
:?map :?filter :?fld
:*$ :$*
Expand Down
4 changes: 2 additions & 2 deletions src/pre-qry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
ctx isect qq)))
(loop for q in qq collect (pre/scan-clause q)))

(defun pre/|| (qq) (unless qq (warn "||: missing args.")) ; pipe
(defun pre/pipe (qq) (unless qq (warn "pipe: missing args.")) ; pipe
(loop for q in (pre/scan-clauses qq '#:pipe '(:@)) collect
(if (dat? q) (kw q)
(typecase q (cons q) (boolean q)
Expand All @@ -110,7 +110,7 @@
(let* ((q* (remove-if #'dat? (pre/scan-clauses q '#:?map '(:@))))
(res (mapcar #'unpack- q*))
(allres (if (= (length q) (length q*)) res (cons `(lit :_) res))))
(if (< (length allres) 2) allres `((|| ,@allres))))))
(if (< (length allres) 2) allres `((pipe ,@allres))))))

(defun pre/?select (q &optional (m :+)) (unless q (warn "?select: missing args."))
(labels ; TODO: how to handle selecting only keys with -@?
Expand Down
9 changes: 5 additions & 4 deletions src/qry-operators.lisp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(in-package :lqn)

(defmacro o∈ (∇ s expr) (declare (ignore s)) `(setf ,,expr))
(defun compile/|| (rec conf d) ; (|| ...) pipe
(defun compile/pipe (rec conf d) ; (|| ...) pipe
(awg (∇-)
(if (< (length d) 2) (funcall rec conf (car d))
`(let ((,∇- ,(gk conf :dat)))
Expand Down Expand Up @@ -267,7 +267,8 @@
((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/pipe #'rec conf (pre/pipe (cdr d))))
((qop? :pipe d) (compile/pipe #'rec conf (pre/pipe (cdr d))))
((qop? :$* d) (compile/$* #'rec conf (pre/?select (cdr d))))
((qop? :*$ d) (compile/*$ #'rec conf (pre/?select (cdr d))))
((qop? :?select d) (compile/?select #'rec conf (pre/?select (cdr d))))
Expand Down Expand Up @@ -300,11 +301,11 @@
(when db (qry/show q cq))
`(funcall ,cq ,dat ":internal:" 0)))
(defmacro qry (dat &rest rest) "query data. rest is wrapped in the pipe operator."
`(qryd ,dat (|| ,@rest)))
`(qryd ,dat (pipe ,@rest)))

; TODO: fix arguments/names in qryd, jsnqryf etc.
(defmacro qrydb (dat &rest rest) "query data. rest is wrapped in the pipe operator."
`(qryd ,dat (|| ,@rest) :db t))
`(qryd ,dat (pipe ,@rest) :db t))
(defun qryl (dat q &key db) "compile lqn query and run on dat"
(eval `(qryd ,dat ,q :db ,db)))
(defmacro jsnqryf (fn q &key db) "run lqn query on json file, fn"
Expand Down
2 changes: 1 addition & 1 deletion src/sh.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
(defun sh/is-query (q ex) (or q (sh/exit-msg 5 "MISSING query!~%~%~a~&" ex)))
(defun sh/parse-query (q) (declare #.*opt*)
(handler-case (let ((rd (read-all-str q)))
`(|| ,@(if rd rd '(nil))))
`(pipe ,@(if rd rd '(nil))))
(error (e) (sh/exit-msg 10 "failed to PARSE qry:~%~%~a~&" e))))

(defun sh/compile-query (qq) (declare #.*opt*)
Expand Down

0 comments on commit 5d9bfa8

Please sign in to comment.