From: Fractal
Subject: how-to create a sandbox macro.
Date: 
Message-ID: <1130417555.213194.185690@g47g2000cwa.googlegroups.com>
Hi,

i am trying the following:

; SLIME 2005-04-27
CL-USER> (make-package :gal)
#<PACKAGE GAL>
CL-USER> (in-package :gal)
#<PACKAGE GAL>
GAL> (defun runner ()
'hellow-world)
RUNNER
GAL> (in-package :cl-user)
#<PACKAGE COMMON-LISP-USER>
CL-USER> (progn
            (in-package :gal)
            (runner))
; Evaluation aborted

the reason is that, the progn expression is first read, and only after
evaluated.
how can i write a macro that will evaluate to multiple top-level forms?
(so the read eval will cycle on each form)
or any other direction as to how to make this work...

thanks,
Gal

From: Marco Antoniotti
Subject: Re: how-to create a sandbox macro.
Date: 
Message-ID: <d098f.54$pa3.19739@typhoon.nyu.edu>
tfeb proposed a nice read-macro that would read all the subsequent 
(enclosed) forms in a given package.  See

http://www.tfeb.org/lisp/hax.html#READ-PACKAGES

Cheers

marco

Fractal wrote:
> Hi,
> 
> i am trying the following:
> 
> ; SLIME 2005-04-27
> CL-USER> (make-package :gal)
> #<PACKAGE GAL>
> CL-USER> (in-package :gal)
> #<PACKAGE GAL>
> GAL> (defun runner ()
> 'hellow-world)
> RUNNER
> GAL> (in-package :cl-user)
> #<PACKAGE COMMON-LISP-USER>
> CL-USER> (progn
>             (in-package :gal)
>             (runner))
> ; Evaluation aborted
> 
> the reason is that, the progn expression is first read, and only after
> evaluated.
> how can i write a macro that will evaluate to multiple top-level forms?
> (so the read eval will cycle on each form)
> or any other direction as to how to make this work...
> 
> thanks,
> Gal
> 
From: Pascal Bourguignon
Subject: Re: how-to create a sandbox macro.
Date: 
Message-ID: <87mzkvw1j7.fsf@thalassa.informatimago.com>
"Fractal" <···········@gmail.com> writes:

> Hi,
>
> i am trying the following:
>
> ; SLIME 2005-04-27
> CL-USER> (make-package :gal)
> #<PACKAGE GAL>
> CL-USER> (in-package :gal)
> #<PACKAGE GAL>
> GAL> (defun runner ()
> 'hellow-world)
> RUNNER
> GAL> (in-package :cl-user)
> #<PACKAGE COMMON-LISP-USER>
> CL-USER> (progn
>             (in-package :gal)
>             (runner))
> ; Evaluation aborted
>
> the reason is that, the progn expression is first read, and only after
> evaluated.
> how can i write a macro that will evaluate to multiple top-level forms?
> (so the read eval will cycle on each form)

You cannot.  That is, the forms given to the macro will have been
interned by the reader long before the macro is called.  The macro
could substitute symbols by those of same name found in the packages
specified though.  But this is a little awkward.

(eval-when (:compile-toplevel :load-toplevel :execute)
 (defun substitute-symbols (form package)
   (cond
     ((symbolp form) (intern (symbol-name form) package))
     ((atom form)    form)
     (t (mapcar (lambda (item) (substitute-symbols item package)) form)))))

(defmacro progp (&body body)
 `(let ((*package* *package*))
    ,@(let ((*package* *package*)) ; note: macro-expansion time!
          (mapcar (lambda (form)
                    (cond ((and (listp form)
                                (= 2 (length form))
                                (eq 'cl:in-package (first form)))
                            (setf *package* (find-package (second form)))
                            `(setf *package* (find-package ',(second form))))
                           (t
                             (substitute-symbols form *package*)))) body))))

(defpackage :p1 (:use :cl))
(defpackage :p2 (:use :cl))
(let ((*print-readably* t))
    (progp (print 'a) (in-package :p1) (print 'a) (in-package :p2) (print 'a)))

|COMMON-LISP-USER|::|A| 
|P1|::|A| 
|P2|::|A| 
P2::A


> or any other direction as to how to make this work...

Qualify the symbols:

    (gal::runner)



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

In a World without Walls and Fences, 
who needs Windows and Gates?