source: trunk/j/src/org/armedbear/lisp/defstruct.lisp @ 4009

Last change on this file since 4009 was 4009, checked in by piso, 20 years ago

Support :PREDICATE option.

File size: 4.8 KB
Line 
1;;; defstruct.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: defstruct.lisp,v 1.21 2003-09-22 22:56:12 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package "SYSTEM")
21
22(defvar *ds-name*)
23(defvar *ds-conc-name*)
24(defvar *ds-constructor*)
25(defvar *ds-copier*)
26(defvar *ds-named*)
27(defvar *ds-predicate*)
28(defvar *ds-print-function*)
29
30(defun define-constructor (slots)
31  (when *ds-constructor*
32    (let* ((constructor (intern *ds-constructor*))
33           (slot-names (mapcar #'(lambda (x) (if (atom x) x (car x))) slots))
34           (inits (mapcar #'(lambda (x) (if (atom x) nil (cadr x))) slots))
35           (slot-descriptions (mapcar #'(lambda (x y) (list x y)) slot-names inits))
36           (keys (cons '&key slot-descriptions)))
37      `((defun ,constructor ,keys
38          (%make-structure ',*ds-name* (list ,@slot-names)))))))
39
40(defun define-predicate ()
41  (when *ds-predicate*
42    (let ((pred (intern *ds-predicate*)))
43      `((defun ,pred (object)
44          (typep object ',*ds-name*))))))
45
46
47(defmacro get-slot-accessor (slot)
48  (case slot
49    (0 #'%structure-ref-0)
50    (1 #'%structure-ref-1)
51    (2 #'%structure-ref-2)
52    (t
53     `(lambda (instance) (%structure-ref instance ,slot)))))
54
55(defmacro get-slot-mutator (slot)
56  (case slot
57    (0 #'%structure-set-0)
58    (1 #'%structure-set-1)
59    (2 #'%structure-set-2)
60    (t
61     `(lambda (instance value) (%structure-set instance ,slot value)))))
62
63(defun define-access-function (slot-name index)
64  (let ((accessor
65         (if *ds-conc-name*
66             (intern (concatenate 'string (symbol-name *ds-conc-name*) (symbol-name slot-name)))
67             slot-name))
68        (setf-expander (gensym)))
69    `((setf (symbol-function ',accessor) (get-slot-accessor ,index))
70      (%put ',accessor *setf-expander* (get-slot-mutator ,index)))))
71
72(defun define-access-functions (slots)
73  (let ((index 0)
74        (result ()))
75    (dolist (slot slots)
76      (let ((slot-name (if (atom slot) slot (car slot))))
77        (setq result (append result (define-access-function slot-name index))))
78      (incf index))
79    result))
80
81(defun define-copier ()
82  (let ((copier (intern (concatenate 'string "COPY-" (symbol-name *ds-name*)))))
83    `((setf (fdefinition ',copier) #'copy-structure))))
84
85(defun parse-1-option (option)
86  (case (car option)
87    (:conc-name
88     (setf *ds-conc-name* (if (symbolp (cadr option))
89                              (cadr option)
90                              (make-symbol (string (cadr option))))))
91    (:constructor
92     (when (= (length option) 2)
93       (if (null (cadr option))
94           (setf *ds-constructor* nil)
95           (setf *ds-constructor* (symbol-name (cadr option))))))
96    (:predicate
97     (when (= (length option) 2)
98       (if (null (cadr option))
99           (setf *ds-predicate* nil)
100           (setf *ds-predicate* (symbol-name (cadr option))))))))
101
102(defun parse-name-and-options (name-and-options)
103  (setf *ds-name* (car name-and-options))
104  (setf *ds-conc-name* (make-symbol (concatenate 'string (symbol-name *ds-name*) "-")))
105  (setf *ds-constructor* (concatenate 'string "MAKE-" (symbol-name *ds-name*)))
106  (setf *ds-predicate* (concatenate 'string (symbol-name *ds-name*) "-P"))
107  (let ((options (cdr name-and-options)))
108    (dolist (option options)
109      (cond ((consp option)
110             (parse-1-option option))
111            ((eq option :named)
112             (setf *ds-named* t))
113            ((member option '(:constructor :copier :predicate :named
114                              :conc-name))
115             (parse-1-option (list option)))
116            (t
117             (error "unrecognized DEFSTRUCT option: ~S" option))))))
118
119(defmacro defstruct (name-and-options &rest slots)
120  (let ((*ds-name* nil)
121        (*ds-conc-name* nil)
122        (*ds-constructor* nil)
123        (*ds-copier* nil)
124        (*ds-predicate* nil)
125        (*ds-print-function* nil))
126    (parse-name-and-options (if (atom name-and-options)
127                                (list name-and-options)
128                                name-and-options))
129    `(progn
130       (make-structure-class ',*ds-name*)
131       ,@(define-constructor slots)
132       ,@(define-predicate)
133       ,@(define-access-functions slots)
134       ,@(define-copier)
135       ',*ds-name*)))
136
137(provide 'defstruct)
Note: See TracBrowser for help on using the repository browser.