definitions in macros?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

definitions in macros?

Han-Wen Nienhuys-3
Hi there,

in my quest to get lilypond working with GUILE 2+, I've hit another
stumbling block.

In order to make compilation with GUILE 2+ working, we have to move
away from runtime symbol definition (ie. module-define! calls).

In the code below, it looks like only one of the two definitions in
the body of my-macro-new takes effect. Is this expected, and if so,
why?

(defmacro-public my-macro-old (command-and-args . definition)
  (module-define! (current-module) 'x1 "I am X1\n")
  (module-define! (current-module) 'x2 "I am X2\n"))

(defmacro-public my-macro-new (command-and-args . definition)
    `(define p "i am P\n")
    `(define q "i am Q\n"))


(my-macro-old 1 2)
(my-macro-new 1 2)
(display x1)
(display x2)
(display q)
(display p)


thanks,

--
Han-Wen Nienhuys - [hidden email] - http://www.xs4all.nl/~hanwen

Reply | Threaded
Open this post in threaded view
|

Re: definitions in macros?

David Kastrup
Han-Wen Nienhuys <[hidden email]> writes:

> Hi there,
>
> in my quest to get lilypond working with GUILE 2+, I've hit another
> stumbling block.
>
> In order to make compilation with GUILE 2+ working, we have to move
> away from runtime symbol definition (ie. module-define! calls).
>
> In the code below, it looks like only one of the two definitions in
> the body of my-macro-new takes effect. Is this expected, and if so,
> why?
>
> (defmacro-public my-macro-old (command-and-args . definition)
>   (module-define! (current-module) 'x1 "I am X1\n")
>   (module-define! (current-module) 'x2 "I am X2\n"))
>
> (defmacro-public my-macro-new (command-and-args . definition)
>     `(define p "i am P\n")
>     `(define q "i am Q\n"))

This is very much expected.  The macro body contains two side-effect
free expressions (namely quoted lists) and returns the last one which is

(define q "i am Q\n")

This then gets evaluated at run time, defining q .

You probably wanted something like
  `(begin (define p ...) (define q ...))

as your body (and return expression) instead.

> (my-macro-old 1 2)
> (my-macro-new 1 2)
> (display x1)
> (display x2)
> (display q)
> (display p)
>
>
> thanks,

--
David Kastrup

Reply | Threaded
Open this post in threaded view
|

Re: definitions in macros?

Han-Wen Nienhuys-3
On Sun, Mar 22, 2020 at 10:09 PM David Kastrup <[hidden email]> wrote:

> > In the code below, it looks like only one of the two definitions in
> > the body of my-macro-new takes effect. Is this expected, and if so,
> > why?
> >
> > (defmacro-public my-macro-old (command-and-args . definition)
> >   (module-define! (current-module) 'x1 "I am X1\n")
> >   (module-define! (current-module) 'x2 "I am X2\n"))
> >
> > (defmacro-public my-macro-new (command-and-args . definition)
> >     `(define p "i am P\n")
> >     `(define q "i am Q\n"))
>
> This is very much expected.  The macro body contains two side-effect
> free expressions (namely quoted lists) and returns the last one which is
..
> You probably wanted something like
>   `(begin (define p ...) (define q ...))

d'oh! I am an idiot.

Thanks,

--
Han-Wen Nienhuys - [hidden email] - http://www.xs4all.nl/~hanwen