all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Heime <heimeborgia@protonmail.com>
To: Drew Adams <drew.adams@oracle.com>
Cc: Heime via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>
Subject: RE: [External] : macros and macroexpand
Date: Mon, 07 Aug 2023 18:22:00 +0000	[thread overview]
Message-ID: <3Ca8oJEYqu9uXkaDShjtUEyySDEEc3IYXg1xELO1RYGAUThEXqzstNasiQ8UtWdjUJptEXjsZB38XVdlGX3QwFO6nA7tFRFAoB4kKp4E_J4=@protonmail.com> (raw)
In-Reply-To: <SJ0PR10MB5488AB1781ECE352186A61C0F30CA@SJ0PR10MB5488.namprd10.prod.outlook.com>






Sent with Proton Mail secure email.

------- Original Message -------
On Tuesday, August 8th, 2023 at 2:28 AM, Drew Adams <drew.adams@oracle.com> wrote:


> > I have made a macro and know that they are supposed to return
> > expanded code for use. Still I cannot understand the need to
> > call "macroexpand". Should't the macro already perform the
> > expansion ?
> 
> 
> This was amply explained in the answers
> to your question when you posted in to
> emacs.SE:
> 
> https://emacs.stackexchange.com/q/78347
> ____
> 
> In sum (repeating), the Lisp interpreter
> evaluates sexps, including macro calls.
> 
> When it evaluates a macro call, it first
> expands it according to the macro body
> (which is effectively a sexp-to-sexp
> pure function, regardless of how it's
> implemented). This is rewriting code.
> 
> After expanding the macro call, i.e.,
> replacing it with a different sexp, the
> interpreter evaluates that sexp (which
> returns the result of that evaluation).
> ____
> 
> The byte-compiler just expands macro
> calls, then byte-compiles the expansions.
> That is, byte-compilation doesn't also
> evaluate the result of macro expansion.
> Evaluation is done when the byte-compiled
> code is evaluated/interpreted.
> ____
> 
> A macro call is a particular kind of
> sexp, of course: it's a list with a
> symbol as car, that is, it looks to Lisp
> like a function call. (There are also
> symbol macros, which act similarly, but
> on symbols not lists.)
> ____
> 
> You've been told all of this a few times
> now. If there's some particular part of
> it that you don't understand then you
> should ask only about that part. Instead,
> your MO is to broadcast the same question
> multiple times to multiple places.

I understand your discussion.  What I am trying to do is print the
last sexp (sexp at last level) produced by a macro.  But without
evaluating the sexp.  I just want the see the command generated by 
the macro in an emacs-lisp-mode buffer.

For this task, our discussion suggests that it becomes necessary 
to use macroexpand-all. 

Have a look at the following.

(defconst buffer-name "BF")

(defun emboss-estring (string &optional bfname)
  "Show STRING in a temporary buffer."
  (or bfname (setq bfname buffer-name))
  (with-output-to-temp-buffer bfname
    (princ string)
    (emacs-lisp-mode)))

(defun emboss-object (object &optional bfname)
  "Show a pretty-printed version of OBJECT in a temporary buffer."
  (or bfname (setq bfname buffer-name))
  (emboss-estring (pp-to-string object) bfname))

(defun emboss-mcode (code &optional bfname)
  "Same as (emboss-object (macroexpand-all CODE))."
  (or bfname (setq bfname buffer-name))
  (apply 'emboss-object (list (macroexpand-all code) bfname)) )

This means that to get the final sexp from a macro named "adder"
I do

(emboss-mcode '(adder (* 3 5) (* 5 7)))

Philip suggests that I could use

(emboss-mcode (adder (* 3 5) (* 5 7)))

I would be keen to see what improvements I can do to this 
aforementioned sexp printing implementation.  And also about
the capability of simplifying the implementation.
 
> I can understand your wanting to get
> different opinions, but your understanding
> (which does progress) isn't reflected in
> narrower questions. Why is that?

I am striving to reflect my progress with the last code 
implementation provided.



  reply	other threads:[~2023-08-07 18:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-07 11:03 macros and macroexpand Heime
2023-08-07 11:46 ` Yuri Khan
2023-08-07 12:43   ` Heime
2023-08-07 14:22     ` Philip Kaludercic
2023-08-07 18:06       ` Heime
2023-08-07 20:08         ` Philip Kaludercic
2023-08-07 20:56           ` Heime
2023-08-07 22:10             ` Emanuel Berg
2023-08-08 20:35               ` Heime
2023-08-08  5:57             ` Philip Kaludercic
2023-08-07 14:28 ` [External] : " Drew Adams
2023-08-07 18:22   ` Heime [this message]
2023-08-07 19:04   ` Emanuel Berg
2023-08-07 18:59 ` Emanuel Berg

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='3Ca8oJEYqu9uXkaDShjtUEyySDEEc3IYXg1xELO1RYGAUThEXqzstNasiQ8UtWdjUJptEXjsZB38XVdlGX3QwFO6nA7tFRFAoB4kKp4E_J4=@protonmail.com' \
    --to=heimeborgia@protonmail.com \
    --cc=drew.adams@oracle.com \
    --cc=help-gnu-emacs@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.