source: trunk/j/src/org/armedbear/lisp/typep.lisp @ 3922

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

TYPEP: support NOT.

File size: 4.0 KB
Line 
1;;; typep.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: typep.lisp,v 1.6 2003-09-20 00:52:48 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;;; Adapted from GCL.
21
22(in-package "SYSTEM")
23
24(resolve 'subtypep)
25
26(defun simple-array-p (object)
27  (and (arrayp object)
28       (not (array-has-fill-pointer-p object))
29       (multiple-value-bind (displaced-to offset) (array-displacement object)
30         (and (null displaced-to) (zerop offset)))))
31
32(defun in-interval-p (x interval)
33  (let (low high)
34    (if (endp interval)
35        (setq low '* high '*)
36        (if (endp (cdr interval))
37            (setq low (car interval) high '*)
38            (setq low (car interval) high (cadr interval))))
39    (cond ((eq low '*))
40          ((consp low)
41           (when (<= x (car low)) (return-from in-interval-p nil)))
42          ((when (< x low) (return-from in-interval-p nil))))
43    (cond ((eq high '*))
44          ((consp high)
45           (when (>= x (car high)) (return-from in-interval-p nil)))
46          ((when (> x high) (return-from in-interval-p nil))))
47    (return-from in-interval-p t)))
48
49(defun match-dimensions (dim pat)
50  (if (null dim)
51      (null pat)
52      (and (or (eq (car pat) '*)
53         (eql (car dim) (car pat)))
54     (match-dimensions (cdr dim) (cdr pat)))))
55
56(defun typep (object type)
57  (when (atom type)
58    (return-from typep (simple-typep object type)))
59  (let ((tp (car type))
60        (i (cdr type)))
61    (case tp
62      (AND
63       (dolist (type i)
64         (unless (typep object type)
65           (return-from typep nil)))
66       t)
67      (OR
68       (dolist (type i)
69         (when (typep object type)
70           (return-from typep t)))
71       nil)
72      (NOT (not (typep object (car i))))
73      (INTEGER
74       (and (integerp object) (in-interval-p object i)))
75      (SIMPLE-BIT-VECTOR
76       (and (simple-bit-vector-p object)
77            (or (endp i) (match-dimensions (array-dimensions object) i))))
78      (BIT-VECTOR
79       (and (bit-vector-p object)
80            (or (endp i) (match-dimensions (array-dimensions object) i))))
81      (SIMPLE-VECTOR
82       (and (simple-vector-p object)
83            (or (endp i) (eq (car i) '*)
84                (= (length object) (car i)))))
85      (VECTOR
86       (and (vectorp object)
87            (or (endp i)
88                (eq (car i) '*)
89                (and (eq (car i) t) (not (stringp object)) (not (bit-vector-p object)))
90                (and (stringp object) (subtypep (car i) 'character))
91                (equal (array-element-type object) (car i)))
92            (or (endp (cdr i)) (match-dimensions (array-dimensions object) (cdr i)))))
93      (SIMPLE-ARRAY
94       (and (simple-array-p object)
95            (or (endp i) (eq (car i) '*)
96                (equal (array-element-type object) (upgraded-array-element-type (car i))))
97            (or (endp (cdr i)) (eq (cadr i) '*)
98                (if (listp (cadr i))
99                    (match-dimensions (array-dimensions object) (cadr i))
100                    (eql (array-rank object) (cadr i))))))
101      (ARRAY
102       (and (arrayp object)
103            (or (null i) (eq (car i) '*)
104                (equal (array-element-type object) (upgraded-array-element-type (car i))))
105            (or (null (cdr i)) (eq (cadr i) '*)
106                (if (listp (cadr i))
107                    (match-dimensions (array-dimensions object) (cadr i))
108                    (eql (array-rank object) (cadr i))))))
109      (t
110       nil))))
Note: See TracBrowser for help on using the repository browser.