[Openmcl-devel] How can I intercept the compilation of symbols?

Ron Garret ron at flownet.com
Sun Feb 21 15:02:12 PST 2010


I'm trying to do something analogous to nx1-combination-hook to change the way that undefined free variables are compiled.  Instead of automatically compiling them into the equivalent of a call to symbol-value I want to instead compile them to a user-definable form.  In other words, if X is not defined as a variable, I want references to X to compile into (FREE-VARIABLE-HOOK 'X) or something like that.

So I tried this:

(defmacro free-variable-hook (var) `(symbol-value ',var))

(advise ccl::nx1-symbol
  (destructuring-bind (var &optional env) ccl::arglist
    (if (and (symbolp var) ; Just to be sure
             (not (keywordp var))
             (null (ccl::variable-information var env)))
      (eval `(define-symbol-macro ,var (free-variable-hook ,var)))))
  :when :before
  :name :free-variable-hook)

This kinda sorta works, but it has a problem: by the time nx1-symbol is called, symbol macros have already been expanded (by nx-transform as far as I can tell, but my powers of reverse-engineering begin to fail me at this point).  So the symbol macro definition is not in effect for the current compilation of the symbol.  If you compile everything twice it works, but that's obviously not the right thing.

So what is the right thing?  I tried this too:

(advise ccl::nx-transform
  (destructuring-bind (form &optional env &rest args) ccl::arglist
    (declare (ignore args))
    (when (and (symbolp form)
               (not (keywordp form))
               (null (ccl::variable-information var env)))
       (eval `(define-symbol-macro ,var (free-variable-hook ,var)))))
  :when :before
  :name :free-variable-hook)

but that seems fraught with peril, particularly since I don't actually understand everything what nx-transform is doing.

Thanks,
rg




More information about the Openmcl-devel mailing list