guile-user
[Top][All Lists]
Advanced

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

Re: Way to control the order of macro expansion


From: Panicz Maciej Godek
Subject: Re: Way to control the order of macro expansion
Date: Fri, 3 Jan 2014 01:21:00 +0100

It turned out, that the above solution doesn't work
exactly as expected -- since the scopes of "private"
and "interface" get separated (by the with-syntax
form that I have been so proud of), it is impossible
for the public forms to refer to private bindings.

To solve that issue, I had to pass the private
bindings to the function passed with-syntax
and then retrieve them. For some reason (which
is unclear to me) I had to convert them to datum
and then back to syntax, because if they were
given verbatim, they failed to get renamed
properly. I'd appreciate if anyone could explain
it to me, or reviewed the code below to tell
whether the names and comments aren't
misleading (e.g. I used the name "lexical-context"
for a variable, but I'm not sure whether the entity
that it refers to can properly be called "lexical
context")

(define-syntax private+public
  (lambda (stx)
    (define (interface+name+body specs lexical-context)
      ;; the second argument is the lexical context that we
      ;; want to preserve while extracting the specs
      (define (interface-name interface)
        (match interface
          ((head . tail)
           (interface-name head))
          ((? symbol? name)
           name)))
      `(,(datum->syntax stx (syntax->datum lexical-context))
        ;; for some reason we need to deconstruct and reconstruct
        ;; lexical-context here
        ,(map (lambda(spec)
                (syntax-case spec ()
                  ((interface . body)
                   (datum->syntax stx `(,(syntax->datum #'interface)
                                        ,(interface-name
                                          (syntax->datum #'interface))
                                        ,(syntax->datum #'body))))))
              specs)))
    (syntax-case stx ()
      ((_ (private ...) ((pub-define . pub-spec) ...))
       ;; we pass the private definitions to the interface+name+body,
       ;; because we want to be able to refer to private definitions
       ;; from within the public ones (otherwise the macro processor
       ;; would treat them as if they were within separate contexts)
       (with-syntax ((((private ...) ((interface name body) ...))
                      (interface+name+body #'(pub-spec ...) #'(private ...))))
         #'(begin
             (define name (and (defined? 'name) name))
             ...
             (let ()
               private ...
               (set! name
                     (let ()
                       (pub-define interface . body)
                       name))
               ...)))))))



reply via email to

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