-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmul-flavors.lisp
1438 lines (1363 loc) · 61.7 KB
/
mul-flavors.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
;;; ----------- M(ITRE)U(niversity of)L(owell)-FLAVORS PACKAGE -----------
;;; First running version by G. Pecelli, 2/86 ULowell
;;; Modified by S. Markowitz, 4/86 MITRE Corp
;;; Modified by M.J. Prelle, S. Goldkind, J. C. Fohlin 11/88 MITRE Corp
;;; Modified by G. Pecelli 12/88 ULowell
;;; Modified by J.I. Leivent to add defwhoppers 2/89 MITRE Corp
;;; ----------------------------------------------------------------------
;;; This is a joint effort, implemented as part of other projects that needed
;;; a rudimentary FLAVORS package over Lisp environments that did not possess
;;; such a package. It is put in the public domain and distributed by the
;;; Computer Science Department of the University of Lowell.
;;; Users are permitted to modify this package and to further distribute it
;;; as a public domain package without any payment of royalties. They are
;;; required to acknowledge the provenance of this code.
;;; COPYRIGHT: MITRE Corporation and University of Lowell
;;; === WARNINGS: This package implements only part of the FLAVORS definition.
;;; It is also non-standard in the sense that its inheritance graph handling
;;; is based on CLOS (Common Lisp Object System) rather than FLAVORS. This
;;; should not be a problem in most garden variety uses, and should make
;;; code portability to CLOS easier.
;;; ===
(provide 'mul-flavors)
(in-package 'mul-flavors)
(export '(defflavor defmethod flavor flavor-instance send
make-instance instantiate-flavor cf-mulf
*out-file* *all-flavor-names* lexpr-send handles-p get-handler-for
instancep instance-typep describe-flavor set-in-instance
symeval-in-instance defwhopper continue-whopper lexpr-continue-whopper
;;; --- Extra Functions
keyword-instance-environment instance-environment instance-keywords
flavor-variables my-flavor-variables flavor-default-environment))
;;; The next two globals are used by CF-MULF, the file compiler function
;;; for MUL-Flavors
(defvar *compiling* nil)
(defvar *all-meth-lists* nil)
(defvar *all-flavor-names* nil)
;;; --> PATTERN DEFINITION.
;;; This is the definition of the class pattern. It attempts to provide most
;;; of the fields normally associated with a flavor. Not everything is
;;; supported. The methods are held in a hash table, and no COMBINED-METHODS
;;; function is provided: the body of SEND will collect and execute.
;;; ---
(defstruct flavor
name
vars
known-lexical-ivs
initable-instance-variables
init-keywords
default-init-plist
required-init-keywords
depends-on
depended-on-by
precedence
methods )
;;; --> END PATTERN DEFINITION
;;; --> FLAVOR-PRINTER
;;; This function providies the default printing capability associated
;;; with the flavor structure
;;; ---
(defun flavor-printer (object &optional (stream t) (depth 1))
"function interface to :print-self method"
(if (handles-p object :print-self)
(send object :print-self stream depth)
(format stream "#<~a ~d>"
(flavor-instance-class-name object)
(flavor-instance-unique-number object))))
;;; --> END FLAVOR-PRINTER
;;; --> INSTANCE DEFINITION
;;; This is the definition of the defstruct that holds a flavor object instance.
;;; It contains fields for the class name, methods and instance variables.
;;; Note that it knows how to print itself without having a method to do it.
;;; The Vanilla Flavor HAS a :print-self method to which every flavor will
;;; then respond. The print function for the defstruct will simply print to
;;; the screen as output from a function or as part of an error message.
;;; ---
(defstruct (flavor-instance :named (:print-function flavor-printer))
class-name ; class name
(unique-number (get-internal-real-time)) ; for identification when printing
vars ; ((var . value) ... ) instance variables
)
;;; --> END INSTANCE DEFINITION
;;; --> DEFFLAVOR
;;; This is the flavor definition macro. The syntax is a subset of the Zetalisp
;;; Flavors syntax. No keywords are supported at the moment.It is the same as
;;; :settable-instance-variables in ZetaLisp. This keyword should be added
;;; on any code that is meant to be transported.
;;; ---
;;; INPUT: an object name; a list of instance variables with possible default
;;; values; a list of parent flavors in order of access; keywords.
;;; OUTPUT: the object name.
;;; SIDE-EFFECTS: a pattern structure is built, accessible from the object, and
;;; the flavor name is defined as a type.
;;; USER-DEFINED-CALLED: defflavor2, write-form, defflavor2-load,
;;; defflavor2-compile.
;;; ---
;;; Example of call:
;;; (defflavor vehicle ((c 3) (d 4)) () :settable-instance-variables)
;;; (defflavor truck ((a 1) (b 2) (c 5))
;;; (vehicle)
;;; (:initable-instance-variables a b)
;;; (:settable-instance-variables b c))
;;; ---
(defmacro defflavor (a-flavor-name class-vars depends-on &rest keyword-props)
(cond (*compiling* ;we are file-compiling a flavor definition
(write-form
`(defflavor2-load
',a-flavor-name ',class-vars ',depends-on ',keyword-props))
`(defflavor2-compile
',a-flavor-name ',class-vars ',depends-on ',keyword-props))
(t ;definition in core
`(defflavor2
',a-flavor-name ',class-vars ',depends-on ',keyword-props))))
;;; --> END DEFFLAVOR
;;; --> DEFFLAVOR2
;;; This function creates the flavor template. It creates a pattern structure,
;;; used by make-instance. It collects all parents' variables, for convenience.
;;; ---
;;; INPUT: flavor name; list of instance variables with optional default
;;; initialization; depends-on list; keyword properties list.
;;; OUTPUT: flavor name, with pattern on its 'flavor-pattern property.
;;; SIDE-EFFECTS: structure creation and binding.
;;; USER-DEFINED-ACCESSED: create-predicate, make-flavor-pattern, flavor-methods,
;;; merge-class-super-methods, get-class-methods, flavor-depends-on,
;;; make-access-methods, flavor-precedence
;;; ASSUMPTIONS: all instance variables are assumed initable
;;; ---
;;; Example of call (should NOT be called by the user)
;;; (DEFFLAVOR2 VEHICLE ((C 3) (D 4)) NIL (:SETTABLE-INSTANCE-VARIABLES))
;;; (DEFFLAVOR2 TRUCK ((A 1) (B 2) (C 5))
;;; (VEHICLE)
;;; ((:INITABLE-INSTANCE-VARIABLES A B) (:SETTABLE-INSTANCE-VARIABLES B C)))
;;; ---
(defun defflavor2 (a-flavor-name class-vars depends-on keyword-props)
(let ((pattern nil))
(create-predicate a-flavor-name)
(setq pattern
(make-flavor-pattern a-flavor-name class-vars
depends-on keyword-props))
(setf (flavor-methods pattern)
(merge-class-super-methods
(get-class-methods (flavor-depends-on pattern))
(flavor-precedence pattern)))
(make-access-methods a-flavor-name class-vars keyword-props)
;(format t "~s~%" a-flavor-name)
a-flavor-name))
;;; --> END DEFFLAVOR2
;;; --> MAKE-FLAVOR-PATTERN
;;; This function sets up the structure holding the flavor pattern. It returns
;;; an incomplete pattern, since it has no methods yet. The methods will be
;;; added by the appropriate version of defflavor2 (plain, load, compile..).
;;; This version understands :required-instance-variables (a nice thing to have
;;; in real flavors, since it allows a mixin to "see" ivs from other mixins
;;; directly without having to go through iv accessor methods). The hack done
;;; here is just to add the :required-instance-variable ivs in to the flavor as
;;; if they were ivs defined directly for the flavor - this gives the desired
;;; result, but doesn't do the same type of error checking that real flavors
;;; would do (real flavors would make sure that this flavor gets mixed in with
;;; one or more that do provide the required instance variables).
;;; ---
;;; INPUT: a flavor name, class variable list with initialization values,
;;; inheritance list in precedence order, keyword list
;;; OUTPUT: the flavor pattern structure
;;; SIDE-EFFECTS: creates a flavor structure and adds it to the property list
;;; of a-flavor-name
;;; USER-DEFINED-CALLED: update-parents, merge-class-super-vars, get-class-vars,
;;; make-flavor, set-precedence, option-find
;;; ---
(defun make-flavor-pattern (a-flavor-name class-vars depends-on keyword-props)
(let ((key-class-vars nil)
(all-key-class-vars nil)
(i-s-vs nil))
(if (and (null depends-on) ;top of a hierarchy
(not (member :no-vanilla-flavor keyword-props)))
(setq depends-on (list 'user::vanilla-flavor)))
(update-parents a-flavor-name depends-on)
(setq key-class-vars ;make everything uniform with "right" names
(mapcar #'(lambda (x)
(if (consp x)
(cons (intern (symbol-name (car x)) 'keyword)
(cadr x))
(cons (intern (symbol-name x) 'keyword) nil)))
(append (option-find :required-instance-variables
keyword-props)
class-vars))) ;done with vars at bottom level
(setq all-key-class-vars
(merge-class-super-vars key-class-vars (get-class-vars depends-on)))
;add vars from upper levels - lower names prevail
(setq i-s-vs
(if (or (member :initable-instance-variables keyword-props)
(member :settable-instance-variables keyword-props))
(mapcar #'car key-class-vars) ;(:A :B :C) ;no only mine???
(option-find :initable-instance-variables keyword-props)));(A B) bug?
(setf (get a-flavor-name 'flavor-pattern)
(make-flavor
:name a-flavor-name
:vars all-key-class-vars
:known-lexical-ivs (mapcar #'(lambda (x)
(intern (symbol-name (car x))))
all-key-class-vars)
:depends-on depends-on
:depended-on-by nil
:precedence (set-precedence a-flavor-name depends-on)
:initable-instance-variables i-s-vs ;locally declared
:init-keywords (remove-duplicates ;all: also inherited
(append
(option-find :init-keywords keyword-props)
i-s-vs
(apply #'append
(mapcar #'flavor-init-keywords
(mapcar #'(lambda (x)
(get x 'flavor-pattern))
depends-on)))))
:default-init-plist (option-find :default-init-plist keyword-props)
:required-init-keywords
(option-find :required-init-keywords keyword-props)
:methods nil))
(if (not (member a-flavor-name *all-flavor-names*))
(push a-flavor-name *all-flavor-names*))
(get a-flavor-name 'flavor-pattern)))
;;; --> END MAKE-FLAVOR-PATTERN
;;; --> MAKE-ACCESS-METHODS
;;; This function creates the automatically generated methods for a flavor
;;; ---
;;; INPUT: a list of (var.val) pairs and the keyword options to defflavor
;;; OUTPUT: a list of (method-name (:primary method-code-pointer)) pairs
;;; SIDE-EFFECTS: none.
;;; USER-DEFINED-CALLED: create-accessor-method, create-setter-method
;;; ---
;;; Example of call (no user calls)
;;; (MAKE-ACCESS-METHODS ((:A 1) (:B 2) (:C 5))
;;; ((:INITABLE-INSTANCE-VARIABLES A B)
;;; (:SETTABLE-INSTANCE-VARIABLES B C)))
;;; ---
(defun make-access-methods (a-flavor-name slots keyword-props)
(let ((the-gets nil)
(the-sets nil))
(if (or (member :gettable-instance-variables keyword-props)
(member :settable-instance-variables keyword-props)
(member :initable-instance-variables keyword-props))
(setq the-gets
(mapcar #'(lambda (x) (if (consp x) (car x) x)) slots))
(setq the-gets
(remove-duplicates
(append
(option-find :initable-instance-variables keyword-props)
(option-find :gettable-instance-variables keyword-props)
(option-find :settable-instance-variables keyword-props)))))
(setq the-gets ;make keywords and intern them in right package
(mapcar #'(lambda (item) (intern (string item) 'keyword))
the-gets))
(if (member :settable-instance-variables keyword-props) ;all settable
(setq the-sets
(mapcar #'(lambda (x) (if (consp x) (car x) x)) slots))
(setq the-sets ;any settable??
(option-find :settable-instance-variables keyword-props)))
(setq the-sets ;make keywords and intern them in right package
(mapcar #'(lambda (item) (intern (string item) 'keyword))
the-sets))
(if (member :no-vanilla-flavor keyword-props)
(create-init-method a-flavor-name))
(dolist (item the-gets)
(create-accessor-method a-flavor-name item))
(dolist (item the-sets)
(create-setter-method a-flavor-name item))))
;;; --> END MAKE-ACCESS-METHODS
;;; --> CREATE-INIT-METHOD
;;; This function creates the :init method for a given flavor class. It checks
;;; that the initialization is legal, while also merging any default
;;; initialization information into the currently specified init-plist
;;; ---
;;; INPUT: a flavor name
;;; OUTPUT: undefined
;;; SIDE-EFFECTS: it creates the :init method for the given flavor
(defun create-init-method (a-flavor-name)
(eval `(defmethod (,a-flavor-name :init) (init-plist)
(let ((fl-patt (get (flavor-instance-class-name user::self)
'flavor-pattern)))
(do ((def-init-plist (flavor-default-init-plist fl-patt)
(cddr def-init-plist)))
((null def-init-plist))
(if (not (assoc (car def-init-plist) init-plist)) ;not there yet
(push (cons (car def-init-plist) (eval (cadr def-init-plist)))
init-plist)))
(setf (flavor-instance-vars user::self)
(nconc init-plist (flavor-instance-vars user::self)))))))
;;; --> END CREATE-INIT-METHOD
;;; --> CREATE-ACCESSOR-METHOD
;;; This creates a unique function that will access the requested structure
;;; field.
;;; ---
;;; INPUT: slot name and offset number
;;; OUTPUT: the unique function assigned to read it
;;; SIDE-EFFECTS: function definition
;;; USER-DEFINED-CALLED: none.
;;; ---
(defun create-accessor-method (a-flavor-name slot-name)
(let ((non-key-slot-name (intern (string slot-name))))
(eval `(defmethod (,a-flavor-name ,slot-name) () ,non-key-slot-name))))
;;; --> END CREATE-ACCESSOR-METHOD
;;; --> CREATE-SETTER-METHOD
;;; This creates a unique function that will set the requested structure field.
;;; The creation of the setter method makes use of the way in which DEFMETHOD
;;; handles Instance Variables: in particular, the SETQ will be changed to a
;;; SETF called on the appropriate accessor for the flavor instance
;;; ---
;;; INPUT: slot name and offset number
;;; OUTPUT: the unique function assigned to set it
;;; SIDE-EFFECTS: function definition
;;; USER-DEFINED-CALLED: none.
;;; ---
(defun create-setter-method (a-flavor-name slot-name)
(let ((non-key-slot-name (intern (string slot-name)))
(set-name
(intern (concatenate 'string "SET-" (string slot-name)) 'keyword)))
(eval `(defmethod (,a-flavor-name ,set-name) (val)
(setq ,non-key-slot-name val)))))
;;; --> END CREATE-SETTER-METHOD
;;;--> FETCH-FLAVORS
;;; This function goes up the hierarchy and collects all the flavors in th
;;; class structure of a flavor pattern. This assumes that remove-duplicates
;;; remove the leftmost occurrences of the name...
;;; ---
;;; INPUT: a flavor name
;;; OUTPUT: a flavor list
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: flavor-depends-on, fetch-flavors.
;;; ---
(defun fetch-flavors (a-flavor-name)
(let ((flavors (flavor-depends-on (get a-flavor-name 'flavor-pattern))))
(if (null flavors)
(list a-flavor-name) ;end of the line
(cons a-flavor-name
(remove-duplicates
(apply #'append (mapcar #'fetch-flavors flavors)))))))
;;; --> END FETCH-FLAVORS
;;; --> CREATE-PREDICATE
;;; This function creates the predicate function used to check if something is
;;; a particular type of object - flavor kind
;;; ---
;;; INPUT: a flavor name
;;; OUTPUT: nothing usable
;;; SIDE-EFFECTS: two functions are created, both capable of checking that
;;; the object in question is of the right type.
;;; USER-DEFINED-CALLED: f-inst-check, fetch-flavors, flavor-instance-class-name
;;; ---
(defun create-predicate (a-flavor-name)
(let ((pred-name (intern (format nil "~a-P" a-flavor-name))))
(compile pred-name `(lambda
;(eval `(defun ,pred-name ; to avoid compilation, instead
(obj)
(and (typep obj 'flavor-instance) ;an instance of something??
(member ',a-flavor-name ;is it the right instance
(fetch-flavors (flavor-instance-class-name obj))))))
(eval `(deftype ,a-flavor-name () '(satisfies ,pred-name)))))
;;; --> END CREATE-PREDICATE
;;; --> UPDATE-PARENTS
;;; This function updates the dependency information for the parents of a given
;;; pattern. It lets the parent patterns know that the new child has been added
;;; ---
;;; INPUT: (something evaluating to) a flavor name and to a parent list
;;; OUTPUT: nil
;;; SIDE-EFFECTS: each parent has the flavor-depended-on slot updated
;;; USER-DEFINED-CALLED: flavor-depended-on
;;; ---
(defun update-parents (a-flavor-name the-parents)
(dolist (item the-parents)
(push a-flavor-name (flavor-depended-on-by (get item 'flavor-pattern)))))
;;; --> END UPDATE-PARENTS
;;; --> SET-PRECEDENCE
;;; This function determines the exact inheritance precedence for the ancestor
;;; graph of a given flavor class. It uses an algorithm derived from W.&H. and
;;; modified for use here. IT DEPENDS VERY HEAVILY ON THE REMOVE-DUPLICATES
;;; function, expecting that the leftmost occurrences of of a symbol will be
;;; removed first, leaving only the rightmost. If your version of
;;; remove-duplicates does not operate this way, please modify code accordingly
;;; ---
;;; INPUT: a flavor name and a parent list
;;; OUTPUT: the dependency list
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: flavor-precedence
;;; ---
;;; Example of call:
;;; (set-precedence 'sail-ship '(ship))
;;; ---
(defun set-precedence (a-flavor-name depends-on)
(if (null depends-on)
(list a-flavor-name)
(let ((new-precedence nil))
(dolist (item (reverse depends-on))
(setq new-precedence
(append (flavor-precedence (get item 'flavor-pattern))
new-precedence)))
(remove-duplicates (cons a-flavor-name new-precedence)))))
;;; --> END SET-PRECEDENCE
;;; --> OPTION-FIND
;;; This function extracts the variables affected by the setting of a given
;;; option.
;;; ---
;;; INPUT: a keyword option
;;; OUTPUT: a variable list (not as keywords)
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: none
;;; ---
;;; Example of call
;;; (option-find :initable-instance-variables
;;; '((:initable-instance-variables a b)
;;; (:settable-instance-variables b c)))
;;; returns --> (A B)
;;; ---
(defun option-find (option keyword-props)
"like (cdr (assoc ...)) but ignores non-list elements in the second argument"
;(defflavor zoom (w x y z) () (:settable-instance-variables x w))
(cdr (find option keyword-props :key #'(lambda(x) (and (consp x) (car x))))))
;;; --> END OPTION-FIND
;;; --> GET-CLASS-VARS
;;; This function retrieves copies of the parents' variables in the hierarchy,
;;; using the dependency list. Assumed that no reach past parents is needed.
;;; ---
;;; INPUT: a depends-on list.
;;; OUTPUT: the (copied) list of all parents' instance variables.
;;; SIDE-EFFECTS: none.
;;; USER-DEFINED-CALLED: flavor-p, flavor-vars, get-class-vars
;;; ---
;;; Example of call:
;;; (get-class-vars '(vehicle))
;;; ((:C . 3) (:D . 4))
;;; ---
(defun get-class-vars (depends-on)
(cond ((null depends-on) nil)
((flavor-p depends-on)
(copy-alist (flavor-vars depends-on))) ;make copy!!!
(t (nconc (get-class-vars (get (car depends-on) ;surgery.
'flavor-pattern))
(get-class-vars (cdr depends-on))))))
;;; --> END GET-CLASS-VARS
;;; --> MERGE-CLASS-SUPER-VARS
;;; This function merges the class and superclass variables, so the lower left
;;; variables will override the higher and to the right ones.
;;; ---
;;; INPUT: list of variables from the hierarchy, list of class variables
;;; OUTPUT: a merged list without repetitions and with the correct scope
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED:
;;; ---
;;; Example of call:
;;; (merge-class-super-vars '((:A . 1) (:B . 2) (:C . 5))
;;; (get-class-vars '(vehicle)))
;;; returns --> ((:A . 1) (:B . 2) (:C . 5) (:D . 4))
;;; ---
(defun merge-class-super-vars (key-class-vars super-vars)
(let ((final-vars key-class-vars))
(dolist (item (reverse super-vars) final-vars)
(if (not (assoc (car item) final-vars))
(push item final-vars)))))
;;; --> END MERGE-CLASS-SUPER-VARS
;;; --> GET-CLASS-METHODS
;;; This function retrieves copies of the parents' methods in the hierarchy,
;;; using the dependency list. Assumed that no reach past parents is needed
;;; ---
;;; INPUT: a depends-on list.
;;; OUTPUT: the (copied) list of all parents' instance variables.
;;; SIDE-EFFECTS: none.
;;; USER-DEFINED-CALLED: flavor-p, flavor-methods, get-class-methods
;;; ---
(defun get-class-methods (depends-on)
(if depends-on
(mapcar #'flavor-methods
(mapcar #'(lambda (x) (get x 'flavor-pattern))
depends-on))))
;;; --> END GET-CLASS-METHODS
;;; --> MERGE-CLASS-SUPER-METHODS
;;; This function merges the local class methods with the inherited methods
;;; from the hierarchy. The major problem is the correct choice of :before
;;; and :after methods. The choice of :primary method is a little easier, since
;;; no sequencing is required, although the correct "sequencing" of the
;;; hierarchy is necessary and non-trivial. CLOS inheritance rather than
;;; FLAVORS inheritance
;;; ---
;;; INPUT: a list of method hashtables in left to right order and a precedence
;;; list
;;; OUTPUT: a methods hashtable
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED:
;;; ---
(defun merge-class-super-methods (super-methods precedence)
(let ((final-methods ;make a new hash table of the right size
(make-hash-table :size (max 30
(apply #'+
(mapcar #'hash-table-count
super-methods)))))
(curr-method nil)
(key-method-list nil))
(dolist (h-table super-methods final-methods)
(setq key-method-list nil)
(maphash #'(lambda (key val)
(push (cons key (copy-alist val)) ;need to make copy...
key-method-list))
h-table)
(dolist (item key-method-list)
(cond ((setq curr-method (gethash (car item) final-methods))
(setf (gethash (car item) final-methods)
(adjust-methods (cadr item) curr-method precedence)))
(t (setf (gethash (car item) final-methods)
(cdr item))))))))
;;; --> END MERGE-CLASS-SUPER-METHODS
;;; --> WITH-INSTANCE-VARIABLES
;;; This macro is a support function for the DEFMETHOD macro.
;;; example:
;;; (pprint (macroexpand '(with-instance-variables ship ship1
;;; (print 'hello1)
;;; (setq zz (send ship1 :set-a (+ a 1)))
;;; (print zz))))
;;; ---
;;; INPUT: A symbol, which is a name of a flavor, a flavor instance and a body
;;; OUTPUT: The body code is replaced. The replacement code begins with a
;;; let that binds a local variable *instance-var-alist* to the function
;;; that accesses the instance variables and there values. The code is
;;; wrapped in a progn. new-body is the same as body except that every
;;; setq is replaced by setf and every occurance of an instance variable
;;; is replaced by
;;; (cdr (assoc ,instance-var *instance-var-alist*)) unless it is quoted
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: none
;;; DOES NOT SUPPORT: rebinding of an instance variable to a local variable name
;;; function names that are the same as instance variables
;;; mjp 11-88
;;; ---
(defmacro with-instance-variables (a-flavor-name flavor-instance &body body)
(let ((new-body nil)
(instance-vars (flavor-known-lexical-ivs (get a-flavor-name
'flavor-pattern))))
(setq new-body (adjust-body instance-vars body))
`(let ((*instance-var-alist*
(flavor-instance-vars ,flavor-instance)));not a gensym because of compiler bug in gclisp
(progn ,@new-body))))
;;; --> END WITH-INSTANCE-VARIABLES
;;; --> ADJUST-BODY
;;; This function "adjusts" the body of a method so that assigments to Instance
;;; Variables are handled in a usable manner: if it is a var replace it by its
;;; value expression; if it is involved in a SETQ, replace by the appropriate
;;; SETF; otherwise leave alone. This does not allow the appearance of local
;;; variables and function names that clash with the Instance Variables. Will
;;; have to be fixed in the future
;;; ---
;;; INPUT: a list of instance variables and a body-form
;;; OUTPUT: return the modified body.
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: adjust-body
;;; ---
#|(defun adjust-body (instance-vars body)
(let ((key nil))
(cond ((and (atom body) (member body instance-vars))
(setq key (intern (string body) 'keyword))
`(cdr (assoc ,key *instance-var-alist*)))
((consp body)
(cond ((eq (car body) 'setq)
(cons 'setf (adjust-body instance-vars (cdr body))))
((eq (car body) 'quote)
body)
(t (cons (adjust-body instance-vars (car body))
(adjust-body instance-vars (cdr body))))))
(t body))))|#
;;; mjp 12/89 handles lambda's that have no arguments
(defun adjust-body (instance-vars body)
(let ((key nil))
(cond ((and (atom body) (member body instance-vars))
(setq key (intern (string body) 'keyword))
`(cdr (assoc ,key *instance-var-alist*)))
((consp body)
(cond ((eq (car body) 'setq)
(cons 'setf (adjust-body instance-vars (cdr body))))
((and (eq (car body) 'quote)
(consp (cadr body))
(eq (caadr body) 'lambda)
(null (second (cadr body))))
(list 'quote (list 'lambda nil
(adjust-body instance-vars (third (cadr body)))))
)
((eq (car body) 'quote)
body)
(t (cons (adjust-body instance-vars (car body))
(adjust-body instance-vars (cdr body))))))
(t body))))
;;; --> END ADJUST-BODY
;;; --> DEFMETHOD
;;; This macro creates a method associated with a particular flavor. The syntax
;;; is a subset of the ZetaLisp syntax. Notice the different calls to variants
;;; of defmethod2 : FLAVOR and method compilation requires a little gymnastics
;;; ---
;;; INPUT: Three or more forms. The first two are lists, the second may be empty
;;; OUTPUT: the method name.
;;; SIDE-EFFECTS: a nameless function is created and saved on the property
;;; list of the flavor under the method name.
;;; USER-DEFINED-CALLED: defmethod2, defmethod2-compile.
;;; ---
(defmacro defmethod (object-key-method parameters &rest body)
(cond (*compiling*
`(defmethod2-compile ',object-key-method ',parameters ',body))
(t
`(defmethod2 ',object-key-method ',parameters ',body))))
;;; ---
(defun defmethod2 (object-key-method parameters body) ;;body is now a list
(let* ((a-flavor-name (car object-key-method))
(pattern (get a-flavor-name 'flavor-pattern))
key method-name)
(cond ((= (length object-key-method) 3)
(setq key (cadr object-key-method))
(setq method-name (caddr object-key-method)))
(t (setq key :primary)
(setq method-name (cadr object-key-method))))
(let* ((func-name ;the method is made into a NAMED function
(intern
(concatenate 'string (string (car object-key-method)) "-"
(string key) "-"
(string method-name))))
(curr-method (gethash method-name (flavor-methods pattern)))
(new-method
(cons key
(cons (car object-key-method)
(compile func-name `(lambda
;(eval `(defun ,func-name ; to avoid compilation, instead
,(cons 'user::self parameters) ;self is first...
,(macroexpand `(with-instance-variables
,a-flavor-name
user::self
,@body))))))))
(if curr-method ;there already - REPLACE
(setf (gethash method-name (flavor-methods pattern))
(adjust-methods new-method curr-method
(flavor-precedence pattern)))
(cond ((flavor-methods pattern)
(setf (gethash method-name (flavor-methods pattern))
(list new-method)))
(t (setf (flavor-methods pattern) (make-hash-table :size 30))
(setf (gethash method-name (flavor-methods pattern))
(list new-method)))))
(adjust-dependent-methods a-flavor-name method-name new-method))
method-name))
;;; --> END DEFMETHOD
;;; --> ADJUST-METHODS
;;; This function gets rid of previous method definitions, allowing the dynamic
;;; change of method to be propagated to the correct descendants of a given class
;;; ---
;;; INPUT: a method, the old :before, :primary and :after list associated with
;;; method, and the precedence list for the flavor class
;;; OUTPUT: nothing usable
;;; SIDE-EFFECTS: alters the methods list
;;; USER-DEFINED-CALLED: primary-insert,combination-insert
;;; ---
(defun adjust-methods (new-method old-method-list precedence-list)
(case (car new-method)
(:primary
(primary-insert new-method old-method-list precedence-list))
(:before
(combination-insert new-method old-method-list precedence-list :before))
(:whopper
(combination-insert new-method old-method-list precedence-list :whopper))
(:after
(reverse
(combination-insert
new-method (reverse old-method-list) precedence-list :after)))
(t (error
"only method combinations supported are primary, before, after, and whopper~s~%"
new-method))))
;;; --> END ADJUST-METHODS
;;; --> ADJUST-DEPENDENTS-METHODS
;;; This function looks at the dependents of a given class and changes the
;;; methods to reflect the changes higher up the hierarchy. Not all methods
;;; are changed, depending on the position of the current changes in the
;;; inheritance graph.
;;; ---
;;; INPUT: a flavor name, a method name and a new method
;;; OUTPUT: nil
;;; SIDE-EFFECTS: dependents of the given class MAY have their methods lists
;;; altered
;;; USER-DEFINED-CALLED: flavor-depended-on, flavor-methods, adjust-methods,
;;; flavor-precedence, adjust-dependent-methods
;;; ---
(defun adjust-dependent-methods (a-flavor-name method-name new-method)
(let ((dependents (flavor-depended-on-by (get a-flavor-name 'flavor-pattern)))
(dependent-pattern nil)
(dependent-methods nil)
(curr-method nil))
(dolist (a-flavor-name-item dependents)
(setq dependent-pattern (get a-flavor-name-item 'flavor-pattern))
(cond ((setq dependent-methods ;do we have them yet???
(flavor-methods dependent-pattern))
(if (setq curr-method (gethash method-name dependent-methods))
(setf (gethash method-name dependent-methods)
(adjust-methods new-method
curr-method
(flavor-precedence dependent-pattern)))
(setf (gethash method-name dependent-methods)
(list new-method)))
(adjust-dependent-methods a-flavor-name-item
method-name new-method))))))
;;; --> END ADJUST-DEPENDENTS-METHODS
;;; --> PRIMARY-INSERT
;;; This function inserts a new primary method, propagating the changes down
;;; the inheritance graph.
;;; ---
;;; INPUT: the new method, the current method info and the precedence info
;;; OUTPUT: the new method info
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: primary-insert
;;; ---
(defun primary-insert (new-method old-method-list precedence-list)
(cond ((null old-method-list)
(list new-method))
((eq (caar old-method-list) :primary)
(cond ((equal (cadr new-method) (cadr (car old-method-list))) ;ship
(cons new-method (cdr old-method-list))) ;replace old
((> (length (member (cadr new-method) precedence-list))
(length (member (cadr (car old-method-list))
precedence-list))) ;higher precedence
(cons new-method (cdr old-method-list))) ;replace old
(t ;lower precedence
old-method-list))) ;forget new
(t (cons (car old-method-list) ;tail recursion..
(primary-insert new-method
(cdr old-method-list)
precedence-list)))))
;;; --> END PRIMARY-INSERT
;;; --> COMBINATION-INSERT
;;; This function inserts the :before and :after methods in the correct position
;;; Note that no destruction takes place
;;; INPUT: a method, a method list, a precedence list, :before or :after
;;; OUTPUT: the new method list
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: combination-insert
;;; ---
(defun combination-insert (new-method old-method-list
precedence-list the-combinator)
(cond ((null old-method-list) (list new-method))
((equal (caar old-method-list) the-combinator)
(cond ((equal (cadr new-method) (cadr (car old-method-list))) ; ship
(cons new-method (cdr old-method-list))) ;replace old
((> (length (member (cadr new-method) precedence-list))
(length (member (cadr (car old-method-list))
precedence-list))) ;higher precedence
(cons new-method old-method-list)) ;add in front
(t (cons (car old-method-list)
(combination-insert new-method (cdr old-method-list)
precedence-list the-combinator)))))
(t (cons (car old-method-list)
(combination-insert new-method (cdr old-method-list)
precedence-list the-combinator)))))
;;; --> END COMBINATION-INSERT
;;; --> MAKE-INSTANCE
;;; This creates an instance of a flavor. Syntax like ZetaLisp. The function
;;; does some error checking, collects the variables from the flavor
;;; pattern and calls the function that will allocate the space and initialize
;;; the instance variables.
;;; ---
;;; INPUT: a flavor name and a list of variables with initial values
;;; OUTPUT: and error message or what returned by make-instance-2
;;; SIDE-EFFECTS: no direct ones.
;;; USER-DEFINED-CALLED: flavor-p, flavor-vars, merge-class-inst-vars,
;;; flavor-instance-vars, make-flavor-instance, eval-class-init,
;;; adjust-inst-vars
;;; ---
;;; Example of call:
;;; (setq ship1 (make-instance 'ship :a 3 :b 5))
;;; ---
(defun make-instance (a-flavor-name &rest init-plist)
(let ((flavor (get a-flavor-name 'flavor-pattern)))
(cond ((oddp (length init-plist))
(error "keyword value mismatch in make-instance ~s ~s~%"
a-flavor-name init-plist))
((flavor-p flavor) ;it is a flavor pattern
(let ((obj (make-flavor-instance :class-name a-flavor-name)))
(send obj ;initialize by evaluating class vars, init-plist
:init ;and then merging with precedence to locals..
(merge-class-inst-vars
(eval-class-init
(copy-alist (flavor-vars flavor)))
(adjust-inst-vars init-plist
(flavor-init-keywords flavor)
flavor)))
obj))
(t (error "~s is not a flavor" a-flavor-name)))))
;;; --> END MAKE-INSTANCE
;;; --> INSTANTIATE-FLAVOR
;;; probably not a complete implementation
;;; allows the init-plist to be passed as a list
(defun instantiate-flavor (a-flavor-name init-plist)
(let ((flavor (get a-flavor-name 'flavor-pattern)))
(cond ((oddp (length init-plist))
(error "keyword value mismatch in make-instance ~s ~s~%"
a-flavor-name init-plist))
((flavor-p flavor) ;it is a flavor pattern
(let ((obj (make-flavor-instance :class-name a-flavor-name)))
(send obj ;initialize by evaluating class vars, init-plist
:init ;and then merging with precedence to locals..
(merge-class-inst-vars
(eval-class-init
(copy-alist (flavor-vars flavor)))
(adjust-inst-vars init-plist
(flavor-init-keywords flavor)
flavor)))
obj))
(t (error "~s is not a flavor" a-flavor-name)))))
;;; --> END INSTANTIATE-FLAVOR
;;; --> EVAL-CLASS-INIT
;;; This function takes a <variable . value> initialization list and evaluates
;;; the value part in the "current environment"
;;; ---
;;; INPUT: a list of <keyword . form> pairs: ((:A . 1) (:B . (+ ZIP 2)))
;;; OUTPUT: the list with each <form> evaluated
;;; SIDE-EFFECTS: the second elements of the pairs are replaced by their current
;;; values.
;;; USER-DEFINED-CALLED: none
;;; ---
(defun eval-class-init (vars)
(dolist (item vars)
(setf (cdr item) (eval (cdr item))))
vars)
;;; --> END-EVAL-CLASS-INIT
;;; --> ADJUST-INST-VARS
;;; Takes the list of variable names and forms at instance creation and
;;; transforms it into a dotted pairs list with all evaluations carried out
;;; ---
;;; INPUT: a list
;;; OUTPUT: a list
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: none
;;; ---
(defun adjust-inst-vars (inst-vars allowable-init-kwrds flavor)
(do ((new-vars inst-vars (cddr new-vars))
(var-list nil))
((null new-vars) var-list)
(if (member (car new-vars) allowable-init-kwrds)
(push (cons (car new-vars) (cadr new-vars)) var-list)
(error "attempted to initialize unallowed variable in: <~s>~%"
(flavor-name flavor)))))
;;; --> END ADJUST-INST-VARS
;;; --> MERGE-CLASS-INST-VARS
;;; This function merges the class and instance variables, so that the
;;; initializations at instantiation time will override the defaults.
;;; ---
;;; INPUT: a list of (variable.value) pairs
;;; OUTPUT: a list of (variable.value) pairs
;;; SIDE-EFFECTS: none
;;; USER-DEFINED-CALLED: none
;;; ---
(defun merge-class-inst-vars (class-vars inst-vars)
(dolist (var inst-vars class-vars)
(rplacd (assoc (car var) class-vars) (cdr var))))
;;; --> END MERGE-CLASS-INST-VARS
;;; --> SEND
;;; This function sends a method and a parameter list to a flavor, executes all
;;; the before methods, the leading primary method and all the after methods.
;;; Mediates ALL communication with the object, including instance variable
;;; access and setting.
;;; ---
;;; INPUT: an flavor instance, a method name and a sequence of parameter values
;;; OUTPUT: the value of the primary method.
;;; SIDE-EFFECTS: no direct side-effects.
;;; USER-DEFINED-CALLED: a large number..., especially SEND-INTERNAL
;;; ---
(defun send (target method-name &rest arguments)
(assert (typep target 'flavor-instance) ()
"The object ~s is not a flavor instance" target)
(let* ((*method-name-methods* ;get method of given name on the methods of class
(gethash method-name
(flavor-methods (get (flavor-instance-class-name target)
'flavor-pattern))))
(*remaining-whoppers* *method-name-methods*))
(declare (special *remaining-whoppers* *method-name-methods*))
(cond ((null *method-name-methods*)
(error
"The object ~s received a ~s message which went unclaimed.~%The rest of the message was ~s~%"
target method-name arguments))
(t
(send-internal target arguments)))))
;;; --- SEND-INTERNAL
;;; Send-internal is called from within send, and by continue-whopper and
;;; lexpr-continue-whopper. It does most of the work of the original send
;;; routine, except for looking up the method-name-methods. Any call to
;;; send-internal should have the special variable *method-name-methods* bound
;;; to the method-name-methods found in send. Also, the special variable
;;; *remaining-whoppers* starts off being eq to *method-name-methods*, but it
;;; is popped while searching for the next whopper to run - the purpose of
;;; this popping is so that a recursive call to send-internal (done from
;;; within continue-whopper or lexpr-continue-whopper) will be able to
;;; determine what whopper to run next. Notice how the processing of the
;;; normal parts of the method (the befores, primary, and afters) is done when
;;; there are no more remaining whoppers - this part of the code is only
;;; reached if every whopper calls [lexpr-]continue-whopper.
;;; ---
(defun send-internal (target args &aux a-meth primary)
(declare (special *remaining-whoppers* *method-name-methods*))
(do ((whops *remaining-whoppers* (cdr whops)))
((null whops) ;; no more whoppers, do the "regular" method stuff
(dolist (a-meth *method-name-methods*)
(if (eq (car a-meth) :before)
(apply (cddr a-meth) target args)))
(multiple-value-prog1
(if (setq primary (cddr (assoc :primary *method-name-methods*)))
(apply primary target args))
(dolist (a-meth *method-name-methods*)
(if (eq (car a-meth) :after)
(apply (cddr a-meth) target args)))))
(setq a-meth (car *remaining-whoppers*))
(when (eq (car a-meth) :whopper)
;; if it's a whopper, call it with *remaining-whoppers* bound to the
;; whoppers it is wrapped around
(let ((*remaining-whoppers* (cdr whops)))
(declare (special *remaining-whoppers*))
(return (apply (cddr a-meth) target args))))))
;;; --- SETF METHOD FOR SEND
(define-setf-method send (target method-name &rest arguments)
(let* ((temp-target (gensym))
(old-method (gensym))
(temp-method (gensym))
(new-val (gensym)))
(values
(list temp-target old-method temp-method)
(list target method-name (intern
(concatenate 'string "SET-"
(princ-to-string method-name))
'keyword))
(list new-val)
`(progn
(send ,temp-target ,temp-method ,new-val)
,new-val)
`(send ,temp-target ,old-method))))
;;; --> END SEND
;;; --- DEFWHOPPER
;;; defwhopper defines a whopper method for a flavor - whoppers are nice
;;; because they are a generalization of all possible types of method
;;; combination (when you have whoppers, you don't even need daemons!)
;;; ---
(defmacro defwhopper ((flavor message-name) args &body body)
;; define continue-whopper and lexpr-continue-whopper locally
`(macrolet ((continue-whopper (&rest args)
`(send-internal self (list . ,args)))
(lexpr-continue-whopper (&rest args)
`(send-internal self (list* . ,args))))
;; the whopper is a :whopper type method
(defmethod (,flavor :whooper ,message-name) ,args . ,body)))
;;; --- END DEFWHOPPER
;;; --- CONTINUE-WHOPPER & LEXPR-CONTINUE-WHOPPER ERROR CHECKING
;;; make continue-whopper and lexpr-continue-whopper give errors when used
;;; outside of defwhopper
(defmacro continue-whopper (&rest ignore)
(error "Continue-whopper can only be used inside a whopper"))
(defmacro lexpr-continue-whopper (&rest ignore)
(error "Lexpr-continue-whopper can only be used inside a whopper"))
;;; --- END CONTINUE-WHOPPER & LEXPR-CONTINUE-WHOPPER ERROR CHECKING
;;; ================== SOME EXPECTED SUPPORT FUNCTIONS ==================
;;; --> LEXPR-SEND
;;; This is slightly different kind of send
;;; ---
(defun lexpr-send (instance message arglist)
(apply #'send (append (list instance message) arglist)))
;;; --> END LEXPR-SEND