guile-user
[Top][All Lists]
Advanced

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

Control the order of expansion of syntax-case macros


From: Vivien Kraus
Subject: Control the order of expansion of syntax-case macros
Date: Tue, 17 Dec 2024 17:33:04 +0100
User-agent: Evolution 3.48.4

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



reply via email to

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