macro returning more than one form

Pascal J. Bourguignon pjb at informatimago.com
Fri Mar 14 20:11:43 UTC 2014


Paul Tarvydas <paultarvydas at gmail.com> writes:

> On 14-03-14 12:57 PM, Pascal J. Bourguignon wrote:
>
> I'm writing diagram-compilers that emit CL.  It seems to me that each
> emitted lump of code should be in it's own package.  Am I thinking
> about this "wrong"?


That said, if you want to "put" each lump of code inside its own
package, nothing prevents you to do so, but you don't need to include an
in-package form in the generated code.

(defvar *generated-packages* '())
(defvar *package-counter* 0)

(defun generate-code (x)
  (let* ((pack (make-package (format nil "GENPACK~D" 
                                          (incf *package-counter*))
                             :use '()))
         (fname (gentemp "FUN" pack))
         (vname (gentemp "VAR" pack)))
    (push pack *generated-packages*)
    `(defun ,fname (,vname)
        (+ ,vname ,x))))


cl-user> (generate-code 1)
(defun genpack1::fun0 (genpack1::var0) (+ genpack1::var0 1))
cl-user> (generate-code 2)
(defun genpack2::fun0 (genpack2::var0) (+ genpack2::var0 2))
cl-user> (generate-code 3)
(defun genpack3::fun0 (genpack3::var0) (+ genpack3::var0 3))
cl-user> 

Then you only need to generate a file that creates the packages:

(dolist (p *generated-packages*)
  (print `(make-package ,(package-name p) :use '())))    

(make-package "GENPACK3" :use 'nil) 
(make-package "GENPACK2" :use 'nil) 
(make-package "GENPACK1" :use 'nil) 

and to load it before the file contining the generated code.


But as I said, this is boring use so fine grained packages, just have
one static package for the whole generated code.


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ?  C'est le moment d'acheter !"



More information about the pro mailing list