emacs-devel
[Top][All Lists]
Advanced

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

Re: lexical mumblings


From: Miles Bader
Subject: Re: lexical mumblings
Date: 01 Nov 2001 09:48:14 +0900

Here's a quick implementation of lexical binding for the lisp interpreter
that I did last night.  Together with byte-code changes I sent earlier,
that should be all the change that's required for the C code (the
remainder, of course, being compiler support for the new bytecodes).

I didn't implement the magic-cookie file-variable discussed earlier (though
that should be simple to do), rather I tested it using this macro:

  (defmacro with-lexical-binding (&rest body)
    "Execute the statements in BODY using lexical binding."
    `(let ((interpreter-lexenv interpreter-lexenv))
       (setq interpreter-lexenv '(t))
       ,@body))

Using that, I can do e.g.:

   (with-lexical-binding
     (let ((z 0))
       (defun count ()
         (prog1 z (setq z (1+ z))))))
   => count
   (count)
   => 0
   (count)
   => 1
   (count)
   => 2
   ...

I think this implementation is pretty simple -- basically places in the
interpreter that deal with variables (varref, `setq', `let', 'lambda')
use something like:

   if (! NILP (Vinterpreter_lexenv))
     ...do lexical stuff...
   else
     ...do normal dynamic stuff...

and places that can deal with lambdas (`defun', `defmacro', `function')
use something like:

   if (! NILP (Vinterpreter_lexenv))
     ...return a closure instead of a normal lambda...
   else
     ...return lambda like normal...

Since these tests against Vinterpreter_lexenv are very cheap, this should
have no noticeable impact on normal dynamic-binding performance.

Lexical bindings are stored in a simple alist, just like a traditional lisp
deep-binding implementation (with the extra twist that a funcall `resets'
the binding stack).  According to the simple measurements I did, there's no
measurable difference in speed between lexical and dynamic variables (in
the interpreter, mind you), however since lexical binding uses an alist,
each lexical binding will use 2 cons cells.  I think this is quite
acceptable for the intepreter.

Anyway, here's the patch (in diff -c format for Richard's benefit):

Attachment: lexbind-interp-1c.patch
Description: Patch to add lexical binding to the elisp interpreter


-Miles
-- 
P.S.  All information contained in the above letter is false,
      for reasons of military security.

reply via email to

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