]> gitweb.factorcode.org Git - factor.git/blob - core/words/words-docs.factor
324f8e755f5aeb55b019d02a2ebaf761b9cd3168
[factor.git] / core / words / words-docs.factor
1 USING: definitions help.markup help.syntax kernel parser
2 kernel.private words.private vocabs classes quotations
3 strings effects compiler.units ;
4 IN: words
5
6 ARTICLE: "interned-words" "Looking up and creating words"
7 "A word is said to be " { $emphasis "interned" } " if it is a member of the vocabulary named by its vocabulary slot. Otherwise, the word is " { $emphasis "uninterned" } "."
8 $nl
9 "Words whose names are known at parse time -- that is, most words making up your program -- can be referenced in source code by stating their name. However, the parser itself, and sometimes code you write, will need to create look up words dynamically."
10 $nl
11 "Parsing words add definitions to the current vocabulary. When a source file is being parsed, the current vocabulary is initially set to " { $vocab-link "scratchpad" } ". The current vocabulary may be changed with the " { $link POSTPONE: IN: } " parsing word (see " { $link "vocabulary-search" } ")."
12 { $subsection create }
13 { $subsection create-in }
14 { $subsection lookup } ;
15
16 ARTICLE: "uninterned-words" "Uninterned words"
17 "A word that is not a member of any vocabulary is said to be " { $emphasis "uninterned" } "."
18 $nl
19 "There are several ways of creating an uninterned word:"
20 { $subsection <word> }
21 { $subsection gensym }
22 { $subsection define-temp } ;
23
24 ARTICLE: "colon-definition" "Word definitions"
25 "Every word has an associated quotation definition that is called when the word is executed."
26 $nl
27 "Defining words at parse time:"
28 { $subsection POSTPONE: : }
29 { $subsection POSTPONE: ; }
30 "Defining words at run time:"
31 { $subsection define }
32 { $subsection define-declared }
33 { $subsection define-inline }
34 "Word definitions should declare their stack effect, unless the definition is completely trivial. See " { $link "effect-declaration" } "."
35 $nl
36 "All other types of word definitions, such as " { $link "symbols" } " and " { $link "generic" } ", are just special cases of the above." ;
37
38 ARTICLE: "symbols" "Symbols"
39 "A symbol pushes itself on the stack when executed. By convention, symbols are used as variable names (" { $link "namespaces" } ")."
40 { $subsection symbol }
41 { $subsection symbol? }
42 "Defining symbols at parse time:"
43 { $subsection POSTPONE: SYMBOL: }
44 "Defining symbols at run time:"
45 { $subsection define-symbol }
46 "Symbols are just compound definitions in disguise. The following two lines are equivalent:"
47 { $code
48     "SYMBOL: foo"
49     ": foo \\ foo ;"
50 } ;
51
52 ARTICLE: "primitives" "Primitives"
53 "Primitives are words defined in the Factor VM. They provide the essential low-level services to the rest of the system."
54 { $subsection primitive }
55 { $subsection primitive? } ;
56
57 ARTICLE: "deferred" "Deferred words and mutual recursion"
58 "Words cannot be referenced before they are defined; that is, source files must order definitions in a strictly bottom-up fashion. This is done to simplify the implementation, facilitate better parse time checking and remove some odd corner cases; it also encourages better coding style."
59 $nl
60 "Sometimes this restriction gets in the way, for example when defining mutually-recursive words; one way to get around this limitation is to make a forward definition."
61 { $subsection POSTPONE: DEFER: }
62 "The class of deferred word definitions:"
63 { $subsection deferred }
64 { $subsection deferred? }
65 "Deferred words throw an error when called:"
66 { $subsection undefined }
67 "Deferred words are just compound definitions in disguise. The following two lines are equivalent:"
68 { $code
69     "DEFER: foo"
70     ": foo undefined ;"
71 } ;
72
73 ARTICLE: "declarations" "Declarations"
74 "Declarations give special behavior to a word. Declarations are parsing words that set a word property in the most recently defined word."
75 $nl
76 "The first declaration specifies the time when a word runs. It affects both the non-optimizing and optimizing compilers:"
77 { $subsection POSTPONE: parsing }
78 "The remaining declarations only affect definitions compiled with the optimizing compiler. They do not change evaluation semantics of a word, but instead declare that the word follows a certain contract, and thus may be compiled differently."
79 { $warning "If a generic word is declared " { $link POSTPONE: foldable } " or " { $link POSTPONE: flushable } ", all methods must satisfy the contract, otherwise unpredicable behavior will occur." }
80 { $subsection POSTPONE: inline }
81 { $subsection POSTPONE: foldable }
82 { $subsection POSTPONE: flushable }
83 { $subsection POSTPONE: recursive }
84 "Stack effect declarations are documented in " { $link "effect-declaration" } "." ;
85
86 ARTICLE: "word-definition" "Defining words"
87 "There are two approaches to creating word definitions:"
88 { $list
89     "using parsing words at parse time,"
90     "using defining words at run time."
91 }
92 "The latter is a more dynamic feature that can be used to implement code generation and such, and in fact parse time defining words are implemented in terms of run time defining words."
93 { $subsection "colon-definition" }
94 { $subsection "symbols" }
95 { $subsection "primitives" }
96 { $subsection "deferred" }
97 { $subsection "declarations" }
98 "Words implement the definition protocol; see " { $link "definitions" } "." ;
99
100 ARTICLE: "word-props" "Word properties"
101 "Each word has a hashtable of properties."
102 { $subsection word-prop }
103 { $subsection set-word-prop }
104 "The stack effect of the above two words is designed so that it is most convenient when " { $snippet "name" } " is a literal pushed on the stack right before executing this word."
105 $nl
106 "The following are some of the properties used by the library:"
107 { $table
108     { "Property" "Documentation" }
109     { { $snippet "\"parsing\"" } { $link "parsing-words" } }
110
111     { { { $snippet "\"inline\"" } ", " { $snippet "\"foldable\"" } ", " { $snippet "flushable" } } { $link "declarations" } }
112
113     { { $snippet "\"loc\"" } { "Location information - " { $link where } } }
114     
115     { { { $snippet "\"methods\"" } ", " { $snippet "\"combination\"" } } { "Set on generic words - " { $link "generic" } } }
116     
117     { { { $snippet "\"reading\"" } ", " { $snippet "\"writing\"" } } { "Set on slot accessor words - " { $link "slots" } } }
118
119     { { $snippet "\"declared-effect\"" } { $link "effect-declaration" } }
120     
121     { { { $snippet "\"help\"" } ", " { $snippet "\"help-loc\"" } ", " { $snippet "\"help-parent\"" } } { "Where word help is stored - " { $link "writing-help" } } }
122
123     { { $snippet "\"infer\"" } { $link "compiler-transforms" } }
124
125     { { { $snippet "\"inferred-effect\"" } } { $link "inference" } }
126
127     { { $snippet "\"specializer\"" } { $link "hints" } }
128     
129     { { { $snippet "\"intrinsics\"" } ", " { $snippet "\"if-intrinsics\"" } } { $link "generator" } }
130
131     { { $snippet "\"predicating\"" } " Set on class predicates, stores the corresponding class word" }
132 }
133 "Properties which are defined for classes only:"
134 { $table
135     { "Property" "Documentation" }
136     { { $snippet "\"class\"" } { "A boolean indicating whether this word is a class - " { $link "classes" } } }
137
138     { { $snippet "\"coercer\"" } { "A quotation for converting the top of the stack to an instance of this class" } }
139     
140     { { $snippet "\"constructor\"" } { $link "tuple-constructors" } }
141     
142     { { $snippet "\"type\"" } { $link "builtin-classes" } }
143     
144     { { { $snippet "\"superclass\"" } ", " { $snippet "\"predicate-definition\"" } } { $link "predicates" } }
145     
146     { { $snippet "\"members\"" } { $link "unions" } }
147
148     { { $snippet "\"slots\"" } { $link "slots" } }
149
150     { { $snippet "\"predicate\"" } { "A quotation that tests if the top of the stack is an instance of this class - " { $link "class-predicates" } } }
151 } ;
152
153 ARTICLE: "word.private" "Word implementation details"
154 "The " { $snippet "def" } " slot of a word holds a " { $link quotation } " instance that is called when the word is executed."
155 $nl
156 "An " { $emphasis "XT" } " (execution token) is the machine code address of a word:"
157 { $subsection word-xt } ;
158
159 ARTICLE: "words" "Words"
160 "Words are the Factor equivalent of functions or procedures; a word is essentially a named quotation."
161 $nl
162 "Word introspection facilities and implementation details are found in the " { $vocab-link "words" } " vocabulary."
163 $nl
164 "Word objects contain several slots:"
165 { $table
166     { { $snippet "name" } "a word name" }
167     { { $snippet "vocabulary" } "a word vocabulary name" }
168     { { $snippet "def" } "a definition quotation" }
169     { { $snippet "props" } "an assoc of word properties, including documentation and other meta-data" }
170 }
171 "Words are instances of a class."
172 { $subsection word }
173 { $subsection word? }
174 { $subsection "interned-words" }
175 { $subsection "uninterned-words" }
176 { $subsection "word-definition" }
177 { $subsection "word-props" }
178 { $subsection "word.private" }
179 { $see-also "vocabularies" "vocabs.loader" "definitions" } ;
180
181 ABOUT: "words"
182
183 HELP: execute ( word -- )
184 { $values { "word" word } }
185 { $description "Executes a word." }
186 { $examples
187     { $example "USING: kernel io words ;" "IN: scratchpad" ": twice dup execute execute ;\n: hello \"Hello\" print ;\n\\ hello twice" "Hello\nHello" }
188 } ;
189
190 HELP: deferred
191 { $class-description "The class of deferred words created by " { $link POSTPONE: DEFER: } "." } ;
192
193 { deferred POSTPONE: DEFER: } related-words
194
195 HELP: primitive
196 { $description "The class of primitive words." } ;
197
198 HELP: symbol
199 { $description "The class of symbols created by " { $link POSTPONE: SYMBOL: } "." } ;
200
201 HELP: word-prop
202 { $values { "word" word } { "name" "a property name" } { "value" "a property value" } }
203 { $description "Retrieves a word property. Word property names are conventionally strings." } ;
204
205 HELP: set-word-prop
206 { $values { "word" word } { "value" "a property value" } { "name" "a property name" } }
207 { $description "Stores a word property. Word property names are conventionally strings." }
208 { $side-effects "word" } ;
209
210 HELP: remove-word-prop
211 { $values { "word" word } { "name" "a property name" } }
212 { $description "Removes a word property, so future lookups will output " { $link f } " until it is set again. Word property names are conventionally strings." }
213 { $side-effects "word" } ;
214
215 HELP: word-xt ( word -- start end )
216 { $values { "word" word } { "start" "the word's start address" } { "end" "the word's end address" } }
217 { $description "Outputs the machine code address of the word's definition." } ;
218
219 HELP: define-symbol
220 { $values { "word" word } }
221 { $description "Defines the word to push itself on the stack when executed. This is the run time equivalent of " { $link POSTPONE: SYMBOL: } "." }
222 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
223 { $side-effects "word" } ;
224
225 HELP: define
226 { $values { "word" word } { "def" quotation } }
227 { $description "Defines the word to call a quotation when executed. This is the run time equivalent of " { $link POSTPONE: : } "." }
228 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
229 { $side-effects "word" } ;
230
231 HELP: reset-props
232 { $values { "word" word } { "seq" "a sequence of word property names" } }
233 { $description "Removes all listed word properties from the word." }
234 { $side-effects "word" } ;
235
236 HELP: reset-word
237 { $values { "word" word } }
238 { $description "Reset word declarations." }
239 $low-level-note
240 { $side-effects "word" } ;
241
242 HELP: reset-generic
243 { $values { "word" word } }
244 { $description "Reset word declarations and generic word properties." }
245 $low-level-note
246 { $side-effects "word" } ;
247
248 HELP: <word> ( name vocab -- word )
249 { $values { "name" string } { "vocab" string } { "word" word } }
250 { $description "Allocates an uninterned word with the specified name and vocabulary, and a blank word property hashtable. User code should call " { $link gensym } " to create uninterned words and " { $link create } " to create interned words." } ;
251
252 HELP: gensym
253 { $values { "word" word } }
254 { $description "Creates an uninterned word that is not equal to any other word in the system." }
255 { $examples { $unchecked-example "gensym ." "G:260561" } }
256 { $notes "Gensyms are often used as placeholder values that have no meaning of their own but must be unique. For example, the compiler uses gensyms to label sections of code." } ;
257
258 HELP: bootstrapping?
259 { $var-description "Set by the library while bootstrap is in progress. Some parsing words need to behave differently during bootstrap." } ;
260
261 HELP: word
262 { $values { "word" word } }
263 { $description "Outputs the most recently defined word." }
264 { $class-description "The class of words. One notable subclass is " { $link class } ", the class of class words." } ;
265
266 { word set-word save-location } related-words
267
268 HELP: set-word
269 { $values { "word" word } }
270 { $description "Sets the recently defined word." } ;
271
272 HELP: lookup
273 { $values { "name" string } { "vocab" string } { "word" "a word or " { $link f } } }
274 { $description "Looks up a word in the dictionary. If the vocabulary or the word is not defined, outputs " { $link f } "." } ;
275
276 HELP: reveal
277 { $values { "word" word } }
278 { $description "Adds a newly-created word to the dictionary. Usually this word does not need to be called directly, and is only called as part of " { $link create } "." } ;
279
280 HELP: check-create
281 { $values { "name" string } { "vocab" string } }
282 { $description "Throws a " { $link check-create } " error if " { $snippet "name" } " or " { $snippet "vocab" } " is not a string." }
283 { $error-description "Thrown if " { $link create } " is called with invalid parameters." } ;
284
285 HELP: create
286 { $values { "name" string } { "vocab" string } { "word" word } }
287 { $description "Creates a new word. If the vocabulary already contains a word with the requested name, outputs the existing word. The vocabulary must exist already; if it does not, you must call " { $link create-vocab } " first." } ;
288
289 HELP: constructor-word
290 { $values { "name" string } { "vocab" string } { "word" word } }
291 { $description "Creates a new word, surrounding " { $snippet "name" } " in angle brackets." }
292 { $examples { $example "USING: prettyprint words ;" "\"salmon\" \"scratchpad\" constructor-word ." "<salmon>" } } ;
293
294 { POSTPONE: FORGET: forget forget* forget-vocab } related-words
295
296 HELP: target-word
297 { $values { "word" word } { "target" word } }
298 { $description "Looks up a word with the same name and vocabulary as the given word. Used during bootstrap to transfer host words to the target dictionary." } ;
299
300 HELP: bootstrap-word
301 { $values { "word" word } { "target" word } }
302 { $description "Looks up a word with the same name and vocabulary as the given word, performing a transformation to handle parsing words in the target dictionary. Used during bootstrap to transfer host words to the target dictionary." } ;
303
304 HELP: parsing-word? ( obj -- ? )
305 { $values { "obj" object } { "?" "a boolean" } }
306 { $description "Tests if an object is a parsing word declared by " { $link POSTPONE: parsing } "." }
307 { $notes "Outputs " { $link f } " if the object is not a word." } ;
308
309 HELP: define-declared
310 { $values { "word" word } { "def" quotation } { "effect" effect } }
311 { $description "Defines a word and declares its stack effect." }
312 { $side-effects "word" } ;
313
314 HELP: define-temp
315 { $values { "quot" quotation } { "word" word } }
316 { $description "Creates an uninterned word that will call " { $snippet "quot" } " when executed." }
317 { $notes
318     "The following phrases are equivalent:"
319     { $code "[ 2 2 + . ] call" }
320     { $code "[ 2 2 + . ] define-temp execute" }
321     "This word must be called from inside " { $link with-compilation-unit } "."
322 } ;
323
324 HELP: quot-uses
325 { $values { "quot" quotation } { "assoc" "an assoc with words as keys" } }
326 { $description "Outputs a set of words referenced by the quotation and any quotations it contains." } ;
327
328 HELP: delimiter?
329 { $values { "obj" object } { "?" "a boolean" } }
330 { $description "Tests if an object is a delimiter word declared by " { $link POSTPONE: delimiter } "." }
331 { $notes "Outputs " { $link f } " if the object is not a word." } ;
332
333 HELP: make-flushable
334 { $values { "word" word } }
335 { $description "Declares a word as " { $link POSTPONE: flushable } "." }
336 { $side-effects "word" } ;
337
338 HELP: make-foldable
339 { $values { "word" word } }
340 { $description "Declares a word as " { $link POSTPONE: foldable } "." }
341 { $side-effects "word" } ;
342
343 HELP: make-inline
344 { $values { "word" word } }
345 { $description "Declares a word as " { $link POSTPONE: inline } "." }
346 { $side-effects "word" } ;
347
348 HELP: define-inline
349 { $values { "word" word } { "quot" quotation } }
350 { $description "Defines a word and makes it " { $link POSTPONE: inline } "." }
351 { $side-effects "word" } ;