Debian Codegraph @

src/lsp/assert.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 (in-package "SYSTEM")
12
13 (defun read-evaluated-form ()
14 (format *query-io* "~&Type a form to be evaluated:~%")
15 (list (eval (read *query-io*))))
16
17 (defun wrong-type-argument (object type &optional place function)
18 #-ecl-min
19 (declare (c::policy-debug-ihs-frame))
20 (tagbody again
21 (restart-case
22 (error 'simple-type-error
23 :format-control
24 "In ~:[an anonymous function~;~:*function ~A~], ~
25 ~:[found object~;~:*the value of ~A is~]~%~8t~S~%~
26 which is not of expected type ~A"
27 :format-arguments (list function place object type)
28 :datum object
29 :expected-type type
30 )
31 (use-value (value)
32 :report (lambda (stream)
33 (format stream "Supply a new value of type ~A." type))
34 :interactive read-evaluated-form
35 (setf object value)
36 (unless (typep object type)
37 (go again)))))
38 object)
39
40 (defmacro check-type (place type &optional type-string)
41 "Args: (check-type place typespec [string-form])
42 Signals a continuable error, if the value of PLACE is not of the specified
43 type. Before continuing, receives a new value of PLACE from the user and
44 checks the type again. Repeats this process until the value of PLACE becomes
45 of the specified type. STRING-FORM, if given, is evaluated only once and the
46 value is used to indicate the expected type in the error message."
47 (let ((aux (gensym)))
48 `(let ((,aux ,place))
49 (declare (:read-only ,aux))
50 (unless (typep ,aux ',type)
51 (setf ,place (do-check-type ,aux ',type ',type-string ',place)))
52 nil)))
53
54 (defun do-check-type (value type type-string place)
55 (tagbody again
56 (unless (typep value type)
57 (restart-case
58 (error 'simple-type-error
59 :datum value
60 :expected-type type
61 :format-control "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
62 :format-arguments (list place value type-string type))
63 (store-value (new-value)
64 :report (lambda (stream)
65 (format stream "Supply a new value of ~S" place))
66 :interactive read-evaluated-form
67 (setf value new-value)
68 (go again)))))
69 value)
70
71 (defmacro assert (test-form &optional places &rest arguments)
72 "Args: (assert form [({place}*) [string {arg}*]])
73 Evaluates FORM and signals a continuable error if the value is NIL. Before
74 continuing, receives new values of PLACEs from user. Repeats this process
75 until FORM returns a non-NIL value. Returns NIL. STRING is the format string
76 for the error message and ARGs are arguments to the format string."
77 (let ((repl
78 (if places
79 `(setf (values ,@places)
80 (assert-failure ',test-form ',places (list ,@places)
81 ,@arguments))
82 `(assert-failure ',test-form
83 ,@(and arguments
84 (list* nil nil arguments))))))
85 `(while (not ,test-form)
86 ,repl)))
87
88 (defun accumulate-cases (cases list-is-atom-p)
89 (declare (si::c-local))
90 (do ((c cases (cdr c))
91 (l '()))
92 ((null c) (nreverse l))
93 (let ((keys (caar c)))
94 (cond ((atom keys) (unless (null keys) (push keys l)))
95 (list-is-atom-p (push keys l))
96 (t (setq l (append keys l)))))))
97
98 (defun ecase-error (value values)
99 (error 'CASE-FAILURE :name 'ECASE
100 :datum value
101 :expected-type (cons 'MEMBER values)
102 :possibilities values))
103
104 (defmacro ecase (keyform &rest clauses)
105 "Syntax: (ecase keyform {({key | ({key}*)} {form}*)}*)
106 Evaluates KEYFORM and tries to find the KEY that is EQL to the value of
107 KEYFORM. If found, then evaluates FORMs that follow the KEY (or the key list
108 that contains the KEY) and returns all values of the last FORM. If not,
109 signals an error."
110 (setq clauses (remove-otherwise-from-clauses clauses))
111 (let ((key (gensym)))
112 `(let ((,key ,keyform))
113 (case ,key ,@clauses
114 (t (si::ecase-error ,key ',(accumulate-cases clauses nil)))))))
115
116 (defun ccase-error (keyform key values)
117 (restart-case (error 'CASE-FAILURE
118 :name 'CCASE
119 :datum key
120 :expected-type (cons 'MEMBER values)
121 :possibilities values)
122 (store-value (value)
123 :REPORT (lambda (stream)
124 (format stream "Supply a new value of ~S" keyform))
125 :INTERACTIVE read-evaluated-form
126 (return-from ccase-error value))))
127
128 (defun remove-otherwise-from-clauses (clauses)
129 (declare (si::c-local))
130 (mapcar #'(lambda (clause)
131 (let ((options (first clause)))
132 (if (member options '(t otherwise))
133 (cons (list options) (rest clause))
134 clause)))
135 clauses))
136
137 (defmacro ccase (keyplace &rest clauses)
138 "Syntax: (ccase place {({key | ({key}*)} {form}*)}*)
139 Searches a KEY that is EQL to the value of PLACE. If found, then evaluates
140 FORMs in order that follow the KEY (or the key list that contains the KEY) and
141 returns all values of the last FORM. If no such KEY is found, signals a
142 continuable error. Before continuing, receives a new value of PLACE from
143 user and searches a KEY again. Repeats this process until the value of PLACE
144 becomes EQL to one of the KEYs."
145 (let* ((key (gensym))
146 (repeat (gensym))
147 (block (gensym)))
148 (setq clauses (remove-otherwise-from-clauses clauses))
149 `(block ,block
150 (tagbody ,repeat
151 (let ((,key ,keyplace))
152 (return-from ,block
153 (case ,key ,@clauses
154 (t (setf ,keyplace
155 (si::ccase-error ',keyplace ,key
156 ',(accumulate-cases clauses nil)))
157 (go ,repeat)))))))))
158
159 (defmacro typecase (keyform &rest clauses)
160 "Syntax: (typecase keyform {(type {form}*)}*)
161 Evaluates KEYFORM and searches a TYPE to which the value of KEYFORM belongs.
162 If found, then evaluates FORMs that follow the TYPE and returns all values of
163 the last FORM. If not, simply returns NIL. The symbols T and OTHERWISE may
164 be used as a TYPE to specify the default case."
165 (do ((l (reverse clauses) (cdr l))
166 (form nil) (key (gensym)))
167 ((endp l) `(let ((,key ,keyform)) ,form))
168 (if (or (eq (caar l) 't) (eq (caar l) 'otherwise))
169 (setq form `(progn ,@(cdar l)))
170 (setq form
171 `(if (typep ,key (quote ,(caar l)))
172 (progn ,@(cdar l))
173 ,form))))
174 )
175
176 (defun etypecase-error (value types)
177 (error 'CASE-FAILURE :name 'ETYPECASE
178 :datum value
179 :expected-type (cons 'OR types)
180 :possibilities types))
181
182 (defmacro etypecase (keyform &rest clauses &aux (key (gensym)))
183 "Syntax: (etypecase keyform {(type {form}*)}*)
184 Evaluates KEYFORM and searches a TYPE to which the value of KEYFORM belongs.
185 If found, then evaluates FORMs that follow the TYPE and returns all values of
186 the last FORM. If not, signals an error."
187 (setq clauses (remove-otherwise-from-clauses clauses))
188 (do ((l (reverse clauses) (cdr l)) ; Beppe
189 (form `(etypecase-error ,key ',(accumulate-cases clauses t))))
190 ((endp l) `(let ((,key ,keyform)) ,form))
191 (setq form `(if (typep ,key ',(caar l))
192 (progn ,@(cdar l))
193 ,form))
194 )
195 )
196
197 (defun ctypecase-error (keyplace value types)
198 (restart-case (error 'CASE-FAILURE
199 :name 'CTYPECASE
200 :datum value
201 :expected-type (cons 'OR types)
202 :possibilities types)
203 (store-value (value)
204 :REPORT (lambda (stream)
205 (format stream "Supply a new value of ~S." keyplace))
206 :INTERACTIVE read-evaluated-form
207 (return-from ctypecase-error value))))
208
209 (defmacro ctypecase (keyplace &rest clauses &aux (key (gensym)))
210 "Syntax: (ctypecase place {(type {form}*)}*)
211 Searches a TYPE to which the value of PLACE belongs. If found, then evaluates
212 FORMs that follow the TYPE and returns all values of the last FORM. If no
213 such TYPE is found, signals a continuable error. Before continuing, receives
214 a new value of PLACE from the user and searches an appropriate TYPE again.
215 Repeats this process until the value of PLACE becomes of one of the TYPEs."
216 (setq clauses (remove-otherwise-from-clauses clauses))
217 `(loop
218 (let ((,key ,keyplace))
219 ,@(mapcar #'(lambda (l)
220 `(when (typep ,key ',(car l))
221 (return (progn ,@(cdr l)))))
222 clauses)
223 (setf ,keyplace (ctypecase-error ',keyplace ,key
224 ',(accumulate-cases clauses t))))))