-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCMLEVAL
2008 lines (1678 loc) · 99.2 KB
/
CMLEVAL
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
(DEFINE-FILE-INFO PACKAGE "INTERLISP" READTABLE "XCL" BASE 10)
(FILECREATED "15-Apr-2024 21:22:06" |{DSK}<home>larry>il>medley>sources>CMLEVAL.;7| 99203
:EDIT-BY "lmm"
:CHANGES-TO (VARS CMLEVALCOMS)
:PREVIOUS-DATE "15-Apr-2024 20:14:11" |{DSK}<home>larry>il>medley>sources>CMLEVAL.;5|)
(PRETTYCOMPRINT CMLEVALCOMS)
(RPAQQ CMLEVALCOMS
(
(* |;;;| "Common Lisp interpreter")
(COMS
(* |;;| "These really don't belong here")
(FUNCTIONS CL:EQUAL CL:EQUALP)
(* |;;|
"For the byte compiler: Optimize by constant fold and coerce to EQ where possible")
(PROP BYTEMACRO CL:EQUAL CL:EQUALP)
(PROP DOPVAL CL:EQUAL))
(COMS (FUNCTIONS \\REMOVE-DECLS)
(FUNCTIONS CL:SPECIAL-FORM-P))
(COMS (SPECIAL-FORMS INTERLISP)
(PROP DMACRO INTERLISP COMMON-LISP)
(FNS COMMON-LISP))
(COMS (ADDVARS (LAMBDASPLST CL:LAMBDA))
(FNS \\TRANSLATE-CL\:LAMBDA)
(VARIABLES *CHECK-ARGUMENT-COUNTS* *SPECIAL-BINDING-MARK*))
(VARIABLES CL:LAMBDA-LIST-KEYWORDS CL:CALL-ARGUMENTS-LIMIT CL:LAMBDA-PARAMETERS-LIMIT)
(STRUCTURES CLOSURE ENVIRONMENT)
(FUNCTIONS \\MAKE-CHILD-ENVIRONMENT)
(COMS (FNS CL:EVAL \\EVAL-INVOKE-LAMBDA \\INTERPRET-ARGUMENTS \\INTERPRETER-LAMBDA
CHECK-BINDABLE CHECK-KEYWORDS)
(FUNCTIONS ARG-REF)
(PROP DMACRO .COMPILER-SPREAD-ARGUMENTS.))
(FNS DECLARED-SPECIAL)
(COMS (* \;
"FUNCALL and APPLY, not quite same as Interlisp")
(FNS CL:FUNCALL CL:APPLY)
(PROP DMACRO CL:APPLY CL:FUNCALL))
(COMS (* \;
"COMPILER-LET needs to work differently compiled and interpreted")
(FNS CL:COMPILER-LET COMP.COMPILER-LET)
(PROP DMACRO CL:COMPILER-LET)
(SPECIAL-FORMS CL:COMPILER-LET))
(COMS (* \;
"Lexical function- and macro-binding forms: FLET, LABELS, and MACROLET.")
(SPECIAL-FORMS CL:MACROLET CL:FLET CL:LABELS))
(SPECIAL-FORMS QUOTE)
(COMS (SPECIAL-FORMS THE)
(PROP DMACRO THE))
(COMS (PROP DMACRO CL:EVAL-WHEN)
(FNS CL:EVAL-WHEN)
(SPECIAL-FORMS CL:EVAL-WHEN))
(COMS (SPECIAL-FORMS DECLARE)
(FUNCTIONS CL:LOCALLY))
(COMS (* \; "Interlisp version on LLINTERP")
(SPECIAL-FORMS PROGN)
(FNS \\EVAL-PROGN))
(COMS (* \;
"Confused because currently Interlisp special form, fixing MACRO-FUNCTION is complex")
(* \;
"The Interlisp function is on LLINTERP")
(SPECIAL-FORMS PROG1)
(FUNCTIONS PROG1))
(COMS (SPECIAL-FORMS LET* LET)
(PROP MACRO LET LET*)
(FNS \\LET*-RECURSION |\\LETtran|))
(COMS (SPECIAL-FORMS COND)
(FUNCTIONS COND))
(COMS (FNS CL:IF)
(SPECIAL-FORMS CL:IF)
(PROP DMACRO CL:IF))
(COMS (* \;
"Interlisp NLAMBDA definitions on LLINTERP")
(* \; "both special form and macro")
(FUNCTIONS AND OR)
(SPECIAL-FORMS AND OR))
(COMS (* \; "BLOCK and RETURN go together")
(FNS CL:BLOCK)
(PROP DMACRO CL:BLOCK)
(SPECIAL-FORMS CL:BLOCK)
(FUNCTIONS RETURN)
(FNS CL:RETURN-FROM)
(SPECIAL-FORMS CL:RETURN-FROM))
(COMS (* \; "IL and CL versions of FUNCTION.")
(FNS CL:FUNCTION)
(PROP DMACRO CL:FUNCTION)
(SPECIAL-FORMS CL:FUNCTION FUNCTION)
(FUNCTIONS CL:FUNCTIONP CL:COMPILED-FUNCTION-P))
(SPECIAL-FORMS CL:MULTIPLE-VALUE-CALL CL:MULTIPLE-VALUE-PROG1)
(FNS COMP.CL-EVAL)
(FUNCTIONS CL:EVALHOOK CL:APPLYHOOK)
(VARIABLES *EVALHOOK* *APPLYHOOK* CL::*SKIP-EVALHOOK* CL::*SKIP-APPLYHOOK*)
(COMS (* \; "CONSTANTS mechanism")
(FNS CL:CONSTANTP)
(SETFS CL:CONSTANTP)
(FUNCTIONS XCL::SET-CONSTANTP))
(COMS (* \;
"Interlisp SETQ for Common Lisp and vice versa")
(SPECIAL-FORMS CL:SETQ SETQ)
(PROP DMACRO CL:SETQ)
(* |;;|
"An nlambda definition for cl:setq so cmldeffer may use cl:setq will run in the init")
(FNS CL:SETQ)
(FUNCTIONS SETQ)
(FNS SET-SYMBOL)
(FUNCTIONS CL:PSETQ)
(FUNCTIONS SETQQ))
(COMS (SPECIAL-FORMS CL:CATCH CL:THROW CL:UNWIND-PROTECT)
(FNS CL:THROW CL:CATCH CL:UNWIND-PROTECT))
(COMS (FUNCTIONS PROG PROG*)
(SPECIAL-FORMS GO CL:TAGBODY)
(FNS CL:TAGBODY))
(COMS (* \; "for macro caching")
(FNS CACHEMACRO)
(VARIABLES *MACROEXPAND-HOOK*)
(VARS (*IN-COMPILER-LET* NIL)))
(COMS
(* |;;| "PROCLAIM and friends.")
(* |;;| "Needs to come first because DEFVARs put it out. With package code in the init, also need this here rather than CMLEVAL")
(FUNCTIONS CL:PROCLAIM)
(* \; "used by the codewalker, too")
(MACROS VARIABLE-GLOBALLY-SPECIAL-P VARIABLE-GLOBAL-P)
(FUNCTIONS XCL::DECL-SPECIFIER-P XCL::SET-DECL-SPECIFIER-P)
(FUNCTIONS XCL::GLOBALLY-NOTINLINE-P XCL::SET-GLOBALLY-NOTINLINE-P)
(SETFS XCL::DECL-SPECIFIER-P XCL::GLOBALLY-NOTINLINE-P)
(PROP PROPTYPE GLOBALLY-SPECIAL GLOBALVAR SI::DECLARATION-SPECIFIER
SI::GLOBALLY-NOTINLINE SPECIAL-FORM))
(PROP (FILETYPE MAKEFILE-ENVIRONMENT)
CMLEVAL)
(DECLARE\: EVAL@COMPILE DONTCOPY (OPTIMIZERS CL-EVAL-FN3-CALL))
(DECLARE\: DONTEVAL@LOAD DOEVAL@COMPILE DONTCOPY (LOCALVARS . T))
(DECLARE\: DONTEVAL@LOAD DOEVAL@COMPILE DONTCOPY COMPILERVARS
(ADDVARS (NLAMA CL:TAGBODY CL:UNWIND-PROTECT CL:CATCH CL:SETQ CL:BLOCK CL:EVAL-WHEN
CL:COMPILER-LET COMMON-LISP)
(NLAML CL:THROW CL:FUNCTION CL:RETURN-FROM CL:IF)
(LAMA CL:APPLY CL:FUNCALL)))))
(* |;;;| "Common Lisp interpreter")
(* |;;| "These really don't belong here")
(CL:DEFUN CL:EQUAL (CL::X CL::Y)
(CL:TYPECASE CL::X
(CL:SYMBOL (EQ CL::X CL::Y))
(CL:NUMBER (EQL CL::X CL::Y))
(CONS (AND (CL:CONSP CL::Y)
(CL:EQUAL (CAR CL::X)
(CAR CL::Y))
(CL:EQUAL (CDR CL::X)
(CDR CL::Y))))
(STRING (AND (CL:STRINGP CL::Y)
(CL:STRING= CL::X CL::Y)))
(CL:BIT-VECTOR (AND (CL:BIT-VECTOR-P CL::Y)
(LET ((CL::SX (CL:LENGTH CL::X)))
(AND (EQL CL::SX (CL:LENGTH CL::Y))
(CL:DOTIMES (CL::I CL::SX T)
(CL:IF (NOT (EQ (BIT CL::X CL::I)
(BIT CL::Y CL::I)))
(RETURN NIL)))))))
(PATHNAME (AND (CL:PATHNAMEP CL::Y)
(%PATHNAME-EQUAL CL::X CL::Y)))
(T (EQ CL::X CL::Y))))
(CL:DEFUN CL:EQUALP (CL::X CL::Y)
(CL:TYPECASE CL::X
(CL:SYMBOL (EQ CL::X CL::Y))
(CL:NUMBER (AND (CL:NUMBERP CL::Y)
(= CL::X CL::Y)))
(CONS (AND (CL:CONSP CL::Y)
(CL:EQUALP (CAR CL::X)
(CAR CL::Y))
(CL:EQUALP (CDR CL::X)
(CDR CL::Y))))
(CL:CHARACTER (AND (CL:CHARACTERP CL::Y)
(CL:CHAR-EQUAL CL::X CL::Y)))
(STRING (AND (CL:STRINGP CL::Y)
(STRING-EQUAL CL::X CL::Y)))
(PATHNAME (AND (CL:PATHNAMEP CL::Y)
(%PATHNAME-EQUAL CL::X CL::Y)))
(CL:VECTOR (AND (CL:VECTORP CL::Y)
(LET ((CL::SX (CL:LENGTH CL::X)))
(AND (EQL CL::SX (CL:LENGTH CL::Y))
(CL:DOTIMES (CL::I CL::SX T)
(CL:IF (NOT (CL:EQUALP (CL:AREF CL::X CL::I)
(CL:AREF CL::Y CL::I)))
(RETURN NIL)))))))
(CL:ARRAY (AND (CL:ARRAYP CL::Y)
(CL:EQUAL (CL:ARRAY-DIMENSIONS CL::X)
(CL:ARRAY-DIMENSIONS CL::Y))
(LET ((CL::FX (%FLATTEN-ARRAY CL::X))
(CL::FY (%FLATTEN-ARRAY CL::Y)))
(CL:DOTIMES (CL::I (CL:ARRAY-TOTAL-SIZE CL::X)
T)
(CL:IF (NOT (CL:EQUALP (CL:AREF CL::FX CL::I)
(CL:AREF CL::FY CL::I)))
(RETURN NIL))))))
(T
(* |;;| "so that datatypes will be properly compared")
(OR (EQ CL::X CL::Y)
(LET ((CL::TYPENAME (TYPENAME CL::X)))
(AND (EQ CL::TYPENAME (TYPENAME CL::Y))
(LET ((CL::DESCRIPTORS (GETDESCRIPTORS CL::TYPENAME)))
(CL:IF CL::DESCRIPTORS
(FOR CL::FIELD IN CL::DESCRIPTORS
ALWAYS (CL:EQUALP (FETCHFIELD CL::FIELD CL::X)
(FETCHFIELD CL::FIELD CL::Y)))))))))))
(* |;;| "For the byte compiler: Optimize by constant fold and coerce to EQ where possible")
(PUTPROPS CL:EQUAL BYTEMACRO COMP.EQ)
(PUTPROPS CL:EQUALP BYTEMACRO COMP.EQ)
(PUTPROPS CL:EQUAL DOPVAL (2 CMLEQUAL))
(CL:DEFUN \\REMOVE-DECLS (CL::BODY CL::ENVIRONMENT)
(* |;;;| "This is like parse-body, except that it returns the body and a list of specials declared in this frame. It side-effects the environment to mark the specials.")
(PROG ((CL::SPECIALS NIL)
CL::FORM)
CL::NEXT-FORM
(CL:IF (NULL CL::BODY)
(GO CL::DONE))
(CL:SETQ CL::FORM (CAR CL::BODY))
CL::RETRY-FORM
(COND
((OR (CL:ATOM CL::FORM)
(NOT (CL:SYMBOLP (CAR CL::FORM))))
(GO CL::DONE))
((EQ (CAR CL::FORM)
'DECLARE)
(CL:MAPC #'(CL:LAMBDA (CL:DECLARATION)
(CL:WHEN (CL:CONSP CL:DECLARATION)
(CL:WHEN (OR (EQ (CAR CL:DECLARATION)
'CL:SPECIAL)
(EQ (CAR CL:DECLARATION)
'SPECVARS))
(CL:IF (EQ (CDR CL:DECLARATION)
T)
(* |;;| "(specvars . t) refers to all variables inside this scope, not just those bound in this frame. So handling (specvars . t) by declaring the variables in this frame special would not be correct. Hence print a warning and continue.")
(CL:WARN
"(IL:SPECVARS . T) has no effect in the CL evaluator."
)
(CL:MAPC #'(CL:LAMBDA (CL::NAME)
(CL:PUSH CL::NAME CL::SPECIALS))
(CDR CL:DECLARATION))))))
(CDR CL::FORM))
(CL:POP CL::BODY)
(GO CL::NEXT-FORM))
((CL:SPECIAL-FORM-P (CAR CL::FORM))
(GO CL::DONE))
(T (LET ((CL::NEW-FORM (CL:MACROEXPAND-1 CL::FORM CL::ENVIRONMENT)))
(COND
((AND (NOT (EQ CL::NEW-FORM CL::FORM))
(CL:CONSP CL::NEW-FORM))
(CL:SETQ CL::FORM CL::NEW-FORM)
(GO CL::RETRY-FORM))
(T (GO CL::DONE))))))
CL::DONE
(RETURN (CL:IF CL::SPECIALS
(PROGN (FOR CL::VAR IN CL::SPECIALS DO (CL:SETF (ENVIRONMENT-VARS
CL::ENVIRONMENT)
(LIST* CL::VAR
*SPECIAL-BINDING-MARK*
(ENVIRONMENT-VARS
CL::ENVIRONMENT))))
(CL:VALUES CL::BODY CL::SPECIALS))
CL::BODY))))
(CL:DEFUN CL:SPECIAL-FORM-P (CL::X)
(GET CL::X 'SPECIAL-FORM))
(DEFINE-SPECIAL-FORM INTERLISP PROGN)
(PUTPROPS INTERLISP DMACRO ((X . Y)
(PROGN X . Y)))
(PUTPROPS COMMON-LISP DMACRO ((X)
X))
(DEFINEQ
(common-lisp
(nlambda common-lisp-forms (* \; "Edited 12-Feb-87 20:24 by Pavel")
(\\eval-progn common-lisp-forms nil)))
)
(ADDTOVAR LAMBDASPLST CL:LAMBDA)
(DEFINEQ
(\\translate-cl\:lambda
(lambda (expr) (* \; "Edited 13-Feb-87 23:20 by Pavel")
(let
(vrbls keyvars optvars auxlist restform vartyp body keywords (cnt 1)
(min 0)
(max 0)
decls
(simplep t))
(|for| binding var |in| (car (cdr expr))
|do|
(selectq binding
((&rest &body)
(setq vartyp '&rest))
(&optional (setq vartyp binding))
(&aux (setq vartyp binding))
(&allow-other-keys
(or (eq vartyp '&key)
(error "&ALLOW-OTHER-KEYS not in &KEY")))
(&key (setq vartyp '&key))
(selectq vartyp
(nil "required" (|push| vrbls binding)
(|add| cnt 1)
(|add| min 1)
(|add| max 1)
(and *check-argument-counts* (setq simplep nil)))
(&rest (setq restform `((,binding (|for| i |from| ,cnt |to| |-args-|
|collect| (arg |-args-| i)))))
(setq max nil)
(setq simplep nil))
(&aux (|push| auxlist binding))
(&key (let*
(svar (init (cond
((listp binding)
(prog1 (cadr binding)
(setq svar (caddr binding))
(setq binding (car binding))))))
(key (cond
((listp binding)
(prog1 (car binding)
(setq binding (cadr binding))))
(t (make-keyword binding)))))
(cond
(svar (|push| keyvars (list svar t))))
(|push|
keyvars
(list binding
`(|for| \\index |from| ,cnt |to| |-args-| |by| 2
|when| (eq (arg |-args-| \\index)
,key) |do| (return (arg |-args-| (add1 \\index))
)
|finally| (return ,(cond
(svar `(progn (setq ,svar nil)
,init))
(t init)))))))
(setq max nil)
(setq simplep nil))
(&optional (or (listp binding)
(setq binding (list binding)))
(let ((svar (caddr binding)))
(cl:when svar (|push| optvars svar)
(setq simplep nil))
(cl:when (cadr binding)
(setq simplep nil))
(|push| optvars
`(,(car binding)
(cond
((igreaterp ,cnt |-args-|)
,(cadr binding))
(t ,@(cond
(svar `((setq ,svar t)))) (arg |-args-|
,cnt))))))
(and max (|add| max 1))
(|add| cnt 1))
(shouldnt))))
(cl:multiple-value-setq (body decls)
(parse-body (cdr (cdr expr))
nil))
(cl:if simplep `(,'lambda (,@(reverse vrbls) ,@(mapcar (reverse optvars)
(function car)))
(declare (localvars . t))
,@decls
(,'let* (,@(reverse auxlist))
,@decls
,@body))
`(lambda |-args-|
(declare (localvars . t))
,@(cond
((and *check-argument-counts* min (neq min 0))
`((cond
((ilessp ,'|-args-| ,min)
(error "Too few args" ,'|-args-|))))))
,@(cond
((and *check-argument-counts* max)
`((cond
((igreaterp ,'|-args-| ,max)
(error "Too many args" ,'|-args-|))))))
(,'let* (,@(|for| var |in| (reverse vrbls) |as| i |from| 1
|collect| (list var `(arg |-args-| ,i))) ,@(reverse optvars)
,@(reverse keyvars)
,@restform
,@(reverse auxlist))
,@decls
,@body))))))
)
(CL:DEFPARAMETER *CHECK-ARGUMENT-COUNTS* NIL)
(DEFGLOBALVAR *SPECIAL-BINDING-MARK* "Variable specially bound. This string should never be visible")
(CL:DEFCONSTANT CL:LAMBDA-LIST-KEYWORDS '(&OPTIONAL &REST &KEY &AUX &BODY &WHOLE &ALLOW-OTHER-KEYS
&ENVIRONMENT &CONTEXT))
(CL:DEFCONSTANT CL:CALL-ARGUMENTS-LIMIT 512)
(CL:DEFCONSTANT CL:LAMBDA-PARAMETERS-LIMIT 512)
(CL:DEFSTRUCT (CLOSURE (:PRINT-FUNCTION (LAMBDA (CLOSURE STREAM)
(LET ((*PRINT-RADIX* NIL))
(CL:FORMAT STREAM "#<Interpreted closure @ ~O,~O>"
(\\HILOC CLOSURE)
(\\LOLOC CLOSURE))))))
(* |;;;| "An interpreted lexical closure. Contains the function and an environment object.")
FUNCTION
ENVIRONMENT)
(CL:DEFSTRUCT (ENVIRONMENT (:CONSTRUCTOR \\MAKE-ENVIRONMENT NIL)
(:COPIER \\COPY-ENVIRONMENT)
(:PRINT-FUNCTION (LAMBDA (ENV STREAM DEPTH)
(DECLARE (IGNORE DEPTH))
(LET ((*PRINT-RADIX* NIL))
(CL:FORMAT STREAM "#<Lexical Environment @ ~O,~O>"
(\\HILOC ENV)
(\\LOLOC ENV))))))
(* |;;;| "An environment used by the Common Lisp interpreter. Every environment contains all of the information of its parents. That is, new child environments are made by copying the parent and then pushing new data onto one of the fields. This makes certain tests very fast.")
(* |;;| "Lexically-bound or -declared variables. A property list mapping names into either *SPECIAL-BINDING-MARK* or their values.")
VARS
(* |;;| "Lexical functions and macros. A property list mapping names into either (:function . fn) or (:macro . expansion-fn).")
FUNCTIONS
(* |;;| "A property list mapping block names into unique blips. RETURN-FROMs can throw to the appropriate blip.")
BLOCKS
(* |;;| "A property list mapping TAGBODY bodies into unique blips. GOs throw the correct tail of the body to the blip.")
TAGBODIES)
(DEFMACRO \\MAKE-CHILD-ENVIRONMENT (PARENT &KEY ((:BLOCK (BLOCK-NAME BLOCK-BLIP))
NIL BLOCK-P)
((:TAGBODY (TAGBODY-TAIL TAGBODY-BLIP))
NIL TAGBODY-P))
`(LET* (($$PARENT ,PARENT)
($$NEW-ENV (CL:IF $$PARENT
(\\COPY-ENVIRONMENT $$PARENT)
(\\MAKE-ENVIRONMENT))))
,@(AND BLOCK-P `((CL:SETF (ENVIRONMENT-BLOCKS $$NEW-ENV)
(LIST* ,BLOCK-NAME ,BLOCK-BLIP (ENVIRONMENT-BLOCKS $$NEW-ENV)))))
,@(AND TAGBODY-P `((CL:SETF (ENVIRONMENT-TAGBODIES $$NEW-ENV)
(LIST* ,TAGBODY-TAIL ,TAGBODY-BLIP (ENVIRONMENT-TAGBODIES
$$NEW-ENV)))))
$$NEW-ENV))
(DEFINEQ
(CL:EVAL
(LAMBDA (CL::EXPRESSION CL::ENVIRONMENT) (* \; "Edited 15-Apr-2024 20:00 by lmm")
(* \; "Edited 1-Apr-92 12:39 by jds")
(* |;;| "This is in Interlisp and not a DEFUN to help avoid bootstrap death, although bootstrap death is quite possible anyway if, for example, any of the macros here are in Common Lisp and the macro definitions are interpreted.")
(DECLARE (LOCALVARS . T))
(COND
((AND *EVALHOOK* (NOT (PROG1 CL::*SKIP-EVALHOOK* (CL:SETQ CL::*SKIP-EVALHOOK* NIL))))
(LET ((CL::HOOKFN *EVALHOOK*)
(*EVALHOOK* NIL))
(CL:FUNCALL CL::HOOKFN CL::EXPRESSION CL::ENVIRONMENT)))
(T
(CL:TYPECASE CL::EXPRESSION
(CL:SYMBOL (COND
((NULL CL::EXPRESSION)
NIL)
((EQ CL::EXPRESSION T)
T)
(T (LET (CL::LOC CL::VAL)
(CL:BLOCK CL::EVAL-VARIABLE
(CL:WHEN CL::ENVIRONMENT
(|for| CL::TAIL |on| (ENVIRONMENT-VARS CL::ENVIRONMENT)
|by| (CDDR CL::TAIL) |when| (EQ CL::EXPRESSION
(CAR CL::TAIL))
|do| (CL:SETQ CL::VAL (CADR CL::TAIL))
(COND
((EQ CL::VAL *SPECIAL-BINDING-MARK*)
(* |;;|
"return from FOR loop, skipping to SPECIALS code below.")
(RETURN NIL))
(T (CL:RETURN-FROM CL::EVAL-VARIABLE CL::VAL)))))
(* |;;|
"following copied from \\EVALVAR in the Interlisp interpreter")
(SETQ CL::LOC (\\STKSCAN CL::EXPRESSION))
(COND
((EQ (CL:SETQ CL::VAL (\\GETBASEPTR CL::LOC 0))
'NOBIND) (* \;
"Value is NOBIND even if it was not found as the top-level value.")
(CL:ERROR 'UNBOUND-VARIABLE :NAME CL::EXPRESSION))
(T CL::VAL)))))))
(CONS
(COND
((CL:CONSP (CAR CL::EXPRESSION))
(LET ((CL::ARGCOUNT 1))
(* |;;| "This is a very very awful hack for getting into internal lambda expressions .COMPILER-SPREAD-ARGUMENTS. is handled specially by the compiler--it iterates over a list pushing things")
(* |;;| "secondly, the (OPCODES) directly calls EVAL-INVOKE-LAMBDA with more args than are given, blowing away the following APPLYFN. Larry thought this level of hackery was important for performance.")
(.COMPILER-SPREAD-ARGUMENTS. (CDR CL::EXPRESSION)
CL::ARGCOUNT
(CL-EVAL-FN3-CALL (CAR CL::EXPRESSION)
CL::ENVIRONMENT)
((CL:EVAL CL::ENVIRONMENT)))))
(T (LET ((CL::FN-DEFN (AND CL::ENVIRONMENT (CL:GETF (ENVIRONMENT-FUNCTIONS
CL::ENVIRONMENT)
(CAR CL::EXPRESSION)))))
(COND
((NULL CL::FN-DEFN) (* \;
"The normal case: the function is not lexically-defined.")
(CASE (ARGTYPE (CAR CL::EXPRESSION))
((0 2)
(* |;;| "has a Interlisp/CommonLisp lambda-spread definition")
(CL:IF (AND *APPLYHOOK* (NOT (PROG1 CL::*SKIP-APPLYHOOK*
(CL:SETQ
CL::*SKIP-APPLYHOOK*
NIL))))
(LET* ((CL::ARGS (CL:MAPCAR #'(CL:LAMBDA (CL::ARG)
(CL:EVAL CL::ARG
CL::ENVIRONMENT)
)
(CDR CL::EXPRESSION)))
(CL::HOOKFN *APPLYHOOK*)
(*APPLYHOOK* NIL))
(CL:FUNCALL CL::HOOKFN (CAR CL::EXPRESSION)
CL::ARGS CL::ENVIRONMENT))
(LET ((CL::ARGCOUNT 0))
(.COMPILER-SPREAD-ARGUMENTS. (CDR CL::EXPRESSION)
CL::ARGCOUNT
(CAR CL::EXPRESSION)
((CL:EVAL CL::ENVIRONMENT))))))
(T
(* |;;|
"in Common Lisp, special form overrides nlambda definition")
(* |;;| "note that the GET will error if not a symbol. ")
(LET ((CL::TEMP (AND (CL:SYMBOLP (CAR CL::EXPRESSION))
(GET (CAR CL::EXPRESSION)
'SPECIAL-FORM))))
(COND
(CL::TEMP (* \;
"CAR is the name of a special form.")
(CL:FUNCALL CL::TEMP (CDR CL::EXPRESSION)
CL::ENVIRONMENT))
((CL:SETQ CL::TEMP (CL:MACRO-FUNCTION (CAR
CL::EXPRESSION
)))
(* \; "CAR is the name of a macro")
(CL:EVAL (CL:FUNCALL CL::TEMP CL::EXPRESSION
CL::ENVIRONMENT)
CL::ENVIRONMENT))
(T (ERROR "Undefined car of form" (CAR CL::EXPRESSION)))
)))))
((EQ (CAR CL::FN-DEFN)
:MACRO) (* \; "A use of a lexical macro.")
(CL:EVAL (CL:FUNCALL (CDR CL::FN-DEFN)
CL::EXPRESSION CL::ENVIRONMENT)
CL::ENVIRONMENT))
(T (* \; "A call to a lexical function")
(LET ((CL::ARGCOUNT 0))
(.COMPILER-SPREAD-ARGUMENTS. (CDR CL::EXPRESSION)
CL::ARGCOUNT
(CDR CL::FN-DEFN)
((CL:EVAL CL::ENVIRONMENT))))))))))
(T
(* |;;| "3.1.2.1.3 Self-Evaluating Objects")
(* |;;| "A form that is neither a symbol nor a cons is defined to be a self-evaluating object. Evaluating such an object yields the same object as a result.")
(* |;;| "See https://interlisp.org/clhs/Issues/iss145_w")
CL::EXPRESSION))))))
(\\eval-invoke-lambda
(lambda (n lam env)
(declare (localvars . t)) (* \; "Edited 28-Apr-87 11:55 by Pavel")
(let ((argblock (addstackbase (- (fetch (fx nextblock) of (\\myalink))
(+ (cl:decf n)
n)))))
(* |;;| "First sub-form is a list of (variable initialization) pairs. Initializes the variables, binding them to new values all at once, then executes the remaining forms as in a PROGN.")
(cl:multiple-value-bind (body specials)
(\\remove-decls (cddr lam)
(cl:setq env (\\make-child-environment env)))
(\\interpret-arguments "a LAMBDA as the CAR of a form"
(case (car lam)
((lambda openlambda)
'&interlisp)
((cl:lambda)
'&required)
(t (cl:error "(~S ...) is not legal as the CAR of a form." (car lam))))
(cadr lam)
specials env body argblock n 0)))))
(\\interpret-arguments
(lambda (\\fn-name \\argtype \\arglist \\specials \\environment \\body \\argument-block \\length
\\index) (* \; "Edited 7-Apr-88 16:16 by amd")
(* |;;| "Written in a somewhat arcane style to avoid recursive calls whenever possible, & keep code inline. RECUR does a recursive call if under a PROGV, but otherwise does a GO. ")
(cl:macrolet
((recur (tag)
`(go ,tag))
(with-binding
(var val &rest forms)
`(progn (check-bindable ,var)
(cl:if (or (fmemb ,var \\specials)
(variable-globally-special-p ,var))
(cl:macrolet ((recur (tag)
`(\\interpret-arguments
\\fn-name
,(cl:if (eq tag 'in-keywords)
'\\argtype
`',tag) \\arglist \\specials \\environment
\\body \\argument-block \\length \\index)))
(cl:progv (list ,var)
(list ,val)
,@forms))
(progn (cl:setf (environment-vars \\environment)
(list* ,var ,val (environment-vars \\environment)))
,@forms)))))
(prog (\\var \\val \\svar \\sp)
(* |;;| "dispatch on input type. The in-keywords case is special, since it needs to pass down where the beginning of the keywords section is")
(case \\argtype (&required (go &required))
(&optional (go &optional))
(&interlisp (go &interlisp))
(&rest (go &rest))
(&key (go &key))
(&aux (go &aux))
(&body (go &body))
(t (go in-keywords)))
&required
(return
(cond
((null \\arglist)
(cl:if (< \\index \\length)
(cl:error 'too-many-arguments :callee \\fn-name :actual \\length :maximum
\\index))
(recur &body))
(t (case (setq \\var (|pop| \\arglist))
(&optional (recur &optional))
(&rest (recur &rest))
(&aux (recur &aux))
(&key (recur &key))
(t (cond
((>= \\index \\length)
(cl:error 'too-few-arguments :callee \\fn-name :actual \\length
:minimum
(+ 1 \\index
(for arg in \\arglist
while (not (fmemb arg '(&optional &rest &aux &key)))
sum 1)))))
(setq \\val (arg-ref \\argument-block (prog1 \\index (cl:incf \\index))))
(with-binding \\var \\val (recur &required)))))))
&optional
(return (cond
((null \\arglist)
(cl:if (< \\index \\length)
(cl:error 'too-many-arguments :callee \\fn-name :actual \\length
:maximum \\index))
(recur &body))
(t (case (setq \\var (|pop| \\arglist))
(&rest (recur &rest))
(&aux (recur &aux))
(&key (recur &key))
(t (cl:if (>= \\index \\length)
(cl:if (cl:consp \\var)
(progn (setq \\val (cl:eval (cadr \\var)
\\environment))
(setq \\svar (caddr \\var))
(setq \\var (car \\var))
(setq \\sp nil))
(setq \\val nil))
(progn (cond
((cl:consp \\var)
(setq \\svar (caddr \\var))
(setq \\sp t)
(setq \\var (car \\var))))
(setq \\val (arg-ref \\argument-block \\index))
(cl:incf \\index)))
(with-binding \\var \\val (cl:if \\svar (with-binding
\\svar \\sp
(recur &optional))
(recur &optional))))))))
&interlisp
(return (cond
((null \\arglist)
(recur &body))
(t (setq \\var (|pop| \\arglist))
(cl:if (>= \\index \\length)
(setq \\val nil)
(progn (setq \\val (arg-ref \\argument-block \\index))
(cl:incf \\index)))
(with-binding \\var \\val (recur &interlisp)))))
&rest
(setq \\var (|pop| \\arglist))
(setq \\val (|for| i |from| \\index |while| (< i \\length)
|collect| (arg-ref \\argument-block i)))
(return (with-binding \\var \\val (cl:if (null \\arglist)
(recur &body)
(case (|pop| \\arglist)
(&aux (recur &aux))
(&key (recur &key))
(t (cl:error 'invalid-argument-list :callee
\\fn-name))))))
&key
(or (evenp (- \\length \\index))
(cl:error "Not an even number of arguments for &KEY"))
(setq \\argtype \\arglist) (* \;
"Type is now the beginning of the keyword arguments")
in-keywords
(return
(cond
((null \\arglist)
(check-keywords \\argtype \\argument-block \\length \\index)
(recur &body))
(t (case (setq \\var (|pop| \\arglist))
(&aux (check-keywords \\argtype \\argument-block \\length \\index)
(recur &aux))
(&allow-other-keys (cl:if (null \\arglist)
(recur &body)
(case (|pop| \\arglist)
(&aux (recur &aux))
(t (cl:error 'invalid-argument-list :callee
\\fn-name)))))
(t (cond
((cl:consp \\var)
(setq \\val (cadr \\var))
(setq \\svar (caddr \\var))
(setq \\var (car \\var)))
(t (setq \\svar nil)
(setq \\val nil)))
(let ((key (cl:if (cl:consp \\var)
(prog1 (car \\var)
(setq \\var (cadr \\var)))
(make-keyword \\var))))
(|for| i |from| \\index |while| (< i \\length)
|by| 2
|do| (cl:if (eq (arg-ref \\argument-block i)
key)
(return (progn (setq \\val (arg-ref
\\argument-block
(+ i 1)))
(setq \\sp t))))
|finally| (setq \\val (cl:eval \\val \\environment))
(setq \\sp nil)))
(with-binding \\var \\val (cl:if \\svar (with-binding \\svar \\sp
(recur in-keywords))
(recur in-keywords))))))))
&aux
(return (cond
((null \\arglist)
(recur &body))
(t (setq \\var (|pop| \\arglist))
(cl:if (cl:consp \\var)
(progn (setq \\val (cl:eval (cadr \\var)
\\environment))
(setq \\var (car \\var)))
(setq \\val nil))
(with-binding \\var \\val (recur &aux)))))
&body
(return (cl:if (null (cdr \\body))
(cl:if (cl:consp (setq \\body (car \\body)))
(case (car \\body)
(cl:block
(* |;;| "special case to handle BLOCK to avoid consing two environments just to enter a normal LAMBDA function")
(let ((blip (cons nil nil)))
(cl:setf (environment-blocks \\environment)
(list* (cadr \\body)
blip
(environment-blocks \\environment)))
(cl:catch blip (\\eval-progn (cddr \\body)
\\environment))))
(t (cl:eval \\body \\environment)))
(cl:eval \\body \\environment))
(progn (cl:eval (pop \\body)
\\environment)
(recur &body))))))))
(\\interpreter-lambda
(lambda (n def env fn) (* \; "Edited 13-Feb-87 21:21 by Pavel")
(declare (localvars . t))
(let ((argblock (addstackbase (|fetch| (bf ivar) |of| (|fetch| (fx blink)
|of| (\\myalink))))))
(setq env (\\make-child-environment env))
(cl:multiple-value-bind (body specials)
(\\remove-decls (cdr (cdr def))
env)
(\\interpret-arguments fn '&required (car (cdr def))
specials env body argblock (- n 1)
0)))))
(check-bindable
(lambda (var) (* \; "Edited 13-Feb-87 22:06 by Pavel")
(cl:unless (cl:symbolp var)
(cl:error "Attempt to bind a non-symbol: ~S" var))
(cl:when (or (cl:constantp var)
(fmemb var cl:lambda-list-keywords))
(cl:error (cl:if (cl:keywordp var)
"Attempt to bind a keyword: ~S" "Attempt to bind a constant: ~S")
var))
(cl:when (variable-global-p var)
(cl:cerror "Go ahead and bind it anyway"
"Attempt to bind a variable proclaimed global: ~S" var))
var))
(check-keywords
(lambda (key-arguments argblock length n) (* \; "Edited 1-Dec-87 16:47 by amd")
(* |;;| "check to see if any keywords in ARGBLOCK are not in the keys - not called if &ALLOW-OTHER-KEYS was set")
(cl:block check-keys
(let (badkeyword)
(cl:do ((i n (+ i 2)))
((>= i length))
(let ((given-key (arg-ref argblock i)))
(cl:if (eq given-key :allow-other-keys)
(cl:if (arg-ref argblock (cl:1+ i))
(cl:return-from check-keys nil)
nil)
(cl:do ((keytail key-arguments (cdr keytail)))
((or (null keytail)
(eq (car keytail)
'&aux)) (* \; "got to end of keyword segment")
(setq badkeyword given-key))
(let ((wanted-key (car keytail)))
(if (cl:consp wanted-key)
then (setq wanted-key (car wanted-key))
(cl:if (cl:consp wanted-key)
(setq wanted-key (car wanted-key))
(setq wanted-key (make-keyword
wanted-key)))
else (setq wanted-key (make-keyword wanted-key))
)
(cl:if (eq wanted-key given-key)
(return nil)))))))
(cl:if badkeyword (cl:error
"Keyword argument doesn't match expected list of keywords: ~A"
badkeyword))))))
)
(DEFMACRO ARG-REF (BLOCK N)
`(\\GETBASEPTR ,BLOCK (LLSH ,N 1)))
(PUTPROPS .COMPILER-SPREAD-ARGUMENTS. DMACRO (APPLY COMP.SPREAD))
(DEFINEQ
(declared-special
(lambda (var decls) (* |lmm| "24-May-86 22:27")
(and decls (or (and (listp (car decls))
(eq (caar decls)
'declare)
(|for| dec |in| (cdar decls) |when| (and (eq (car dec)
'cl:special)
(fmemb var (cdr dec)))
|do| (return t)))
(declared-special var (cdr decls))))))
)
(* \; "FUNCALL and APPLY, not quite same as Interlisp")
(DEFINEQ
(cl:funcall
(cl:lambda (cl::fn &rest cl::args) (* \; "Edited 14-Feb-87 00:16 by Pavel")
(cl:apply cl::fn cl::args)))
(cl:apply
(lambda cl::n (* \; "Edited 14-Feb-87 00:16 by Pavel")
(cl:if (eq cl::n 0)
(error "TOO FEW ARGUMENTS TO APPLY")
(spreadapply (arg cl::n 1)
(let ((cl::av (arg cl::n cl::n)))
(for cl::i from (cl:1- cl::n) to 2 by -1
do (cl:push (arg cl::n cl::i)
cl::av))
cl::av)))))
)
(PUTPROPS CL:APPLY DMACRO (DEFMACRO (FN &REST ARGS) (CASE COMPILE.CONTEXT
((EFFECT RETURN)
`(LET ((FN ,FN)
(CNT ,(LENGTH (CDR ARGS))))
(.SPREAD. ((OPCODES)
\,@ ARGS)
CNT FN)))
(T
(* |;;|
"otherwise might not return multiple values")
'IGNOREMACRO))))
(PUTPROPS CL:FUNCALL DMACRO (DEFMACRO (FN &REST ARGS) (COND
((AND (NLISTP FN)
(EVERY ARGS (FUNCTION NLISTP)))
`((OPCODES APPLYFN)
,@ARGS
,(LENGTH ARGS)
,FN))
(T (LET ((TEM (GENSYM)))
`((LAMBDA (,TEM)
((OPCODES APPLYFN)
,@ARGS
,(LENGTH ARGS)
,TEM))
,FN))))))
(* \; "COMPILER-LET needs to work differently compiled and interpreted")
(DEFINEQ
(cl:compiler-let
(nlambda $$compiler-let-tail (* \; "Edited 7-Apr-88 16:05 by amd")
(cl:progv (|for| x |in| (car $$compiler-let-tail) |collect| (cond
((cl:consp x)
(car x))
(t x)))
(|for| x |in| (car $$compiler-let-tail) |collect| (cond
((cl:consp x)
(\\eval (cadr x)))))
(\\evprogn (cdr $$compiler-let-tail)))))
(comp.compiler-let
(lambda (\\a)
(declare (localvars . t)) (* \; "Edited 7-Apr-88 16:38 by amd")
(* entry point into bytecompiler)
(* |lmm| "27-May-86 11:17")
(cl:progv (|for| x |in| (car \\a) |collect| (|if| (cl:consp x)
|then| (car x)
|else| x))
(|for| x |in| (car \\a) |collect| (cond
((cl:consp x)
(eval (cadr x)))))
(comp.progn (cdr \\a)))))
)
(PUTPROPS CL:COMPILER-LET DMACRO COMP.COMPILER-LET)
(DEFINE-SPECIAL-FORM CL:COMPILER-LET (CL::ARGS &REST CL::BODY &ENVIRONMENT CL::ENV)
(LET ((*IN-COMPILER-LET* T))
(DECLARE (CL:SPECIAL *IN-COMPILER-LET*)) (* \;