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

#%app problems with nested macro



I'm having trouble *nesting* a macro within another.

This macro works fine at top level:

(define-syntax my-let
  (lambda (x)
    (syntax-case x ()
      [(_ () body) #'((lambda () body))]
      [(_ ((var* val*) ...) body)
       #'((lambda (var* ...) body) val* ...)])))

So does this one:

(define-syntax with-values
  (lambda (x)
    (syntax-case x ()
      [(_ P C) #'(call-with-values (lambda () P) C)])))


But if I nest the second macro with the first --- like this:

(define-syntax my-let
  (lambda (x)

    (define-syntax with-values
      (lambda (x)
        (syntax-case x ()
          [(_ P C) #'(call-with-values (lambda () P) C)])))

    (syntax-case x ()
      [(_ () body) #'((lambda () body))]
      [(_ ((var* val*) ...) body)
       #'((lambda (var* ...) body) val* ...)])))

I will get the following error:

compile: bad syntax; function application is not allowed, because no #%app
syntax transformer is bound in: (lambda (x) (syntax-case x () ((_ p c)
(syntax (call-with-values (lambda () p) c)))))


+--------------------------------------------------
So...

I tried writing this macro at top level:

(define-syntax #%app
      (lambda (x)
        (syntax-case x ()
          [(_ rator rands ...) #'(apply rator rands ...)])))

When I click execute, it goes forever (actually, I didn't wait that long).

If I change the name to foo:

(define-syntax foo
      (lambda (x)
        (syntax-case x ()
          [(_ rator rands ...) #'(apply rator rands ...)])))

It works as expected.

If I put the #%app macro at the same scope as with values:

(define-syntax my-let
  (lambda (x)

    (define-syntax #%app
      (lambda (x)
        (syntax-case x ()
          [(_ rator rands ...) #'(apply rator rands ...)])))

    (define-syntax with-values
      (lambda (x)
        (syntax-case x ()
          [(_ P C) #'(call-with-values (lambda () P) C)])))

    (syntax-case x ()
      [(_ () body) #'((lambda () body))]
      [(_ ((var* val*) ...) body)
       #'((lambda (var* ...) body) val* ...)])))

I get:

compile: identifier used out of context in: #%app

+-----------------------------------------------------
Finally I tried this:

(define-syntax my-let
  (lambda (x)

    (define-syntax with-values
      (lambda (x)
        (define-syntax #%app
          (lambda (x)
            (syntax-case x ()
              [(_ rator rands ...) #'(apply rator rands ...)])))
        (syntax-case x ()
          [(_ P C) #'(call-with-values (lambda () P) C)])))

    (syntax-case x ()
      [(_ () body) #'((lambda () body))]
      [(_ ((var* val*) ...) body)
       #'((lambda (var* ...) body) val* ...)])))

And I get the seemingly contradictory error message:

compile: bad syntax; function application is not allowed, because no #%app
syntax transformer is bound in: (lambda (x) (define-syntax #%app (lambda (x)
(syntax-case x () ((_ rator rands ...) (syntax (apply rator rands ...))))))
(syntax-case x () ((_ p c) (syntax (call-with-values (lambda () p) c)))))