1 | ;;; boot.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003 Peter Graves |
---|
4 | ;;; $Id: boot.lisp,v 1.110 2003-09-25 18:19:29 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 | (sys::%in-package "COMMON-LISP") |
---|
21 | |
---|
22 | |
---|
23 | (defmacro in-package (name) |
---|
24 | (list 'sys::%in-package (string name))) |
---|
25 | |
---|
26 | |
---|
27 | (defmacro when (pred &rest body) |
---|
28 | (list 'if pred (if (> (length body) 1) |
---|
29 | (append '(progn) body) |
---|
30 | (car body)))) |
---|
31 | |
---|
32 | (defmacro unless (pred &rest body) |
---|
33 | (list 'if (list 'not pred) (if (> (length body) 1) |
---|
34 | (append '(progn) body) |
---|
35 | (car body)))) |
---|
36 | |
---|
37 | (defmacro defun (name lambda-list &rest body) |
---|
38 | (list 'sys::%defun (list 'QUOTE name) (list 'QUOTE lambda-list) (list 'QUOTE body))) |
---|
39 | |
---|
40 | (defmacro defconstant (name initial-value &optional docstring) |
---|
41 | (list 'sys::%defconstant (list 'QUOTE name) initial-value docstring)) |
---|
42 | |
---|
43 | (defmacro defparameter (name initial-value &optional docstring) |
---|
44 | (list 'sys::%defparameter (list 'QUOTE name) initial-value docstring)) |
---|
45 | |
---|
46 | (sys::%load "autoloads.lisp") |
---|
47 | (sys::%load "early-defuns.lisp") |
---|
48 | (sys::%load "backquote.lisp") |
---|
49 | (sys::%load "setf.lisp") |
---|
50 | (sys::%load "documentation.lisp") |
---|
51 | |
---|
52 | (defmacro defvar (var &optional (val nil valp) (doc nil docp)) |
---|
53 | `(progn |
---|
54 | (sys::%defvar ',var) |
---|
55 | ,@(when valp |
---|
56 | `((unless (boundp ',var) |
---|
57 | (setq ,var ,val)))) |
---|
58 | ,@(when docp |
---|
59 | `((setf (documentation ',var 'variable) ',doc))) |
---|
60 | ',var)) |
---|
61 | |
---|
62 | |
---|
63 | (defvar *features* |
---|
64 | '(:armedbear)) |
---|
65 | |
---|
66 | |
---|
67 | (defun make-package (package-name &key nicknames use) |
---|
68 | (sys::%make-package package-name nicknames use)) |
---|
69 | |
---|
70 | |
---|
71 | ;;; READ-CONDITIONAL (from OpenMCL) |
---|
72 | |
---|
73 | (defconstant *keyword-package* |
---|
74 | (find-package "KEYWORD")) |
---|
75 | |
---|
76 | (defun read-conditional (stream subchar int) |
---|
77 | (cond (*read-suppress* (read stream t nil t) (values)) |
---|
78 | ((eql subchar (read-feature stream)) (read stream t nil t)) |
---|
79 | (t (let* ((*read-suppress* t)) |
---|
80 | (read stream t nil t) |
---|
81 | (values))))) |
---|
82 | |
---|
83 | (defun read-feature (stream) |
---|
84 | (let* ((f (let* ((*package* *keyword-package*)) |
---|
85 | (read stream t nil t)))) |
---|
86 | (labels ((eval-feature (form) |
---|
87 | (cond ((atom form) |
---|
88 | (member form *features*)) |
---|
89 | ((eq (car form) :not) |
---|
90 | (not (eval-feature (cadr form)))) |
---|
91 | ((eq (car form) :and) |
---|
92 | (dolist (subform (cdr form) t) |
---|
93 | (unless (eval-feature subform) (return)))) |
---|
94 | ((eq (car form) :or) |
---|
95 | (dolist (subform (cdr form) nil) |
---|
96 | (when (eval-feature subform) (return t)))) |
---|
97 | (t (error "READ-FEATURE"))))) |
---|
98 | (if (eval-feature f) #\+ #\-)))) |
---|
99 | |
---|
100 | (set-dispatch-macro-character #\# #\+ #'read-conditional) |
---|
101 | (set-dispatch-macro-character #\# #\- #'read-conditional) |
---|
102 | |
---|
103 | |
---|
104 | (sys::%load "macros.lisp") |
---|
105 | (sys::%load "fixme.lisp") |
---|
106 | (sys::%load "destructuring-bind.lisp") |
---|
107 | (sys::%load "arrays.lisp") |
---|
108 | (sys::%load "compiler.lisp") |
---|
109 | (sys::%load "list.lisp") |
---|
110 | (sys::%load "sequences.lisp") |
---|
111 | (sys::%load "error.lisp") |
---|
112 | (sys::%load "defpackage.lisp") |
---|
113 | (sys::%load "debug.lisp") |
---|
114 | |
---|
115 | |
---|
116 | (defpackage "JVM" (:use "COMMON-LISP" "EXTENSIONS")) |
---|
117 | |
---|
118 | |
---|
119 | ;;; PROVIDE, REQUIRE (from SBCL) |
---|
120 | (defun provide (module-name) |
---|
121 | (pushnew (string module-name) *modules* :test #'string=) |
---|
122 | t) |
---|
123 | |
---|
124 | (defun require (module-name &optional pathnames) |
---|
125 | (finish-output) |
---|
126 | (unless (member (string module-name) *modules* :test #'string=) |
---|
127 | (let ((saved-modules (copy-list *modules*))) |
---|
128 | (cond (pathnames |
---|
129 | (unless (listp pathnames) (setf pathnames (list pathnames))) |
---|
130 | (dolist (x pathnames) |
---|
131 | (load x))) |
---|
132 | (t |
---|
133 | (sys::%load (concatenate 'string (string-downcase (string module-name)) |
---|
134 | ".lisp")))) |
---|
135 | (set-difference *modules* saved-modules)))) |
---|
136 | |
---|
137 | (defun read-from-string (string &optional eof-error-p eof-value |
---|
138 | &key (start 0) end preserve-whitespace) |
---|
139 | (sys::%read-from-string string eof-error-p eof-value start end preserve-whitespace)) |
---|
140 | |
---|
141 | (defconstant call-arguments-limit 50) |
---|
142 | |
---|
143 | (defconstant lambda-parameters-limit 50) |
---|
144 | |
---|
145 | (defconstant multiple-values-limit 20) |
---|
146 | |
---|
147 | (defconstant char-code-limit 128) |
---|
148 | |
---|
149 | (defconstant internal-time-units-per-second 1000) |
---|
150 | |
---|
151 | (defconstant boole-clr 0) |
---|
152 | (defconstant boole-set 1) |
---|
153 | (defconstant boole-1 2) |
---|
154 | (defconstant boole-2 3) |
---|
155 | (defconstant boole-c1 4) |
---|
156 | (defconstant boole-c2 5) |
---|
157 | (defconstant boole-and 6) |
---|
158 | (defconstant boole-ior 7) |
---|
159 | (defconstant boole-xor 8) |
---|
160 | (defconstant boole-eqv 9) |
---|
161 | (defconstant boole-nand 10) |
---|
162 | (defconstant boole-nor 11) |
---|
163 | (defconstant boole-andc1 12) |
---|
164 | (defconstant boole-andc2 13) |
---|
165 | (defconstant boole-orc1 14) |
---|
166 | (defconstant boole-orc2 15) |
---|
167 | |
---|
168 | |
---|
169 | ;; AND, OR (from CMUCL) |
---|
170 | |
---|
171 | (defmacro and (&rest forms) |
---|
172 | (cond ((endp forms) t) |
---|
173 | ((endp (rest forms)) (first forms)) |
---|
174 | (t |
---|
175 | `(if ,(first forms) |
---|
176 | (and ,@(rest forms)) |
---|
177 | nil)))) |
---|
178 | |
---|
179 | (defmacro or (&rest forms) |
---|
180 | (cond ((endp forms) nil) |
---|
181 | ((endp (rest forms)) (first forms)) |
---|
182 | (t |
---|
183 | (let ((n-result (gensym))) |
---|
184 | `(let ((,n-result ,(first forms))) |
---|
185 | (if ,n-result |
---|
186 | ,n-result |
---|
187 | (or ,@(rest forms)))))))) |
---|
188 | |
---|
189 | |
---|
190 | ;; CASE (from CLISP) |
---|
191 | |
---|
192 | (defun case-expand (form-name test keyform clauses) |
---|
193 | (let ((var (gensym))) |
---|
194 | `(let ((,var ,keyform)) |
---|
195 | (cond |
---|
196 | ,@(maplist |
---|
197 | #'(lambda (remaining-clauses) |
---|
198 | (let ((clause (first remaining-clauses)) |
---|
199 | (remaining-clauses (rest remaining-clauses))) |
---|
200 | (unless (consp clause) |
---|
201 | (error 'program-error "~S: missing key list" form-name)) |
---|
202 | (let ((keys (first clause))) |
---|
203 | `(,(cond ((or (eq keys 'T) (eq keys 'OTHERWISE)) |
---|
204 | (if remaining-clauses |
---|
205 | (error 'program-error |
---|
206 | "~S: the ~S clause must be the last one" |
---|
207 | form-name keys) |
---|
208 | 't)) |
---|
209 | ((listp keys) |
---|
210 | `(or ,@(mapcar #'(lambda (key) |
---|
211 | `(,test ,var ',key)) |
---|
212 | keys))) |
---|
213 | (t `(,test ,var ',keys))) |
---|
214 | ,@(rest clause))))) |
---|
215 | clauses))))) |
---|
216 | |
---|
217 | (defmacro case (keyform &rest clauses) |
---|
218 | (case-expand 'case 'eql keyform clauses)) |
---|
219 | |
---|
220 | |
---|
221 | ;; CCASE (from CLISP) |
---|
222 | |
---|
223 | (defun parenthesize-keys (clauses) |
---|
224 | ;; PARENTHESIZE-KEYS is necessary to avoid confusing |
---|
225 | ;; the symbols OTHERWISE and T used as keys, with the same |
---|
226 | ;; symbols used in the syntax of the non exhaustive CASE. |
---|
227 | (mapcar #'(lambda (c) |
---|
228 | (cond ((or (eq (car c) 't) |
---|
229 | (eq (car c) 'otherwise)) |
---|
230 | (cons (list (car c)) (cdr c))) |
---|
231 | (t c))) |
---|
232 | clauses)) |
---|
233 | |
---|
234 | (defmacro ccase (keyplace &rest clauses) |
---|
235 | (let ((g (gensym)) |
---|
236 | (h (gensym))) |
---|
237 | `(block ,g |
---|
238 | (tagbody |
---|
239 | ,h |
---|
240 | (return-from ,g |
---|
241 | (case ,keyplace |
---|
242 | ,@(parenthesize-keys clauses) |
---|
243 | (otherwise |
---|
244 | (error 'type-error "CCASE error") ;; FIXME |
---|
245 | (go ,h)))))))) |
---|
246 | |
---|
247 | |
---|
248 | ;;; TYPECASE (from CLISP) |
---|
249 | |
---|
250 | (defmacro typecase (keyform &rest typeclauselist) |
---|
251 | (let* ((tempvar (gensym)) |
---|
252 | (condclauselist nil)) |
---|
253 | (do ((typeclauselistr typeclauselist (cdr typeclauselistr))) |
---|
254 | ((atom typeclauselistr)) |
---|
255 | (cond ((atom (car typeclauselistr)) |
---|
256 | (error 'program-error |
---|
257 | "invalid clause in ~S: ~S" |
---|
258 | 'typecase (car typeclauselistr))) |
---|
259 | ((let ((type (caar typeclauselistr))) |
---|
260 | (or (eq type T) (eq type 'OTHERWISE))) |
---|
261 | (push `(T ,@(or (cdar typeclauselistr) '(NIL))) condclauselist) |
---|
262 | (return)) |
---|
263 | (t (push `((TYPEP ,tempvar (QUOTE ,(caar typeclauselistr))) |
---|
264 | ,@(or (cdar typeclauselistr) '(NIL))) |
---|
265 | condclauselist)))) |
---|
266 | `(LET ((,tempvar ,keyform)) (COND ,@(nreverse condclauselist))))) |
---|
267 | |
---|
268 | |
---|
269 | (defmacro etypecase (keyform &rest clauses) |
---|
270 | (let ((var (gensym))) |
---|
271 | `(let ((,var ,keyform)) |
---|
272 | (typecase ,var |
---|
273 | ,@clauses |
---|
274 | (otherwise |
---|
275 | (error 'type-error "~S fell through ETYPECASE expression" ,var)))))) |
---|
276 | |
---|
277 | |
---|
278 | (defmacro ctypecase (keyplace &rest clauses) |
---|
279 | (let ((g (gensym)) |
---|
280 | (h (gensym))) |
---|
281 | `(block ,g |
---|
282 | (tagbody |
---|
283 | ,h |
---|
284 | (return-from ,g |
---|
285 | (typecase ,keyplace |
---|
286 | ,@clauses |
---|
287 | (otherwise |
---|
288 | (error 'type-error "CTYPECASE error") ;; FIXME |
---|
289 | (go ,h)))))))) |
---|
290 | |
---|
291 | |
---|
292 | (defmacro cond (&rest clauses) |
---|
293 | (if (endp clauses) |
---|
294 | nil |
---|
295 | (let ((clause (first clauses))) |
---|
296 | (when (atom clause) |
---|
297 | (error "COND clause is not a list: ~S" clause)) |
---|
298 | (let ((test (first clause)) |
---|
299 | (forms (rest clause))) |
---|
300 | (if (endp forms) |
---|
301 | (let ((n-result (gensym))) |
---|
302 | `(let ((,n-result ,test)) |
---|
303 | (if ,n-result |
---|
304 | ,n-result |
---|
305 | (cond ,@(rest clauses))))) |
---|
306 | `(if ,test |
---|
307 | (progn ,@forms) |
---|
308 | (cond ,@(rest clauses)))))))) |
---|
309 | |
---|
310 | |
---|
311 | ;;; PROG, PROG* (from GCL) |
---|
312 | |
---|
313 | (defmacro prog (vl &rest body &aux (decl nil)) |
---|
314 | (do () |
---|
315 | ((or (endp body) |
---|
316 | (not (consp (car body))) |
---|
317 | (not (eq (caar body) 'declare))) |
---|
318 | `(block nil (let ,vl ,@decl (tagbody ,@body)))) |
---|
319 | (push (car body) decl) |
---|
320 | (pop body))) |
---|
321 | |
---|
322 | (defmacro prog* (vl &rest body &aux (decl nil)) |
---|
323 | (do () |
---|
324 | ((or (endp body) |
---|
325 | (not (consp (car body))) |
---|
326 | (not (eq (caar body) 'declare))) |
---|
327 | `(block nil (let* ,vl ,@decl (tagbody ,@body)))) |
---|
328 | (push (car body) decl) |
---|
329 | (pop body))) |
---|
330 | |
---|
331 | |
---|
332 | ;;; DOTIMES (from CMUCL) |
---|
333 | (defmacro dotimes ((var count &optional (result nil)) &body body) |
---|
334 | (cond ((numberp count) |
---|
335 | `(do ((,var 0 (1+ ,var))) |
---|
336 | ((>= ,var ,count) ,result) |
---|
337 | ,@body)) |
---|
338 | (t (let ((v1 (gensym))) |
---|
339 | `(do ((,var 0 (1+ ,var)) (,v1 ,count)) |
---|
340 | ((>= ,var ,v1) ,result) |
---|
341 | ,@body))))) |
---|
342 | |
---|
343 | |
---|
344 | ;;; DOLIST (from CMUCL) |
---|
345 | |
---|
346 | ;;; We repeatedly bind the var instead of setting it so that we never give the |
---|
347 | ;;; var a random value such as NIL (which might conflict with a declaration). |
---|
348 | ;;; If there is a result form, we introduce a gratitous binding of the variable |
---|
349 | ;;; to NIL w/o the declarations, then evaluate the result form in that |
---|
350 | ;;; environment. We spuriously reference the gratuitous variable, since we |
---|
351 | ;;; don't want to use IGNORABLE on what might be a special var. |
---|
352 | ;;; |
---|
353 | (defmacro dolist ((var list &optional (result nil)) &body body) |
---|
354 | (multiple-value-bind (forms decls) |
---|
355 | (parse-body body nil nil) |
---|
356 | (let ((n-list (gensym))) |
---|
357 | `(do* ((,n-list ,list (cdr ,n-list))) |
---|
358 | ((endp ,n-list) |
---|
359 | ,@(if (constantp result) |
---|
360 | `(,result) |
---|
361 | `((let ((,var nil)) |
---|
362 | ,@decls |
---|
363 | ,var |
---|
364 | ,result)))) |
---|
365 | (let ((,var (car ,n-list))) |
---|
366 | ,@decls |
---|
367 | (tagbody |
---|
368 | ,@forms)))))) |
---|
369 | |
---|
370 | |
---|
371 | ;;; From CMUCL. |
---|
372 | |
---|
373 | (defmacro with-output-to-string ((var &optional string &key element-type) |
---|
374 | &body forms) |
---|
375 | "If STRING is specified, it must be a string with a fill pointer; |
---|
376 | the output is incrementally appended to the string (as if by use of |
---|
377 | VECTOR-PUSH-EXTEND)." |
---|
378 | (declare (ignore element-type)) |
---|
379 | (if string |
---|
380 | `(let ((,var (sys::make-fill-pointer-output-stream ,string))) |
---|
381 | (unwind-protect |
---|
382 | (progn ,@forms) |
---|
383 | (close ,var))) |
---|
384 | `(let ((,var (make-string-output-stream))) |
---|
385 | (unwind-protect |
---|
386 | (progn ,@forms) |
---|
387 | (close ,var)) |
---|
388 | (get-output-stream-string ,var)))) |
---|
389 | |
---|
390 | |
---|
391 | (defmacro print-unreadable-object ((object stream &key type identity) &body body) |
---|
392 | `(let ((s ,stream) |
---|
393 | (obj ,object)) |
---|
394 | (format s "#<") |
---|
395 | ,(when type |
---|
396 | '(format s "~S" (type-of obj))) |
---|
397 | ,(when (and type (or body identity)) |
---|
398 | '(format s " ")) |
---|
399 | ,@body |
---|
400 | ,(when (and identity body) |
---|
401 | '(format s " ")) |
---|
402 | ,(when identity |
---|
403 | '(format s "@ ~A" (sys::hashcode-to-string obj))) |
---|
404 | (format s ">") |
---|
405 | nil)) |
---|
406 | |
---|
407 | |
---|
408 | ;;; MULTIPLE-VALUE-BIND (from CLISP) |
---|
409 | (defmacro multiple-value-bind (varlist form &body body) |
---|
410 | (let ((g (gensym)) |
---|
411 | (poplist nil)) |
---|
412 | (dolist (var varlist) (setq poplist (cons `(,var (pop ,g)) poplist))) |
---|
413 | `(let* ((,g (multiple-value-list ,form)) ,@(nreverse poplist)) |
---|
414 | ,@body))) |
---|
415 | |
---|
416 | |
---|
417 | (sys::%load "top-level.lisp") |
---|