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