Subject: Re: De-facto standard CL implementation of continuations?
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 02 Sep 2005 03:05:05 -0500
Newsgroups: comp.lang.lisp
Message-ID: <jKednaLffIgsmoXeRVn-jQ@speakeasy.net>
Thomas F. Burdick <tfb@conquest.OCF.Berkeley.EDU> wrote:
+---------------
| "ivant" <itoshkov@gmail.com> writes:
| > Unfortunatelly, the code in this chapter is not quite ANSI CL
| > compatible.  One has to find a way to make a lexical global variable.
| > In SBCL and CMUCL one can use the deprecated set function:
| >   (set 'some-var 2)
| 
| Or you can fake it portably without invoking undefined behavior:
|   (defvar *some-var* 2)
|   (define-symbol-macro some-var (symbol-value '*some-var*))
| And now you have some-var, which behaves like a global lexical.
+---------------

;;; DEFLEX -- Define "global lexical variables", that is, top-level
;;; variables (convenient when debugging) that are lexical in scope.
;;; Thanks to the denizens of the "comp.lang.lisp" newsgroup for many
;;; useful discussions (and flames!) on this topic, and for the suggestion
;;; for the simple and efficient (albeit inelegant) "shadow" variable
;;; approach used here.
;;;
;;; 2005-06-12 -- Package bugfix courtesy Adam Warner <adam@consulting.net.nz>
;;;
(defmacro deflex (var val &optional (doc nil docp))    
  "Define a top level (global) lexical VAR with initial value VAL,
  which is assigned unconditionally as with DEFPARAMETER. If a DOC
  string is provided, it is attached to both the name |VAR| and the
  name *STORAGE-FOR-DEFLEX-VAR-|VAR|* as a documentation string of
  kind 'VARIABLE. The new VAR will have lexical scope and thus may be
  shadowed by LET bindings without affecting its dynamic (global) value."
  (let* ((s0 (symbol-name '#:*storage-for-deflex-var-))
         (s1 (symbol-name var))
         (s2 (symbol-name '#:*))
         (s3 (symbol-package var))      ; BUGFIX [see above]
         (backing-var (intern (concatenate 'string s0 s1 s2) s3)))
    ;; Note: The DEFINE-SYMBOL-MACRO must be the last thing we do so
    ;; that the value of the form is the symbol VAR.
    (if docp
      `(progn
         (defparameter ,backing-var ,val ,doc)
         (setf (documentation ',var 'variable) ,doc)
         (define-symbol-macro ,var ,backing-var))
      `(progn
         (defparameter ,backing-var ,val)
         (define-symbol-macro ,var ,backing-var)))))


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607