Fix pcase memoizing; change lexbound byte-code marker.
[bpt/emacs.git] / lisp / emacs-lisp / pcase.el
1 ;;; pcase.el --- ML-style pattern-matching macro for Elisp -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords:
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; ML-style pattern matching.
26 ;; The entry points are autoloaded.
27
28 ;; Todo:
29
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.
37
38 ;;; Code:
39
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 'key :test 'eq))
46
47 (defconst pcase--dontcare-upats '(t _ dontcare))
48
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...).
53
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.
64
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.
71
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 ;; We want to use a weak hash table as a cache, but the key will unavoidably
82 ;; be based on `exp' and `cases', yet `cases' is a fresh new list each time
83 ;; we're called so it'll be immediately GC'd. So we use (car cases) as key
84 ;; which does come straight from the source code and should hence not be GC'd
85 ;; so easily.
86 (let ((data (gethash (car cases) pcase--memoize)))
87 ;; data = (EXP CASES . EXPANSION)
88 (if (and (equal exp (car data)) (equal cases (cadr data)))
89 ;; We have the right expansion.
90 (cddr data)
91 (when data
92 (message "pcase-memoize: equal first branch, yet different"))
93 (let ((expansion (pcase--expand exp cases)))
94 (puthash (car cases) (cons exp (cons cases expansion)) pcase--memoize)
95 expansion))))
96
97 ;;;###autoload
98 (defmacro pcase-let* (bindings &rest body)
99 "Like `let*' but where you can use `pcase' patterns for bindings.
100 BODY should be an expression, and BINDINGS should be a list of bindings
101 of the form (UPAT EXP)."
102 (declare (indent 1) (debug let))
103 (cond
104 ((null bindings) (if (> (length body) 1) `(progn ,@body) (car body)))
105 ((pcase--trivial-upat-p (caar bindings))
106 `(let (,(car bindings)) (pcase-let* ,(cdr bindings) ,@body)))
107 (t
108 `(pcase ,(cadr (car bindings))
109 (,(caar bindings) (pcase-let* ,(cdr bindings) ,@body))
110 ;; We can either signal an error here, or just use `dontcare' which
111 ;; generates more efficient code. In practice, if we use `dontcare' we
112 ;; will still often get an error and the few cases where we don't do not
113 ;; matter that much, so it's a better choice.
114 (dontcare nil)))))
115
116 ;;;###autoload
117 (defmacro pcase-let (bindings &rest body)
118 "Like `let' but where you can use `pcase' patterns for bindings.
119 BODY should be a list of expressions, and BINDINGS should be a list of bindings
120 of the form (UPAT EXP)."
121 (declare (indent 1) (debug let))
122 (if (null (cdr bindings))
123 `(pcase-let* ,bindings ,@body)
124 (let ((matches '()))
125 (dolist (binding (prog1 bindings (setq bindings nil)))
126 (cond
127 ((memq (car binding) pcase--dontcare-upats)
128 (push (cons (make-symbol "_") (cdr binding)) bindings))
129 ((pcase--trivial-upat-p (car binding)) (push binding bindings))
130 (t
131 (let ((tmpvar (make-symbol (format "x%d" (length bindings)))))
132 (push (cons tmpvar (cdr binding)) bindings)
133 (push (list (car binding) tmpvar) matches)))))
134 `(let ,(nreverse bindings) (pcase-let* ,matches ,@body)))))
135
136 (defmacro pcase-dolist (spec &rest body)
137 (if (pcase--trivial-upat-p (car spec))
138 `(dolist ,spec ,@body)
139 (let ((tmpvar (make-symbol "x")))
140 `(dolist (,tmpvar ,@(cdr spec))
141 (pcase-let* ((,(car spec) ,tmpvar))
142 ,@body)))))
143
144
145 (defun pcase--trivial-upat-p (upat)
146 (and (symbolp upat) (not (memq upat pcase--dontcare-upats))))
147
148 (defun pcase--expand (exp cases)
149 ;; (message "pid=%S (pcase--expand %S ...hash=%S)"
150 ;; (emacs-pid) exp (sxhash cases))
151 (let* ((defs (if (symbolp exp) '()
152 (let ((sym (make-symbol "x")))
153 (prog1 `((,sym ,exp)) (setq exp sym)))))
154 (seen '())
155 (codegen
156 (lambda (code vars)
157 (let ((prev (assq code seen)))
158 (if (not prev)
159 (let ((res (pcase-codegen code vars)))
160 (push (list code vars res) seen)
161 res)
162 ;; Since we use a tree-based pattern matching
163 ;; technique, the leaves (the places that contain the
164 ;; code to run once a pattern is matched) can get
165 ;; copied a very large number of times, so to avoid
166 ;; code explosion, we need to keep track of how many
167 ;; times we've used each leaf and move it
168 ;; to a separate function if that number is too high.
169 ;;
170 ;; We've already used this branch. So it is shared.
171 (let* ((code (car prev)) (cdrprev (cdr prev))
172 (prevvars (car cdrprev)) (cddrprev (cdr cdrprev))
173 (res (car cddrprev)))
174 (unless (symbolp res)
175 ;; This is the first repeat, so we have to move
176 ;; the branch to a separate function.
177 (let ((bsym
178 (make-symbol (format "pcase-%d" (length defs)))))
179 (push `(,bsym (lambda ,(mapcar #'car prevvars) ,@code)) defs)
180 (setcar res 'funcall)
181 (setcdr res (cons bsym (mapcar #'cdr prevvars)))
182 (setcar (cddr prev) bsym)
183 (setq res bsym)))
184 (setq vars (copy-sequence vars))
185 (let ((args (mapcar (lambda (pa)
186 (let ((v (assq (car pa) vars)))
187 (setq vars (delq v vars))
188 (cdr v)))
189 prevvars)))
190 (when vars ;New additional vars.
191 (error "The vars %s are only bound in some paths"
192 (mapcar #'car vars)))
193 `(funcall ,res ,@args)))))))
194 (main
195 (pcase--u
196 (mapcar (lambda (case)
197 `((match ,exp . ,(car case))
198 ,(apply-partially
199 (if (pcase--small-branch-p (cdr case))
200 ;; Don't bother sharing multiple
201 ;; occurrences of this leaf since it's small.
202 #'pcase-codegen codegen)
203 (cdr case))))
204 cases))))
205 (if (null defs) main
206 `(let ,defs ,main))))
207
208 (defun pcase-codegen (code vars)
209 `(let ,(mapcar (lambda (b) (list (car b) (cdr b))) vars)
210 ,@code))
211
212 (defun pcase--small-branch-p (code)
213 (and (= 1 (length code))
214 (or (not (consp (car code)))
215 (let ((small t))
216 (dolist (e (car code))
217 (if (consp e) (setq small nil)))
218 small))))
219
220 ;; Try to use `cond' rather than a sequence of `if's, so as to reduce
221 ;; the depth of the generated tree.
222 (defun pcase--if (test then else)
223 (cond
224 ((eq else :pcase--dontcare) then)
225 ((eq (car-safe else) 'if)
226 (if (equal test (nth 1 else))
227 ;; Doing a test a second time: get rid of the redundancy.
228 ;; FIXME: ideally, this should never happen because the pcase--split-*
229 ;; funs should have eliminated such things, but pcase--split-member
230 ;; is imprecise, so in practice it can happen occasionally.
231 `(if ,test ,then ,@(nthcdr 3 else))
232 `(cond (,test ,then)
233 (,(nth 1 else) ,(nth 2 else))
234 (t ,@(nthcdr 3 else)))))
235 ((eq (car-safe else) 'cond)
236 `(cond (,test ,then)
237 ;; Doing a test a second time: get rid of the redundancy, as above.
238 ,@(remove (assoc test else) (cdr else))))
239 (t `(if ,test ,then ,else))))
240
241 (defun pcase--upat (qpattern)
242 (cond
243 ((eq (car-safe qpattern) '\,) (cadr qpattern))
244 (t (list '\` qpattern))))
245
246 ;; Note about MATCH:
247 ;; When we have patterns like `(PAT1 . PAT2), after performing the `consp'
248 ;; check, we want to turn all the similar patterns into ones of the form
249 ;; (and (match car PAT1) (match cdr PAT2)), so you naturally need conjunction.
250 ;; Earlier code hence used branches of the form (MATCHES . CODE) where
251 ;; MATCHES was a list (implicitly a conjunction) of (SYM . PAT).
252 ;; But if we have a pattern of the form (or `(PAT1 . PAT2) PAT3), there is
253 ;; no easy way to eliminate the `consp' check in such a representation.
254 ;; So we replaced the MATCHES by the MATCH below which can be made up
255 ;; of conjunctions and disjunctions, so if we know `foo' is a cons, we can
256 ;; turn (match foo . (or `(PAT1 . PAT2) PAT3)) into
257 ;; (or (and (match car . `PAT1) (match cdr . `PAT2)) (match foo . PAT3)).
258 ;; The downside is that we now have `or' and `and' both in MATCH and
259 ;; in PAT, so there are different equivalent representations and we
260 ;; need to handle them all. We do not try to systematically
261 ;; canonicalize them to one form over another, but we do occasionally
262 ;; turn one into the other.
263
264 (defun pcase--u (branches)
265 "Expand matcher for rules BRANCHES.
266 Each BRANCH has the form (MATCH CODE . VARS) where
267 CODE is the code generator for that branch.
268 VARS is the set of vars already bound by earlier matches.
269 MATCH is the pattern that needs to be matched, of the form:
270 (match VAR . UPAT)
271 (and MATCH ...)
272 (or MATCH ...)"
273 (when (setq branches (delq nil branches))
274 (let* ((carbranch (car branches))
275 (match (car carbranch)) (cdarbranch (cdr carbranch))
276 (code (car cdarbranch))
277 (vars (cdr cdarbranch)))
278 (pcase--u1 (list match) code vars (cdr branches)))))
279
280 (defun pcase--and (match matches)
281 (if matches `(and ,match ,@matches) match))
282
283 (defun pcase--split-match (sym splitter match)
284 (cond
285 ((eq (car match) 'match)
286 (if (not (eq sym (cadr match)))
287 (cons match match)
288 (let ((pat (cddr match)))
289 (cond
290 ;; Hoist `or' and `and' patterns to `or' and `and' matches.
291 ((memq (car-safe pat) '(or and))
292 (pcase--split-match sym splitter
293 (cons (car pat)
294 (mapcar (lambda (alt)
295 `(match ,sym . ,alt))
296 (cdr pat)))))
297 (t (let ((res (funcall splitter (cddr match))))
298 (cons (or (car res) match) (or (cdr res) match))))))))
299 ((memq (car match) '(or and))
300 (let ((then-alts '())
301 (else-alts '())
302 (neutral-elem (if (eq 'or (car match))
303 :pcase--fail :pcase--succeed))
304 (zero-elem (if (eq 'or (car match)) :pcase--succeed :pcase--fail)))
305 (dolist (alt (cdr match))
306 (let ((split (pcase--split-match sym splitter alt)))
307 (unless (eq (car split) neutral-elem)
308 (push (car split) then-alts))
309 (unless (eq (cdr split) neutral-elem)
310 (push (cdr split) else-alts))))
311 (cons (cond ((memq zero-elem then-alts) zero-elem)
312 ((null then-alts) neutral-elem)
313 ((null (cdr then-alts)) (car then-alts))
314 (t (cons (car match) (nreverse then-alts))))
315 (cond ((memq zero-elem else-alts) zero-elem)
316 ((null else-alts) neutral-elem)
317 ((null (cdr else-alts)) (car else-alts))
318 (t (cons (car match) (nreverse else-alts)))))))
319 (t (error "Uknown MATCH %s" match))))
320
321 (defun pcase--split-rest (sym splitter rest)
322 (let ((then-rest '())
323 (else-rest '()))
324 (dolist (branch rest)
325 (let* ((match (car branch))
326 (code&vars (cdr branch))
327 (splitted
328 (pcase--split-match sym splitter match)))
329 (unless (eq (car splitted) :pcase--fail)
330 (push (cons (car splitted) code&vars) then-rest))
331 (unless (eq (cdr splitted) :pcase--fail)
332 (push (cons (cdr splitted) code&vars) else-rest))))
333 (cons (nreverse then-rest) (nreverse else-rest))))
334
335 (defun pcase--split-consp (syma symd pat)
336 (cond
337 ;; A QPattern for a cons, can only go the `then' side.
338 ((and (eq (car-safe pat) '\`) (consp (cadr pat)))
339 (let ((qpat (cadr pat)))
340 (cons `(and (match ,syma . ,(pcase--upat (car qpat)))
341 (match ,symd . ,(pcase--upat (cdr qpat))))
342 :pcase--fail)))
343 ;; A QPattern but not for a cons, can only go the `else' side.
344 ((eq (car-safe pat) '\`) (cons :pcase--fail nil))))
345
346 (defun pcase--split-equal (elem pat)
347 (cond
348 ;; The same match will give the same result.
349 ((and (eq (car-safe pat) '\`) (equal (cadr pat) elem))
350 (cons :pcase--succeed :pcase--fail))
351 ;; A different match will fail if this one succeeds.
352 ((and (eq (car-safe pat) '\`)
353 ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
354 ;; (consp (cadr pat)))
355 )
356 (cons :pcase--fail nil))))
357
358 (defun pcase--split-member (elems pat)
359 ;; Based on pcase--split-equal.
360 (cond
361 ;; The same match (or a match of membership in a superset) will
362 ;; give the same result, but we don't know how to check it.
363 ;; (???
364 ;; (cons :pcase--succeed nil))
365 ;; A match for one of the elements may succeed or fail.
366 ((and (eq (car-safe pat) '\`) (member (cadr pat) elems))
367 nil)
368 ;; A different match will fail if this one succeeds.
369 ((and (eq (car-safe pat) '\`)
370 ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
371 ;; (consp (cadr pat)))
372 )
373 (cons :pcase--fail nil))))
374
375 (defun pcase--split-pred (upat pat)
376 ;; FIXME: For predicates like (pred (> a)), two such predicates may
377 ;; actually refer to different variables `a'.
378 (if (equal upat pat)
379 (cons :pcase--succeed :pcase--fail)))
380
381 (defun pcase--fgrep (vars sexp)
382 "Check which of the symbols VARS appear in SEXP."
383 (let ((res '()))
384 (while (consp sexp)
385 (dolist (var (pcase--fgrep vars (pop sexp)))
386 (unless (memq var res) (push var res))))
387 (and (memq sexp vars) (not (memq sexp res)) (push sexp res))
388 res))
389
390 ;; It's very tempting to use `pcase' below, tho obviously, it'd create
391 ;; bootstrapping problems.
392 (defun pcase--u1 (matches code vars rest)
393 "Return code that runs CODE (with VARS) if MATCHES match.
394 and otherwise defers to REST which is a list of branches of the form
395 \(ELSE-MATCH ELSE-CODE . ELSE-VARS)."
396 ;; Depending on the order in which we choose to check each of the MATCHES,
397 ;; the resulting tree may be smaller or bigger. So in general, we'd want
398 ;; to be careful to chose the "optimal" order. But predicate
399 ;; patterns make this harder because they create dependencies
400 ;; between matches. So we don't bother trying to reorder anything.
401 (cond
402 ((null matches) (funcall code vars))
403 ((eq :pcase--fail (car matches)) (pcase--u rest))
404 ((eq :pcase--succeed (car matches))
405 (pcase--u1 (cdr matches) code vars rest))
406 ((eq 'and (caar matches))
407 (pcase--u1 (append (cdar matches) (cdr matches)) code vars rest))
408 ((eq 'or (caar matches))
409 (let* ((alts (cdar matches))
410 (var (if (eq (caar alts) 'match) (cadr (car alts))))
411 (simples '()) (others '()))
412 (when var
413 (dolist (alt alts)
414 (if (and (eq (car alt) 'match) (eq var (cadr alt))
415 (let ((upat (cddr alt)))
416 (and (eq (car-safe upat) '\`)
417 (or (integerp (cadr upat)) (symbolp (cadr upat))
418 (stringp (cadr upat))))))
419 (push (cddr alt) simples)
420 (push alt others))))
421 (cond
422 ((null alts) (error "Please avoid it") (pcase--u rest))
423 ((> (length simples) 1)
424 ;; De-hoist the `or' MATCH into an `or' pattern that will be
425 ;; turned into a `memq' below.
426 (pcase--u1 (cons `(match ,var or . ,(nreverse simples)) (cdr matches))
427 code vars
428 (if (null others) rest
429 (cons (cons
430 (pcase--and (if (cdr others)
431 (cons 'or (nreverse others))
432 (car others))
433 (cdr matches))
434 (cons code vars))
435 rest))))
436 (t
437 (pcase--u1 (cons (pop alts) (cdr matches)) code vars
438 (if (null alts) (progn (error "Please avoid it") rest)
439 (cons (cons
440 (pcase--and (if (cdr alts)
441 (cons 'or alts) (car alts))
442 (cdr matches))
443 (cons code vars))
444 rest)))))))
445 ((eq 'match (caar matches))
446 (let* ((popmatches (pop matches))
447 (_op (car popmatches)) (cdrpopmatches (cdr popmatches))
448 (sym (car cdrpopmatches))
449 (upat (cdr cdrpopmatches)))
450 (cond
451 ((memq upat '(t _)) (pcase--u1 matches code vars rest))
452 ((eq upat 'dontcare) :pcase--dontcare)
453 ((functionp upat) (error "Feature removed, use (pred %s)" upat))
454 ((memq (car-safe upat) '(guard pred))
455 (let* ((splitrest
456 (pcase--split-rest
457 sym (apply-partially #'pcase--split-pred upat) rest))
458 (then-rest (car splitrest))
459 (else-rest (cdr splitrest)))
460 (pcase--if (if (and (eq (car upat) 'pred) (symbolp (cadr upat)))
461 `(,(cadr upat) ,sym)
462 (let* ((exp (cadr upat))
463 ;; `vs' is an upper bound on the vars we need.
464 (vs (pcase--fgrep (mapcar #'car vars) exp))
465 (call (cond
466 ((eq 'guard (car upat)) exp)
467 ((functionp exp) `(,exp ,sym))
468 (t `(,@exp ,sym)))))
469 (if (null vs)
470 call
471 ;; Let's not replace `vars' in `exp' since it's
472 ;; too difficult to do it right, instead just
473 ;; let-bind `vars' around `exp'.
474 `(let ,(mapcar (lambda (var)
475 (list var (cdr (assq var vars))))
476 vs)
477 ;; FIXME: `vars' can capture `sym'. E.g.
478 ;; (pcase x ((and `(,x . ,y) (pred (fun x)))))
479 ,call))))
480 (pcase--u1 matches code vars then-rest)
481 (pcase--u else-rest))))
482 ((symbolp upat)
483 (if (not (assq upat vars))
484 (pcase--u1 matches code (cons (cons upat sym) vars) rest)
485 ;; Non-linear pattern. Turn it into an `eq' test.
486 (pcase--u1 (cons `(match ,sym . (pred (eq ,(cdr (assq upat vars)))))
487 matches)
488 code vars rest)))
489 ((eq (car-safe upat) '\`)
490 (pcase--q1 sym (cadr upat) matches code vars rest))
491 ((eq (car-safe upat) 'or)
492 (let ((all (> (length (cdr upat)) 1))
493 (memq-fine t))
494 (when all
495 (dolist (alt (cdr upat))
496 (unless (and (eq (car-safe alt) '\`)
497 (or (symbolp (cadr alt)) (integerp (cadr alt))
498 (setq memq-fine nil)
499 (stringp (cadr alt))))
500 (setq all nil))))
501 (if all
502 ;; Use memq for (or `a `b `c `d) rather than a big tree.
503 (let* ((elems (mapcar 'cadr (cdr upat)))
504 (splitrest
505 (pcase--split-rest
506 sym (apply-partially #'pcase--split-member elems) rest))
507 (then-rest (car splitrest))
508 (else-rest (cdr splitrest)))
509 (pcase--if `(,(if memq-fine #'memq #'member) ,sym ',elems)
510 (pcase--u1 matches code vars then-rest)
511 (pcase--u else-rest)))
512 (pcase--u1 (cons `(match ,sym ,@(cadr upat)) matches) code vars
513 (append (mapcar (lambda (upat)
514 `((and (match ,sym . ,upat) ,@matches)
515 ,code ,@vars))
516 (cddr upat))
517 rest)))))
518 ((eq (car-safe upat) 'and)
519 (pcase--u1 (append (mapcar (lambda (upat) `(match ,sym ,@upat))
520 (cdr upat))
521 matches)
522 code vars rest))
523 ((eq (car-safe upat) 'not)
524 ;; FIXME: The implementation below is naive and results in
525 ;; inefficient code.
526 ;; To make it work right, we would need to turn pcase--u1's
527 ;; `code' and `vars' into a single argument of the same form as
528 ;; `rest'. We would also need to split this new `then-rest' argument
529 ;; for every test (currently we don't bother to do it since
530 ;; it's only useful for odd patterns like (and `(PAT1 . PAT2)
531 ;; `(PAT3 . PAT4)) which the programmer can easily rewrite
532 ;; to the more efficient `(,(and PAT1 PAT3) . ,(and PAT2 PAT4))).
533 (pcase--u1 `((match ,sym . ,(cadr upat)))
534 ;; FIXME: This codegen is not careful to share its
535 ;; code if used several times: code blow up is likely.
536 (lambda (_vars)
537 ;; `vars' will likely contain bindings which are
538 ;; not always available in other paths to
539 ;; `rest', so there' no point trying to pass
540 ;; them down.
541 (pcase--u rest))
542 vars
543 (list `((and . ,matches) ,code . ,vars))))
544 (t (error "Unknown upattern `%s'" upat)))))
545 (t (error "Incorrect MATCH %s" (car matches)))))
546
547 (defun pcase--q1 (sym qpat matches code vars rest)
548 "Return code that runs CODE if SYM matches QPAT and if MATCHES match.
549 and if not, defers to REST which is a list of branches of the form
550 \(OTHER_MATCH OTHER-CODE . OTHER-VARS)."
551 (cond
552 ((eq (car-safe qpat) '\,) (error "Can't use `,UPATTERN"))
553 ((floatp qpat) (error "Floating point patterns not supported"))
554 ((vectorp qpat)
555 ;; FIXME.
556 (error "Vector QPatterns not implemented yet"))
557 ((consp qpat)
558 (let ((syma (make-symbol "xcar"))
559 (symd (make-symbol "xcdr")))
560 (let* ((splitrest (pcase--split-rest
561 sym
562 (apply-partially #'pcase--split-consp syma symd)
563 rest))
564 (then-rest (car splitrest))
565 (else-rest (cdr splitrest)))
566 (pcase--if `(consp ,sym)
567 `(let ((,syma (car ,sym))
568 (,symd (cdr ,sym)))
569 ,(pcase--u1 `((match ,syma . ,(pcase--upat (car qpat)))
570 (match ,symd . ,(pcase--upat (cdr qpat)))
571 ,@matches)
572 code vars then-rest))
573 (pcase--u else-rest)))))
574 ((or (integerp qpat) (symbolp qpat) (stringp qpat))
575 (let* ((splitrest (pcase--split-rest
576 sym (apply-partially 'pcase--split-equal qpat) rest))
577 (then-rest (car splitrest))
578 (else-rest (cdr splitrest)))
579 (pcase--if `(,(if (stringp qpat) #'equal #'eq) ,sym ',qpat)
580 (pcase--u1 matches code vars then-rest)
581 (pcase--u else-rest))))
582 (t (error "Unkown QPattern %s" qpat))))
583
584
585 (provide 'pcase)
586 ;;; pcase.el ends here