]> gitweb.factorcode.org Git - factor.git/blob - basis/delegate/delegate.factor
57f9b35c96373bff90e534fd3023fbab35b06dbf
[factor.git] / basis / delegate / delegate.factor
1 ! Copyright (C) 2007, 2008 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors parser generic kernel classes classes.tuple
4 words slots assocs sequences arrays vectors definitions
5 math hashtables sets generalizations namespaces make ;
6 IN: delegate
7
8 : protocol-words ( protocol -- words )
9     \ protocol-words word-prop ;
10
11 : protocol-consult ( protocol -- consulters )
12     \ protocol-consult word-prop ;
13
14 GENERIC: group-words ( group -- words )
15
16 M: tuple-class group-words
17     all-slots [
18         name>>
19         [ reader-word 0 2array ]
20         [ writer-word 0 2array ] bi
21         2array
22     ] map concat ;
23
24 ! Consultation
25
26 : consult-method ( word class quot -- )
27     [ drop swap first create-method ]
28     [ nip [ , dup second , \ ndip , first , ] [ ] make ] 3bi
29     define ;
30
31 : change-word-prop ( word prop quot -- )
32     rot props>> swap change-at ; inline
33
34 : register-protocol ( group class quot -- )
35     rot \ protocol-consult [ swapd ?set-at ] change-word-prop ;
36
37 : define-consult ( group class quot -- )
38     [ register-protocol ]
39     [ [ group-words ] 2dip [ consult-method ] 2curry each ]
40     3bi ;
41
42 : CONSULT:
43     scan-word scan-word parse-definition define-consult ; parsing
44
45 ! Protocols
46
47 : cross-2each ( seq1 seq2 quot -- )
48     [ with each ] 2curry each ; inline
49
50 : forget-all-methods ( classes words -- )
51     [ first method forget ] cross-2each ;
52
53 : protocol-users ( protocol -- users )
54     protocol-consult keys ;
55
56 : lost-words ( protocol wordlist -- lost-words )
57     [ protocol-words ] dip diff ;
58
59 : forget-old-definitions ( protocol new-wordlist -- )
60     [ drop protocol-users ] [ lost-words ] 2bi
61     forget-all-methods ;
62
63 : added-words ( protocol wordlist -- added-words )
64     swap protocol-words diff ;
65
66 : add-new-definitions ( protocol wordlist -- )
67     [ drop protocol-consult >alist ] [ added-words ] 2bi
68     [ swap first2 consult-method ] cross-2each ;
69
70 : initialize-protocol-props ( protocol wordlist -- )
71     [
72         drop \ protocol-consult
73         [ H{ } assoc-like ] change-word-prop
74     ] [ { } like \ protocol-words set-word-prop ] 2bi ;
75
76 : fill-in-depth ( wordlist -- wordlist' )
77     [ dup word? [ 0 2array ] when ] map ;
78
79 : define-protocol ( protocol wordlist -- )
80     fill-in-depth
81     [ forget-old-definitions ]
82     [ add-new-definitions ]
83     [ initialize-protocol-props ] 2tri ;
84
85 : PROTOCOL:
86     CREATE-WORD
87     [ define-symbol ]
88     [ f "inline" set-word-prop ]
89     [ parse-definition define-protocol ] tri ; parsing
90
91 PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
92
93 M: protocol forget*
94     [ f forget-old-definitions ] [ call-next-method ] bi ;
95
96 : show-words ( wordlist' -- wordlist )
97     [ dup second zero? [ first ] when ] map ;
98
99 M: protocol definition protocol-words show-words ;
100
101 M: protocol definer drop \ PROTOCOL: \ ; ;
102
103 M: protocol group-words protocol-words ;