Function: pcase--expand

pcase--expand is a byte-compiled function defined in pcase.el.gz.

Signature

(pcase--expand EXP CASES)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/pcase.el.gz
(defun pcase--expand (exp cases)
  ;; (message "pid=%S (pcase--expand %S ...hash=%S)"
  ;;          (emacs-pid) exp (sxhash cases))
  (let* ((defs ())
         (codegen
          (lambda (code)
            (if (member code '(nil (nil) ('nil)))
                (lambda (&rest _) ''nil)
              (let ((bsym ()))
                (lambda (varvals count &rest _)
                  (let* ((ignored-vars
                          (delq nil (mapcar (lambda (vv) (if (nth 2 vv) (car vv)))
                                            varvals)))
                         (ignores (if ignored-vars
                                      `((ignore . ,ignored-vars)))))
                    ;; Since we use a tree-based pattern matching
                    ;; technique, the leaves (the places that contain the
                    ;; code to run once a pattern is matched) can get
                    ;; copied a very large number of times, so to avoid
                    ;; code explosion, we need to keep track of how many
                    ;; times we've used each leaf and move it
                    ;; to a separate function if that number is too high.
                    (if (or (< count 2) (pcase--small-branch-p code))
                        `(let ,(mapcar (lambda (vv) (list (car vv) (cadr vv)))
                                       varvals)
                           ;; Try and silence some of the most common
                           ;; spurious "unused var" warnings.
                           ,@ignores
                           ,@code)
                    ;; Several occurrence of this non-small branch in
                    ;; the output.
                    (unless bsym
                      (setq bsym (make-symbol
                                  (format "pcase-%d" (length defs))))
                      (push `(,bsym (lambda ,(mapcar #'car varvals)
                                      ,@ignores ,@code))
                            defs))
                    `(funcall ,bsym ,@(mapcar #'cadr varvals)))))))))
         (main
          (pcase-compile-patterns
           exp
           (mapcar (lambda (case)
                     (cons (car case) (funcall codegen (cdr case))))
                   cases))))
    (macroexp-let* defs main)))