forked from forestfoxx/Serial-Experiments-Lain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Afx-n-nPrv-View.CL
2237 lines (1983 loc) · 75.7 KB
/
Afx-n-nPrv-View.CL
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
;;;-*- Mode:LISP; Package:(WALKER LISP 1000); Base:10; Syntax:Common-lisp -*-
;;;
;;; *************************************************************************
;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
;;; All rights reserved.
;;;
;;; Use and copying of this software and preparation of derivative works
;;; based upon this software are permitted. Any distribution of this
;;; software or derivative works must comply with all applicable United
;;; States export control laws.
;;;
;;; This software is made available AS IS, and Xerox Corporation makes no
;;; warranty about the software, its performance or its conformity to any
;;; specification.
;;;
;;; Any person obtaining a copy of this software is requested to send their
;;; name and post office or electronic mail address to:
;;; CommonLoops Coordinator
;;; Xerox PARC
;;; 3333 Coyote Hill Rd.
;;; Palo Alto, CA 94304
;;; (or send Arpanet mail to [email protected])
;;;
;;; Suggestions, comments and requests for improvements are also welcome.
;;; *************************************************************************
;;;
;;; A simple code walker, based IN PART on: (roll the credits)
;;; Larry Masinter's Masterscope
;;; Moon's Common Lisp code walker
;;; Gary Drescher's code walker
;;; Larry Masinter's simple code walker
;;; .
;;; .
;;; boy, thats fair (I hope).
;;;
;;; For now at least, this code walker really only does what PCL needs it to
;;; do. Maybe it will grow up someday.
;;;
;;;
;;; This code walker used to be completely portable. Now it is just "Real
;;; easy to port". This change had to happen because the hack that made it
;;; completely portable kept breaking in different releases of different
;;; Common Lisps, and in addition it never worked entirely anyways. So,
;;; its now easy to port. To port this walker, all you have to write is one
;;; simple macro and two simple functions. These macros and functions are
;;; used by the walker to manipluate the macroexpansion environments of
;;; the Common Lisp it is running in.
;;;
;;; The code which implements the macroexpansion environment manipulation
;;; mechanisms is in the first part of the file, the real walker follows it.
;;;
;;; Change Log:
;;;
;;; This version was obtained from
;;; ftp.cs.cmu.edu:user/ai/lang/lisp/code/codewalker/walk/
;;; and contains the following fixes.
;;;
;;; 17-NOV-93 mk Fixes from Fernando D. Mato Mira <[email protected]>.
;;; - Missing semicolon before #+(and :Coral :mcl)
;;; - Structure of environments changed in Allegro CL.
;;; - Wrapped a handler-case around one of the tests to
;;; prevent an error.
;;; - COMPILER-LET is not in CLtL2.
#+(or mcl (and excl cltl2)) (defpackage :walker)
(in-package :walker)
;;;
;;; The user entry points are walk-form and nested-walked-form. In addition,
;;; it is legal for user code to call the variable information functions:
;;; variable-lexical-p, variable-special-p and variable-class. Some users
;;; will need to call define-walker-template, they will have to figure that
;;; out for themselves.
;;;
(export '(define-walker-template
walk-form
nested-walk-form
variable-lexical-p
variable-special-p
variable-globally-special-p
*variable-declarations*
variable-declaration
))
;;;
;;; On the following pages are implementations of the implementation specific
;;; environment hacking functions for each of the implementations this walker
;;; has been ported to. If you add a new one, so this walker can run in a new
;;; implementation of Common Lisp, please send the changes back to us so that
;;; others can also use this walker in that implementation of Common Lisp.
;;;
;;; This code just hacks 'macroexpansion environments'. That is, it is only
;;; concerned with the function binding of symbols in the environment. The
;;; walker needs to be able to tell if the symbol names a lexical macro or
;;; function, and it needs to be able to build environments which contain
;;; lexical macro or function bindings. It must be able, when walking a
;;; macrolet, flet or labels form to construct an environment which reflects
;;; the bindings created by that form. Note that the environment created
;;; does NOT have to be sufficient to evaluate the body, merely to walk its
;;; body. This means that definitions do not have to be supplied for lexical
;;; functions, only the fact that that function is bound is important. For
;;; macros, the macroexpansion function must be supplied.
;;;
;;; This code is organized in a way that lets it work in implementations that
;;; stack cons their environments. That is reflected in the fact that the
;;; only operation that lets a user build a new environment is a with-body
;;; macro which executes its body with the specified symbol bound to the new
;;; environment. No code in this walker or in PCL will hold a pointer to
;;; these environments after the body returns. Other user code is free to do
;;; so in implementations where it works, but that code is not considered
;;; portable.
;;;
;;; There are 3 environment hacking tools. One macro which is used for
;;; creating new environments, and two functions which are used to access the
;;; bindings of existing environments.
;;;
;;; WITH-AUGMENTED-ENVIRONMENT
;;;
;;; ENVIRONMENT-FUNCTION
;;;
;;; ENVIRONMENT-MACRO
;;;
(defun unbound-lexical-function (&rest args)
(declare (ignore args))
(error "The evaluator was called to evaluate a form in a macroexpansion~%~
environment constructed by the PCL portable code walker. These~%~
environments are only useful for macroexpansion, they cannot be~%~
used for evaluation.~%~
This error should never occur when using PCL.~%~
This most likely source of this error is a program which tries to~%~
to use the PCL portable code walker to build its own evaluator."))
;;;
;;; In Coral Common Lisp, the macroexpansion environment is just a list
;;; of environment entries. The cadr of each element specifies the type
;;; of the element. The only types that interest us are CCL::MACRO and
;;; FUNCTION. In these cases the element is interpreted as follows.
;;;
;;; (<function-name> CCL::MACRO . macroexpansion-function)
;;;
;;; (<function-name> FUNCTION . <fn>)
;;;
;;; When in the compiler, <fn> is a gensym which will be
;;; a variable which bound at run-time to the function.
;;; When in the interpreter, <fn> is the actual function.
;;;
;;;
#+(and :Coral (not :mcl))
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
(dolist (f functions)
(push (list* f 'function (gensym)) env))
(dolist (m macros)
(push (list* (car m) 'ccl::macro (cadr m)) env))
env)
(defun environment-function (env fn)
(let ((entry (assoc fn env :test #'equal)))
(and entry
(eq (cadr entry) 'function)
(cddr entry))))
(defun environment-macro (env macro)
(let ((entry (assoc macro env :test #'equal)))
(and entry
(eq (cadr entry) 'ccl::macro)
(cddr entry))))
);#+(and :Coral (not :mcl))
#+(and :Coral :mcl)
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
(augment-environment
env
:function (mapcar #'car functions)
:macro macros))
;;; This doesn't seem to be used!
(defun environment-function (env fn)
(function-information fn env))
#|
;;; This worked in alpha but doesn't work anymore...
;;; I'm not too sure about this one! We should ask the Apple people for help.
(defun environment-macro (env macro)
(when env
(let ((info (assoc macro (ccl::lexenv.functions env))))
(if info
(when (eq (cadr info) 'ccl::macro)
(let ((my-lock (cddr info)))
(if (listp my-lock) my-lock (list my-lock nil nil nil))))
(environment-macro (ccl::lexenv.parent-env env) macro)))))
|#
;;; SEM 06/12/91
;;; This lets things run without errors, but I have no idea if it's correct.
(defun environment-macro (env macro)
(when env
(cl:macro-function macro env)))
);#+(and :Coral :mcl)
;;;
;;; Franz Common Lisp is a lot like Coral Lisp. The macroexpansion
;;; environment is just a list of entries.
;;;
;;; - The first entry is a list of lists where the cadr of each list
;;; specifies the type of the element. The types that interest us
;;; are FUNCTION, EXCL::MACRO, and COMPILER::FUNCTION-VALUE. These
;;; are interpreted as follows:
;;;
;;; (<function-name> FUNCTION . <a lexical closure>)
;;;
;;; This happens in the interpreter with lexically
;;; bound functions.
;;;
;;; (<function-name> COMPILER::FUNCTION-VALUE . <gensym>)
;;;
;;; This happens in the compiler. The gensym represents
;;; a variable which will be bound at run time to the
;;; function object.
;;;
;;; (<function-name> EXCL::MACRO . <a lambda>)
;;;
;;; In both interpreter and compiler, this is the
;;; representation used for macro definitions.
;;;
;;; - The rest of the entries correspond to symbol macros introduced by
;;; SYMBOL-MACROLET.
#+:excl
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
(let* ((new-env (copy-list env))
(new-defs (car new-env)))
(dolist (f functions)
(push (list* f 'function #'unbound-lexical-function) new-defs))
(dolist (m macros)
(push (list* (car m) 'excl::macro (cadr m)) new-defs))
(if new-env
(rplaca new-env new-defs)
(setq new-env (list new-defs)))
new-env))
(defun environment-function (env fn)
(let ((entry (assoc fn (car env) :test #'equal)))
(and entry
(or (eq (cadr entry) 'function)
(eq (cadr entry) 'compiler::function-value))
(cddr entry))))
(defun environment-macro (env macro)
(let ((entry (assoc macro (car env) :test #'equal)))
(and entry
(eq (cadr entry) 'excl::macro)
(cddr entry))))
);#+:ExCL
#+Lucid
(progn
(proclaim '(inline
%alphalex-p
add-contour-to-env-shape
make-function-variable
make-sfc-contour
sfc-contour-type
sfc-contour-elements
add-sfc-contour
add-function-contour
add-macrolet-contour
find-variable-in-contour
find-alist-element-in-contour
find-macrolet-in-contour))
(defun %alphalex-p (object)
#-Prime
(eq (cadddr (cddddr object)) 'lucid::%alphalex)
#+Prime
(eq (caddr (cddddr object)) 'lucid::%alphalex))
#+Prime
(defun lucid::augment-lexenv-fvars-dummy (lexical vars)
(lucid::augment-lexenv-fvars-aux lexical vars '() '() 'flet '()))
(defconstant function-contour 1)
(defconstant macrolet-contour 5)
(defstruct lucid::contour
type
elements)
(defun add-contour-to-env-shape (contour-type elements env-shape)
(cons (make-contour :type contour-type
:elements elements)
env-shape))
(defstruct (variable (:constructor make-variable (name source-type)))
name
(identifier nil)
source-type)
(defconstant function-sfc-contour 1)
(defconstant macrolet-sfc-contour 8)
(defconstant function-variable-type 1)
(defun make-function-variable (name)
(make-variable name function-variable-type))
(defun make-sfc-contour (type elements)
(cons type elements))
(defun sfc-contour-type (sfc-contour)
(car sfc-contour))
(defun sfc-contour-elements (sfc-contour)
(cdr sfc-contour))
(defun add-sfc-contour (element-list environment type)
(cons (make-sfc-contour type element-list) environment))
(defun add-function-contour (variable-list environment)
(add-sfc-contour variable-list environment function-sfc-contour))
(defun add-macrolet-contour (alist environment)
(add-sfc-contour alist environment macrolet-sfc-contour))
(defun find-variable-in-contour (name contour)
(dolist (element (sfc-contour-elements contour) nil)
(when (eq (variable-name element) name)
(return element))))
(defun find-alist-element-in-contour (name contour)
(cdr (assoc name (sfc-contour-elements contour))))
(defun find-macrolet-in-contour (name contour)
(find-alist-element-in-contour name contour))
(defmacro do-sfc-contours ((contour-var environment &optional result)
&body body)
`(dolist (,contour-var ,environment ,result) ,@body))
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let* ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
;;;
;;; with-augmented-environment-internal is where the real work of augmenting
;;; the environment happens.
;;;
(defun with-augmented-environment-internal (env functions macros)
(let ((function-names (mapcar #'first functions))
(macro-names (mapcar #'first macros))
(macro-functions (mapcar #'second macros)))
(cond ((or (null env)
(contour-p (first env)))
(when function-names
(setq env (add-contour-to-env-shape function-contour
function-names
env)))
(when macro-names
(setq env (add-contour-to-env-shape macrolet-contour
(pairlis macro-names
macro-functions)
env))))
((%alphalex-p env)
(when function-names
(setq env (lucid::augment-lexenv-fvars-dummy env function-names)))
(when macro-names
(setq env (lucid::augment-lexenv-mvars env
macro-names
macro-functions))))
(t
(when function-names
(setq env (add-function-contour
(mapcar #'make-function-variable function-names)
env)))
(when macro-names
(setq env (add-macrolet-contour
(pairlis macro-names macro-functions)
env)))))
env))
(defun environment-function (env fn)
(cond ((null env) nil)
((contour-p (first env))
(if (lucid::find-lexical-function fn env)
t
nil))
((%alphalex-p env)
(if (lucid::lexenv-fvar fn env)
t
nil))
(t (do-sfc-contours (contour env nil)
(let ((type (sfc-contour-type contour)))
(cond ((eql type function-sfc-contour)
(when (find-variable-in-contour fn contour)
(return t)))
((eql type macrolet-sfc-contour)
(when (find-macrolet-in-contour fn contour)
(return nil)))))))))
(defun environment-macro (env macro)
(cond ((null env) nil)
((contour-p (first env))
(lucid::find-lexical-macro macro env))
((%alphalex-p env)
(lucid::lexenv-mvar macro env))
(t (do-sfc-contours (contour env nil)
(let ((type (sfc-contour-type contour)))
(cond ((eql type function-sfc-contour)
(when (find-variable-in-contour macro contour)
(return nil)))
((eql type macrolet-sfc-contour)
(let ((fn (find-macrolet-in-contour macro contour)))
(when fn
(return fn))))))))))
);#+Lucid
;;;
;;; On the 3600, the documentation for how the environments are represented
;;; is in sys:sys;eval.lisp. That total information is not repeated here.
;;; The important points are that:
;;; si:env-variables returns a list of which each element is:
;;;
;;; (symbol value)
;;; or (symbol . locative)
;;;
;;; The first form is for lexical variables, the second for
;;; special and instance variables. In either case CADR of
;;; the entry is the value and SETF of CADR is used to change
;;; the value. Variables are looked up with ASSQ.
;;;
;;; si:env-functions returns a list of which each element is:
;;;
;;; (symbol definition)
;;;
;;; where definition is anything that could go in a function cell.
;;; This is used for both local functions and local macros.
;;;
;;; The 3600 stack conses its environments (at least in the interpreter).
;;; This means that code written using this walker and running on the 3600
;;; must not hold on to the environment after the walk-function returns.
;;; No code in this walker or in PCL does that.
;;;
#+Genera
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
(let ((funs (make-symbol "FNS"))
(macs (make-symbol "MACROS"))
(new (make-symbol "NEW")))
`(let ((,funs ,functions)
(,macs ,macros)
(,new ()))
(dolist (f ,funs)
(push `(,(car f) ,#'unbound-lexical-function) ,new))
(dolist (m ,macs)
(push `(,(car m) (special ,(cadr m))) ,new))
(let* ((.old-env. ,old-env)
(.old-vars. (pop .old-env.))
(.old-funs. (pop .old-env.))
(.old-blks. (pop .old-env.))
(.old-tags. (pop .old-env.))
(.old-dcls. (pop .old-env.)))
(si:with-interpreter-environment (,new-env
.old-env.
.old-vars.
(append ,new .old-funs.)
.old-blks.
.old-tags.
.old-dcls.)
,@body)))))
(defun environment-function (env fn)
(if (null env)
(values nil nil)
(let ((entry (assoc fn (si:env-functions env) :test #'equal)))
(if (and entry
(or (not (listp (cadr entry)))
(not (eq (caadr entry) 'special))))
(values (cadr entry) t)
(environment-function (si:env-parent env) fn)))))
(defun environment-macro (env macro)
(if (null env)
(values nil nil)
(let ((entry (assoc macro (si:env-functions env) :test #'equal)))
(if (and entry
(listp (cadr entry))
(eq (caadr entry) 'special))
(values (cadadr entry) t)
(environment-macro (si:env-parent env) macro)))))
);#+Genera
#+Cloe-Runtime
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env ,functions ,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
functions
(dolist (m macros)
(setf env `(,(first m) (compiler::macro . ,(second m)) ,@env)))
env)
(defun environment-function (env fn)
nil)
(defun environment-macro (env macro)
(let ((entry (getf env macro)))
(if (and (consp entry)
(eq (car entry) 'compiler::macro))
(values (cdr entry) t)
(values nil nil))))
);#+Cloe-Runtime
;;;
;;; In Xerox Lisp, the compiler and interpreter use different structures for
;;; the environment. This doesn't cause a serious problem, the parts of the
;;; environments we are concerned with are fairly similar.
;;;
#+:Xerox
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let* ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
;;;
;;; with-augmented-environment-internal is where the real work of augmenting
;;; the environment happens. Before it gets there, env had better not be NIL
;;; anymore because we have to know what kind of environment we are supposed
;;; to be building up. This is probably never a real concern in practice.
;;; It better not be because we don't do anything about it.
;;;
(defun with-augmented-environment-internal (env functions macros)
(cond
((compiler::env-p env)
(dolist (f functions)
(setq env (compiler::copy-env-with-function
env f :function)))
(dolist (m macros)
(setq env (compiler::copy-env-with-function
env (car m) :macro (cadr m)))))
(t (setq env (if (il:environment-p env)
(il:\\copy-environment env)
(il:\\make-environment)))
;; The functions field of the environment is a plist of function names
;; and conses like (:function . fn) or (:macro . expansion-fn).
;; Note that we can't smash existing entries in this plist since these
;; are likely shared with older environments.
(dolist (f functions)
(setf (il:environment-functions env)
(list* f (cons :function #'unbound-lexical-function)
(il:environment-functions env))))
(dolist (m macros)
(setf (il:environment-functions env)
(list* (car m) (cons :macro (cadr m))
(il:environment-functions env))))))
env)
(defun environment-function (env fn)
(cond ((compiler::env-p env) (eq (compiler:env-fboundp env fn) :function))
((il:environment-p env) (eq (getf (il:environment-functions env) fn)
:function))
(t nil)))
(defun environment-macro (env macro)
(cond ((compiler::env-p env)
(multiple-value-bind (type def)
(compiler:env-fboundp env macro)
(when (eq type :macro) def)))
((il:environment-p env)
(xcl:destructuring-bind (type . def)
(getf (il:environment-functions env) macro)
(when (eq type :macro) def)))
(t nil)))
);#+:Xerox
;;;
;;; In IBUKI Common Lisp, the macroexpansion environment is a three element
;;; list. The second element describes lexical functions and macros. The
;;; function entries in this list have the form
;;; (<name> . (FUNCTION . (<function-value> . nil))
;;; The macro entries have the form
;;; (<name> . (MACRO . (<macro-value> . nil)).
;;;
;;;
#+(or KCL IBCL)
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
(let ((first (first env))
(lexicals (second env))
(third (third env)))
(dolist (f functions)
(push `(,(car f) . (function . (,#'unbound-lexical-function . nil)))
lexicals))
(dolist (m macros)
(push `(,(car m) . (macro . ( ,(cadr m) . nil)))
lexicals))
(list first lexicals third)))
(defun environment-function (env fn)
(when env
(let ((entry (assoc fn (second env))))
(and entry
(eq (cadr entry) 'function)
(caddr entry)))))
(defun environment-macro (env macro)
(when env
(let ((entry (assoc macro (second env))))
(and entry
(eq (cadr entry) 'macro)
(caddr entry)))))
);#+(or KCL IBCL)
;;; --- TI Explorer --
;;; An environment is a two element list, whose car we can ignore and
;;; whose cadr is list of the local-definitions-frames. Each
;;; local-definitions-frame holds either macros or functions, but not
;;; both. Each frame is a plist of <name> <def> <name> <def> ... where
;;; <name> is a locative to the function cell of the symbol that names
;;; the function or macro, and <def> is the new def or NIL if this is function
;;; redefinition or (cons 'ticl:macro <macro-expansion-function>) if this is a macro
;;; redefinition.
;;;
;;; Here's an example. For the form:
;;; (defun foo ()
;;; (macrolet ((bar (a b) (list a b))
;;; (bar2 (a b) (list a b)))
;;; (flet ((some-local-fn (c d) (print (list c d)))
;;; (another (c d) (print (list c d))))
;;; (bar (some-local-fn 1 2) 3))))
;;; the environment arg to macroexpand-1 when called on
;;; (bar (some-local-fn 1 2) 3)
;;;is
;;;(NIL ((#<DTP-LOCATIVE 4710602> NIL
;;; #<DTP-LOCATIVE 4710671> NIL)
;;; (#<DTP-LOCATIVE 7346562>
;;; (TICL:MACRO TICL:NAMED-LAMBDA (BAR (:DESCRIPTIVE-ARGLIST (A B)))
;;; (SYS::*MACROARG* &OPTIONAL SYS::*MACROENVIRONMENT*)
;;; (BLOCK BAR ....))
;;; #<DTP-LOCATIVE 4710664>
;;; (TICL:MACRO TICL:NAMED-LAMBDA (BAR2 (:DESCRIPTIVE-ARGLIST (A B)))
;;; (SYS::*MACROARG* &OPTIONAL SYS::*MACROENVIRONMENT*)
;;; (BLOCK BAR2 ....))))
#+TI
(progn
;;; from sys:site;macros.lisp
(eval-when (compile load eval)
(DEFMACRO MACRO-DEF? (thing)
`(AND (CONSP ,thing) (EQ (CAR ,thing) 'TICL::MACRO)))
;; the following macro generates code to check the 'local' environment
;; for a macro definition for THE SYMBOL <name>. Such a definition would
;; be set up only by a MACROLET. If a macro definition for <name> is
;; found, its expander function is returned.
(DEFMACRO FIND-LOCAL-DEFINITION (name local-function-environment)
`(IF ,local-function-environment
(LET ((vcell (ticl::LOCF (SYMBOL-FUNCTION ,name))))
(DOLIST (frame ,local-function-environment)
;; <value> is nil or a locative
(LET ((value (sys::GET-LOCATION-OR-NIL (ticl::LOCF frame)
vcell)))
(When value (RETURN (CAR value))))))
nil)))
;;;Edited by Reed Hastings 13 Jan 88 16:29
(defun environment-macro (env macro)
"returns what macro-function would, ie. the expansion function"
;;some code picked off macroexpand-1
(let* ((local-definitions (cadr env))
(local-def (find-local-definition macro local-definitions)))
(if (macro-def? local-def)
(cdr local-def))))
;;;Edited by Reed Hastings 13 Jan 88 16:29
;;;Edited by Reed Hastings 7 Mar 88 19:07
(defun environment-function (env fn)
(let* ((local-definitions (cadr env)))
(dolist (frame local-definitions)
(let ((val (getf frame
(ticl::locf (symbol-function fn))
:not-found-marker)))
(cond ((eq val :not-found-marker))
((functionp val) (return t))
((and (listp val)
(eq (car val) 'ticl::macro))
(return nil))
(t
(error "we are confused")))))))
;;;Edited by Reed Hastings 13 Jan 88 16:29
;;;Edited by Reed Hastings 7 Mar 88 19:07
(defun with-augmented-environment-internal (env functions macros)
(let ((local-definitions (cadr env))
(new-local-fns-frame
(mapcan #'(lambda (fn)
(list (ticl:locf (symbol-function (car fn)))
#'unbound-lexical-function))
functions))
(new-local-macros-frame
(mapcan #'(lambda (m)
(list (ticl:locf (symbol-function (car m))) (cons 'ticl::macro (cadr m))))
macros)))
(when new-local-fns-frame
(push new-local-fns-frame local-definitions))
(when new-local-macros-frame
(push new-local-macros-frame local-definitions))
`(,(car env) ,local-definitions)))
;;;Edited by Reed Hastings 7 Mar 88 19:07
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
);#+TI
#+(and dec vax common)
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
#'(lambda (op &optional (arg nil arg-p))
(cond ((eq op :macro-function)
(unless arg-p (error "Invalid environment use."))
(lookup-macro-function arg env functions macros))
(arg-p
(error "Invalid environment operation: ~S ~S" op arg))
(t
(lookup-macro-function op env functions macros)))))
(defun lookup-macro-function (name env fns macros)
(let ((m (assoc name macros)))
(cond (m (cadr m))
((assoc name fns) :function)
(env (funcall env name))
(t nil))))
(defun environment-macro (env macro)
(let ((m (and env (funcall env macro))))
(and (not (eq m :function))
m)))
;;; Nobody calls environment-function. What would it return, anyway?
);#+(and dec vax common)
;;;
;;; In Golden Common Lisp, the macroexpansion environment is just a list
;;; of environment entries. Unless the car of the list is :compiler-menv
;;; it is an interpreted environment. The cadr of each element specifies
;;; the type of the element. The only types that interest us are GCL:MACRO
;;; and FUNCTION. In these cases the element is interpreted as follows.
;;;
;;; Compiled:
;;; (<function-name> <gensym> macroexpansion-function)
;;; (<function-name> <fn>)
;;;
;;; Interpreted:
;;; (<function-name> GCL:MACRO macroexpansion-function)
;;; (<function-name> <fn>)
;;;
;;; When in the compiler, <fn> is a gensym which will be
;;; a variable which bound at run-time to the function.
;;; When in the interpreter, <fn> is the actual function.
;;;
;;;
#+gclisp
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
(let ((new-entries nil))
(dolist (f functions)
(push (cons (car f) nil) new-entries))
(dolist (m macros)
(push (cons (car m)
(if (eq :compiler-menv (car env))
(if (eq (caadr m) 'lisp::lambda)
`(,(gensym) ,(cadr m))
`(,(gensym) ,@(cadr m)))
`(gclisp:MACRO ,@(cadr m))))
new-entries))
(if (eq :compiler-menv (car env))
`(:compiler-menv ,@new-entries ,@(cdr env))
(append new-entries env))))
(defun environment-function (env fn)
(let ((entry (lisp::lexical-function fn env)))
(and entry
(eq entry 'lisp::lexical-function)
fn)))
(defun environment-macro (env macro)
(let ((entry (assoc macro (if (eq :compiler-menv (first env))
(rest env)
env))))
(and entry
(consp entry)
(symbolp (car entry)) ;name
(symbolp (cadr entry)) ;gcl:macro or gensym
(nthcdr 2 entry))))
);#+gclisp
;;;; CMU Common Lisp version of environment frobbing stuff.
;;; In CMU Common Lisp, the environment is represented with a structure
;;; that holds alists for the functional things, variables, blocks, etc.
;;; Only the c::lexenv-functions slot is relevent. It holds:
;;; Alist (name . what), where What is either a Functional (a local function)
;;; or a list (MACRO . <function>) (a local macro, with the specifier
;;; expander.) Note that Name may be a (SETF <name>) function.
#+:CMU
(progn
(defmacro with-augmented-environment
((new-env old-env &key functions macros) &body body)
`(let ((,new-env (with-augmented-environment-internal ,old-env
,functions
,macros)))
,@body))
(defun with-augmented-environment-internal (env functions macros)
;; Note: In order to record the correct function definition, we would
;; have to create an interpreted closure, but the with-new-definition
;; macro down below makes no distinction between flet and labels, so
;; we have no idea what to use for the environment. So we just blow it
;; off, 'cause anything real we do would be wrong. We still have to
;; make an entry so we can tell functions from macros.
(c::make-lexenv :default (or env (c::make-null-environment))
:functions
(append (mapcar #'(lambda (f)
(cons (car f) (c::make-functional)))
functions)
(mapcar #'(lambda (m)
(list* (car m) 'c::macro
(coerce (cadr m) 'function)))
macros))))
(defun environment-function (env fn)
(when env
(let ((entry (assoc fn (c::lexenv-functions env) :test #'equal)))
(and entry
(c::functional-p (cdr entry))
(cdr entry)))))
(defun environment-macro (env macro)
(when env
(let ((entry (assoc macro (c::lexenv-functions env) :test #'eq)))
(and entry
(eq (cadr entry) 'c::macro)
(function-lambda-expression (cddr entry))))))
); end of #+:CMU
(defmacro with-new-definition-in-environment
((new-env old-env macrolet/flet/labels-form) &body body)
(let ((functions (make-symbol "Functions"))
(macros (make-symbol "Macros")))
`(let ((,functions ())
(,macros ()))
(ecase (car ,macrolet/flet/labels-form)
((flet labels)
(dolist (fn (cadr ,macrolet/flet/labels-form))
(push fn ,functions)))
((macrolet)
(dolist (mac (cadr ,macrolet/flet/labels-form))
(push (list (car mac)
(convert-macro-to-lambda (cadr mac)
(cddr mac)
(string (car mac))))
,macros))))
(with-augmented-environment
(,new-env ,old-env :functions ,functions :macros ,macros)
,@body))))
#-Genera
(defun convert-macro-to-lambda (llist body &optional (name "Dummy Macro"))
(let ((gensym (make-symbol name)))
(eval `(defmacro ,gensym ,llist ,@body))
(macro-function gensym)))
#+Genera
(defun convert-macro-to-lambda (llist body &optional (name "Dummy Macro"))
(si:defmacro-1
'sys:named-lambda 'sys:special (make-symbol name) llist body))
;;;
;;; Now comes the real walker.
;;;
;;; As the walker walks over the code, it communicates information to itself
;;; about the walk. This information includes the walk function, variable
;;; bindings, declarations in effect etc. This information is inherently
;;; lexical, so the walker passes it around in the actual environment the
;;; walker passes to macroexpansion functions. This is what makes the
;;; nested-walk-form facility work properly.
;;;
(defmacro walker-environment-bind ((var env &rest key-args)