* doc/lispref/variables.texi (Scope): Mention the availability of lexbind.
[emacs.git] / lisp / emacs-lisp / pcase.el
blob89bbff980c40405994322dad49129d9ee1ed979a
1 ;;; pcase.el --- ML-style pattern-matching macro for Elisp -*- lexical-binding: t -*-
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords:
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; ML-style pattern matching.
26 ;; The entry points are autoloaded.
28 ;; Todo:
30 ;; - provide ways to extend the set of primitives, with some kind of
31 ;; define-pcase-matcher. We could easily make it so that (guard BOOLEXP)
32 ;; could be defined this way, as a shorthand for (pred (lambda (_) BOOLEXP)).
33 ;; But better would be if we could define new ways to match by having the
34 ;; extension provide its own `pcase--split-<foo>' thingy.
35 ;; - ideally we'd want (pcase s ((re RE1) E1) ((re RE2) E2)) to be able to
36 ;; generate a lex-style DFA to decide whether to run E1 or E2.
38 ;;; Code:
40 ;; Macro-expansion of pcase is reasonably fast, so it's not a problem
41 ;; when byte-compiling a file, but when interpreting the code, if the pcase
42 ;; is in a loop, the repeated macro-expansion becomes terribly costly, so we
43 ;; memoize previous macro expansions to try and avoid recomputing them
44 ;; over and over again.
45 (defconst pcase-memoize (make-hash-table :weakness t :test 'equal))
47 (defconst pcase--dontcare-upats '(t _ dontcare))
49 ;;;###autoload
50 (defmacro pcase (exp &rest cases)
51 "Perform ML-style pattern matching on EXP.
52 CASES is a list of elements of the form (UPATTERN CODE...).
54 UPatterns can take the following forms:
55 _ matches anything.
56 SYMBOL matches anything and binds it to SYMBOL.
57 (or UPAT...) matches if any of the patterns matches.
58 (and UPAT...) matches if all the patterns match.
59 `QPAT matches if the QPattern QPAT matches.
60 (pred PRED) matches if PRED applied to the object returns non-nil.
61 (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil.
62 If a SYMBOL is used twice in the same pattern (i.e. the pattern is
63 \"non-linear\"), then the second occurrence is turned into an `eq'uality test.
65 QPatterns can take the following forms:
66 (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr.
67 ,UPAT matches if the UPattern UPAT matches.
68 STRING matches if the object is `equal' to STRING.
69 ATOM matches if the object is `eq' to ATOM.
70 QPatterns for vectors are not implemented yet.
72 PRED can take the form
73 FUNCTION in which case it gets called with one argument.
74 (FUN ARG1 .. ARGN) in which case it gets called with N+1 arguments.
75 A PRED of the form FUNCTION is equivalent to one of the form (FUNCTION).
76 PRED patterns can refer to variables bound earlier in the pattern.
77 E.g. you can match pairs where the cdr is larger than the car with a pattern
78 like `(,a . ,(pred (< a))) or, with more checks:
79 `(,(and a (pred numberp)) . ,(and (pred numberp) (pred (< a))))"
80 (declare (indent 1) (debug case)) ;FIXME: edebug `guard' and vars.
81 (or (gethash (cons exp cases) pcase-memoize)
82 (puthash (cons exp cases)
83 (pcase--expand exp cases)
84 pcase-memoize)))
86 ;;;###autoload
87 (defmacro pcase-let* (bindings &rest body)
88 "Like `let*' but where you can use `pcase' patterns for bindings.
89 BODY should be an expression, and BINDINGS should be a list of bindings
90 of the form (UPAT EXP)."
91 (declare (indent 1) (debug let))
92 (cond
93 ((null bindings) (if (> (length body) 1) `(progn ,@body) (car body)))
94 ((pcase--trivial-upat-p (caar bindings))
95 `(let (,(car bindings)) (pcase-let* ,(cdr bindings) ,@body)))
97 `(pcase ,(cadr (car bindings))
98 (,(caar bindings) (pcase-let* ,(cdr bindings) ,@body))
99 ;; We can either signal an error here, or just use `dontcare' which
100 ;; generates more efficient code. In practice, if we use `dontcare' we
101 ;; will still often get an error and the few cases where we don't do not
102 ;; matter that much, so it's a better choice.
103 (dontcare nil)))))
105 ;;;###autoload
106 (defmacro pcase-let (bindings &rest body)
107 "Like `let' but where you can use `pcase' patterns for bindings.
108 BODY should be a list of expressions, and BINDINGS should be a list of bindings
109 of the form (UPAT EXP)."
110 (declare (indent 1) (debug let))
111 (if (null (cdr bindings))
112 `(pcase-let* ,bindings ,@body)
113 (let ((matches '()))
114 (dolist (binding (prog1 bindings (setq bindings nil)))
115 (cond
116 ((memq (car binding) pcase--dontcare-upats)
117 (push (cons (make-symbol "_") (cdr binding)) bindings))
118 ((pcase--trivial-upat-p (car binding)) (push binding bindings))
120 (let ((tmpvar (make-symbol (format "x%d" (length bindings)))))
121 (push (cons tmpvar (cdr binding)) bindings)
122 (push (list (car binding) tmpvar) matches)))))
123 `(let ,(nreverse bindings) (pcase-let* ,matches ,@body)))))
125 (defmacro pcase-dolist (spec &rest body)
126 (if (pcase--trivial-upat-p (car spec))
127 `(dolist ,spec ,@body)
128 (let ((tmpvar (make-symbol "x")))
129 `(dolist (,tmpvar ,@(cdr spec))
130 (pcase-let* ((,(car spec) ,tmpvar))
131 ,@body)))))
134 (defun pcase--trivial-upat-p (upat)
135 (and (symbolp upat) (not (memq upat pcase--dontcare-upats))))
137 (defun pcase--expand (exp cases)
138 (let* ((defs (if (symbolp exp) '()
139 (let ((sym (make-symbol "x")))
140 (prog1 `((,sym ,exp)) (setq exp sym)))))
141 (seen '())
142 (codegen
143 (lambda (code vars)
144 (let ((prev (assq code seen)))
145 (if (not prev)
146 (let ((res (pcase-codegen code vars)))
147 (push (list code vars res) seen)
148 res)
149 ;; Since we use a tree-based pattern matching
150 ;; technique, the leaves (the places that contain the
151 ;; code to run once a pattern is matched) can get
152 ;; copied a very large number of times, so to avoid
153 ;; code explosion, we need to keep track of how many
154 ;; times we've used each leaf and move it
155 ;; to a separate function if that number is too high.
157 ;; We've already used this branch. So it is shared.
158 (let* ((code (car prev)) (cdrprev (cdr prev))
159 (prevvars (car cdrprev)) (cddrprev (cdr cdrprev))
160 (res (car cddrprev)))
161 (unless (symbolp res)
162 ;; This is the first repeat, so we have to move
163 ;; the branch to a separate function.
164 (let ((bsym
165 (make-symbol (format "pcase-%d" (length defs)))))
166 (push `(,bsym (lambda ,(mapcar #'car prevvars) ,@code)) defs)
167 (setcar res 'funcall)
168 (setcdr res (cons bsym (mapcar #'cdr prevvars)))
169 (setcar (cddr prev) bsym)
170 (setq res bsym)))
171 (setq vars (copy-sequence vars))
172 (let ((args (mapcar (lambda (pa)
173 (let ((v (assq (car pa) vars)))
174 (setq vars (delq v vars))
175 (cdr v)))
176 prevvars)))
177 (when vars ;New additional vars.
178 (error "The vars %s are only bound in some paths"
179 (mapcar #'car vars)))
180 `(funcall ,res ,@args)))))))
181 (main
182 (pcase--u
183 (mapcar (lambda (case)
184 `((match ,exp . ,(car case))
185 ,(apply-partially
186 (if (pcase--small-branch-p (cdr case))
187 ;; Don't bother sharing multiple
188 ;; occurrences of this leaf since it's small.
189 #'pcase-codegen codegen)
190 (cdr case))))
191 cases))))
192 (if (null defs) main
193 `(let ,defs ,main))))
195 (defun pcase-codegen (code vars)
196 `(let ,(mapcar (lambda (b) (list (car b) (cdr b))) vars)
197 ,@code))
199 (defun pcase--small-branch-p (code)
200 (and (= 1 (length code))
201 (or (not (consp (car code)))
202 (let ((small t))
203 (dolist (e (car code))
204 (if (consp e) (setq small nil)))
205 small))))
207 ;; Try to use `cond' rather than a sequence of `if's, so as to reduce
208 ;; the depth of the generated tree.
209 (defun pcase--if (test then else)
210 (cond
211 ((eq else :pcase--dontcare) then)
212 ((eq (car-safe else) 'if)
213 (if (equal test (nth 1 else))
214 ;; Doing a test a second time: get rid of the redundancy.
215 ;; FIXME: ideally, this should never happen because the pcase--split-*
216 ;; funs should have eliminated such things, but pcase--split-member
217 ;; is imprecise, so in practice it can happen occasionally.
218 `(if ,test ,then ,@(nthcdr 3 else))
219 `(cond (,test ,then)
220 (,(nth 1 else) ,(nth 2 else))
221 (t ,@(nthcdr 3 else)))))
222 ((eq (car-safe else) 'cond)
223 `(cond (,test ,then)
224 ;; Doing a test a second time: get rid of the redundancy, as above.
225 ,@(remove (assoc test else) (cdr else))))
226 (t `(if ,test ,then ,else))))
228 (defun pcase--upat (qpattern)
229 (cond
230 ((eq (car-safe qpattern) '\,) (cadr qpattern))
231 (t (list '\` qpattern))))
233 ;; Note about MATCH:
234 ;; When we have patterns like `(PAT1 . PAT2), after performing the `consp'
235 ;; check, we want to turn all the similar patterns into ones of the form
236 ;; (and (match car PAT1) (match cdr PAT2)), so you naturally need conjunction.
237 ;; Earlier code hence used branches of the form (MATCHES . CODE) where
238 ;; MATCHES was a list (implicitly a conjunction) of (SYM . PAT).
239 ;; But if we have a pattern of the form (or `(PAT1 . PAT2) PAT3), there is
240 ;; no easy way to eliminate the `consp' check in such a representation.
241 ;; So we replaced the MATCHES by the MATCH below which can be made up
242 ;; of conjunctions and disjunctions, so if we know `foo' is a cons, we can
243 ;; turn (match foo . (or `(PAT1 . PAT2) PAT3)) into
244 ;; (or (and (match car . `PAT1) (match cdr . `PAT2)) (match foo . PAT3)).
245 ;; The downside is that we now have `or' and `and' both in MATCH and
246 ;; in PAT, so there are different equivalent representations and we
247 ;; need to handle them all. We do not try to systematically
248 ;; canonicalize them to one form over another, but we do occasionally
249 ;; turn one into the other.
251 (defun pcase--u (branches)
252 "Expand matcher for rules BRANCHES.
253 Each BRANCH has the form (MATCH CODE . VARS) where
254 CODE is the code generator for that branch.
255 VARS is the set of vars already bound by earlier matches.
256 MATCH is the pattern that needs to be matched, of the form:
257 (match VAR . UPAT)
258 (and MATCH ...)
259 (or MATCH ...)"
260 (when (setq branches (delq nil branches))
261 (let* ((carbranch (car branches))
262 (match (car carbranch)) (cdarbranch (cdr carbranch))
263 (code (car cdarbranch))
264 (vars (cdr cdarbranch)))
265 (pcase--u1 (list match) code vars (cdr branches)))))
267 (defun pcase--and (match matches)
268 (if matches `(and ,match ,@matches) match))
270 (defun pcase--split-match (sym splitter match)
271 (cond
272 ((eq (car match) 'match)
273 (if (not (eq sym (cadr match)))
274 (cons match match)
275 (let ((pat (cddr match)))
276 (cond
277 ;; Hoist `or' and `and' patterns to `or' and `and' matches.
278 ((memq (car-safe pat) '(or and))
279 (pcase--split-match sym splitter
280 (cons (car pat)
281 (mapcar (lambda (alt)
282 `(match ,sym . ,alt))
283 (cdr pat)))))
284 (t (let ((res (funcall splitter (cddr match))))
285 (cons (or (car res) match) (or (cdr res) match))))))))
286 ((memq (car match) '(or and))
287 (let ((then-alts '())
288 (else-alts '())
289 (neutral-elem (if (eq 'or (car match))
290 :pcase--fail :pcase--succeed))
291 (zero-elem (if (eq 'or (car match)) :pcase--succeed :pcase--fail)))
292 (dolist (alt (cdr match))
293 (let ((split (pcase--split-match sym splitter alt)))
294 (unless (eq (car split) neutral-elem)
295 (push (car split) then-alts))
296 (unless (eq (cdr split) neutral-elem)
297 (push (cdr split) else-alts))))
298 (cons (cond ((memq zero-elem then-alts) zero-elem)
299 ((null then-alts) neutral-elem)
300 ((null (cdr then-alts)) (car then-alts))
301 (t (cons (car match) (nreverse then-alts))))
302 (cond ((memq zero-elem else-alts) zero-elem)
303 ((null else-alts) neutral-elem)
304 ((null (cdr else-alts)) (car else-alts))
305 (t (cons (car match) (nreverse else-alts)))))))
306 (t (error "Uknown MATCH %s" match))))
308 (defun pcase--split-rest (sym splitter rest)
309 (let ((then-rest '())
310 (else-rest '()))
311 (dolist (branch rest)
312 (let* ((match (car branch))
313 (code&vars (cdr branch))
314 (splitted
315 (pcase--split-match sym splitter match)))
316 (unless (eq (car splitted) :pcase--fail)
317 (push (cons (car splitted) code&vars) then-rest))
318 (unless (eq (cdr splitted) :pcase--fail)
319 (push (cons (cdr splitted) code&vars) else-rest))))
320 (cons (nreverse then-rest) (nreverse else-rest))))
322 (defun pcase--split-consp (syma symd pat)
323 (cond
324 ;; A QPattern for a cons, can only go the `then' side.
325 ((and (eq (car-safe pat) '\`) (consp (cadr pat)))
326 (let ((qpat (cadr pat)))
327 (cons `(and (match ,syma . ,(pcase--upat (car qpat)))
328 (match ,symd . ,(pcase--upat (cdr qpat))))
329 :pcase--fail)))
330 ;; A QPattern but not for a cons, can only go the `else' side.
331 ((eq (car-safe pat) '\`) (cons :pcase--fail nil))))
333 (defun pcase--split-equal (elem pat)
334 (cond
335 ;; The same match will give the same result.
336 ((and (eq (car-safe pat) '\`) (equal (cadr pat) elem))
337 (cons :pcase--succeed :pcase--fail))
338 ;; A different match will fail if this one succeeds.
339 ((and (eq (car-safe pat) '\`)
340 ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
341 ;; (consp (cadr pat)))
343 (cons :pcase--fail nil))))
345 (defun pcase--split-member (elems pat)
346 ;; Based on pcase--split-equal.
347 (cond
348 ;; The same match (or a match of membership in a superset) will
349 ;; give the same result, but we don't know how to check it.
350 ;; (???
351 ;; (cons :pcase--succeed nil))
352 ;; A match for one of the elements may succeed or fail.
353 ((and (eq (car-safe pat) '\`) (member (cadr pat) elems))
354 nil)
355 ;; A different match will fail if this one succeeds.
356 ((and (eq (car-safe pat) '\`)
357 ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
358 ;; (consp (cadr pat)))
360 (cons :pcase--fail nil))))
362 (defun pcase--split-pred (upat pat)
363 ;; FIXME: For predicates like (pred (> a)), two such predicates may
364 ;; actually refer to different variables `a'.
365 (if (equal upat pat)
366 (cons :pcase--succeed :pcase--fail)))
368 (defun pcase--fgrep (vars sexp)
369 "Check which of the symbols VARS appear in SEXP."
370 (let ((res '()))
371 (while (consp sexp)
372 (dolist (var (pcase--fgrep vars (pop sexp)))
373 (unless (memq var res) (push var res))))
374 (and (memq sexp vars) (not (memq sexp res)) (push sexp res))
375 res))
377 ;; It's very tempting to use `pcase' below, tho obviously, it'd create
378 ;; bootstrapping problems.
379 (defun pcase--u1 (matches code vars rest)
380 "Return code that runs CODE (with VARS) if MATCHES match.
381 and otherwise defers to REST which is a list of branches of the form
382 \(ELSE-MATCH ELSE-CODE . ELSE-VARS)."
383 ;; Depending on the order in which we choose to check each of the MATCHES,
384 ;; the resulting tree may be smaller or bigger. So in general, we'd want
385 ;; to be careful to chose the "optimal" order. But predicate
386 ;; patterns make this harder because they create dependencies
387 ;; between matches. So we don't bother trying to reorder anything.
388 (cond
389 ((null matches) (funcall code vars))
390 ((eq :pcase--fail (car matches)) (pcase--u rest))
391 ((eq :pcase--succeed (car matches))
392 (pcase--u1 (cdr matches) code vars rest))
393 ((eq 'and (caar matches))
394 (pcase--u1 (append (cdar matches) (cdr matches)) code vars rest))
395 ((eq 'or (caar matches))
396 (let* ((alts (cdar matches))
397 (var (if (eq (caar alts) 'match) (cadr (car alts))))
398 (simples '()) (others '()))
399 (when var
400 (dolist (alt alts)
401 (if (and (eq (car alt) 'match) (eq var (cadr alt))
402 (let ((upat (cddr alt)))
403 (and (eq (car-safe upat) '\`)
404 (or (integerp (cadr upat)) (symbolp (cadr upat))
405 (stringp (cadr upat))))))
406 (push (cddr alt) simples)
407 (push alt others))))
408 (cond
409 ((null alts) (error "Please avoid it") (pcase--u rest))
410 ((> (length simples) 1)
411 ;; De-hoist the `or' MATCH into an `or' pattern that will be
412 ;; turned into a `memq' below.
413 (pcase--u1 (cons `(match ,var or . ,(nreverse simples)) (cdr matches))
414 code vars
415 (if (null others) rest
416 (cons (cons
417 (pcase--and (if (cdr others)
418 (cons 'or (nreverse others))
419 (car others))
420 (cdr matches))
421 (cons code vars))
422 rest))))
424 (pcase--u1 (cons (pop alts) (cdr matches)) code vars
425 (if (null alts) (progn (error "Please avoid it") rest)
426 (cons (cons
427 (pcase--and (if (cdr alts)
428 (cons 'or alts) (car alts))
429 (cdr matches))
430 (cons code vars))
431 rest)))))))
432 ((eq 'match (caar matches))
433 (let* ((popmatches (pop matches))
434 (_op (car popmatches)) (cdrpopmatches (cdr popmatches))
435 (sym (car cdrpopmatches))
436 (upat (cdr cdrpopmatches)))
437 (cond
438 ((memq upat '(t _)) (pcase--u1 matches code vars rest))
439 ((eq upat 'dontcare) :pcase--dontcare)
440 ((functionp upat) (error "Feature removed, use (pred %s)" upat))
441 ((memq (car-safe upat) '(guard pred))
442 (let* ((splitrest
443 (pcase--split-rest
444 sym (apply-partially #'pcase--split-pred upat) rest))
445 (then-rest (car splitrest))
446 (else-rest (cdr splitrest)))
447 (pcase--if (if (and (eq (car upat) 'pred) (symbolp (cadr upat)))
448 `(,(cadr upat) ,sym)
449 (let* ((exp (cadr upat))
450 ;; `vs' is an upper bound on the vars we need.
451 (vs (pcase--fgrep (mapcar #'car vars) exp))
452 (call (cond
453 ((eq 'guard (car upat)) exp)
454 ((functionp exp) `(,exp ,sym))
455 (t `(,@exp ,sym)))))
456 (if (null vs)
457 call
458 ;; Let's not replace `vars' in `exp' since it's
459 ;; too difficult to do it right, instead just
460 ;; let-bind `vars' around `exp'.
461 `(let ,(mapcar (lambda (var)
462 (list var (cdr (assq var vars))))
464 ;; FIXME: `vars' can capture `sym'. E.g.
465 ;; (pcase x ((and `(,x . ,y) (pred (fun x)))))
466 ,call))))
467 (pcase--u1 matches code vars then-rest)
468 (pcase--u else-rest))))
469 ((symbolp upat)
470 (if (not (assq upat vars))
471 (pcase--u1 matches code (cons (cons upat sym) vars) rest)
472 ;; Non-linear pattern. Turn it into an `eq' test.
473 (pcase--u1 (cons `(match ,sym . (pred (eq ,(cdr (assq upat vars)))))
474 matches)
475 code vars rest)))
476 ((eq (car-safe upat) '\`)
477 (pcase--q1 sym (cadr upat) matches code vars rest))
478 ((eq (car-safe upat) 'or)
479 (let ((all (> (length (cdr upat)) 1))
480 (memq-fine t))
481 (when all
482 (dolist (alt (cdr upat))
483 (unless (and (eq (car-safe alt) '\`)
484 (or (symbolp (cadr alt)) (integerp (cadr alt))
485 (setq memq-fine nil)
486 (stringp (cadr alt))))
487 (setq all nil))))
488 (if all
489 ;; Use memq for (or `a `b `c `d) rather than a big tree.
490 (let* ((elems (mapcar 'cadr (cdr upat)))
491 (splitrest
492 (pcase--split-rest
493 sym (apply-partially #'pcase--split-member elems) rest))
494 (then-rest (car splitrest))
495 (else-rest (cdr splitrest)))
496 (pcase--if `(,(if memq-fine #'memq #'member) ,sym ',elems)
497 (pcase--u1 matches code vars then-rest)
498 (pcase--u else-rest)))
499 (pcase--u1 (cons `(match ,sym ,@(cadr upat)) matches) code vars
500 (append (mapcar (lambda (upat)
501 `((and (match ,sym . ,upat) ,@matches)
502 ,code ,@vars))
503 (cddr upat))
504 rest)))))
505 ((eq (car-safe upat) 'and)
506 (pcase--u1 (append (mapcar (lambda (upat) `(match ,sym ,@upat))
507 (cdr upat))
508 matches)
509 code vars rest))
510 ((eq (car-safe upat) 'not)
511 ;; FIXME: The implementation below is naive and results in
512 ;; inefficient code.
513 ;; To make it work right, we would need to turn pcase--u1's
514 ;; `code' and `vars' into a single argument of the same form as
515 ;; `rest'. We would also need to split this new `then-rest' argument
516 ;; for every test (currently we don't bother to do it since
517 ;; it's only useful for odd patterns like (and `(PAT1 . PAT2)
518 ;; `(PAT3 . PAT4)) which the programmer can easily rewrite
519 ;; to the more efficient `(,(and PAT1 PAT3) . ,(and PAT2 PAT4))).
520 (pcase--u1 `((match ,sym . ,(cadr upat)))
521 ;; FIXME: This codegen is not careful to share its
522 ;; code if used several times: code blow up is likely.
523 (lambda (_vars)
524 ;; `vars' will likely contain bindings which are
525 ;; not always available in other paths to
526 ;; `rest', so there' no point trying to pass
527 ;; them down.
528 (pcase--u rest))
529 vars
530 (list `((and . ,matches) ,code . ,vars))))
531 (t (error "Unknown upattern `%s'" upat)))))
532 (t (error "Incorrect MATCH %s" (car matches)))))
534 (defun pcase--q1 (sym qpat matches code vars rest)
535 "Return code that runs CODE if SYM matches QPAT and if MATCHES match.
536 and if not, defers to REST which is a list of branches of the form
537 \(OTHER_MATCH OTHER-CODE . OTHER-VARS)."
538 (cond
539 ((eq (car-safe qpat) '\,) (error "Can't use `,UPATTERN"))
540 ((floatp qpat) (error "Floating point patterns not supported"))
541 ((vectorp qpat)
542 ;; FIXME.
543 (error "Vector QPatterns not implemented yet"))
544 ((consp qpat)
545 (let ((syma (make-symbol "xcar"))
546 (symd (make-symbol "xcdr")))
547 (let* ((splitrest (pcase--split-rest
549 (apply-partially #'pcase--split-consp syma symd)
550 rest))
551 (then-rest (car splitrest))
552 (else-rest (cdr splitrest)))
553 (pcase--if `(consp ,sym)
554 `(let ((,syma (car ,sym))
555 (,symd (cdr ,sym)))
556 ,(pcase--u1 `((match ,syma . ,(pcase--upat (car qpat)))
557 (match ,symd . ,(pcase--upat (cdr qpat)))
558 ,@matches)
559 code vars then-rest))
560 (pcase--u else-rest)))))
561 ((or (integerp qpat) (symbolp qpat) (stringp qpat))
562 (let* ((splitrest (pcase--split-rest
563 sym (apply-partially 'pcase--split-equal qpat) rest))
564 (then-rest (car splitrest))
565 (else-rest (cdr splitrest)))
566 (pcase--if `(,(if (stringp qpat) #'equal #'eq) ,sym ',qpat)
567 (pcase--u1 matches code vars then-rest)
568 (pcase--u else-rest))))
569 (t (error "Unkown QPattern %s" qpat))))
572 (provide 'pcase)
573 ;;; pcase.el ends here