]> gitweb.factorcode.org Git - factor.git/blob - core/words/words-docs.factor
799b8a886a5404fa0ff231c6d12b01d2e3ad8fa2
[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 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           { $snippet "\"recursive\""  }
104       }
105       { $link "declarations" }
106   }
107   {
108       {
109           { $snippet "\"help\"" } ", "
110           { $snippet "\"help-loc\"" } ", "
111           { $snippet "\"help-parent\"" }
112       }
113       { "Where word help is stored - " { $link "writing-help" } }
114   }
115   {
116       { $snippet "\"intrinsic\"" }
117       { "Quotation run by the compiler during cfg building to emit the word inline." }
118   }
119   {
120       { $snippet "\"loc\"" }
121       { "Location information - " { $link where } }
122   }
123   {
124       { { $snippet "\"methods\"" } ", " { $snippet "\"combination\"" } }
125       { "Set on generic words - " { $link "generic" } }
126   }
127   {
128       {
129           { $snippet "\"outputs\"" } ", "
130           { $snippet "\"input-classes\"" } ", "
131           { $snippet "\"default-output-classes\"" }
132       }
133       { "A bunch of metadata used during the value propagation step of the compilation to produce type-optimized code." }
134   }
135   {
136       { $snippet "\"parsing\"" }
137       { $link "parsing-words" }
138   }
139   {
140       { $snippet "\"predicating\"" }
141       "Set on class predicates, stores the corresponding class word."
142   }
143   {
144       { { $snippet "\"reading\"" } ", " { $snippet "\"writing\"" } }
145       { "Set on slot accessor words - " { $link "slots" } }
146   }
147   {
148       { $snippet "\"specializer\"" }
149       { $link "hints" }
150   }
151 }
152 "Properties which are defined for classes only:"
153 { $table
154     { "Property" "Documentation" }
155     { { $snippet "\"class\"" } { "A boolean indicating whether this word is a class - " { $link "classes" } } }
156
157     { { $snippet "\"coercer\"" } { "A quotation for converting the top of the stack to an instance of this class" } }
158
159     { { $snippet "\"constructor\"" } { $link "tuple-constructors" } }
160
161     { { $snippet "\"type\"" } { $link "builtin-classes" } }
162
163     { { { $snippet "\"superclass\"" } ", " { $snippet "\"predicate-definition\"" } } { $link "predicates" } }
164
165     { { $snippet "\"members\"" } { $link "unions" } }
166
167     { { $snippet "\"slots\"" } { $link "slots" } }
168
169     { { $snippet "\"predicate\"" } { "A quotation that tests if the top of the stack is an instance of this class - " { $link "class-predicates" } } }
170 } ;
171
172 ARTICLE: "word.private" "Word implementation details"
173 "The " { $snippet "def" } " slot of a word holds a " { $link quotation } " instance that is called when the word is executed."
174 $nl
175 "A primitive to get the memory range storing the machine code for a word:"
176 { $subsections word-code } ;
177
178 ARTICLE: "words.introspection" "Word introspection"
179 "Word introspection facilities and implementation details are found in the " { $vocab-link "words" } " vocabulary."
180 $nl
181 "Word objects contain several slots:"
182 { $table
183     { { $snippet "name" } "a word name" }
184     { { $snippet "vocabulary" } "a word vocabulary name" }
185     { { $snippet "def" } "a definition quotation" }
186     { { $snippet "props" } "an assoc of word properties, including documentation and other meta-data" }
187 }
188 "Words are instances of a class."
189 { $subsections
190     word
191     word?
192 }
193 "Words implement the definition protocol; see " { $link "definitions" } "."
194 { $subsections
195     "interned-words"
196     "uninterned-words"
197     "word-props"
198     "word.private"
199 } ;
200
201 ARTICLE: "words" "Words"
202 "Words are the Factor equivalent of functions or procedures in other languages. Words are essentially named " { $link "quotations" } "."
203 $nl
204 "There are two ways of creating word definitions:"
205 { $list
206     "using parsing words at parse time."
207     "using defining words at run time."
208 }
209 "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."
210 $nl
211 "Types of words:"
212 { $subsections
213     "colon-definition"
214     "words.symbol"
215     "words.alias"
216     "words.constant"
217     "primitives"
218 }
219 "Advanced topics:"
220 { $subsections
221     "deferred"
222     "declarations"
223     "words.introspection"
224     "word-props"
225 }
226 { $see-also "vocabularies" "vocabs.loader" "definitions" "see" } ;
227
228 ABOUT: "words"
229
230 HELP: changed-effect
231 { $values { "word" word } }
232 { $description "Signals to the compilation unit that the word has changed. It causes all words that depend on it to be recompiled in response." } ;
233
234 HELP: deferred
235 { $class-description "The class of deferred words created by " { $link POSTPONE: DEFER: } "." } ;
236
237 { deferred POSTPONE: DEFER: } related-words
238
239 HELP: undefined
240 { $error-description "This error is thrown in two cases, and the debugger's summary message reflects the cause:"
241     { $list
242         { "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." }
243         { "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." }
244     }
245 } ;
246
247 HELP: primitive
248 { $description "The class of primitive words." } ;
249
250 HELP: word-prop
251 { $values { "word" word } { "name" "a property name" } { "value" "a property value" } }
252 { $description "Retrieves a word property. Word property names are conventionally strings." } ;
253
254 HELP: set-word-prop
255 { $values { "word" word } { "value" "a property value" } { "name" "a property name" } }
256 { $description "Stores a word property. Word property names are conventionally strings." }
257 { $side-effects "word" } ;
258
259 HELP: remove-word-prop
260 { $values { "word" word } { "name" "a property name" } }
261 { $description "Removes a word property, so future lookups will output " { $link f } " until it is set again. Word property names are conventionally strings." }
262 { $side-effects "word" } ;
263
264 HELP: remove-word-props
265 { $values { "word" word } { "seq" { $sequence "word property names" } } }
266 { $description "Removes all listed word properties from the word." }
267 { $side-effects "word" } ;
268
269 HELP: word-code
270 { $values { "word" word } { "start" "the word's start address" } { "end" "the word's end address" } }
271 { $description "Outputs the memory range containing the word's machine code." } ;
272
273 HELP: define
274 { $values { "word" word } { "def" quotation } }
275 { $description "Defines the word to call a quotation when executed. This is the run time equivalent of " { $link POSTPONE: : } "." }
276 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
277 { $side-effects "word" } ;
278
279 HELP: reset-word
280 { $values { "word" word } }
281 { $description "Reset word declarations." }
282 $low-level-note
283 { $side-effects "word" } ;
284
285 HELP: reset-generic
286 { $values { "word" word } }
287 { $description "Reset word declarations and generic word properties." }
288 $low-level-note
289 { $side-effects "word" } ;
290
291 HELP: <word>
292 { $values { "name" string } { "vocab" string } { "word" word } }
293 { $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." }
294 { $notes "This word must be called from inside " { $link with-compilation-unit } "." } ;
295
296 HELP: <uninterned-word>
297 { $values { "name" string } { "word" word } }
298 { $description "Creates an uninterned word with the specified name,  that is not equal to any other word in the system." }
299 { $notes "Unlike " { $link create-word } ", this word does not have to be called from inside " { $link with-compilation-unit } "." } ;
300
301 HELP: gensym
302 { $values { "word" word } }
303 { $description "Creates an uninterned word that is not equal to any other word in the system." }
304 { $examples { $example "USING: prettyprint words ;"
305     "gensym ."
306     "( gensym )"
307     }
308 }
309 { $notes "Unlike " { $link create-word } ", this word does not have to be called from inside " { $link with-compilation-unit } "." } ;
310
311 HELP: bootstrapping?
312 { $var-description "Set by the library while bootstrap is in progress. Some parsing words need to behave differently during bootstrap." } ;
313
314 HELP: last-word
315 { $values { "word" word } }
316 { $description "Outputs the most recently defined word." } ;
317
318 HELP: word
319 { $class-description "The class of words. One notable subclass is " { $link class } ", the class of class words." } ;
320
321 { last-word set-last-word save-location } related-words
322
323 HELP: set-last-word
324 { $values { "word" word } }
325 { $description "Sets the recently defined word." } ;
326
327 HELP: lookup-word
328 { $values { "name" string } { "vocab" string } { "word" { $maybe word } } }
329 { $description "Looks up a word in the dictionary. If the vocabulary or the word is not defined, outputs " { $link f } "." } ;
330
331 HELP: reveal
332 { $values { "word" word } }
333 { $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 } "." } ;
334
335 HELP: check-create
336 { $values { "name" string } { "vocab" string } }
337 { $description "Throws a " { $link check-create } " error if " { $snippet "name" } " or " { $snippet "vocab" } " is not a string." }
338 { $error-description "Thrown if " { $link create-word } " is called with invalid parameters." } ;
339
340 HELP: create-word
341 { $values { "name" string } { "vocab" string } { "word" word } }
342 { $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." }
343 { $notes "This word must be called from inside " { $link with-compilation-unit } ". Parsing words should call " { $link create-word-in } " instead of this word." } ;
344
345 HELP: constructor-word
346 { $values { "name" string } { "vocab" string } { "word" word } }
347 { $description "Creates a new word, surrounding " { $snippet "name" } " in angle brackets." }
348 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
349 { $examples { $example "USING: compiler.units prettyprint words ;" "[ \"salmon\" \"scratchpad\" constructor-word ] with-compilation-unit ." "<salmon>" } } ;
350
351 { POSTPONE: FORGET: forget forget* forget-vocab } related-words
352
353 HELP: target-word
354 { $values { "word" word } { "target" word } }
355 { $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." } ;
356
357 HELP: bootstrap-word
358 { $values { "word" word } { "target" word } }
359 { $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." } ;
360
361 HELP: parsing-word?
362 { $values { "object" object } { "?" boolean } }
363 { $description "Tests if an object is a parsing word declared by " { $link POSTPONE: SYNTAX: } "." }
364 { $notes "Outputs " { $link f } " if the object is not a word." } ;
365
366 HELP: define-declared
367 { $values { "word" word } { "def" quotation } { "effect" effect } }
368 { $description "Defines a word and declares its stack effect." }
369 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
370 { $side-effects "word" } ;
371
372 HELP: define-temp
373 { $values { "quot" quotation } { "effect" effect } { "word" word } }
374 { $description "Creates an uninterned word that will call " { $snippet "quot" } " when executed." }
375 { $notes
376     "The following phrases are equivalent:"
377     { $code "[ 2 2 + . ] call" }
378     { $code "[ 2 2 + . ] ( -- ) define-temp execute" }
379     "This word must be called from inside " { $link with-compilation-unit } "."
380 } ;
381
382 HELP: delimiter?
383 { $values { "obj" object } { "?" boolean } }
384 { $description "Tests if an object is a delimiter word declared by " { $link POSTPONE: delimiter } "." }
385 { $notes "Outputs " { $link f } " if the object is not a word." } ;
386
387 HELP: deprecated?
388 { $values { "obj" object } { "?" boolean } }
389 { $description "Tests if an object is " { $link POSTPONE: deprecated } "." }
390 { $notes "Outputs " { $link f } " if the object is not a word." } ;
391
392 HELP: inline?
393 { $values { "obj" object } { "?" boolean } }
394 { $description "Tests if an object is " { $link POSTPONE: inline } "." }
395 { $notes "Outputs " { $link f } " if the object is not a word." } ;
396
397 HELP: subwords
398 { $values { "word" word } { "seq" sequence } }
399 { $description "Lists all specializations for the given word." }
400 { $examples
401   { $example
402     "USING: math.functions prettyprint words ;"
403     "\\ sin subwords ."
404     "{ M\\ object sin M\\ complex sin M\\ real sin M\\ float sin }"
405   }
406 }
407 { $notes "Outputs " { $link f } " if the word isn't generic." } ;
408
409 HELP: make-deprecated
410 { $values { "word" word } }
411 { $description "Declares a word as " { $link POSTPONE: deprecated } "." }
412 { $side-effects "word" } ;
413
414 HELP: make-flushable
415 { $values { "word" word } }
416 { $description "Declares a word as " { $link POSTPONE: flushable } "." }
417 { $side-effects "word" } ;
418
419 HELP: make-foldable
420 { $values { "word" word } }
421 { $description "Declares a word as " { $link POSTPONE: foldable } "." }
422 { $side-effects "word" } ;
423
424 HELP: make-inline
425 { $values { "word" word } }
426 { $description "Declares a word as " { $link POSTPONE: inline } "." }
427 { $side-effects "word" } ;
428
429 HELP: define-inline
430 { $values { "word" word } { "def" quotation } { "effect" effect } }
431 { $description "Defines a word and makes it " { $link POSTPONE: inline } "." }
432 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
433 { $side-effects "word" } ;