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