-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.lisp
1999 lines (1696 loc) · 66.8 KB
/
parse.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
;;;; -*- Mode: Lisp -*-
;;;; parse.lisp --
;;;; CLAST exports one main function that "parses" CL code into an AST
;;;; tree. The "analysis functions" "traverse" (or "walk, or "visit")
;;;; the resulting AST.
;;;;
;;;; Parsing is CLOS based.
;;;; See the file COPYING for copyright and license information.
;;;; Portions of code lifted from IT.BESE.ARNESI and CL-WALKER.
(in-package "CLAST")
#| This is moved to 'clast-parse-protocol.lisp'
;;;;===========================================================================
;;;; Interface
;;;;---------------------------------------------------------------------------
;;;; PARSE, PARSE-FORM
(defgeneric parse (form &rest keys
&key
enclosing-form
macroexpand
environment
&allow-other-keys)
(:documentation "Parses a form in a given 'environment'.
The methods of this generic function return a AST 'node' (a CLAST-ELEMENT)
and the - possibly modified - ENVIRONMENT.
The methods of PARSE dispatch on 'atomic' and on 'compound' (i.e.,
CONS) forms. Atomic forms - numbers, string, arrays, and symbols -
are dealt with directly. Compound forms are dispatched to PARSE-FORM.
Arguments and Values:
form : the form to be parsed.
keys : the collection of key-value pairs passed to the call.
enclosing-form : the form that \"contains\" the form beling parsed.
environment : the environment in which the form is being parsed; it
defaults to *CL-GLOBAL-ENV*
element : a CLAST-ELEMENT representing the AST node just parsed.
environment1 : the environment resulting from parsing the FORM.
See Also:
*CL-GLOBAL-ENV*")
)
(defgeneric parse-form (op form &rest keys
&key
enclosing-form
macroexpand
environment
&allow-other-keys)
(:documentation "Parses a form in a given 'ENVIRONMENT' given its 'op'.
The methods of PARSE-FORM descend recursively in a form, by
dispatching on the form 'operator'. Each sub-form is passed,
recursively to PARSE.
Arguments and Values:
form : the form to be parsed.
keys : the collection of key-value pairs passed to the call.
enclosing-form : the form that \"contains\" the form beling parsed.
environment : the environment in which the form is being parsed; it
defaults to *CL-GLOBAL-ENV*
element : a CLAST-ELEMENT representing the AST node just parsed.
environment1 : the environment resulting from parsing the FORM.
See Also:
*CL-GLOBAL-ENV*, PARSE")
)
;;;;---------------------------------------------------------------------------
;;;; Conditions
(define-condition ast-parse-error (parse-error simple-error)
()
(:default-initargs
:format-control ""
:format-arguments ())
(:report
(lambda (ape stream)
(format stream
"CLAST: ~?"
(simple-condition-format-control ape)
(simple-condition-format-arguments ape))))
)
(define-condition unknown-operator-error (ast-parse-error)
((op :reader unknown-operator-name
:initarg :name))
(:report
(lambda (usoe stream)
(format stream "CLAST: Unknownw operator ~S is not handled."
(unknown-operator-name usoe))))
)
(define-condition unknown-special-operator-error (unknown-operator-error)
()
(:report
(lambda (usoe stream)
(format stream "CLAST: Special form ~S not handled."
(unknown-operator-name usoe))))
)
;;; All of the above is in 'clast-parse-protocol.lisp'
|#
;;;;===========================================================================
;;;; Implementation
(declaim (ftype (function (t) boolean) is-lambda-form)
(inline is-lambda-form)
)
(declaim (ftype (function (cons) t) operator)
(ftype (function (cons) list) arguments)
(inline operator arguments)
)
(defun operator (form)
(declare (type cons form))
(first form))
(defun arguments (form)
(declare (type cons form))
(rest form))
(declaim (ftype (function (t) boolean)
is-lambda-form
is-declaration
is-quoted-form
is-compound-form)
(inline is-lambda-form
is-declaration
is-quoted-form
is-compound-form)
)
(defun is-lambda-form (form)
(and (listp form) (eq (first form) 'lambda)))
(defun is-declaration (form)
(and (listp form) (eq (first form) 'declare)))
(defun is-quoted-form (form)
(and (listp form) (eq (first form) 'quote)))
(defun is-compound-form (form)
(consp form))
;;;;---------------------------------------------------------------------------
;;;; PARSE and PARSE-FORM methods.
(eval-when (:load-toplevel :compile-toplevel :execute)
(defmacro def-parse-self-evaluating-method (self-evaluating-type)
`(defmethod parse ((form ,self-evaluating-type)
&rest keys
&key
(environment *cl-global-env*)
macroexpand
enclosing-form
&allow-other-keys)
(declare (ignore keys macroexpand))
(values (make-instance 'constant-form
:type (type-of form)
:source form
:top enclosing-form
:value form)
environment)))
)
#|
(defmethod parse ((form number)
&key
environment
enclosing-form
&allow-other-keys)
(values (make-instance 'constant-form
:type (type-of form)
:source form
:top enclosing-form
:value form)
environment))
|#
(def-parse-self-evaluating-method number)
(def-parse-self-evaluating-method string)
(def-parse-self-evaluating-method array)
(defun build-variable-reference (v kind local-p decls
&optional
enclosing-form
(environment *cl-global-env*)
)
(declare (type symbol v)
(type boolean local-p)
(type (member :special :lexical nil) kind)
)
(let* ((var-decl-type (assoc 'type decls :test #'eq))
(var-type (if var-decl-type
(cdr var-decl-type)
t))
)
(values
;; Value 1
(ecase kind
(:special (make-instance 'special-variable-ref
:symbol v
:local local-p
:source v
:top enclosing-form
:type var-type))
(:lexical (make-instance 'variable-ref
:symbol v
:local local-p
:source v
:top enclosing-form
:type var-type))
((nil) (make-instance 'free-variable-ref
:symbol v
:local (assert (null local-p)) ; This'd better be NIL.
:source v
:top enclosing-form
:type var-type))
)
;; Value 2
environment)
))
(defun build-constant-reference (v kind local-p decls
&optional
enclosing-form
(environment *cl-global-env*)
)
(declare (type symbol v)
(type boolean local-p)
;; (type (member :constant) kind)
(ignore kind)
)
(let* ((var-decl-type (assoc 'type decls :test #'eq))
(var-type (if var-decl-type
(cdr var-decl-type)
(type-of (symbol-value v))))
)
(values
(make-instance 'constant-ref
:symbol v
:value (symbol-value v)
:local local-p ; Also this'd better be NIL.
:source v
:top enclosing-form
:type var-type)
environment)
))
;;; parse symbol
(defmethod parse ((s symbol)
&rest keys
&key
(environment *cl-global-env*)
macroexpand
enclosing-form
is-bare-name
&allow-other-keys)
(declare (type list keys)
(type boolean macroexpand is-bare-name)
(ignore is-bare-name))
(multiple-value-bind (kind local-p decls)
(variable-information s environment)
(case kind
((:special :lexical)
(build-variable-reference s kind local-p decls enclosing-form environment))
(:constant
(build-constant-reference s kind local-p decls enclosing-form environment))
(:symbol-macro
(if macroexpand ; CHANGE THIS to keep on macroexpanding and
; save the macroexpansion
(apply #'parse (macroexpand-1 s) keys)
(values
(make-instance 'symbol-macro-ref
:symbol s
:local local-p
:source s
:top enclosing-form
:type t)
environment)))
((nil)
;; We may have three cases:
;; 1 - The symbol is a variable that is "free"
;; 2 - The symbol is a block name
;; 3 - The symbol is a tag name
;; 4 - The symbol appears as an unevaluated 'name' in certain
;; forms (e.g., a :READER in DEFCLASS); in this case we
;; resort to the :IS-BARE-NAME parameter and the symbol is
;; just represented as a bare SYMBOL-REF.
;; (Not yet implemented and unused.)
(cond ((eq :block (block-information s environment)) ; Case 1.
(values
(make-instance 'block-name
:symbol s
:source s
:top enclosing-form)
environment))
((eq :tag (tag-information s environment)) ; Case 2.
(values
(make-instance 'go-tag
:symbol s
:source s
:top enclosing-form)
environment))
;; (is-bare-name ...) ; Not yet implemented.
(t ; Case 1.
(build-variable-reference s kind local-p decls enclosing-form environment)
)))
(:otherwise
(error "Unrecognized (implementation dependent) variable kind ~S."
kind)
)
)))
;;; parse cons
;;; The reliance on "constantp" is an inherently
;;; implementation-dependent.
;;; Now I explore alternatives.
(defmethod parse ((form cons)
&rest keys
&key
(environment *cl-global-env*)
enclosing-form
&allow-other-keys)
(if (and (ignore-errors
(constantp form
(if (typep environment 'environment)
(environment-env environment)
environment)) ; This is VERY iffy. CONSTANTP
; is inherently implementation
; dependent.
)
;; The IGNORE-ERRORS may be too strict, but is works around
;; (yet) another Allegro indiosyncracy.
(not (is-quoted-form form))
)
(values (make-instance 'constant-form ;
:type (type-of form)
:source form
:top enclosing-form
:value form)
environment)
(apply #'parse-form (operator form) form keys)
))
#|
(defmethod parse ((form cons)
&rest keys
&key
environment
enclosing-form
&allow-other-keys)
(if (not (is-quoted-form form))
;; Only (real) quoted forms are "constant"; see above.
(values (make-instance 'constant-form ;
:type (type-of form)
:source form
:top enclosing-form
:value form)
environment)
(apply #'parse-form (operator form) form keys)
))
|#
;;;;---------------------------------------------------------------------------
;;;; parse-form --
(defmethod parse-form ((op symbol) form ; FORM is (OP . ARGS)
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(let ((args (arguments form)))
(multiple-value-bind (kind local-p info)
(function-information op environment)
(multiple-value-bind (parsed-args resulting-env)
(apply #'parse-args args keys)
(values
;; value 1
(ecase kind
((nil)
(make-instance 'application
:operator (make-instance 'function-name-ref
:symbol op
:local local-p
:source op
:top enclosing-form
:type 'function
)
:arguments parsed-args))
(:function
(let* ((f-type-decl (assoc 'ftype info :test #'eq))
(f-type (if f-type-decl
(cdr f-type-decl)
'function))
)
(make-instance 'function-application
:operator (make-instance 'function-name-ref
:symbol op
:local local-p
:source op
:top enclosing-form
:type f-type
)
:arguments parsed-args)))
(:macro
(multiple-value-bind (expanded-form expanded-p)
(if macroexpand
(macroexpand-1 form
(get-implementation-env environment))
(values form nil))
(make-instance 'macro-application
:operator (make-instance 'macro-name-ref
:symbol op
:local local-p
:source op
:top enclosing-form
:type 'macro)
:arguments parsed-args
:expansion (if expanded-p
(apply #'parse expanded-form keys)
nil)))
)
((:special-form :special-operator) ; Allegro, again messes things up.
(error 'unknown-special-operator-error :name op))
)
;; value 2
resulting-env)
))))
(defun parse-form-seq (forms
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
;; This is tricky. I "should" be able to "just parse" the list of
;; forms or arguments, but, in reality, I may have to deal with
;; possible side effects on the environment.
;;
;; Also, this is still incorrect for macro arguments, which may be
;; arbitrarily destructured.
(declare (type list forms)
(ignore enclosing-form macroexpand))
;; (format t "~&~%>>> PARSE-FORM-SEQ~%")
(loop with acc-env = (augment-environment environment)
for a in forms
for (a-form a-env)
= (multiple-value-list (apply #'parse a :environment acc-env keys))
then
;; (multiple-value-list (apply #'parse a :environment a-env keys))
(multiple-value-list (apply #'parse a :environment acc-env keys))
collect a-form into a-forms
;; do (format t "~&>>> Form ~S~%" a-form)
do (setf acc-env a-env)
;; do (describe a-env)
;; do (terpri)
finally (return (values a-forms acc-env))))
(defun parse-args (args &rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore enclosing-form environment macroexpand))
(apply #'parse-form-seq args keys))
(defun parse-binding-seq (bindings
&rest keys
&key
(environment *cl-global-env*)
enclosing-form
macroexpand
&allow-other-keys)
;; Similar to PARSE-FORM-SEQ but I need to update the environment
;; while going...
(declare (ignore enclosing-form macroexpand))
(loop with result-env = (augment-environment environment)
for (var value) in bindings
for (bind-form bind-env)
= (multiple-value-list (apply #'parse value :environment result-env keys))
collect (list var bind-form) into resulting-bindings
do (setf result-env
(augment-environment bind-env
:variable (list var)))
finally (return (values resulting-bindings result-env))))
;;; parse-form --
;;; Parsing of forms like ((lambda ....) . args) and similar things.
(defmethod parse-form ((op cons) form ; FORM is (OP . ARGS)
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore environment macroexpand))
(multiple-value-bind (parsed-args env)
(apply #'parse-args (arguments form) keys)
(values
;; Value 1
(if (is-lambda-form op)
(make-instance 'lambda-application
:operator (apply #'parse-lambda-form op
:environment env
keys)
:arguments parsed-args
:top enclosing-form
:source form
)
(make-instance 'functional-operator-application
:operator (apply #'parse op :environment env keys)
:arguments parsed-args
:top enclosing-form
:source form))
;; Value 2
env)))
;;;---------------------------------------------------------------------------
;;; Specialized PARSE-FORMs.
;;; At a minimum, we need all the special operators of Figure 3.2 of ANSI
;;; Section 3.1.2.1.2.1
(defmethod parse-form ((op (eql 'block)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
(let ((block-name (second form)) ; Should add this a "lexical tag"
; in the environment if not nil.
)
(values
(make-instance 'block-form
:name block-name
:body-env environment
:top enclosing-form
:source form
;; The use of PARSE-FORM-SEQ must be reviewed.
:progn (apply #'parse-form-seq (cddr form) keys)
)
environment)))
(defmethod parse-form ((op (eql 'return-from)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
(let ((block-name (second form))
(result (third form))
)
(values
(make-instance 'return-from-form
:name block-name
:result (apply #'parse result keys)
:top enclosing-form
:source form
)
environment)
))
(defmethod parse-form ((op (eql 'tagbody)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
(let* ((tags-n-stmts (rest form))
(tags (remove-if (complement #'symbolp) tags-n-stmts))
;; (stmts (remove-if #'symbolp tags-n-stmts))
(ne (augment-environment environment :tag tags))
(parsed-forms (apply #'parse-form-seq tags-n-stmts
:environment ne
keys))
)
(values
(make-instance 'tagbody-form
:tags tags
:body parsed-forms
:source form
:top enclosing-form)
environment)
))
(defmethod parse-form ((op (eql 'catch)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
(values
(make-instance 'catch-form
:tag (apply #'parse (second form) keys)
:body-env environment
:top enclosing-form
:source form
;; The use of PARSE-FORM-SEQ must be reviewed.
:progn (apply #'parse-form-seq (cddr form) keys)
)
environment))
(defmethod parse-form ((op (eql 'declare)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
;; This is a complicated one as it must be able to change the
;; environmment.
(declare (ignore keys))
(multiple-value-bind (decls-forms new-env)
(parse-declarations (rest form)
environment
enclosing-form
macroexpand)
(values
(make-instance 'declaration-form
:declarations decls-forms
:resulting-environment new-env
:top enclosing-form
:source form)
new-env)
))
(defmethod parse-form ((op (eql 'progn)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
;; Within a PROGN I may define things, especially it the form is at
;; "top level".
;; Hence I must keep track of the environment as I move along.
(multiple-value-bind (progn-forms progn-env)
(apply #'parse-form-seq (rest form) :environment environment keys)
(values
(make-instance 'progn-form
:top enclosing-form
:source form
:body-env progn-env
:progn progn-forms
)
progn-env)
))
(defmethod parse-form ((op (eql 'progv)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
(destructuring-bind (progv-kwd symbols values &body body)
form
(declare (ignore progv-kwd))
(warn "Parsing of PROGV forms may not be completely precise.")
(values
(make-instance 'progv-form
:top enclosing-form
:source form
:body-env environment
:symbols (apply #'parse symbols keys)
:values (apply #'parse values keys)
:progn (apply #'parse-form-seq body keys)
)
environment)
))
(defmethod parse-form ((op (eql 'prog)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
(destructuring-bind (prog-kwd vars &body decls-tags-stmts)
form
(declare (ignore prog-kwd))
(let* ((normalized-bindings (listify vars))
(parsed-bindings (mapcar (lambda (b)
(list (first b)
(apply #'parse (second b)
keys)))
normalized-bindings))
(decls (remove-if (complement #'is-declaration)
decls-tags-stmts))
(tags-stmts (remove-if #'is-declaration
decls-tags-stmts)) ; Yeah: two REMOVE-IFs
; in a row like these
; are inefficient!
(ne (augment-environment environment
:variable (mapcar #'first
normalized-bindings)
;; Missing :declare...
))
)
(declare (ignore decls))
(values
(make-instance 'prog-form
:binds parsed-bindings
:body (apply #'parse `(tagbody ,.tags-stmts)
:environment ne
keys)
:body-env ne
:top enclosing-form
:source form)
environment)
)))
(defmethod parse-form ((op (eql 'prog*)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
(destructuring-bind (prog*-kwd vars &body decls-tags-stmts)
form
(declare (ignore prog*-kwd))
(let ((normalized-bindings (listify vars)))
(multiple-value-bind (parsed-bindings ne)
(apply #'parse-binding-seq normalized-bindings keys)
(let ((decls (remove-if (complement #'is-declaration)
decls-tags-stmts))
(tags-stmts (remove-if #'is-declaration
decls-tags-stmts)) ; Yeah: two REMOVE-IFs
; in a row like these
; are inefficient!
)
;; Should add declarations to environment.
(declare (ignore decls))
(values
(make-instance 'prog*-form
:binds parsed-bindings
:body (apply #'parse `(tagbody ,.tags-stmts)
:environment ne
keys)
:body-env ne
:top enclosing-form
:source form)
environment)
)))))
(defmethod parse-form ((op (eql 'eval-when)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
;; This is essentially a PROGN. Treat it as such FTTB (2024-06-05).
(multiple-value-bind (ev-forms ev-env)
(apply #'parse-form-seq (cddr form) :environment environment keys)
(values
(make-instance 'eval-when-form
:situations (second form)
:top enclosing-form
:source form
:body-env ev-env
:progn ev-forms
)
ev-env))
)
(defmethod parse-form ((op (eql 'declaim)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore keys))
(multiple-value-bind (pdecls denv)
(parse-declarations (rest form)
environment
enclosing-form
macroexpand)
;; (mapcar #'describe pdecls)
;; (terpri)
;; Now I need to collect all the declarations and change to
;; (global) environment.
(let ((type-decls)
(ftype-decls)
(decl-decls)
(optimize-decls)
(special-decls)
(other-decls)
(vars)
(funs)
(declaim-env denv)
)
(declare (type list
type-decls
ftype-decls
decl-decls
optimize-decls
special-decls
other-decls
vars
funs
))
;; This can become a function.
(dolist (d pdecls)
(etypecase d
(type-declaration-specifier-form (push d type-decls))
(ftype-declaration-specifier-form (push d ftype-decls))
(id-declaration-specifier-form
(case (declaration-specifier-form-identifier d)
(declaration (push d decl-decls))
(optimize (push d optimize-decls))
(special (push d special-decls))
(t (push d other-decls)))
))
)
(psetq type-decls (nreverse type-decls)
ftype-decls (nreverse ftype-decls)
decl-decls (nreverse decl-decls)
optimize-decls (nreverse optimize-decls)
special-decls (nreverse special-decls)
other-decls (nreverse other-decls)
)
(psetq vars
(flatten
(append (mapcar #'declaration-type-spec-symbols type-decls)
(mapcar #'declaration-type-spec-symbols special-decls)))
funs
(mapcan #'declaration-type-spec-symbols ftype-decls)
)
;; (format t "~&>>> VARS ~S~%>>> FUNS ~S~%>>> DECL ~S~2%"
;; vars funs (rest form))
(setq declaim-env
(augment-environment declaim-env
:variable vars
:function funs
:declare (rest form)
:global t))
;; (describe declaim-env)
;; (format t "~&>>> VAR INFO 'second ~S~%"
;; (multiple-value-list (variable-information 'second declaim-env)))
(values
(make-instance 'declaim-form
:top enclosing-form
:source form
:declarations pdecls
:resulting-environment declaim-env
)
declaim-env
))
))
(defmethod parse-form ((op (eql 'flet)) form
&rest keys
&key
enclosing-form
(environment *cl-global-env*)
macroexpand
&allow-other-keys)
(declare (ignore macroexpand))
;; The body is a PROGN; treat is as such.
(let* ((functions (second form))
(body (cddr form))
(binds (mapcar (lambda (f)
(apply #'parse-local-function f keys))
functions))