[568] | 1 | ;;; boot.lisp |
---|
[1041] | 2 | ;;; |
---|
| 3 | ;;; Copyright (C) 2003 Peter Graves |
---|
[4220] | 4 | ;;; $Id: boot.lisp,v 1.116 2003-10-06 13:55:22 piso Exp $ |
---|
[1041] | 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. |
---|
[568] | 19 | |
---|
[2872] | 20 | (sys::%in-package "COMMON-LISP") |
---|
[1153] | 21 | |
---|
[1168] | 22 | |
---|
[2872] | 23 | (defmacro in-package (name) |
---|
| 24 | (list 'sys::%in-package (string name))) |
---|
[1168] | 25 | |
---|
[2872] | 26 | |
---|
[1400] | 27 | (defmacro when (pred &rest body) |
---|
[2100] | 28 | (list 'if pred (if (> (length body) 1) |
---|
| 29 | (append '(progn) body) |
---|
| 30 | (car body)))) |
---|
[1400] | 31 | |
---|
| 32 | (defmacro unless (pred &rest body) |
---|
[2100] | 33 | (list 'if (list 'not pred) (if (> (length body) 1) |
---|
| 34 | (append '(progn) body) |
---|
| 35 | (car body)))) |
---|
[1400] | 36 | |
---|
[1945] | 37 | (defmacro defun (name lambda-list &rest body) |
---|
[2733] | 38 | (list 'sys::%defun (list 'QUOTE name) (list 'QUOTE lambda-list) (list 'QUOTE body))) |
---|
[1168] | 39 | |
---|
[3491] | 40 | (defmacro defconstant (name initial-value &optional docstring) |
---|
| 41 | (list 'sys::%defconstant (list 'QUOTE name) initial-value docstring)) |
---|
| 42 | |
---|
[3495] | 43 | (defmacro defparameter (name initial-value &optional docstring) |
---|
| 44 | (list 'sys::%defparameter (list 'QUOTE name) initial-value docstring)) |
---|
| 45 | |
---|
[3497] | 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 | |
---|
[1168] | 63 | (defvar *features* |
---|
| 64 | '(:armedbear)) |
---|
| 65 | |
---|
| 66 | |
---|
[2867] | 67 | (defun make-package (package-name &key nicknames use) |
---|
| 68 | (sys::%make-package package-name nicknames use)) |
---|
| 69 | |
---|
| 70 | |
---|
[1168] | 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 | |
---|
[3431] | 103 | (sys::%load "macros.lisp") |
---|
| 104 | (sys::%load "fixme.lisp") |
---|
| 105 | (sys::%load "destructuring-bind.lisp") |
---|
| 106 | (sys::%load "arrays.lisp") |
---|
| 107 | (sys::%load "compiler.lisp") |
---|
| 108 | (sys::%load "list.lisp") |
---|
| 109 | (sys::%load "sequences.lisp") |
---|
| 110 | (sys::%load "error.lisp") |
---|
| 111 | (sys::%load "defpackage.lisp") |
---|
[4207] | 112 | (sys::%load "define-modify-macro.lisp") |
---|
[4195] | 113 | |
---|
[4191] | 114 | (defpackage "JVM" (:use "COMMON-LISP" "EXTENSIONS")) |
---|
[882] | 115 | |
---|
[2961] | 116 | ;;; PROVIDE, REQUIRE (from SBCL) |
---|
| 117 | (defun provide (module-name) |
---|
| 118 | (pushnew (string module-name) *modules* :test #'string=) |
---|
| 119 | t) |
---|
| 120 | |
---|
| 121 | (defun require (module-name &optional pathnames) |
---|
| 122 | (finish-output) |
---|
| 123 | (unless (member (string module-name) *modules* :test #'string=) |
---|
| 124 | (let ((saved-modules (copy-list *modules*))) |
---|
| 125 | (cond (pathnames |
---|
| 126 | (unless (listp pathnames) (setf pathnames (list pathnames))) |
---|
| 127 | (dolist (x pathnames) |
---|
| 128 | (load x))) |
---|
| 129 | (t |
---|
[3431] | 130 | (sys::%load (concatenate 'string (string-downcase (string module-name)) |
---|
| 131 | ".lisp")))) |
---|
[2961] | 132 | (set-difference *modules* saved-modules)))) |
---|
| 133 | |
---|
[1142] | 134 | (defun read-from-string (string &optional eof-error-p eof-value |
---|
| 135 | &key (start 0) end preserve-whitespace) |
---|
[3513] | 136 | (sys::%read-from-string string eof-error-p eof-value start end preserve-whitespace)) |
---|
[1142] | 137 | |
---|
[4180] | 138 | (defconstant lambda-list-keywords |
---|
| 139 | '(&optional &rest &key &aux &body &whole &allow-other-keys &environment)) |
---|
| 140 | |
---|
[942] | 141 | (defconstant call-arguments-limit 50) |
---|
[950] | 142 | |
---|
| 143 | (defconstant lambda-parameters-limit 50) |
---|
[1028] | 144 | |
---|
| 145 | (defconstant multiple-values-limit 20) |
---|
[1056] | 146 | |
---|
[3312] | 147 | (defconstant char-code-limit 128) |
---|
[1056] | 148 | |
---|
[1910] | 149 | (defconstant internal-time-units-per-second 1000) |
---|
[1093] | 150 | |
---|
[3679] | 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) |
---|
[1910] | 167 | |
---|
[3679] | 168 | |
---|
[2448] | 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 | |
---|
[2504] | 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 | |
---|
[3502] | 217 | (defmacro case (keyform &rest clauses) |
---|
[2504] | 218 | (case-expand 'case 'eql keyform clauses)) |
---|
| 219 | |
---|
| 220 | |
---|
[3502] | 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 | |
---|
[3240] | 248 | ;;; TYPECASE (from CLISP) |
---|
[2913] | 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 | |
---|
[3484] | 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 | |
---|
[3503] | 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 | |
---|
[3151] | 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 | |
---|
[2842] | 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 | |
---|
[3592] | 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 | |
---|
[3240] | 344 | ;;; DOLIST (from CMUCL) |
---|
[3135] | 345 | |
---|
[3240] | 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) |
---|
[4154] | 355 | (sys::parse-body body nil nil) |
---|
[3240] | 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)))))) |
---|
[3135] | 369 | |
---|
[3240] | 370 | |
---|
[3319] | 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)))) |
---|
[3505] | 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 |
---|
[4220] | 403 | '(format s "@ #x~A" (sys::hashcode-to-string obj))) |
---|
[3505] | 404 | (format s ">") |
---|
| 405 | nil)) |
---|
[3524] | 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))) |
---|