all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Alan Mackenzie <acm@muc.de>
Cc: emacs-devel@gnu.org
Subject: Re: pcase generates an unprintable expansion for a form in test erc--restore-initialize-priors
Date: Mon, 27 May 2024 14:23:55 -0400	[thread overview]
Message-ID: <jwva5kbce1k.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <jwvy17vch3x.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Mon, 27 May 2024 13:39:47 -0400")

[-- Attachment #1: Type: text/plain, Size: 476 bytes --]

> I suspect it can be done with no change to the core pcase code by
> rewriting
>
>     (pcase-defmacro \` (qpat)
>
> so it does the recursion on its own (e.g. instead of expanding `(A . B)
> to a pattern which contains `A and `B).  Then at any level if it notices
> that no `,` was found during the recursion it can just use `equal`.

The patch below does that.
For that ERC test, the resulting code is still very heavily nested, of
course, but ... less so.


        Stefan

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pcase.patch --]
[-- Type: text/x-diff, Size: 2056 bytes --]

diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index 1a58c60734a..bbc08493ec9 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -1124,21 +1124,35 @@ \`
  - True!  (The second element can be anything, and for the sake
    of the body forms, its value is bound to the symbol `forum'.)"
   (declare (debug (pcase-QPAT)))
+  (pcase--expand-\` qpat))
+
+(defun pcase--expand-\` (qpat)
   (cond
    ((eq (car-safe qpat) '\,) (cadr qpat))
    ((eq (car-safe qpat) '\,@) (error "Unsupported QPAT: %S" qpat))
    ((vectorp qpat)
-    `(and (pred vectorp)
-          (app length ,(length qpat))
-          ,@(let ((upats nil))
-              (dotimes (i (length qpat))
-                (push `(app (aref _ ,i) ,(list '\` (aref qpat i)))
-                      upats))
-              (nreverse upats))))
+    (let* ((trivial t)
+           (upats (mapcar (lambda (qpat)
+                            (let ((upat (pcase--expand-\` qpat)))
+                              (unless (eq (car-safe upat) 'quote)
+                                (setq trivial nil))
+                              upat))
+                          qpat)))
+      (if trivial
+          `',qpat
+        `(and (pred vectorp)
+              (app length ,(length qpat))
+              ,@(let ((i -1))
+                  (mapcar (lambda (upat) `(app (aref _ ,(setq i (+ i 1)))  ,upat))
+                          upats))))))
    ((consp qpat)
-    `(and (pred consp)
-          (app car-safe ,(list '\` (car qpat)))
-          (app cdr-safe ,(list '\` (cdr qpat)))))
+    (let ((upata (pcase--expand-\` (car qpat)))
+          (upatd (pcase--expand-\` (cdr qpat))))
+      (if (and (eq (car-safe upata) 'quote) (eq (car-safe upatd) 'quote))
+          `',qpat
+        `(and (pred consp)
+              (app car-safe ,upata)
+              (app cdr-safe ,upatd)))))
    ((or (stringp qpat) (numberp qpat) (symbolp qpat)) `',qpat)
    ;; In all other cases just raise an error so we can't break
    ;; backward compatibility when adding \` support for other

  reply	other threads:[~2024-05-27 18:23 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-26 15:30 pcase generates an unprintable expansion for a form in test erc--restore-initialize-priors Alan Mackenzie
2024-05-26 21:19 ` Michael Heerdegen via Emacs development discussions.
2024-05-27 17:39 ` Stefan Monnier
2024-05-27 18:23   ` Stefan Monnier [this message]
2024-05-27 19:29     ` Michael Heerdegen via Emacs development discussions.
2024-05-27 19:42       ` Stefan Monnier
2024-05-29 15:39         ` Michael Heerdegen
2024-05-30 23:43           ` Stefan Monnier
2024-05-31  7:32             ` Andrea Corallo
2024-05-31 12:45               ` Stefan Monnier
2024-06-03 17:42           ` Stefan Monnier
2024-06-04  0:57             ` Michael Heerdegen
2024-05-27 20:14   ` F. Jason Park
2024-05-27 22:31     ` Stefan Monnier
2024-05-30 23:45 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwva5kbce1k.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=acm@muc.de \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.