]> gitweb.factorcode.org Git - factor.git/blob - core/vocabs/vocabs.factor
f1fc12391edb9cf82ec11411558c99631fce3611
[factor.git] / core / vocabs / vocabs.factor
1 ! Copyright (C) 2007, 2009 Eduardo Cavazos, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs strings kernel sorting namespaces
4 sequences definitions sets combinators splitting ;
5 IN: vocabs
6
7 SYMBOL: dictionary
8
9 TUPLE: vocab < identity-tuple
10 name words
11 main help
12 source-loaded? docs-loaded? ;
13
14 ! sources-loaded? slot is one of these three
15 SYMBOL: +parsing+
16 SYMBOL: +running+
17 SYMBOL: +done+
18
19 : <vocab> ( name -- vocab )
20     \ vocab new
21         swap >>name
22         H{ } clone >>words ;
23
24 ERROR: bad-vocab-name name ;
25
26 : check-vocab-name ( name -- name )
27     dup string? [ bad-vocab-name ] unless
28     dup ":/\\ " intersects? [ bad-vocab-name ] when ;
29
30 TUPLE: vocab-link name ;
31
32 C: <vocab-link> vocab-link
33
34 UNION: vocab-spec vocab vocab-link ;
35
36 GENERIC: vocab-name ( vocab-spec -- name )
37
38 M: vocab vocab-name name>> ;
39
40 M: vocab-link vocab-name name>> ;
41
42 M: object vocab-name check-vocab-name ;
43
44 GENERIC: lookup-vocab ( vocab-spec -- vocab )
45
46 M: vocab lookup-vocab ;
47
48 M: object lookup-vocab ( name -- vocab ) vocab-name dictionary get at ;
49
50 GENERIC: vocab-words ( vocab-spec -- words )
51
52 M: vocab vocab-words words>> ;
53
54 M: object vocab-words lookup-vocab vocab-words ;
55
56 M: f vocab-words ;
57
58 GENERIC: vocab-help ( vocab-spec -- help )
59
60 M: vocab vocab-help help>> ;
61
62 M: object vocab-help lookup-vocab vocab-help ;
63
64 M: f vocab-help ;
65
66 GENERIC: vocab-main ( vocab-spec -- main )
67
68 M: vocab vocab-main main>> ;
69
70 M: object vocab-main lookup-vocab vocab-main ;
71
72 M: f vocab-main ;
73
74 SYMBOL: vocab-observers
75
76 GENERIC: vocabs-changed ( obj -- )
77
78 : add-vocab-observer ( obj -- )
79     vocab-observers get push ;
80
81 : remove-vocab-observer ( obj -- )
82     vocab-observers get remove-eq! drop ;
83
84 : notify-vocab-observers ( -- )
85     vocab-observers get [ vocabs-changed ] each ;
86
87 : create-vocab ( name -- vocab )
88     check-vocab-name
89     dictionary get [ <vocab> ] cache
90     notify-vocab-observers ;
91
92 ERROR: no-vocab name ;
93
94 : vocabs ( -- seq )
95     dictionary get keys natural-sort ;
96
97 : words ( vocab -- seq )
98     vocab-words values ;
99
100 : all-words ( -- seq )
101     dictionary get values [ words ] map concat ;
102
103 : words-named ( str -- seq )
104     dictionary get values
105     [ vocab-words at ] with map
106     sift ;
107
108 : child-vocab? ( prefix name -- ? )
109     2dup = pick empty? or
110     [ 2drop t ] [ swap CHAR: . suffix head? ] if ;
111
112 : child-vocabs ( vocab -- seq )
113     vocab-name vocabs [ child-vocab? ] with filter ;
114
115 GENERIC: >vocab-link ( name -- vocab )
116
117 M: vocab-spec >vocab-link ;
118
119 M: object >vocab-link dup lookup-vocab [ ] [ <vocab-link> ] ?if ;
120
121 : forget-vocab ( vocab -- )
122     [ words forget-all ]
123     [ vocab-name dictionary get delete-at ] bi
124     notify-vocab-observers ;
125
126 M: vocab-spec forget* forget-vocab ;
127
128 SYMBOL: require-hook
129
130 PREDICATE: runnable-vocab < vocab
131     vocab-main >boolean ;
132
133 INSTANCE: vocab-spec definition
134
135 : call-require-hook ( name -- )
136     require-hook get call( name -- ) ;
137
138 GENERIC: require ( object -- )
139
140 M: vocab require name>> require ;
141 M: vocab-link require name>> require ;
142
143 ! When calling "foo.private" require, load "foo" instead, but only when
144 ! "foo.private" does not exist. The reason for this is that stage1 bootstrap
145 ! starts out with some .private vocabs that contain primitives, and
146 ! loading the public vocabs would cause circularity issues.
147 M: string require ( vocab -- )
148     dup ".private" ?tail [
149         over lookup-vocab
150         [ 2drop ]
151         [ nip call-require-hook ]
152         if
153     ] [
154         nip call-require-hook
155     ] if ;
156
157 : load-vocab ( name -- vocab )
158     [ require ] [ lookup-vocab ] bi ;