source: trunk/j/src/org/armedbear/lisp/deftype.lisp @ 9378

Last change on this file since 9378 was 9378, checked in by piso, 16 years ago

DEFTYPE: signal an error if NAME is a symbol in the CL package.

File size: 2.3 KB
Line 
1;;; deftype.lisp
2;;;
3;;; Copyright (C) 2004-2005 Peter Graves
4;;; $Id: deftype.lisp,v 1.9 2005-06-14 12:06:11 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(defmacro deftype (name lambda-list &rest body)
23  (when (eq (symbol-package name) +cl-package+)
24    (error :format-control "Attempt to define ~S, a symbol in the COMMON-LISP package, as a type specifier."
25           :format-arguments (list name)))
26  (check-declaration-type name)
27  ;; Optional and keyword parameters default to * rather than NIL.
28  (when (or (memq '&optional lambda-list)
29            (memq '&key lambda-list))
30    (let ((new-lambda-list ())
31          (state nil))
32      (dolist (thing lambda-list)
33        (cond ((eq thing '&optional)
34               (setf state '&optional))
35              ((eq thing '&key)
36               (setf state '&key))
37              ((memq thing lambda-list-keywords)
38               (setf state nil))
39              ((eq state '&optional)
40               (when (symbolp thing)
41                 (setf thing (list thing ''*))))
42              ((eq state '&key)
43               (when (symbolp thing)
44                 (setf thing (list thing ''*)))))
45        (push thing new-lambda-list))
46      (setf lambda-list (nreverse new-lambda-list))))
47  `(progn
48     (setf (get ',name 'deftype-definition)
49           #'(lambda ,lambda-list (block ,name ,@body)))
50     ',name))
51
52(defun expand-deftype (type)
53  (let (tp i)
54    (loop
55      (if (consp type)
56          (setf tp (%car type) i (%cdr type))
57          (setf tp type
58                i nil))
59      (if (and (symbolp tp) (get tp 'deftype-definition))
60          (setf type (apply (get tp 'deftype-definition) i))
61          (return))))
62  type)
Note: See TracBrowser for help on using the repository browser.