Python syntax in Lisp and Scheme (macro tangent)

Raymond Wiker Raymond.Wiker at fast.no
Mon Oct 6 11:36:52 EDT 2003


        Another example:

        Given a set of files containing database data, I want to create

        - classes that represent (the interesting bits of) each table

        - functions that parse lines from the files, create instances
of a given class, and returns the instance along with the "primary
key" of the instance.

        The interface to this functionality is the macro

(defmacro define-record (name key args &body body)
        ...)
        
which I use like this:
        
(define-record category oid ((oid 0 integer)
			     (name 1 string)
			     (status 4 integer)
			     (deleted 5 integer)
			     (parent 9 integer)
			     active)
	       (unless (zerop deleted)
		 (return t)))

which expands into

(PROGN
 (DEFCLASS CATEGORY-CLASS
           NIL
           ((OID :INITARG :OID) (NAME :INITARG :NAME) (STATUS :INITARG :STATUS)
            (DELETED :INITARG :DELETED) (PARENT :INITARG :PARENT)
            (ACTIVE :INITARG :ACTIVE)))
 (DEFUN HANDLE-CATEGORY (#:LINE-2524)
   (WHEN #:LINE-2524
     (LET ((#:FIELDS-2525
            (SPLIT-LINE-COLLECT #:LINE-2524
                                '((0 . INTEGER) (1 . STRING) (4 . INTEGER)
                                  (5 . INTEGER) (9 . INTEGER)))))
       (WHEN #:FIELDS-2525
         (PROGV
             '(OID NAME STATUS DELETED PARENT)
             #:FIELDS-2525
           (BLOCK NIL
             (LET (ACTIVE)
               (UNLESS (ZEROP DELETED) (RETURN T))
               (VALUES OID
                       (MAKE-INSTANCE 'CATEGORY-CLASS
                                      :OID
                                      OID
                                      :NAME
                                      NAME
                                      :STATUS
                                      STATUS
                                      :DELETED
                                      DELETED
                                      :PARENT
                                      PARENT
                                      :ACTIVE
                                      ACTIVE))))))))))

        The implementation of this macro is probably not perfect (I've
learnt more about Common Lisp since I wrote it). This is OK, since I
can go back and change the innards of the macro whenever I want to :-)
Actually, this is probably something that calls for the use of MOP.

-- 
Raymond Wiker                        Mail:  Raymond.Wiker at fast.no
Senior Software Engineer             Web:   http://www.fast.no/
Fast Search & Transfer ASA           Phone: +47 23 01 11 60
P.O. Box 1677 Vika                   Fax:   +47 35 54 87 99
NO-0120 Oslo, NORWAY                 Mob:   +47 48 01 11 60

Try FAST Search: http://alltheweb.com/




More information about the Python-list mailing list