forked from factor/factor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocabs.factor
201 lines (133 loc) · 4.42 KB
/
vocabs.factor
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
! Copyright (C) 2007, 2009 Eduardo Cavazos, Slava Pestov.
! See https://factorcode.org/license.txt for BSD license.
USING: accessors assocs definitions kernel namespaces sequences
sets sorting splitting strings ;
IN: vocabs
SYMBOL: dictionary
TUPLE: vocab < identity-tuple
name words
main help
source-loaded? docs-loaded? ;
! sources-loaded? and docs-loaded? slots could be
SYMBOLS: +parsing+ +done+ ;
: <vocab> ( name -- vocab )
vocab new
swap >>name
H{ } clone >>words ;
<PRIVATE
: valid-vocab-name? ( name -- ? )
dup string? [ [ ":/\\ \"" member? ] none? ] [ drop f ] if ;
PRIVATE>
ERROR: bad-vocab-name name ;
: check-vocab-name ( name -- name )
dup valid-vocab-name? [ bad-vocab-name ] unless ;
TUPLE: vocab-link name ;
C: <vocab-link> vocab-link
UNION: vocab-spec vocab vocab-link ;
INSTANCE: vocab-spec definition-mixin
GENERIC: vocab-name ( vocab-spec -- name )
M: vocab vocab-name name>> ;
M: vocab-link vocab-name name>> ;
M: object vocab-name check-vocab-name ;
GENERIC: lookup-vocab ( vocab-spec -- vocab )
M: vocab lookup-vocab ;
M: object lookup-vocab vocab-name dictionary get at ;
ERROR: no-vocab-named name ;
: ?lookup-vocab ( vocab-spec -- vocab )
[ lookup-vocab ] [ no-vocab-named ] ?unless ;
GENERIC: vocab-words-assoc ( vocab-spec -- assoc/f )
M: vocab vocab-words-assoc words>> ;
M: object vocab-words-assoc lookup-vocab vocab-words-assoc ;
M: f vocab-words-assoc ;
GENERIC: vocab-help ( vocab-spec -- help )
M: vocab vocab-help help>> ;
M: object vocab-help lookup-vocab vocab-help ;
M: f vocab-help ;
GENERIC: vocab-main ( vocab-spec -- main )
M: vocab vocab-main main>> ;
M: object vocab-main lookup-vocab vocab-main ;
M: f vocab-main ;
PREDICATE: runnable-vocab < vocab
vocab-main >boolean ;
SYMBOL: vocab-observers
GENERIC: vocab-changed ( vocab obj -- )
: add-vocab-observer ( obj -- )
vocab-observers get push ;
: remove-vocab-observer ( obj -- )
vocab-observers get remove-eq! drop ;
: notify-vocab-observers ( vocab -- )
vocab-observers get [ vocab-changed ] with each ;
: create-vocab ( name -- vocab )
check-vocab-name dictionary get [ <vocab> ] cache
dup notify-vocab-observers ;
ERROR: no-vocab name ;
: loaded-vocab-names ( -- seq )
dictionary get keys sort ;
: vocab-words ( vocab-spec -- seq )
vocab-words-assoc values ;
: all-words ( -- seq )
dictionary get values [ vocab-words ] map concat ;
: words-named ( str -- seq )
dictionary get
[ values [ vocab-words-assoc at ] with map sift ]
[
[ ":" split1 swap ] dip at
[ vocab-words-assoc at [ suffix ] when* ] [ drop ] if*
] 2bi ;
: child-vocab? ( prefix name -- ? )
swap [ drop t ] [
2dup = [ 2drop t ] [
2dup head? [
length swap ?nth CHAR: . =
] [ 2drop f ] if
] if
] if-empty ;
: loaded-child-vocab-names ( vocab-spec -- seq )
vocab-name loaded-vocab-names [ child-vocab? ] with filter ;
GENERIC: >vocab-link ( name -- vocab )
M: vocab-spec >vocab-link ;
M: object >vocab-link [ lookup-vocab ] [ <vocab-link> ] ?unless ;
<PRIVATE
: (forget-vocab) ( vocab -- )
[ vocab-words forget-all ]
[ vocab-name dictionary get delete-at ]
[ notify-vocab-observers ] tri ;
PRIVATE>
: forget-vocab ( vocab -- )
[ (forget-vocab) ] [
vocab-name dup ".private" tail? [ drop ] [
".private" append (forget-vocab)
] if
] bi ;
M: vocab-spec forget* forget-vocab ;
SYMBOL: require-hook
<PRIVATE
SYMBOL: requiring
: with-requiring ( quot -- )
requiring get [
swap call
] [
HS{ } clone dup requiring [ swap call ] with-variable
] if* ; inline
PRIVATE>
GENERIC: require ( object -- )
M: vocab require name>> require ;
M: vocab-link require name>> require ;
! When calling "foo.private" require, load "foo" instead, but
! only when "foo.private" does not exist. The reason for this is
! that stage1 bootstrap starts out with some .private vocabs
! that contain primitives, and loading the public vocabs would
! cause circularity issues.
M: string require
[ ".private" ?tail ] 1check [ lookup-vocab not ] when [
[
dupd ?adjoin
[ require-hook get call( name -- ) ] [ drop ] if
] with-requiring
] [ drop ] if ;
: require-all ( vocabs -- )
[ require ] each ;
: load-vocab ( name -- vocab )
[ require ] [ lookup-vocab ] bi ;
: ?load-vocab ( name -- vocab )
[ require ] [ ?lookup-vocab ] bi ;