]> gitweb.factorcode.org Git - factor.git/blob - core/words/words-docs.factor
91b10936c0ad8208309c3e361853160ebafe3b13
[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 ( -- value ) \\ 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 "\"predicating\"" } " Set on class predicates, stores the corresponding class word" }
130 }
131 "Properties which are defined for classes only:"
132 { $table
133     { "Property" "Documentation" }
134     { { $snippet "\"class\"" } { "A boolean indicating whether this word is a class - " { $link "classes" } } }
135
136     { { $snippet "\"coercer\"" } { "A quotation for converting the top of the stack to an instance of this class" } }
137     
138     { { $snippet "\"constructor\"" } { $link "tuple-constructors" } }
139     
140     { { $snippet "\"type\"" } { $link "builtin-classes" } }
141     
142     { { { $snippet "\"superclass\"" } ", " { $snippet "\"predicate-definition\"" } } { $link "predicates" } }
143     
144     { { $snippet "\"members\"" } { $link "unions" } }
145
146     { { $snippet "\"slots\"" } { $link "slots" } }
147
148     { { $snippet "\"predicate\"" } { "A quotation that tests if the top of the stack is an instance of this class - " { $link "class-predicates" } } }
149 } ;
150
151 ARTICLE: "word.private" "Word implementation details"
152 "The " { $snippet "def" } " slot of a word holds a " { $link quotation } " instance that is called when the word is executed."
153 $nl
154 "An " { $emphasis "XT" } " (execution token) is the machine code address of a word:"
155 { $subsection word-xt } ;
156
157 ARTICLE: "words" "Words"
158 "Words are the Factor equivalent of functions or procedures; a word is essentially a named quotation."
159 $nl
160 "Word introspection facilities and implementation details are found in the " { $vocab-link "words" } " vocabulary."
161 $nl
162 "Word objects contain several slots:"
163 { $table
164     { { $snippet "name" } "a word name" }
165     { { $snippet "vocabulary" } "a word vocabulary name" }
166     { { $snippet "def" } "a definition quotation" }
167     { { $snippet "props" } "an assoc of word properties, including documentation and other meta-data" }
168 }
169 "Words are instances of a class."
170 { $subsection word }
171 { $subsection word? }
172 { $subsection "interned-words" }
173 { $subsection "uninterned-words" }
174 { $subsection "word-definition" }
175 { $subsection "word-props" }
176 { $subsection "word.private" }
177 { $see-also "vocabularies" "vocabs.loader" "definitions" } ;
178
179 ABOUT: "words"
180
181 HELP: execute ( word -- )
182 { $values { "word" word } }
183 { $description "Executes a word." }
184 { $examples
185     { $example "USING: kernel io words ;" "IN: scratchpad" ": twice dup execute execute ;\n: hello \"Hello\" print ;\n\\ hello twice" "Hello\nHello" }
186 } ;
187
188 HELP: deferred
189 { $class-description "The class of deferred words created by " { $link POSTPONE: DEFER: } "." } ;
190
191 { deferred POSTPONE: DEFER: } related-words
192
193 HELP: primitive
194 { $description "The class of primitive words." } ;
195
196 HELP: symbol
197 { $description "The class of symbols created by " { $link POSTPONE: SYMBOL: } "." } ;
198
199 HELP: word-prop
200 { $values { "word" word } { "name" "a property name" } { "value" "a property value" } }
201 { $description "Retrieves a word property. Word property names are conventionally strings." } ;
202
203 HELP: set-word-prop
204 { $values { "word" word } { "value" "a property value" } { "name" "a property name" } }
205 { $description "Stores a word property. Word property names are conventionally strings." }
206 { $side-effects "word" } ;
207
208 HELP: remove-word-prop
209 { $values { "word" word } { "name" "a property name" } }
210 { $description "Removes a word property, so future lookups will output " { $link f } " until it is set again. Word property names are conventionally strings." }
211 { $side-effects "word" } ;
212
213 HELP: word-xt ( word -- start end )
214 { $values { "word" word } { "start" "the word's start address" } { "end" "the word's end address" } }
215 { $description "Outputs the machine code address of the word's definition." } ;
216
217 HELP: define-symbol
218 { $values { "word" word } }
219 { $description "Defines the word to push itself on the stack when executed. This is the run time equivalent of " { $link POSTPONE: SYMBOL: } "." }
220 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
221 { $side-effects "word" } ;
222
223 HELP: define
224 { $values { "word" word } { "def" quotation } }
225 { $description "Defines the word to call a quotation when executed. This is the run time equivalent of " { $link POSTPONE: : } "." }
226 { $notes "This word must be called from inside " { $link with-compilation-unit } "." }
227 { $side-effects "word" } ;
228
229 HELP: reset-props
230 { $values { "word" word } { "seq" "a sequence of word property names" } }
231 { $description "Removes all listed word properties from the word." }
232 { $side-effects "word" } ;
233
234 HELP: reset-word
235 { $values { "word" word } }
236 { $description "Reset word declarations." }
237 $low-level-note
238 { $side-effects "word" } ;
239
240 HELP: reset-generic
241 { $values { "word" word } }
242 { $description "Reset word declarations and generic word properties." }
243 $low-level-note
244 { $side-effects "word" } ;
245
246 HELP: <word> ( name vocab -- word )
247 { $values { "name" string } { "vocab" string } { "word" word } }
248 { $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." } ;
249
250 HELP: gensym
251 { $values { "word" word } }
252 { $description "Creates an uninterned word that is not equal to any other word in the system." }
253 { $examples { $unchecked-example "gensym ." "G:260561" } }
254 { $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." } ;
255
256 HELP: bootstrapping?
257 { $var-description "Set by the library while bootstrap is in progress. Some parsing words need to behave differently during bootstrap." } ;
258
259 HELP: word
260 { $values { "word" word } }
261 { $description "Outputs the most recently defined word." }
262 { $class-description "The class of words. One notable subclass is " { $link class } ", the class of class words." } ;
263
264 { word set-word save-location } related-words
265
266 HELP: set-word
267 { $values { "word" word } }
268 { $description "Sets the recently defined word." } ;
269
270 HELP: lookup
271 { $values { "name" string } { "vocab" string } { "word" "a word or " { $link f } } }
272 { $description "Looks up a word in the dictionary. If the vocabulary or the word is not defined, outputs " { $link f } "." } ;
273
274 HELP: reveal
275 { $values { "word" word } }
276 { $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 } "." } ;
277
278 HELP: check-create
279 { $values { "name" string } { "vocab" string } }
280 { $description "Throws a " { $link check-create } " error if " { $snippet "name" } " or " { $snippet "vocab" } " is not a string." }
281 { $error-description "Thrown if " { $link create } " is called with invalid parameters." } ;
282
283 HELP: create
284 { $values { "name" string } { "vocab" string } { "word" word } }
285 { $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." } ;
286
287 HELP: constructor-word
288 { $values { "name" string } { "vocab" string } { "word" word } }
289 { $description "Creates a new word, surrounding " { $snippet "name" } " in angle brackets." }
290 { $examples { $example "USING: prettyprint words ;" "\"salmon\" \"scratchpad\" constructor-word ." "<salmon>" } } ;
291
292 { POSTPONE: FORGET: forget forget* forget-vocab } related-words
293
294 HELP: target-word
295 { $values { "word" word } { "target" word } }
296 { $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." } ;
297
298 HELP: bootstrap-word
299 { $values { "word" word } { "target" word } }
300 { $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." } ;
301
302 HELP: parsing-word? ( obj -- ? )
303 { $values { "obj" object } { "?" "a boolean" } }
304 { $description "Tests if an object is a parsing word declared by " { $link POSTPONE: parsing } "." }
305 { $notes "Outputs " { $link f } " if the object is not a word." } ;
306
307 HELP: define-declared
308 { $values { "word" word } { "def" quotation } { "effect" effect } }
309 { $description "Defines a word and declares its stack effect." }
310 { $side-effects "word" } ;
311
312 HELP: define-temp
313 { $values { "quot" quotation } { "word" word } }
314 { $description "Creates an uninterned word that will call " { $snippet "quot" } " when executed." }
315 { $notes
316     "The following phrases are equivalent:"
317     { $code "[ 2 2 + . ] call" }
318     { $code "[ 2 2 + . ] define-temp execute" }
319     "This word must be called from inside " { $link with-compilation-unit } "."
320 } ;
321
322 HELP: quot-uses
323 { $values { "quot" quotation } { "assoc" "an assoc with words as keys" } }
324 { $description "Outputs a set of words referenced by the quotation and any quotations it contains." } ;
325
326 HELP: delimiter?
327 { $values { "obj" object } { "?" "a boolean" } }
328 { $description "Tests if an object is a delimiter word declared by " { $link POSTPONE: delimiter } "." }
329 { $notes "Outputs " { $link f } " if the object is not a word." } ;
330
331 HELP: make-flushable
332 { $values { "word" word } }
333 { $description "Declares a word as " { $link POSTPONE: flushable } "." }
334 { $side-effects "word" } ;
335
336 HELP: make-foldable
337 { $values { "word" word } }
338 { $description "Declares a word as " { $link POSTPONE: foldable } "." }
339 { $side-effects "word" } ;
340
341 HELP: make-inline
342 { $values { "word" word } }
343 { $description "Declares a word as " { $link POSTPONE: inline } "." }
344 { $side-effects "word" } ;
345
346 HELP: define-inline
347 { $values { "word" word } { "def" quotation } { "effect" effect } }
348 { $description "Defines a word and makes it " { $link POSTPONE: inline } "." }
349 { $side-effects "word" } ;