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

Access to lexical environment?



    Date: Sun, 8 Nov 87 21:12:26 EST
    From: futrell%corwin.ccs.northeastern.edu@RELAY.CS.NET

    Creating new syntax using Lisp -- access to lexical environment?

    I am not sure how to properly develop simple extensions to Lisp
    syntax (using CL).  For example, suppose that I'd like to set up a
    way to create new things that has the following (familiar) syntax:

    (def-newthing fred (other-thing) ((n :initform (+ a b))
				      (ls :initform (rest ls1)))
				     ...etc.)

    Now a,b, and ls1 would typically be lexically scoped.  Given this, how
    do I define "def-newthing"?  If I use defun, it will attempt to
    evaluate (other-thing), and worse, ((n :initform ....)).  This won't
    work.  If I use defmacro, when I pull out an initform to evaluate, such
    as (+ a b), the defmacro won't allow me access to the lexical bindings
    of a and b.  eval has similar problems. I'm stuck.  Any ideas?  (There
    might be a lisp group to send this to, but we don't subscribe --
    anyway, I have confidence that I'll be enlightened on this by sluggers.)

       Bob Futrelle
       College of Computer Science  161CN
       Northeastern University
       360 Huntington Ave.
       Boston, MA 02115

       (617)-437-2076
       CSNet: futrelle@corwin.ccs.northeastern.edu


I may be dense this morning (but after all, it is Monday), but I don't
see what's wrong with defmacro.  So long as the :initform's aren't
evaluated by the macro expansion code, they will be in the correct
lexical environment.

For a simple example.

(defmacro def-thing (foo bar)
   `(defvar ,foo ,bar))

(let ((a 3))
   (def-thing foo (+ a 5)))

will expand to

(let ((a 3))
   (defvar foo (+ a 5)))

which is referencing `a' lexically.

The important thing is that the defmacro form can't eval its arguments
at expansion time and expect to be able to reference any lexical
variables.