0
votes

In Guile scheme, I have a macro which calls another macro, and I want the call to the inner macro to reference a symbol used in the inner macro. Here is a simple example to illustrate the kind of thing I'm trying to acheive:

(define-syntax macro1 
  (lambda (x)
    (syntax-case x ()
      ((macro1 expr1)
       (with-syntax ((f (datum->syntax x 'f)))
         #'(lambda (f) expr1))))))

(define-syntax macro2 (lambda (x)
  (syntax-case x ()
    ((macro2 expr2)
     #'(macro1 (expr2 f))))))

((macro2 1+) 2)

I expected the final line to return 3, but instead I got the following error:

;;; <stdin>:142:0: warning: possibly unbound variable `f'
ice-9/boot-9.scm:1669:16: In procedure raise-exception:
Unbound variable: f

Is it possible to do what I want?

1

1 Answers

2
votes

OK, so I discoverd how to do this. I have to use #'macro1 instead of x in the datum->syntax call:

(define-syntax macro1 
  (lambda (x)
    (syntax-case x ()
      ((macro1 expr1)
       (with-syntax ((f (datum->syntax #'macro1 'f)))
         #'(lambda (f) expr1))))))

(define-syntax macro2 (lambda (x)
  (syntax-case x ()
    ((macro2 expr2)
     #'(macro1 (expr2 f))))))

Not sure why it doesn't work with x, and I couldn't find any explanation in the manual.