]> gitweb.factorcode.org Git - factor.git/blob - core/words/words-docs.factor
core: use $sequence help-markup in a few places.
[factor.git] / core / words / words-docs.factor
1 USING: classes compiler.units definitions effects help.markup
2 help.syntax kernel parser quotations sequences strings vocabs ;
3 IN: words
4
5 ARTICLE: "interned-words" "Looking up and creating words"
6 "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" } "."
7 $nl
8 "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."
9 $nl
10 "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 "word-search" } ")."
11 { $subsections
12     create-word
13     create-word-in
14     lookup-word
15 } ;
16
17 ARTICLE: "uninterned-words" "Uninterned words"
18 "A word that is not a member of any vocabulary is said to be " { $emphasis "uninterned" } "."
19 $nl
20 "There are several ways of creating an uninterned word:"
21 { $subsections
22     <word>
23     <uninterned-word>
24     gensym
25     define-temp
26 } ;
27
28 ARTICLE: "colon-definition" "Colon definitions"
29 "All words have associated definition " { $link "quotations" } ". A word's definition quotation is called when the word is executed. A " { $emphasis "colon definition" } " is a word where this quotation is supplied directly by the user. This is the simplest and most common type of word definition."
30 $nl
31 "Defining words at parse time:"
32 { $subsections
33     POSTPONE: :
34     POSTPONE: ;
35 }
36 "Defining words at run time:"
37 { $subsections
38     define
39     define-declared
40     define-inline
41 }
42 "Word definitions must declare their stack effect. See " { $link "effects" } "."
43 $nl
44 "All other types of word definitions, such as " { $link "words.symbol" } " and " { $link "generic" } ", are just special cases of the above." ;
45
46 ARTICLE: "primitives" "Primitives"
47 "Primitives are words defined in the Factor VM. They provide the essential low-level services to the rest of the system."
48 { $subsections
49     primitive
50     primitive?
51 } ;
52
53 ARTICLE: "deferred" "Deferred words and mutual recursion"
54 "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."
55 $nl
56 "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."
57 { $subsections POSTPONE: DEFER: }
58 "The class of deferred word definitions:"
59 { $subsections
60     deferred
61     deferred?
62 }
63 "Deferred words throw an error when called:"
64 { $subsections undefined }
65 "Deferred words are just compound definitions in disguise. The following two lines are equivalent:"
66 { $code
67     "DEFER: foo"
68     ": foo ( -- * ) undefined ;"
69 } ;
70
71 ARTICLE: "declarations" "Compiler declarations"
72 "Compiler declarations are parsing words that set a word property in the most recently defined word. They appear after the final " { $link POSTPONE: ; } " of a word definition:"
73 { $code ": cubed ( x -- y ) dup dup * * ; foldable" }
74 "Compiler declarations assert that the word follows a certain contract, enabling certain optimizations that are not valid in general."
75 { $subsections
76     POSTPONE: inline
77     POSTPONE: foldable
78     POSTPONE: flushable
79     POSTPONE: recursive
80 }
81 "It is entirely up to the programmer to ensure that the word satisfies the contract of a declaration. Furthermore, if a generic word is declared " { $link POSTPONE: foldable } " or " { $link POSTPONE: flushable } ", all methods must satisfy the contract. Unspecified behavior may result if a word does not follow the contract of one of its declarations."
82 { $see-also "effects" } ;
83
84 ARTICLE: "word-props" "Word properties"
85 "Each word has a hashtable of properties."
86 { $subsections
87     word-prop
88     set-word-prop
89 }
90 "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."
91 $nl
92 "The following are some of the properties used by the library:"
93 { $table
94   { "Property" "Documentation" }
95   {
96       { $snippet "\"declared-effect\"" } { $link "effects" }
97   }
98   {
99       {
100           { $snippet "\"inline\"" } ", "
101           { $snippet "\"foldable\"" } ", "
102           { $snippet "flushable" }
103       }
104       { $link "declarations" }
105   }
106   {
107       {
108           { $snippet "\"help\"" } ", "
109           { $snippet "\"help-loc\"" } ", "
110           { $snippet "\"help-parent\"" }
111       }
112       { "Where word help is stored - " { $link "writing-help" } }
113   }
114   {
115       { $snippet "\"intrinsic\"" }
116       { "Quotation run by the compiler during cfg building to emit the word inline." }
117   }
118   {
119       { $snippet "\"loc\"" }
120       { "Location information - " { $link where } }
121   }
122   {
123       { { $snippet "\"methods\"" } ", " { $snippet "\"combination\"" } }
124       { "Set on generic words - " { $link "generic" } }
125   }
126   {
127       {
128           { $snippet "\"outputs\"" } ", "
129           { $snippet "\"input-classes\"" } ", "
130           { $snippet "\"default-output-classes\"" }
131       }
132       { "A bunch of metadata used during the value propagation step of the compilation to produce type-optimized code." }
133   }
134   {
135       { $snippet "\"parsing\"" }
136       { $link "parsing-words" }
137   }
138   {
139       { $snippet "\"predicating\"" }
140       " Set on class predicates, stores the corresponding class word"
141   }
142   {
143       { { $snippet "\"reading\"" } ", " { $snippet "\"writing\"" } }
144       { "Set on slot accessor words - " { $link "slots" } }
145   }
146   {
147       { $snippet "\"specializer\"" }
148       { $link "hints" }
149   }
150 }
151 "Properties which are defined for classes only:"
152 { $table
153     { "Property" "Documentation" }
154     { { $snippet "\"class\"" } { "A boolean indicating whether this word is a class - " { $link "classes" } } }
155
156     { { $snippet "\"coercer\"" } { "A quotation for converting the top of the stack to an instance of this class" } }
157
158     { { $snippet "\"constructor\"" } { $link "tuple-constructors" } }
159
160     { { $snippet "\"type\"" } { $link "builtin-classes" } }
161
162     { { { $snippet "\"superclass\"" } ", " { $snippet "\"predicate-definition\"" } } { $link "predicates" } }
163
164     { { $snippet "\"members\"" } { $link "unions" } }
165
166     { { $snippet "\"slots\"" } { $link "slots" } }
167
168     { { $snippet "\"predicate\"" } { "A quotation that tests if the top of the stack is an instance of this class - " { $link "class-predicates" } } }
169 } ;
170
171 ARTICLE: "word.private" "Word implementation details"
172 "The " { $snippet "def" } " slot of a word holds a " { $link quotation } " instance that is called when the word is executed."
173 $nl
174 "A primitive to get the memory range storing the machine code for a word:"
175 { $subsections word-code } ;
176
177 ARTICLE: "words.introspection" "Word introspection"
178 "Word introspection facilities and implementation details are found in the " { $vocab-link "words" } " vocabulary."
179 $nl
180 "Word objects contain several slots:"
181 { $table
182     { { $snippet "name" } "a word name" }
183     { { $snippet "vocabulary" } "a word vocabulary name" }
184     { { $snippet "def" } "a definition quotation" }
185     { { $snippet "props" } "an assoc of word properties, including documentation and other meta-data" }
186 }
187 "Words are instances of a class."
188 { $subsections
189     word
190     word?
191 }
192 "Words implement the definition protocol; see " { $link "definitions" } "."
193 { $subsections
194     "interned-words"
195     "uninterned-words"
196     "word-props"
197     "word.private"
198 } ;
199
200 ARTICLE: "words" "Words"
201 "Words are the Factor equivalent of functions or procedures in other languages. Words are essentially named " { $link "quotations" } "."
202 $nl
203 "There are two ways of creating word definitions:"
204 { $list
205     "using parsing words at parse time."
206     "using defining words at run time."
207 }
208 "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."
209 $nl
210 "Types of words:"
211 { $subsections
212     "colon-definition"
213     "words.symbol"
214     "words.alias"
215     "words.constant"
216     "primitives"
217 }
218 "Advanced topics:"
219 { $subsections
220     "deferred"
221     "declarations"
222     "words.introspection"
223     "word-props"
224 }
225 { $see-also "vocabularies" "vocabs.loader" "definitions" "see" } ;
226
227 ABOUT: "words"
228
229 HELP: changed-effect
230 { $values { "word" word } }
231 { $description "Signals to the compilation unit that the word has changed. It causes all words that depend on it to be recompiled in response." } ;
232
233 HELP: deferred
234 { $class-description "The class of deferred words created by " { $link POSTPONE: DEFER: } "." } ;
235
236 { deferred POSTPONE: DEFER: } related-words
237
238 HELP: undefined
239 { $error-description "This error is thrown in two cases, and the debugger's summary message reflects the cause:"
240     { $list
241         { "A word was executed before being compiled. For example, this can happen if a macro is defined in the same compilation unit where it was used. See " { $link "compilation-units" } " for a discussion." }
242         { "A word defined with " { $link POSTPONE: DEFER: } " was executed. Since this syntax is usually used for mutually-recursive word definitions, executing a deferred word usually indicates a programmer mistake." }
243     }
244 } ;
245
246 HELP: primitive
247 { $description "The class of primitive words." } ;
248
249 HELP: word-prop
250 { $values { "word" word } { "name" "a property name" } { "value" "a property value" } }
251 { $description "Retrieves a word property. Word property names are conventionally strings." } ;
252
253 HELP: set-word-prop
254 { $values { "word" word } { "value" "a property value" } { "name" "a property name" } }
255 { $description "Stores a word property. Word property names are conventionally strings." }
256 { $side-effects "word" } ;
257
258 HELP: remove-word-prop
259 { $values { "word" word } { "name" "a property name" } }
260 { $description "Removes a word property, so future lookups will output " { $link f } " until it is set again. Word property names are conventionally strings." }
261 { $side-effects "word" } ;
262
263 HELP: remove-word-props
264 { $values { "word" word } { "seq" { $sequence "word property names" } } }
265 { $description "Removes all listed word properties from the word." }
266 { $side-effects "word" } ;
267
268 HELP: word-code
269 { $values { "word" word } { "start" "the word's start address" } { "end" "the word's end address" } }
270 { $description "Outputs the memory range containing the word's machine code." } ;
271
272 HELP: define
273 { $values { "word" word } { "def" quotation } }
274 { $description "Defines the word to call a quotation when executed. This is the run time equivalent of " { $link POSTPONE: : } "." }
275 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
276 { $side-effects "word" } ;
277
278 HELP: reset-word
279 { $values { "word" word } }
280 { $description "Reset word declarations." }
281 $low-level-note
282 { $side-effects "word" } ;
283
284 HELP: reset-generic
285 { $values { "word" word } }
286 { $description "Reset word declarations and generic word properties." }
287 $low-level-note
288 { $side-effects "word" } ;
289
290 HELP: <word>
291 { $values { "name" string } { "vocab" string } { "word" word } }
292 { $description "Allocates a word with the specified name and vocabulary. User code should call " { $link <uninterned-word> } " to create uninterned words and " { $link create-word } " to create interned words, instead of calling this constructor directly." }
293 { $notes "This word must be called from inside " { $link with-compilation-unit } "." } ;
294
295 HELP: <uninterned-word>
296 { $values { "name" string } { "word" word } }
297 { $description "Creates an uninterned word with the specified name,  that is not equal to any other word in the system." }
298 { $notes "Unlike " { $link create-word } ", this word does not have to be called from inside " { $link with-compilation-unit } "." } ;
299
300 HELP: gensym
301 { $values { "word" word } }
302 { $description "Creates an uninterned word that is not equal to any other word in the system." }
303 { $examples { $example "USING: prettyprint words ;"
304     "gensym ."
305     "( gensym )"
306     }
307 }
308 { $notes "Unlike " { $link create-word } ", this word does not have to be called from inside " { $link with-compilation-unit } "." } ;
309
310 HELP: bootstrapping?
311 { $var-description "Set by the library while bootstrap is in progress. Some parsing words need to behave differently during bootstrap." } ;
312
313 HELP: last-word
314 { $values { "word" word } }
315 { $description "Outputs the most recently defined word." } ;
316
317 HELP: word
318 { $class-description "The class of words. One notable subclass is " { $link class } ", the class of class words." } ;
319
320 { last-word set-last-word save-location } related-words
321
322 HELP: set-last-word
323 { $values { "word" word } }
324 { $description "Sets the recently defined word." } ;
325
326 HELP: lookup-word
327 { $values { "name" string } { "vocab" string } { "word" { $maybe word } } }
328 { $description "Looks up a word in the dictionary. If the vocabulary or the word is not defined, outputs " { $link f } "." } ;
329
330 HELP: reveal
331 { $values { "word" word } }
332 { $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-word } "." } ;
333
334 HELP: check-create
335 { $values { "name" string } { "vocab" string } }
336 { $description "Throws a " { $link check-create } " error if " { $snippet "name" } " or " { $snippet "vocab" } " is not a string." }
337 { $error-description "Thrown if " { $link create-word } " is called with invalid parameters." } ;
338
339 HELP: create-word
340 { $values { "name" string } { "vocab" string } { "word" word } }
341 { $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." }
342 { $notes "This word must be called from inside " { $link with-compilation-unit } ". Parsing words should call " { $link create-word-in } " instead of this word." } ;
343
344 HELP: constructor-word
345 { $values { "name" string } { "vocab" string } { "word" word } }
346 { $description "Creates a new word, surrounding " { $snippet "name" } " in angle brackets." }
347 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
348 { $examples { $example "USING: compiler.units prettyprint words ;" "[ \"salmon\" \"scratchpad\" constructor-word ] with-compilation-unit ." "<salmon>" } } ;
349
350 { POSTPONE: FORGET: forget forget* forget-vocab } related-words
351
352 HELP: target-word
353 { $values { "word" word } { "target" word } }
354 { $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." } ;
355
356 HELP: bootstrap-word
357 { $values { "word" word } { "target" word } }
358 { $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." } ;
359
360 HELP: parsing-word?
361 { $values { "object" object } { "?" boolean } }
362 { $description "Tests if an object is a parsing word declared by " { $link POSTPONE: SYNTAX: } "." }
363 { $notes "Outputs " { $link f } " if the object is not a word." } ;
364
365 HELP: define-declared
366 { $values { "word" word } { "def" quotation } { "effect" effect } }
367 { $description "Defines a word and declares its stack effect." }
368 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
369 { $side-effects "word" } ;
370
371 HELP: define-temp
372 { $values { "quot" quotation } { "effect" effect } { "word" word } }
373 { $description "Creates an uninterned word that will call " { $snippet "quot" } " when executed." }
374 { $notes
375     "The following phrases are equivalent:"
376     { $code "[ 2 2 + . ] call" }
377     { $code "[ 2 2 + . ] ( -- ) define-temp execute" }
378     "This word must be called from inside " { $link with-compilation-unit } "."
379 } ;
380
381 HELP: delimiter?
382 { $values { "obj" object } { "?" boolean } }
383 { $description "Tests if an object is a delimiter word declared by " { $link POSTPONE: delimiter } "." }
384 { $notes "Outputs " { $link f } " if the object is not a word." } ;
385
386 HELP: deprecated?
387 { $values { "obj" object } { "?" boolean } }
388 { $description "Tests if an object is " { $link POSTPONE: deprecated } "." }
389 { $notes "Outputs " { $link f } " if the object is not a word." } ;
390
391 HELP: inline?
392 { $values { "obj" object } { "?" boolean } }
393 { $description "Tests if an object is " { $link POSTPONE: inline } "." }
394 { $notes "Outputs " { $link f } " if the object is not a word." } ;
395
396 HELP: subwords
397 { $values { "word" word } { "seq" sequence } }
398 { $description "Lists all specializations for the given word." }
399 { $examples
400   { $example
401     "USING: math.functions prettyprint words ;"
402     "\\ sin subwords ."
403     "{ M\\ object sin M\\ complex sin M\\ real sin M\\ float sin }"
404   }
405 }
406 { $notes "Outputs " { $link f } " if the word isn't generic." } ;
407
408 HELP: make-deprecated
409 { $values { "word" word } }
410 { $description "Declares a word as " { $link POSTPONE: deprecated } "." }
411 { $side-effects "word" } ;
412
413 HELP: make-flushable
414 { $values { "word" word } }
415 { $description "Declares a word as " { $link POSTPONE: flushable } "." }
416 { $side-effects "word" } ;
417
418 HELP: make-foldable
419 { $values { "word" word } }
420 { $description "Declares a word as " { $link POSTPONE: foldable } "." }
421 { $side-effects "word" } ;
422
423 HELP: make-inline
424 { $values { "word" word } }
425 { $description "Declares a word as " { $link POSTPONE: inline } "." }
426 { $side-effects "word" } ;
427
428 HELP: define-inline
429 { $values { "word" word } { "def" quotation } { "effect" effect } }
430 { $description "Defines a word and makes it " { $link POSTPONE: inline } "." }
431 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
432 { $side-effects "word" } ;