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