unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Control the order of expansion of syntax-case macros
@ 2024-12-17 16:33 Vivien Kraus
  2024-12-20  9:15 ` Maxime Devos via General Guile related discussions
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Vivien Kraus @ 2024-12-17 16:33 UTC (permalink / raw)
  To: Guile User

Dear Guile users,

syntax-case macros can have cool side effects at expansion time.
However, they are still draped in a veil of mystery to me.

Consider this code:

(define-syntax hello
  (lambda (stx)
    (syntax-case stx ()
      (_
       (begin
         (format (current-error-port) "Hello!\n")
         (datum->syntax #f "hello"))))))

(define-syntax world
  (lambda (stx)
    (syntax-case stx ()
      (_
       (begin
         (format (current-error-port) "World!\n")
         (datum->syntax #f "world"))))))

(define-syntax hello-world
  (lambda (stx)
    (syntax-case stx ()
      (_
       #`(string-append #,hello " " #,world)))))

,expand hello-world


Running it gives me:

Hello!
World!
$1 = (string-append "hello" " " "world")

Cool! Now, I suspect there are no clear rules to guess the order of
expansion (whether Hello! is printed before World! or after). I would
very much like to twist that order and reliably get World! and then
Hello!. How can I achieve that?

(define-syntax hello-world
  (lambda (stx)
    (syntax-case stx ()
      (_
       (let* ((w #'world)
              (h #'hello))
         #`(string-append #,h " " #,w))))))

With this modification, I get the same order:

Hello!
World!
$1 = (string-append "hello" " " "world")

So my guess is that syntax objects are expanded lazily. Is there
something I can do to get #'world expanded before #'hello?

Best regards,

Vivien



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-12-20 20:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-17 16:33 Control the order of expansion of syntax-case macros Vivien Kraus
2024-12-20  9:15 ` Maxime Devos via General Guile related discussions
2024-12-20 17:01   ` Vivien Kraus
2024-12-20 10:38 ` Nala Ginrut
2024-12-20 20:12 ` Zelphir Kaltstahl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).