help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [External] : macros and macroexpand


From: Heime
Subject: RE: [External] : macros and macroexpand
Date: Mon, 07 Aug 2023 18:22:00 +0000





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 via email to

[Prev in Thread] Current Thread [Next in Thread]