/
init500.lisp
5821 lines (5821 loc) · 207 KB
/
init500.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(funcall #'(setf iref)
#'(lambda (object) (= (ldb (cons 2 0) (ival object)) 1))
'consp 5)
(funcall #'(setf iref)
#'(lambda (new-definition function-name)
(if (consp function-name)
(funcall #'(setf iref) new-definition
(car (cdr function-name)) 6)
(progn
(funcall #'(setf iref) new-definition function-name 5)
(funcall #'(setf iref)
(dpb 0 '(1 . 1) (iref function-name 8))
function-name 8)))
new-definition)
'fdefinition 6)
(funcall #'(setf iref)
#'(lambda (new-function symbol &optional environment)
(funcall #'(setf iref) new-function symbol 5)
(funcall #'(setf iref) (dpb 1 '(1 . 1) (iref symbol 8))
symbol 8)
new-function)
'macro-function 6)
(funcall #'(setf macro-function)
#'(lambda (name lambda-list &rest body)
(list 'progn
(list 'funcall '#'(setf macro-function)
(list 'function
(cons 'lambda (cons lambda-list body)))
(list 'quote name))
(list 'quote name)))
'defmacro)
(defmacro defun (name lambda-list &rest body)
(list 'progn
(list 'funcall '#'(setf fdefinition)
(list 'function
(list 'lambda lambda-list
(cons 'block (cons (if (consp name)
(car (cdr name))
name)
body))))
(list 'quote name))
(list 'quote name)))
(defmacro setf (place new-value)
(if (consp place)
(cons 'funcall (cons (list 'function (list 'setf (car place)))
(cons new-value (cdr place))))
(list 'setq place new-value)))
(defun append (&rest lists)
(if (cdr lists)
(let ((list (car lists))
(result nil)
(end nil))
(if list
(tagbody
start
(if list
(progn
(setf end (if end
(setf (cdr end) (list (car list)))
(setf result (list (car list)))))
(setf list (cdr list))
(go start)))
(setf (cdr end) (apply #'append (cdr lists)))
(return-from append result))
(apply #'append (cdr lists))))
(car lists)))
(defun backquote-expand (list level)
(if (consp list)
(if (eq 'backquote (car list))
(list 'list ''backquote
(backquote-expand (car (cdr list)) (+ level 1)))
(if (eq 'unquote (car list))
(if (= level 0)
(car (cdr list))
(list 'list ''unquote
(backquote-expand (car (cdr list)) (- level 1))))
(if (eq 'unquote-splicing (car list))
(if (= level 0)
(values (car (cdr list)) t)
(list 'list ''unquote-splicing
(backquote-expand (car (cdr list)) (- level 1))))
(labels ((collect (list)
(if (consp list)
(cons (multiple-value-call
#'(lambda (value
&optional splicingp)
(if splicingp
value
(list 'list value)))
(backquote-expand (car list) level))
(collect (cdr list)))
(list (list 'quote list)))))
(cons 'append (collect list))))))
(list 'quote list)))
(defmacro backquote (form)
(backquote-expand form 0))
(defun macro-function (symbol &optional environment)
"(dolist (binding environment)
(when (and (consp (car binding))
(= (floor (ival (cdar binding)) 16) 1)
(eq (caar binding) symbol))
(return-from macro-function
(when (= (ldb '(1 . 4) (ival (cdr binding))) 1)
(cdr binding)))))"
(if (= (ldb '(1 . 1) (iref symbol 8)) 1)
(iref symbol 5)))
(defun macroexpand-1 (form &optional env)
(if (consp form)
(let ((definition (macro-function (car form) env)))
(if definition
(values (apply definition (cdr form)) t)
(values form nil)))
(if (and form (symbolp form) (= (ldb '(1 . 0) (iref form 8)) 1))
(values (iref form 4) t)
(values form nil))))
(defun macroexpand (form &optional env)
(multiple-value-bind (form expanded-p)
(macroexpand-1 form env)
(if expanded-p
(tagbody
start
(multiple-value-bind (expansion expanded-p)
(macroexpand-1 form env)
(if expanded-p
(progn
(setq form expansion)
(go start))
(return-from macroexpand (values expansion t)))))
(values form nil))))
(defmacro define-symbol-macro (symbol expansion)
`(progn
(setf (iref ',symbol 4) ',expansion)
(setf (iref ',symbol 8) (dpb 1 (cons 1 0) (iref ',symbol 8)))
',symbol))
(defun special-operator-p (symbol)
(member symbol '(block catch eval-when flet function go if labels let let*
load-time-value locally macrolet multiple-value-call
multiple-value-prog1 progn progv quote return-from setq
symbol-macrolet tagbody the throw unwind-protect)))
(defun constantp (form &optional environment)
(not (or (and (symbolp form)
(zerop (ldb '(1 . 4) (iref form 8))))
(and (consp form)
(not (eq (car form) 'quote))))))
(defun null (object) (if object nil t))
(defun not (object) (if object nil t))
(defun length (sequence)
(let ((tag (ldb '(2 . 0) (ival sequence))))
(if (= tag 0)
0
(if (= tag 1)
(let ((i 0)) (dolist (elem sequence i) (setf i (+ 1 i))))
(if (= tag 2)
(let ((subtag (iref sequence 1)))
(if (= subtag 3)
(/ (ival (iref sequence 0)) 256)
(if (= subtag 4)
(let ((dimensions/fill (iref sequence 3)))
(if (consp dimensions/fill)
(error "not a sequence")
(or dimensions/fill
(length (iref sequence 4)))))
0)))
(let ((subtag (jref sequence 1)))
(if (= subtag 20)
(- (/ (jref sequence 0) 64) 4)
(if (= subtag 116)
(- (/ (jref sequence 0) 8) 31)
(error "not a sequence")))))))))
(defun mod (x y) (multiple-value-call #'(lambda (q r) r) (floor x y)))
(defun functionp (object) (eq (type-of object) 'function))
(defun coerce (object result-type)
(if (typep object result-type)
object
(case result-type
((t) object)
(character (character object))
(function (if (and (consp object) (eq (car object) 'lambda))
(eval (list 'function object))
(if (fboundp object)
(fdefinition object))
(error 'type-error :datum object
:expected-type result-type)))
(t (error 'type-error :datum object :expected-type result-type)))))
(defun ensure-type (name expander)
(let ((cons (assoc name *type-expanders*)))
(if cons
(setf (cdr cons) expander)
(push (cons name expander) *type-expanders*))
name))
(defmacro deftype (name lambda-list &rest forms)
`(ensure-type ',name #'(lambda ,lambda-list (block ,name ,@forms))))
(defun *= (cons number)
(or (not cons) (eq (car cons) '*) (= (car cons) number)))
(defun typep (object type-specifier &optional environment)
(let ((tag (ldb '(2 . 0) (ival object))))
(case type-specifier
((nil extended-char) nil)
((t *) t)
(null (not object))
(list (or (not object) (= tag 1)))
(fixnum (and (= tag 0) (= (ldb '(5 . 0) (ival object)) 16)))
(package (and (= tag 2) (= (iref object 1) 5)))
(symbol (or (not object) (and (= tag 2) (= (iref object 1) 0))))
((character base-char)
(and (= tag 0) (= (ldb '(5 . 0) (ival object)) 24)))
(standard-char (and (= tag 0)
(= (ldb '(5 . 0) (ival object)) 24)
(let ((code (char-code object)))
(or (= code 10)
(< 31 code 127)))))
(bit (member object '(0 1)))
(t (setq type-specifier (designator-list type-specifier))
(case (car type-specifier)
(cons (and (= tag 1)
(or (not (cdr type-specifier))
(typep (car object) (cadr type-specifier)))
(or (not (cddr type-specifier))
(typep (car object) (caddr type-specifier)))))
((string base-string) (and (stringp object)
(*= (cdr type-specifier)
(length object))))
(satisfies (funcall (cadr type-specifier) object))
(member (member object (cdr type-specifier)))
(not (not (typep object (cadr type-specifier))))
(and (every #'(lambda (spec) (typep object spec))
(cdr type-specifier)))
(or (some #'(lambda (spec) (typep object spec))
(cdr type-specifier)))
(eql (eql object (cadr type-specifier)))
(t (when (= tag 2)
(let ((class (iref object 1)))
(when (= (ldb '(2 . 0) (ival class)) 2)
(member (car type-specifier)
(mapcar #'class-name
(class-precedence-list class))))))))))))
(defun fboundp (function-name)
(if (consp function-name)
(iboundp (cadr function-name) 6)
(iboundp function-name 5)))
(defun fdefinition (function-name)
(if (consp function-name)
(if (iboundp (cadr function-name) 6)
(iref (cadr function-name) 6)
(error 'undefined-error :name function-name))
(if (iboundp function-name 5)
(iref function-name 5)
(error 'undefined-error :name function-name))))
(defun function-lambda-expression (function)
(values (list* 'lambda (iref function 4) (iref function 5))
(iref function 3)
(iref function 6)))
(defmacro defconstant (name initial-value &optional documentation)
`(progn
(setf (iref ',name 4) ,initial-value)
(setf (iref ',name 8) (dpb 1 '(1 . 4) (iref ',name 8)))
',name))
(defmacro defparameter (name initial-value &optional documentation)
`(progn
(setf (iref ',name 4) ,initial-value)
(setf (iref ',name 8) (dpb 1 (cons 1 2) (iref ',name 8)))
',name))
(defparameter *type-expanders* nil)
(defconstant call-arguments-limit 65536)
(defconstant lambda-parameters-limit 65536)
(defconstant multiple-values-limit 65536)
(defconstant lambda-list-keywords
'(&allow-other-keys &aux &body &environment &key &optional &rest &whole))
(defmacro defvar (name &rest rest)
`(progn
(unless (or (iboundp ',name 4) ,(not rest))
(setf (iref ',name 4) ,(car rest)))
(setf (iref ',name 8) (dpb 1 (cons 1 2) (iref ',name 8)))
',name))
(defmacro psetq (&rest rest)
(let ((inits nil)
(sets nil)
(list rest))
(tagbody
start
(when (cddr list)
(push (list (gensym) (cadr list)) inits)
(setq list (cddr list))
(go start)))
(setq list inits)
(tagbody
start
(when (cddr rest)
(push (caar list) sets)
(push (car rest) sets)
(setq list (cdr list))
(setq rest (cddr rest))
(go start)))
`(let ,(reverse inits)
(setq ,@sets ,@rest))))
(defmacro return (&optional result)
`(return-from nil ,result))
(defmacro when (test-form &rest forms)
`(if ,test-form (progn ,@forms)))
(defmacro unless (test-form &rest forms)
`(if (not ,test-form) (progn ,@forms)))
(defmacro and (&rest forms)
(if forms
(if (cdr forms)
`(when ,(car forms) (and ,@(cdr forms)))
(car forms))
`t))
(defmacro or (&rest forms)
(if forms
(if (cdr forms)
(let ((temp (gensym)))
`(let ((,temp ,(car forms)))
(if ,temp
,temp
(or ,@(cdr forms)))))
(car forms))
`nil))
(defmacro cond (&rest clauses)
(when clauses
(if (cdar clauses)
`(if ,(caar clauses)
(progn ,@(cdar clauses))
(cond ,@(cdr clauses)))
`(or ,(caar clauses)
(cond ,@(cdr clauses))))))
(defmacro case (keyform &rest clauses)
(let ((temp (gensym)))
(labels ((recur (clauses)
(when clauses
(if (member (caar clauses) '(otherwise t))
`(progn ,@(cdar clauses))
`(if ,(if (listp (caar clauses))
`(member ,temp ',(caar clauses))
`(eql ,temp ',(caar clauses)))
(progn ,@(cdar clauses))
,(recur (cdr clauses)))))))
`(let ((,temp ,keyform))
,(recur clauses)))))
(defun type-of (object)
(case (ldb (cons 2 0) (ival object))
(0 (if (eq object nil)
'null
(if (= (ldb (cons 2 3) (ival object)) 2)
'fixnum
'character)))
(1 'cons)
(2 (case (iref object 1)
(0 'symbol)
(3 'simple-vector)
(4 'array)
(5 'package)
(6 'function)
(t (class-name (iref object 1)))))
(3 (case (jref object 1)
(20 'simple-string)
(84 'double)
(116 'simple-bit-vector)
(t 'file-stream)))))
(defmacro ecase (keyform &rest clauses)
(let ((temp (gensym)))
`(let ((,temp ,keyform))
(case ,temp ,@clauses
(error 'type-error :datum ,temp
:expected-type `(member ,@(mapcan #'(lambda (x)
(if (listp (car x))
(car x)
(list (car x))))
clauses)))))))
(defmacro multiple-value-bind (vars values-form &rest forms)
`(multiple-value-call #'(lambda (&optional ,@vars &rest ,(gensym))
,@forms)
,values-form))
(defmacro multiple-value-list (form)
`(multiple-value-call #'list ,form))
(defun values-list (list)
(apply #'values list))
(defmacro nth-value (n form)
`(nth ,n (multiple-value-list ,form)))
(defmacro prog (inits &rest forms)
`(block nil
(let ,inits
(tagbody ,@forms))))
(defmacro prog* (inits &rest forms)
`(block nil
(let* ,inits
(tagbody ,@forms))))
(defmacro prog1 (first-form &rest forms)
(let ((temp (gensym)))
`(let ((,temp ,first-form))
,@forms
,temp)))
(defmacro prog2 (first-form second-form &rest forms)
(let ((temp (gensym)))
`(progn
,first-form
(let ((,temp ,second-form))
,@forms
,temp))))
(defun eql (a b)
(or (eq a b)
(and (= (ldb '(2 . 0) (ival a)) 3)
(= (ldb '(2 . 0) (ival b)) 3)
(= (jref a 1) 84)
(= (jref b 1) 84)
(= a b))))
(defun equal (a b)
(or (eql a b)
(cond
((not a) nil)
((consp a) (and (consp b)
(equal (car a) (car b))
(equal (cdr a) (cdr b))))
((stringp a) (and (stringp b)
(string= a b)))
((bit-vector-p a) (and (bit-vector-p b)
(= (length a) (length b))
(dotimes (i (length a) t)
(when (/= (aref a i) (aref b i))
(return))))))))
(defun equalp (a b)
(or (eql a b)
(cond
((not a) nil)
((characterp a) (and (characterp b)
(char-equal a b)))
((consp a) (and (consp b)
(equalp (car a) (car b))
(equalp (cdr a) (cdr b))))
((arrayp a) (and (arrayp b)
(equal (array-dimensions a) (array-dimensions b))
(dotimes (i (apply #'* (array-dimensions a)) t)
(unless (equalp (row-major-aref a i)
(row-major-aref b i))
(return)))))
((= (ldb '(2 . 0) (ival a)) 2)
(and (= (ldb '(2 . 0) (ival b)) 2)
(eq (iref a 1) (iref b 1))
(= (ldb '(2 . 0) (ival (iref a 1))) 2)
(dotimes (i (iref a 0) t)
(unless (equalp (iref a (+ 2 i)) (iref b (+ 2 i)))
(return))))))))
(defun identity (object) object)
(defun complement (function)
#'(lambda (&rest rest) (not (apply function rest))))
(defun constantly (value) #'(lambda (&rest rest) value))
(defmacro do (vars (end-test-form &rest result-forms) &rest forms)
(let ((start (gensym))
(inits nil)
(steps nil))
`(block nil
(let ,(dolist (var vars (reverse inits))
(push (if (consp var)
(list (car var) (cadr var))
(list var)) inits))
(tagbody
,start
(if ,end-test-form (return (progn ,@result-forms)))
,@forms
,@(dolist (var vars (when steps `((psetq ,@(reverse steps)))))
(when (and (consp var) (cddr var))
(push (car var) steps)
(push (caddr var) steps)))
(go ,start))))))
(defmacro do* (vars (end-test-form &rest result-forms) &rest forms)
(let ((start (gensym))
(inits nil)
(steps nil))
`(block nil
(let* ,(dolist (var vars (reverse inits))
(push (if (consp var)
(list (car var) (cadr var))
(list var)) inits))
(tagbody
,start
(if ,end-test-form (return (progn ,@result-forms)))
,@forms
,@(dolist (var vars (when steps `((setq ,@(reverse steps)))))
(when (and (consp var) (cddr var))
(push (car var) steps)
(push (caddr var) steps)))
(go ,start))))))
(defmacro dotimes ((var count-form &optional result-form) &rest forms)
(let ((start (gensym))
(count (gensym)))
`(block nil
(let ((,var 0)
(,count ,count-form))
(tagbody
,start
(when (< ,var ,count)
,@forms
(incf ,var)
(go ,start)))
,result-form))))
(defmacro dolist ((var list-form &optional result-form) &rest forms)
(let ((start (gensym))
(list (gensym)))
`(block nil
(let ((,list ,list-form)
(,var nil))
(tagbody
,start
(unless ,list
(setf ,var nil)
(return-from nil ,result-form))
(setf ,var (car ,list))
(setf ,list (cdr ,list))
,@forms
(go ,start))))))
(defmacro check-type (place typespec &optional string)
`(tagbody
start
(unless (typep ,place ',typespec)
(restart-case
(error 'type-error :datum ,place :expected-type ',typespec)
(store-value (value)
(setf ,place value)))
(go start))))
(defun designator-condition (default-type datum arguments)
(if (symbolp datum)
(apply #'make-condition datum arguments)
(if (or (stringp datum) (functionp datum))
(make-condition default-type
:format-control datum
:format-arguments arguments)
datum)))
(defun error (datum &rest arguments)
(let ((condition (designator-condition 'simple-error datum arguments)))
(when (typep condition *break-on-signals*)
(invoke-debugger condition))
(invoke-handler condition)
(invoke-debugger condition)))
(defun cerror (continue-format-control datum &rest arguments)
`(with-simple-restart (continue continue-format-control)
(apply #'error datum arguments)))
(defun signal (datum &rest arguments)
(let ((condition (designator-condition 'simple-condition datum arguments)))
(when (typep condition *break-on-signals*)
(invoke-debugger condition))
(invoke-handler condition)
nil))
(defun warn (datum &rest arguments)
(restart-case
(let ((warning (if (symbolp datum)
(apply #'make-condition 'warning datum arguments)
datum)))
(signal warning)
(print-object warning *error-output*))
(muffle-warning () nil))
nil)
(defun show-frame (frame index)
(let* ((length (fref (- frame 2)))
(fn (fref (- frame length 3))))
(when (and (= (ldb '(2 . 0) (ival fn)) 2) (= (iref fn 1) 6))
(format *debug-io* "~A: (~A" index (iref fn 6))
(dotimes (i length)
(format *debug-io* " ~A" (fref (+ i (- frame length 2)))))
(format *debug-io* ")~%"))))
(defun next-frame (frame)
(- frame (fref (- frame 2)) 3))
(defun next-function-frame (frame)
(do* ((f (next-frame frame) (next-frame f)))
((or (< f 6)
(let ((fn (fref (- f (fref (- f 2)) 3))))
(and (= (ldb '(2 . 0) (ival fn)) 2) (= (iref fn 1) 6))))
(and (> f 5) f))))
(defun invoke-debugger (condition)
(let ((debugger-hook *debugger-hook*)
(*debugger-hook* nil))
(when debugger-hook
(funcall debugger-hook condition debugger-hook))
(format *debug-io* "Entering debugger.~%")
(princ condition *debug-io*)
(terpri *debug-io*)
(let ((restarts (compute-restarts condition))
(stack (makef))
(frame-depth 0)
(active-frame nil))
(let ((count 0))
(dolist (restart restarts)
(format *debug-io* "~A: " count)
(princ restart *debug-io*)
(terpri *debug-io*)
(incf count)))
(setq active-frame (next-function-frame (- stack 20)))
(show-frame active-frame 0)
(tagbody
start
(format *debug-io* ";~A> " frame-depth)
(let ((form (read)))
(case form
(:help (format *debug-io* "Type :help to get help.~%")
(format *debug-io* "Type :continue <index> to invoke the indexed restart.~%"))
(:back (do ((frame (next-function-frame (- stack 20))
(next-function-frame frame))
(index 0 (+ 1 index)))
((not frame))
(show-frame frame index)))
(:up (if (plusp frame-depth)
(progn
(decf frame-depth)
(do ((frame (next-function-frame (- stack 20))
(next-function-frame frame))
(index 0 (+ 1 index)))
((= index frame-depth) (setq active-frame frame)))
(show-frame active-frame frame-depth))
(format *debug-io* "Top of stack.~%")))
(:down (let ((frame (next-function-frame active-frame)))
(if frame
(progn
(incf frame-depth)
(setq active-frame frame)
(show-frame active-frame frame-depth))
(format *debug-io* "Bottom of stack.~%"))))
(:locals (do ((env (fref (- active-frame 1)) (cdr env)))
((not env))
(when (symbolp (caar env))
(format *debug-io* "~A~%" (caar env)))))
(:continue (let ((index (read)))
(invoke-restart-interactively (nth index restarts))))
(t (let ((values (multiple-value-list
(eval form (fref (- active-frame 1)))))
(count 0))
(if values
(dolist (value values)
(format *debug-io* ";~A: ~S~%" count value)
(incf count))
(format *debug-io* ";No values.~%")))))
(go start))))))
(defun break (&optional format-control &rest format-arguments)
(with-simple-restart (continue "Return from BREAK.")
(let ((*debugger-hook* nil))
(invoke-debugger (make-condition 'simple-condition
:format-control format-control
:format-arguments format-arguments))))
nil)
(defparameter *debugger-hook* nil)
(defparameter *break-on-signals* nil)
(defparameter *handlers* nil)
(defun invoke-handler (condition)
(dolist (handler *handlers*)
(when (typep condition (car handler))
(setq *handlers* (caddr handler))
(funcall (cadr handler) condition))))
(defmacro handler-bind (bindings &rest forms)
(let ((form '*handlers*)
(handlers (gensym)))
(dolist (binding (reverse bindings))
(setq form
`(cons (list ',(car binding) ,(cadr binding) ',handlers) ,form)))
`(let ((handlers *handlers*)
(*handlers* ,form))
,@forms)))
(defmacro handler-case (expression &rest clauses)
(let ((tag (gensym))
(bindings nil))
`(handler-bind
,(dolist (clause clauses (reverse bindings))
(let ((typespec (car clause))
(var-list (cadr clause))
(forms (cddr clauses)))
(push `(typespec #'(lambda (,(if var-list (car var-list) (gensym)))
(return-from tag (progn ,@forms))))
bindings)))
,expression)))
(defmacro ignore-errors (&rest forms)
`(handler-case (progn ,@forms)
(error (condition) (values nil condition))))
(defparameter *restarts* nil)
(defun compute-restarts (&optional condition)
"FIXME restarts associated with conditions"
(if condition
*restarts*
*restarts*))
(defun find-restart (identifier &optional condition)
(dolist (restart *restarts*)
(when (eq restart identifier)
(return restart))
(when (eq (restart-name restart) identifier)
(return restart))))
(defun designator-restart (designator)
(if (restartp designator)
designator
(dolist (restart *restarts* (error 'type-error :datum designator
:expected-type 'restart))
(when (eq (restart-name restart) designator)
(return restart)))))
(defun invoke-restart (restart &rest arguments)
(setq restart (designator-restart restart))
(apply (restart-function restart) arguments))
(defun invoke-restart-interactively (restart)
(setq restart (designator-restart restart))
(apply (restart-function restart)
(funcall (restart-interactive-function restart))))
(defmacro restart-bind (restart-bindings &rest forms)
(let ((form '*restarts*))
(dolist (binding (reverse restart-bindings))
(setq form
`(cons (make-restart ',(car binding) ,@(cdr binding)) ,form)))
`(let ((*restarts* ,form))
,@forms)))
(defmacro restart-case (restartable-form &rest clauses)
(let ((catch-tag (gensym))
(bindings nil))
`(catch ',catch-tag
(restart-bind
,(dolist (clause clauses (reverse bindings))
(let ((name (car clause))
(lambda-list (cadr clause))
(rest (cddr clause))
(interactive '#'(lambda () nil))
(report '#'(lambda (stream)
(format stream "~A" (car clause))))
(test '#'(lambda (condition) t)))
(tagbody
start
(when (member (car rest) '(:interactive :report :test))
(let ((value (cadr rest)))
(case (car rest)
(:interactive (setq interactive `(function ,value)))
(:report (setq report
(if (stringp value)
`#'(lambda (stream)
(write-string ,value stream))
`(function ,value))))
(:test (setq test `(function ,value)))))
(setq rest (cddr rest))
(go start)))
(push `(,(car clause)
#'(lambda ,(cadr clause)
(throw ',catch-tag (progn ,@rest)))
:interactive-function ,interactive
:report-function ,report
:test-function ,test)
bindings)))
,restartable-form))))
(defmacro with-simple-restart ((name format-control &rest format-arguments)
&rest forms)
(let ((tag (gensym)))
`(block ,tag
(restart-bind
((,name
#'(lambda () (return-from ,tag (values nil t)))
:interactive-function #'(lambda () nil)
:report-function #'(lambda (stream)
(apply #'format stream ',format-control
',format-arguments))
:test-function #'(lambda () t)))
,@forms))))
(defun abort (&optional condition)
(invoke-restart (find-restart 'abort condition))
(error 'control-error))
(defun continue (&optional condition)
(invoke-restart (find-restart 'continue condition)))
(defun muffle-warning (&optional condition)
(invoke-restart (find-restart 'muffle-warning condition))
(error 'control-error))
(defun store-value (value &optional condition)
(invoke-restart (find-restart 'store-value condition) value))
(defun use-value (value &optional condition)
(invoke-restart (find-restart 'use-value condition) value))
(defun integer-string (integer &optional (radix 10))
(if (= integer 0)
"0"
(labels ((recur (i l)
(if (= i 0)
l
(multiple-value-bind (ni r)
(floor i radix)
(recur ni (cons (code-char (+ (if (< r 10) 48 55) r))
l))))))
(apply #'string (if (< 0 integer)
(recur integer nil)
(cons (code-char 45) (recur (- integer) nil)))))))
(defun designator-symbol (designator)
(if (symbolp designator)
designator
(find-symbol designator)))
(defun symbolp (object) (or (null object) (eq (type-of object) 'symbol)))
(defun keywordp (object)
(and (symbolp object)
(string= (package-name (symbol-package object)) "KEYwORD")))
(defun make-symbol (name)
(let ((symbol (makei 9 0 name nil nil nil nil (- 1) 0)))
(imakunbound symbol 4)
(imakunbound symbol 5)
(imakunbound symbol 6)
symbol))
(defun copy-symbol (symbol &optional copy-properties)
(let ((new-symbol (make-symbol (iref symbol 2))))
(cond
(copy-properties
(setf (iref new-symbol 4) (iref symbol 4))
(setf (iref new-symbol 5) (iref symbol 5))
(setf (iref new-symbol 6) (iref symbol 6))
(setf (iref new-symbol 10) (iref symbol 10)))
(t
(imakunbound new-symbol 4)
(imakunbound new-symbol 5)
(imakunbound new-symbol 6)))
new-symbol))
(defun fixnump (object)
(= (ldb '(5 . 0) (ival object)) 16))
(defvar *gensym-counter* 0)
(defun gen-sym (&optional x)
(let ((prefix (if (stringp x) x "G"))
(suffix (if (fixnump x)
x
(let ((x *gensym-counter*))
(setf *gensym-counter* (+ 1 *gensym-counter*))))))
(make-symbol (conc-string prefix (integer-string suffix)))))
(let ((gentemp-counter 0))
(defun gentemp (&optional (prefix "T") (package *package*))
(setf gentemp-counter (+ 1 gentemp-counter))
(intern (conc-string prefix (integer-string gentemp-counter))
package)))
(defun symbol-function (symbol)
(if (iboundp symbol 5)
(iref symbol 5)
(error 'undefined-function :name symbol)))
(defun (setf symbol-function) (new-contents symbol)
(setf (iref symbol 5) new-contents))
(defun symbol-name (symbol) (iref symbol 2))
(defun symbol-package (symbol) (iref symbol 9))
(defun symbol-plist (symbol) (iref symbol 10))
(defun (setf symbol-plist) (new-plist symbol)
(setf (iref symbol 10) new-plist))
(defun symbol-value (symbol)
(if (iboundp symbol 4)
(iref symbol 4)
(error 'unbound-variable :name symbol)))
(defun (setf symbol-value) (new-value symbol) (setf (iref symbol 4) new-value))
(defun get (symbol indicator &optional default)
(getf (symbol-plist symbol) indicator default))
(defun (setf get) (new-value symbol indicator &optional default)
(setf (getf (symbol-plist symbol) indicator default) new-value))
(defun remprop (symbol indicator) (remf (symbol-plist symbol) indicator))
(defun boundp (symbol) (iboundp symbol 4))
(defun makunbound (symbol) (imakunbound symbol 4))
(defun set (symbol value) (setf (symbol-value symbol) value))
(defun designator-string (designator)
(if (stringp designator)
designator
(if (characterp designator)
(string designator)
(symbol-name designator))))
(defvar *package* (car (cdr *packages*)))
(defun find-package (name)
(if (packagep name)
name
(let ((string (designator-string name)))
(dolist (package *packages*)
(dolist (package-name (iref package 2))
(when (string= package-name string)
(return-from find-package package)))))))
(defun export (symbols &optional (package *package*))
(setq package (find-package package))
(dolist (symbol (designator-list symbols))
(setf symbol (designator-symbol symbol))
(dolist (using-package (iref package 7))
(when (find-symbol (symbol-name symbol) using-package)
(cerror 'package-error :package using-package)))
(unless (atom (package-get (iref package 3) (symbol-name symbol)))
(unless (atom (package-get (iref package 4) (symbol-name symbol)))
(cerror 'package-error :package package))
(package-rem (iref package 4) symbol)
(package-put (iref package 3) symbol))))
(defun package-rehash (old-vector new-vector)
(let ((old-length (length old-vector))
(new-length (length new-vector))
(i 0))
(tagbody
start
(when (< i old-length)
(dolist (symbol (iref old-vector (+ 2 i)))
(push symbol (iref new-vector (+ 2 (mod (hash (symbol-name symbol))
new-length)))))
(incf i)
(go start)))
new-vector))
(defun package-get (vector string)
(dolist (symbol (iref vector (+ 2 (mod (hash string) (length vector))))
'(nil))
(when (string= (symbol-name symbol) string)
(return symbol))))
(defun package-put (vector symbol)
(push symbol (iref vector (+ 2 (mod (hash (symbol-name symbol))
(length vector))))))
(defun package-rem (vector string)
(let ((index (+ 2 (mod (hash string) (length vector)))))
(setf (iref vector index)
(delete string (iref vector index) :key #'symbol-name))))
(defun find-symbol (string &optional (package *package*))
(setq package (find-package package))
(unless package
(error "Package does not exist."))
(if (and (string= "NIL" string) (eq (find-package "CL") package))
(values nil :external)
(let ((symbol (package-get (iref package 4) string)))
(if (atom symbol)
(values symbol :internal)
(let ((symbol (package-get (iref package 3) string)))
(if (atom symbol)
(values symbol :external)
(dolist (used-package (package-use-list package)
(values nil nil))
(let ((symbol (package-get (iref used-package 3) string)))
(when (atom symbol)
(values symbol :inherited))))))))))
(defun find-all-symbols (string)
(let ((symbols nil))
(dolist (package *packages*)
(let ((symbol (package-get (iref package 4) string)))
(when (atom symbol)
(push symbol symbols)))
(let ((symbol (package-get (iref package 3) string)))
(when (atom symbol)
(push symbol symbols))))
symbols))
(defun import (symbols &optional (package *package*))
(setq package (find-package package))
(dolist (symbol (designator-list symbols))
(multiple-value-bind (symbol status)
(find-symbol (symbol-name symbol) package)
(case status
(:inherited
(cerror 'package-error :package package))
((nil)
(package-put (iref package 4) symbol)
(unless (iref symbol 9)
(setf (iref symbol 9) package))))))
t)
(defun list-all-packages ()
(copy-list *packages*))
(defun rename-package (package new-name &optional new-nicknames)
(setq package (find-package package))
(when (packagep new-name) (setq new-name (package-name new-name)))
(setf new-nicknames (mapcar #'designator-string new-nicknames))
(setf (iref package 2) (cons new-name new-nicknames))
package)
(defun shadow (symbol-names &optional (package *package*))
(setq package (find-package package))
(dolist (symbol-name symbol-names)
(multiple-value-bind (symbol status)
(find-symbol symbol-name package)
(unless (member status '(:internal :external))
(setq symbol (make-symbol string))
(setf (iref symbol 9) package)
(package-put (iref package 4) symbol))
(pushnew symbol (iref package 5))))
t)
(defun shadowing-import (symbols &optional package)
(setq package (find-package package))
(dolist (symbol (designator-list symbols))
(package-rem (iref package 3) (symbol-name symbol))
(package-rem (iref package 4) (symbol-name symbol))
(package-put (iref package 4) symbol)
(push symbol (iref package 5)))
t)
(defun delete-package (package)
(setq package (find-package package))
(when (iref package 7)
(cerror 'package-error package))
(prog1
(package-name package)
(setf (iref package 2) nil)))
(defun make-package (package-name &key nicknames (use '("cl")))
(let ((all-names (cons package-name nicknames)))
(mapc #'(lambda (name)
(when (find-package name)
(cerror 'package-error :package name)))
all-names)
(let ((package (makei 6 5 all-names (make-array 1021) (make-array 1021) nil
(mapcar #'find-package use))))
(mapc #'(lambda (used-package)
(push package (iref (find-package used-package) 7)))
use)
(push package *packages*)
package)))
(defun make-package-iterator (package symbol-types)
(setq package (find-package package))
(list nil package symbol-types))
(defun package-iterate (iterator)
(unless (first iterator)
(setf (first iterator)
(case (pop (third iterator))
(:internal (iref (second iterator) 4))
(:external (iref (second iterator) 3))
(:inherited "FIXME")
((nil) (return-from package-iterate nil)))))
(pop (first iterator)))
(defmacro with-package-iterator ((name package-list-form &rest symbol-types)
&rest forms)
(let ((package (gensym))
(iterator (gensym)))
`(dolist (,package (designator-list ,package-list-form))
(let ((,iterator (make-package-iterator ,package ',symbol-types)))
(macrolet ((,name () (package-iterate ,iterator)))
,@forms)))))
(defun unexport (symbols &optional (package *package*))