]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax-docs.factor
Update documentation for stricter vocabulary search path semantics
[factor.git] / core / syntax / syntax-docs.factor
1 USING: generic help.syntax help.markup kernel math parser words
2 effects classes generic.standard classes.tuple generic.math
3 generic.standard generic.single arrays io.pathnames vocabs.loader io
4 sequences assocs words.symbol words.alias words.constant combinators 
5 vocabs.parser ;
6 IN: syntax
7
8 ARTICLE: "parser-algorithm" "Parser algorithm"
9 "At the most abstract level, Factor syntax consists of whitespace-separated tokens. The parser tokenizes the input on whitespace boundaries. The parser is case-sensitive and whitespace between tokens is significant, so the following three expressions tokenize differently:"
10 { $code "2X+\n2 X +\n2 x +" }
11 "As the parser reads tokens it makes a distinction between numbers, ordinary words, and parsing words. Tokens are appended to the parse tree, the top level of which is a quotation returned by the original parser invocation. Nested levels of the parse tree are created by parsing words."
12 $nl
13 "The parser iterates through the input text, checking each character in turn. Here is the parser algorithm in more detail -- some of the concepts therein will be defined shortly:"
14 { $list
15     { "If the current character is a double-quote (\"), the " { $link POSTPONE: " } " parsing word is executed, causing a string to be read." }
16     {
17         "Otherwise, the next token is taken from the input. The parser searches for a word named by the token in the currently used set of vocabularies. If the word is found, one of the following two actions is taken:"
18         { $list
19             "If the word is an ordinary word, it is appended to the parse tree."
20             "If the word is a parsing word, it is executed."
21         }
22     }
23     "Otherwise if the token does not represent a known word, the parser attempts to parse it as a number. If the token is a number, the number object is added to the parse tree. Otherwise, an error is raised and parsing halts."
24 }
25 "Parsing words play a key role in parsing; while ordinary words and numbers are simply added to the parse tree, parsing words execute in the context of the parser, and can do their own parsing and create nested data structures in the parse tree. Parsing words are also able to define new words."
26 $nl
27 "While parsing words supporting arbitrary syntax can be defined, the default set is found in the " { $vocab-link "syntax" } " vocabulary and provides the basis for all further syntactic interaction with Factor." ;
28
29 ARTICLE: "syntax-comments" "Comments"
30 { $subsection POSTPONE: ! }
31 { $subsection POSTPONE: #! } ;
32
33 ARTICLE: "syntax-immediate" "Parse time evaluation"
34 "Code can be evaluated at parse time. This is a rarely-used feature; one use-case is " { $link "loading-libs" } ", where you want to execute some code before the words in a source file are compiled."
35 { $subsection POSTPONE: << }
36 { $subsection POSTPONE: >> } ;
37
38 ARTICLE: "syntax-integers" "Integer syntax"
39 "The printed representation of an integer consists of a sequence of digits, optionally prefixed by a sign."
40 { $code
41     "123456"
42     "-10"
43     "2432902008176640000"
44 }
45 "Integers are entered in base 10 unless prefixed with a base change parsing word."
46 { $subsection POSTPONE: BIN: }
47 { $subsection POSTPONE: OCT: }
48 { $subsection POSTPONE: HEX: }
49 "More information on integers can be found in " { $link "integers" } "." ;
50
51 ARTICLE: "syntax-ratios" "Ratio syntax"
52 "The printed representation of a ratio is a pair of integers separated by a slash (/), prefixed by an optional whole number part followed by a plus (+). No intermediate whitespace is permitted. Here are some examples:"
53 { $code
54     "75/33"
55     "1/10"
56     "-5/-6"
57     "1+1/3"
58     "-10+1/7"
59 }
60 "More information on ratios can be found in " { $link "rationals" } ;
61
62 ARTICLE: "syntax-floats" "Float syntax"
63 "Floating point literals must contain a decimal point, and may contain an exponent:"
64 { $code
65     "10.5"
66     "-3.1456"
67     "7.e13"
68     "1.0e-5"
69 }
70 "There are three special float values:"
71 { $table
72 { "Positive infinity" { $snippet "1/0." } }
73 { "Negative infinity" { $snippet "-1/0." } }
74 { "Not-a-number" { $snippet "0/0." } }
75 }
76 "More information on floats can be found in " { $link "floats" } "." ;
77
78 ARTICLE: "syntax-complex-numbers" "Complex number syntax"
79 "A complex number is given by two components, a “real” part and “imaginary” part. The components must either be integers, ratios or floats."
80 { $code
81     "C{ 1/2 1/3 }   ! the complex number 1/2+1/3i"
82     "C{ 0 1 }       ! the imaginary unit"
83 }
84 { $subsection POSTPONE: C{ }
85 "More information on complex numbers can be found in " { $link "complex-numbers" } "." ;
86
87 ARTICLE: "syntax-numbers" "Number syntax"
88 "If a vocabulary lookup of a token fails, the parser attempts to parse it as a number."
89 { $subsection "syntax-integers" }
90 { $subsection "syntax-ratios" }
91 { $subsection "syntax-floats" }
92 { $subsection "syntax-complex-numbers" } ;
93
94 ARTICLE: "syntax-words" "Word syntax"
95 "A word occurring inside a quotation is executed when the quotation is called. Sometimes a word needs to be pushed on the data stack instead. The canonical use-case for this is passing the word to the " { $link execute } " combinator, or alternatively, reflectively accessing word properties (" { $link "word-props" } ")."
96 { $subsection POSTPONE: \ }
97 { $subsection POSTPONE: POSTPONE: }
98 "The implementation of the " { $link POSTPONE: \ } " word is discussed in detail in " { $link "reading-ahead" } ". Words are documented in " { $link "words" } "." ;
99
100 ARTICLE: "escape" "Character escape codes"
101 { $table
102     { "Escape code" "Meaning" }
103     { { $snippet "\\\\" } { $snippet "\\" } }
104     { { $snippet "\\s" } "a space" }
105     { { $snippet "\\t" } "a tab" }
106     { { $snippet "\\n" } "a newline" }
107     { { $snippet "\\r" } "a carriage return" }
108     { { $snippet "\\0" } "a null byte (ASCII 0)" }
109     { { $snippet "\\e" } "escape (ASCII 27)" }
110     { { $snippet "\\\"" } { $snippet "\"" } }
111     { { $snippet "\\u" { $emphasis "xxxxxx" } } { "The Unicode code point with hexadecimal number " { $snippet { $emphasis "xxxxxx" } } } }
112     { { $snippet "\\u{" { $emphasis "name" } "}" } { "The Unicode code point named " { $snippet { $emphasis "name" } } } }
113 } ;
114
115 ARTICLE: "syntax-strings" "Character and string syntax"
116 "Factor has no distinct character type, however Unicode character value integers can be read by specifying a literal character, or an escaped representation thereof."
117 { $subsection POSTPONE: CHAR: }
118 { $subsection POSTPONE: " }
119 { $subsection "escape" }
120 "Strings are documented in " { $link "strings" } "." ;
121
122 ARTICLE: "syntax-sbufs" "String buffer syntax"
123 { $subsection POSTPONE: SBUF" }
124 "String buffers are documented in " { $link "sbufs" } "." ;
125
126 ARTICLE: "syntax-arrays" "Array syntax"
127 { $subsection POSTPONE: { }
128 { $subsection POSTPONE: } }
129 "Arrays are documented in " { $link "arrays" } "." ;
130
131 ARTICLE: "syntax-vectors" "Vector syntax"
132 { $subsection POSTPONE: V{ }
133 "Vectors are documented in " { $link "vectors" } "." ;
134
135 ARTICLE: "syntax-hashtables" "Hashtable syntax"
136 { $subsection POSTPONE: H{ }
137 "Hashtables are documented in " { $link "hashtables" } "." ;
138
139 ARTICLE: "syntax-tuples" "Tuple syntax"
140 { $subsection POSTPONE: T{ }
141 "Tuples are documented in " { $link "tuples" } "."  ;
142
143 ARTICLE: "syntax-quots" "Quotation syntax"
144 { $subsection POSTPONE: [ }
145 { $subsection POSTPONE: ] }
146 "Quotations are documented in " { $link "quotations" } "." ;
147
148 ARTICLE: "syntax-byte-arrays" "Byte array syntax"
149 { $subsection POSTPONE: B{ }
150 "Byte arrays are documented in " { $link "byte-arrays" } "." ;
151
152 ARTICLE: "syntax-pathnames" "Pathname syntax"
153 { $subsection POSTPONE: P" }
154 "Pathnames are documented in " { $link "io.pathnames" } "." ;
155
156 ARTICLE: "syntax-effects" "Stack effect syntax"
157 "Note that this is " { $emphasis "not" } " syntax to declare stack effects of words. This pushes an " { $link effect } " instance on the stack for reflection, for use with words such as " { $link define-declared } ", " { $link call-effect } " and " { $link execute-effect } "."
158 { $subsection POSTPONE: (( }
159 { $see-also "effects" "inference" "tools.inference" } ;
160
161 ARTICLE: "syntax-literals" "Literals"
162 "Many different types of objects can be constructed at parse time via literal syntax. Numbers are a special case since support for reading them is built-in to the parser. All other literals are constructed via parsing words."
163 $nl
164 "If a quotation contains a literal object, the same literal object instance is used each time the quotation executes; that is, literals are “live”."
165 $nl
166 "Using mutable object literals in word definitions requires care, since if those objects are mutated, the actual word definition will be changed, which is in most cases not what you would expect. Literals should be " { $link clone } "d before being passed to word which may potentially mutate them."
167 { $subsection "syntax-numbers" }
168 { $subsection "syntax-words" }
169 { $subsection "syntax-quots" }
170 { $subsection "syntax-arrays" }
171 { $subsection "syntax-strings" }
172 { $subsection "syntax-byte-arrays" }
173 { $subsection "syntax-vectors" }
174 { $subsection "syntax-sbufs" }
175 { $subsection "syntax-hashtables" }
176 { $subsection "syntax-tuples" }
177 { $subsection "syntax-pathnames" }
178 { $subsection "syntax-effects" } ;
179
180 ARTICLE: "syntax" "Syntax"
181 "Factor has two main forms of syntax: " { $emphasis "definition" } " syntax and " { $emphasis "literal" } " syntax. Code is data, so the syntax for code is a special case of object literal syntax. This section documents literal syntax. Definition syntax is covered in " { $link "words" } ". Extending the parser is the main topic of " { $link "parser" } "."
182 { $subsection "parser-algorithm" }
183 { $subsection "word-search" }
184 { $subsection "top-level-forms" }
185 { $subsection "syntax-comments" }
186 { $subsection "syntax-literals" }
187 { $subsection "syntax-immediate" } ;
188
189 ABOUT: "syntax"
190
191 HELP: delimiter
192 { $syntax ": foo ... ; delimiter" }
193 { $description "Declares the most recently defined word as a delimiter. Delimiters are words which are only ever valid as the end of a nested block to be read by " { $link parse-until } ". An unpaired occurrence of a delimiter is a parse error." } ;
194
195 HELP: SYNTAX:
196 { $syntax "SYNTAX: foo ... ;" }
197 { $description "Defines a parsing word." }
198 { $examples "In the below example, the " { $snippet "world" } " word is never called, however its body references a parsing word which executes immediately:" { $example "USE: io" "IN: scratchpad" "<< SYNTAX: HELLO \"Hello parser!\" print ; >>\n: world ( -- ) HELLO ;" "Hello parser!" } } ;
199
200 HELP: inline
201 { $syntax ": foo ... ; inline" }
202 { $description
203     "Declares the most recently defined word as an inline word. The optimizing compiler copies definitions of inline words when compiling calls to them."
204     $nl
205     "Combinators must be inlined in order to compile with the optimizing compiler - see " { $link "inference-combinators" } ". For any other word, inlining is merely an optimization."
206     $nl
207     "The non-optimizing quotation compiler ignores inlining declarations."
208 } ;
209
210 HELP: recursive
211 { $syntax ": foo ... ; recursive" }
212 { $description "Declares the most recently defined word as a recursive word." }
213 { $notes "This declaration is only required for " { $link POSTPONE: inline } " words which call themselves. See " { $link "inference-recursive-combinators" } "." } ;
214
215 HELP: foldable
216 { $syntax ": foo ... ; foldable" }
217 { $description
218     "Declares that the most recently defined word may be evaluated at compile-time if all inputs are literal. Foldable words must satisfy a very strong contract:"
219     { $list
220         "foldable words must not have any observable side effects,"
221         "foldable words must halt - for example, a word computing a series until it coverges should not be foldable, since compilation will not halt in the event the series does not converge."
222         "both inputs and outputs of foldable words must be immutable."
223     }
224     "The last restriction ensures that words such as " { $link clone } " do not satisfy the foldable word contract. Indeed, " { $link clone } " will output a mutable object if its input is mutable, and so it is undesirable to evaluate it at compile-time, since doing so would give incorrect semantics for code that clones mutable objects and proceeds to mutate them."
225 }
226 { $notes
227     "Folding optimizations are not applied if the call site of a word is in the same source file as the word. This is a side-effect of the compilation unit system; see " { $link "compilation-units" } "."
228 }
229 { $examples "Most operations on numbers are foldable. For example, " { $snippet "2 2 +" } " compiles to a literal 4, since " { $link + } " is declared foldable." } ;
230
231 HELP: flushable
232 { $syntax ": foo ... ; flushable" }
233 { $description
234     "Declares that the most recently defined word has no side effects, and thus calls to this word may be pruned by the compiler if the outputs are not used."
235     $nl
236     "Note that many words are flushable but not foldable, for example " { $link clone } " and " { $link <array> } "."
237 } ;
238
239 HELP: t
240 { $syntax "t" }
241 { $values { "t" "the canonical truth value" } }
242 { $class-description "The canonical truth value, which is an instance of itself." } ;
243
244 HELP: f
245 { $syntax "f" }
246 { $values { "f" "the singleton false value" } }
247 { $description "The " { $link f } " parsing word adds the " { $link f } " object to the parse tree, and is also the class whose sole instance is the " { $link f } " object. The " { $link f } " object is the singleton false value, the only object that is not true. The " { $link f } " object is not equal to the " { $link f } " class word, which can be pushed on the stack using word wrapper syntax:"
248 { $code "f    ! the singleton f object denoting falsity\n\\ f  ! the f class word" } } ;
249
250 HELP: [
251 { $syntax "[ elements... ]" }
252 { $description "Marks the beginning of a literal quotation." }
253 { $examples { $code "[ 1 2 3 ]" } } ;
254
255 { POSTPONE: [ POSTPONE: ] } related-words
256
257 HELP: ]
258 { $syntax "]" }
259 { $description "Marks the end of a literal quotation."
260 $nl
261 "Parsing words can use this word as a generic end delimiter." } ;
262
263 HELP: }
264 { $syntax "}" }
265 { $description "Marks the end of an array, vector, hashtable, complex number, tuple, or wrapper."
266 $nl
267 "Parsing words can use this word as a generic end delimiter." } ;
268
269 { POSTPONE: { POSTPONE: V{ POSTPONE: H{ POSTPONE: C{ POSTPONE: T{ POSTPONE: W{ POSTPONE: } } related-words
270
271 HELP: {
272 { $syntax "{ elements... }" }
273 { $values { "elements" "a list of objects" } }
274 { $description "Marks the beginning of a literal array. Literal arrays are terminated by " { $link POSTPONE: } } "." } 
275 { $examples { $code "{ 1 2 3 }" } } ;
276
277 HELP: V{
278 { $syntax "V{ elements... }" }
279 { $values { "elements" "a list of objects" } }
280 { $description "Marks the beginning of a literal vector. Literal vectors are terminated by " { $link POSTPONE: } } "." } 
281 { $examples { $code "V{ 1 2 3 }" } } ;
282
283 HELP: B{
284 { $syntax "B{ elements... }" }
285 { $values { "elements" "a list of integers" } }
286 { $description "Marks the beginning of a literal byte array. Literal byte arrays are terminated by " { $link POSTPONE: } } "." } 
287 { $examples { $code "B{ 1 2 3 }" } } ;
288
289 HELP: H{
290 { $syntax "H{ { key value }... }" }
291 { $values { "key" "an object" } { "value" "an object" } }
292 { $description "Marks the beginning of a literal hashtable, given as a list of two-element arrays holding key/value pairs. Literal hashtables are terminated by " { $link POSTPONE: } } "." } 
293 { $examples { $code "H{ { \"tuna\" \"fish\" } { \"jalapeno\" \"vegetable\" } }" } } ;
294
295 HELP: C{
296 { $syntax "C{ real-part imaginary-part }" }
297 { $values { "real-part" "a real number" } { "imaginary-part" "a real number" } }
298 { $description "Parses a complex number given in rectangular form as a pair of real numbers. Literal complex numbers are terminated by " { $link POSTPONE: } } "." }  ;
299
300 HELP: T{
301 { $syntax "T{ class slots... }" }
302 { $values { "class" "a tuple class word" } { "slots" "slot values" } }
303 { $description "Marks the beginning of a literal tuple."
304 $nl
305 "Three literal syntax forms are recognized:"
306 { $list
307     { "empty tuple form: if no slot values are specified, then the literal tuple will have all slots set to their initial values (see " { $link "slot-initial-values" } ")." }
308     { "BOA-form: if the first element of " { $snippet "slots" } " is " { $snippet "f" } ", then the remaining elements are slot values corresponding to slots in the order in which they are defined in the " { $link POSTPONE: TUPLE: } " form." }
309     { "assoc-form: otherwise, " { $snippet "slots" } " is interpreted as a sequence of " { $snippet "{ slot-name value }" } " pairs. The " { $snippet "slot-name" } " should not be quoted." }
310 }
311 "BOA form is more concise, whereas assoc form is more readable for larger tuples with many slots, or if only a few slots are to be specified."
312 $nl
313 "With BOA form, specifying an insufficient number of values is given after the class word, the remaining slots of the tuple are set to their initial values (see " { $link "slot-initial-values" } "). If too many values are given, an error will be raised." }
314 { $examples
315 "An empty tuple; since vectors have their own literal syntax, the above is equivalent to " { $snippet "V{ }" } ""
316 { $code "T{ vector }" }
317 "A BOA-form tuple:"
318 { $code
319     "USE: colors"
320     "T{ rgba f 1.0 0.0 0.5 }"
321 }
322 "An assoc-form tuple equal to the above:"
323 { $code
324     "USE: colors"
325     "T{ rgba { red 1.0 } { green 0.0 } { blue 0.5 } }"
326 } } ;
327
328 HELP: W{
329 { $syntax "W{ object }" }
330 { $values { "object" "an object" } }
331 { $description "Marks the beginning of a literal wrapper. Literal wrappers are terminated by " { $link POSTPONE: } } "." }  ;
332
333 HELP: POSTPONE:
334 { $syntax "POSTPONE: word" }
335 { $values { "word" "a word" } }
336 { $description "Reads the next word from the input string and appends the word to the parse tree, even if it is a parsing word." }
337 { $examples "For an ordinary word " { $snippet "foo" } ", " { $snippet "foo" } " and " { $snippet "POSTPONE: foo" } " are equivalent; however, if " { $snippet "foo" } " is a parsing word, the former will execute it at parse time, while the latter will execute it at runtime." }
338 { $notes "This word is used inside parsing words to delegate further action to another parsing word, and to refer to parsing words literally from literal arrays and such." } ;
339
340 HELP: :
341 { $syntax ": word ( stack -- effect ) definition... ;" }
342 { $values { "word" "a new word to define" } { "definition" "a word definition" } }
343 { $description "Defines a word with the given stack effect in the current vocabulary. The stack effect is optional for words which only push literals on the stack." }
344 { $examples { $code ": ask-name ( -- name )\n    \"What is your name? \" write readln ;\n: greet ( name -- )\n    \"Greetings, \" write print ;\n: friend ( -- )\n    ask-name greet ;" } } ;
345
346 { POSTPONE: : POSTPONE: ; define } related-words
347
348 HELP: ;
349 { $syntax ";" }
350 { $description
351     "Marks the end of a definition."
352     $nl
353     "Parsing words can use this word as a generic end delimiter."
354 } ;
355
356 HELP: SYMBOL:
357 { $syntax "SYMBOL: word" }
358 { $values { "word" "a new word to define" } }
359 { $description "Defines a new symbol word in the current vocabulary. Symbols push themselves on the stack when executed, and are used to identify variables (see " { $link "namespaces" } ") as well as for storing crufties in word properties (see " { $link "word-props" } ")." }
360 { $examples { $example "USE: prettyprint" "IN: scratchpad" "SYMBOL: foo\nfoo ." "foo" } } ;
361
362 { define-symbol POSTPONE: SYMBOL: POSTPONE: SYMBOLS: } related-words
363
364 HELP: SYMBOLS:
365 { $syntax "SYMBOLS: words... ;" }
366 { $values { "words" "a sequence of new words to define" } }
367 { $description "Creates a new symbol for every token until the " { $snippet ";" } "." }
368 { $examples { $example "USING: prettyprint ;" "IN: scratchpad" "SYMBOLS: foo bar baz ;\nfoo . bar . baz ." "foo\nbar\nbaz" } } ;
369
370 HELP: SINGLETON:
371 { $syntax "SINGLETON: class" }
372 { $values
373     { "class" "a new singleton to define" }
374 }
375 { $description
376     "Defines a new singleton class. The class word itself is the sole instance of the singleton class."
377 }
378 { $examples
379     { $example "USING: classes.singleton kernel io ;" "IN: singleton-demo" "USE: prettyprint SINGLETON: foo\nGENERIC: bar ( obj -- )\nM: foo bar drop \"a foo!\" print ;\nfoo bar" "a foo!" }
380 } ;
381     
382 HELP: SINGLETONS:
383 { $syntax "SINGLETONS: words... ;" }
384 { $values { "words" "a sequence of new words to define" } }
385 { $description "Creates a new singleton for every token until the " { $snippet ";" } "." } ;
386
387 HELP: ALIAS:
388 { $syntax "ALIAS: new-word existing-word" }
389 { $values { "new-word" word } { "existing-word" word } }
390 { $description "Creates a new inlined word that calls the existing word." }
391 { $examples
392     { $example "USING: prettyprint sequences ;"
393                "IN: alias.test"
394                "ALIAS: sequence-nth nth"
395                "0 { 10 20 30 } sequence-nth ."
396                "10"
397     }
398 } ;
399
400 { define-alias POSTPONE: ALIAS: } related-words
401
402 HELP: CONSTANT:
403 { $syntax "CONSTANT: word value" }
404 { $values { "word" word } { "value" object } }
405 { $description "Creates a word which pushes a value on the stack." }
406 { $examples { $code "CONSTANT: magic 1" "CONSTANT: science HEX: ff0f" } } ;
407
408 { define-constant POSTPONE: CONSTANT: } related-words
409
410 HELP: \
411 { $syntax "\\ word" }
412 { $values { "word" "a word" } }
413 { $description "Reads the next word from the input and appends a wrapper holding the word to the parse tree. When the evaluator encounters a wrapper, it pushes the wrapped word literally on the data stack." }
414 { $examples "The following two lines are equivalent:" { $code "0 \\ <vector> execute\n0 <vector>" } "If " { $snippet "foo" } " is a symbol, the following two lines are equivalent:" { $code "foo" "\\ foo" } } ;
415
416 HELP: DEFER:
417 { $syntax "DEFER: word" }
418 { $values { "word" "a new word to define" } }
419 { $description "Create a word in the current vocabulary that simply raises an error when executed. Usually, the word will be replaced with a real definition later." }
420 { $notes "Due to the way the parser works, words cannot be referenced before they are defined; that is, source files must order definitions in a strictly bottom-up fashion. Mutually-recursive pairs of words can be implemented by " { $emphasis "deferring" } " one of the words in the pair allowing the second word in the pair to parse, then by defining the first word." }
421 { $examples { $code "DEFER: foe\n: fie ... foe ... ;\n: foe ... fie ... ;" } } ;
422
423 HELP: FORGET:
424 { $syntax "FORGET: word" }
425 { $values { "word" "a word" } }
426 { $description "Removes the word from its vocabulary, or does nothing if no such word exists. Existing definitions that reference forgotten words will continue to work, but new occurrences of the word will not parse." } ;
427
428 HELP: USE:
429 { $syntax "USE: vocabulary" }
430 { $values { "vocabulary" "a vocabulary name" } }
431 { $description "Adds a new vocabulary to the search path, loading it first if necessary." }
432 { $notes "If adding the vocabulary introduces ambiguity, referencing the ambiguous names will throw a " { $link ambiguous-use-error } "." }
433 { $errors "Throws an error if the vocabulary does not exist or could not be loaded." } ;
434
435 HELP: UNUSE:
436 { $syntax "UNUSE: vocabulary" }
437 { $values { "vocabulary" "a vocabulary name" } }
438 { $description "Removes a vocabulary from the search path." }
439 { $errors "Throws an error if the vocabulary does not exist." } ;
440
441 HELP: USING:
442 { $syntax "USING: vocabularies... ;" }
443 { $values { "vocabularies" "a list of vocabulary names" } }
444 { $description "Adds a list of vocabularies to the search path." }
445 { $notes "If adding the vocabularies introduces ambiguity, referencing the ambiguous names will throw a " { $link ambiguous-use-error } "." }
446 { $errors "Throws an error if one of the vocabularies does not exist." } ;
447
448 HELP: QUALIFIED:
449 { $syntax "QUALIFIED: vocab" }
450 { $description "Adds the vocabulary's words, prefixed with the vocabulary name, to the search path." }
451 { $notes "If adding the vocabulary introduces ambiguity, the vocabulary will take precedence when resolving any ambiguous names. This is a rare case; for example, suppose a vocabulary " { $snippet "fish" } " defines a word named " { $snippet "go:fishing" } ", and a vocabulary named " { $snippet "go" } " defines a word named " { $snippet "finishing" } ". Then, the following will call the latter word:"
452   { $code
453   "USE: fish"
454   "QUALIFIED: go"
455   "go:fishing"
456   }
457 }
458 { $examples { $example
459     "USING: prettyprint ;"
460     "QUALIFIED: math"
461     "1 2 math:+ ." "3"
462 } } ;
463
464 HELP: QUALIFIED-WITH:
465 { $syntax "QUALIFIED-WITH: vocab word-prefix" }
466 { $description "Like " { $link POSTPONE: QUALIFIED: } " but uses " { $snippet "word-prefix" } " as prefix." }
467 { $examples { $code
468     "USING: prettyprint ;"
469     "QUALIFIED-WITH: math m"
470     "1 2 m:+ ."
471     "3"
472 } } ;
473
474 HELP: FROM:
475 { $syntax "FROM: vocab => words ... ;" }
476 { $description "Adds " { $snippet "words" } " from " { $snippet "vocab" } " to the search path." }
477 { $notes "If adding the words introduces ambiguity, the words will take precedence when resolving any ambiguous names." }
478 { $examples
479   "Both the " { $vocab-link "vocabs.parser" } " and " { $vocab-link "binary-search" } " vocabularies define a word named " { $snippet "search" } ". The following will throw an " { $link ambiguous-use-error } ":"
480   { $code "USING: vocabs.parser binary-search ;" "... search ..." }
481   "Because " { $link POSTPONE: FROM: } " takes precedence over a " { $link POSTPONE: USING: } ", the ambiguity can be resolved explicitly. Suppose you wanted the " { $vocab-link "binary-search" } " vocabulary's " { $snippet "search" } " word:"
482   { $code "USING: vocabs.parser binary-search ;" "FROM: binary-search => search ;" "... search ..." }
483  } ;
484
485 HELP: EXCLUDE:
486 { $syntax "EXCLUDE: vocab => words ... ;" }
487 { $description "Adds all words except for " { $snippet "words" } " from " { $snippet "vocab" } "  to the search path." }
488 { $examples { $code
489     "EXCLUDE: math.parser => bin> hex> ;" "! imports everything but bin> and hex>" } } ;
490
491 HELP: RENAME:
492 { $syntax "RENAME: word vocab => new-name" }
493 { $description "Imports " { $snippet "word" } " from " { $snippet "vocab" } ", but renamed to " { $snippet "new-name" } "." }
494 { $notes "If adding the words introduces ambiguity, the words will take precedence when resolving any ambiguous names." }
495 { $examples { $example
496     "USING: prettyprint ;"
497     "RENAME: + math => -"
498     "2 3 - ."
499     "5"
500 } } ;
501
502 HELP: IN:
503 { $syntax "IN: vocabulary" }
504 { $values { "vocabulary" "a new vocabulary name" } }
505 { $description "Sets the current vocabulary where new words will be defined, creating the vocabulary first if it does not exist. After the vocabulary has been created, it can be listed in " { $link POSTPONE: USE: } " and " { $link POSTPONE: USING: } " declarations." } ;
506
507 HELP: CHAR:
508 { $syntax "CHAR: token" }
509 { $values { "token" "a literal character, escape code, or Unicode character name" } }
510 { $description "Adds a Unicode code point to the parse tree." }
511 { $examples
512     { $code
513         "CHAR: x"
514         "CHAR: \\u000032"
515         "CHAR: \\u{exclamation-mark}"
516         "CHAR: exclamation-mark"
517         "CHAR: ugaritic-letter-samka"
518     }
519 } ;
520
521 HELP: "
522 { $syntax "\"string...\"" }
523 { $values { "string" "literal and escaped characters" } }
524 { $description "Reads from the input string until the next occurrence of " { $link POSTPONE: " } ", and appends the resulting string to the parse tree. String literals cannot span multiple lines. Strings containing the " { $link POSTPONE: " } " character and various other special characters can be read by inserting " { $link "escape" } "." }
525 { $examples
526   "A string with a newline in it:"
527   { $example "USE: io" "\"Hello\\nworld\" print" "Hello\nworld" }
528   "A string with a named Unicode code point:"
529   { $example "USE: io" "\"\\u{greek-capital-letter-sigma}\" print" "\u{greek-capital-letter-sigma}" }
530 } ;
531
532 HELP: SBUF"
533 { $syntax "SBUF\" string... \"" }
534 { $values { "string" "literal and escaped characters" } }
535 { $description "Reads from the input string until the next occurrence of " { $link POSTPONE: " } ", converts the string to a string buffer, and appends it to the parse tree." }
536 { $examples { $example "USING: io strings ;" "SBUF\" Hello world\" >string print" "Hello world" } } ;
537
538 HELP: P"
539 { $syntax "P\" pathname\"" }
540 { $values { "pathname" "a pathname string" } }
541 { $description "Reads from the input string until the next occurrence of " { $link POSTPONE: " } ", creates a new " { $link pathname } ", and appends it to the parse tree." }
542 { $examples { $example "USING: accessors io io.files ;" "P\" foo.txt\" string>> print" "foo.txt" } } ;
543
544 HELP: (
545 { $syntax "( inputs -- outputs )" }
546 { $values { "inputs" "a list of tokens" } { "outputs" "a list of tokens" } }
547 { $description "A stack effect declaration. This is treated as a comment unless it appears inside a word definition." }
548 { $see-also "effects" } ;
549
550 HELP: ((
551 { $syntax "(( inputs -- outputs ))" }
552 { $values { "inputs" "a list of tokens" } { "outputs" "a list of tokens" } }
553 { $description "Literal stack effect syntax." }
554 { $notes "Useful for meta-programming with " { $link define-declared } "." }
555 { $examples
556     { $example
557         "USING: compiler.units kernel math prettyprint random words ;"
558         "IN: scratchpad"
559         ""
560         "SYMBOL: my-dynamic-word"
561         ""
562         "["
563         "    my-dynamic-word 2 { [ + ] [ * ] } random curry"
564         "    (( x -- y )) define-declared"
565         "] with-compilation-unit"
566         ""
567         "2 my-dynamic-word ."
568         "4"
569     }
570 } ;
571
572 HELP: !
573 { $syntax "! comment..." }
574 { $values { "comment" "characters" } }
575 { $description "Discards all input until the end of the line." } ;
576
577 { POSTPONE: ! POSTPONE: #! } related-words
578
579 HELP: #!
580 { $syntax "#! comment..." }
581 { $values { "comment" "characters" } }
582 { $description "Discards all input until the end of the line." } ;
583
584 HELP: HEX:
585 { $syntax "HEX: integer" }
586 { $values { "integer" "hexadecimal digits (0-9, a-f, A-F)" } }
587 { $description "Adds an integer read from a hexadecimal literal to the parse tree." }
588 { $examples { $example "USE: prettyprint" "HEX: ff ." "255" } } ;
589
590 HELP: OCT:
591 { $syntax "OCT: integer" }
592 { $values { "integer" "octal digits (0-7)" } }
593 { $description "Adds an integer read from an octal literal to the parse tree." }
594 { $examples { $example "USE: prettyprint" "OCT: 31337 ." "13023" } } ;
595
596 HELP: BIN:
597 { $syntax "BIN: integer" }
598 { $values { "integer" "binary digits (0 and 1)" } }
599 { $description "Adds an integer read from an binary literal to the parse tree." }
600 { $examples { $example "USE: prettyprint" "BIN: 100 ." "4" } } ;
601
602 HELP: GENERIC:
603 { $syntax "GENERIC: word ( stack -- effect )" }
604 { $values { "word" "a new word to define" } }
605 { $description "Defines a new generic word in the current vocabulary. Initially, it contains no methods, and thus will throw a " { $link no-method } " error when called." } ;
606
607 HELP: GENERIC#
608 { $syntax "GENERIC# word n ( stack -- effect )" }
609 { $values { "word" "a new word to define" } { "n" "the stack position to dispatch on" } }
610 { $description "Defines a new generic word which dispatches on the " { $snippet "n" } "th most element from the top of the stack in the current vocabulary. Initially, it contains no methods, and thus will throw a " { $link no-method } " error when called." }
611 { $notes
612     "The following two definitions are equivalent:"
613     { $code "GENERIC: foo ( obj -- )" }
614     { $code "GENERIC# foo 0 ( obj -- )" }
615 } ;
616
617 HELP: MATH:
618 { $syntax "MATH: word" }
619 { $values { "word" "a new word to define" } }
620 { $description "Defines a new generic word which uses the " { $link math-combination } " method combination." } ;
621
622 HELP: HOOK:
623 { $syntax "HOOK: word variable ( stack -- effect ) " }
624 { $values { "word" "a new word to define" } { "variable" word } }
625 { $description "Defines a new hook word in the current vocabulary. Hook words are generic words which dispatch on the value of a variable, so methods are defined with " { $link POSTPONE: M: } ". Hook words differ from other generic words in that the dispatch value is removed from the stack before the chosen method is called." }
626 { $examples
627     { $example
628         "USING: io namespaces ;"
629         "IN: scratchpad"
630         "SYMBOL: transport"
631         "TUPLE: land-transport ;"
632         "TUPLE: air-transport ;"
633         "HOOK: deliver transport ( destination -- )"
634         "M: land-transport deliver \"Land delivery to \" write print ;"
635         "M: air-transport deliver \"Air delivery to \"  write print ;"
636         "T{ air-transport } transport set"
637         "\"New York City\" deliver"
638         "Air delivery to New York City"
639     }
640 }
641 { $notes
642     "Hook words are really just generic words with a custom method combination (see " { $link "method-combination" } ")."
643 } ;
644
645 HELP: M:
646 { $syntax "M: class generic definition... ;" }
647 { $values { "class" "a class word" } { "generic" "a generic word" } { "definition" "a method definition" } }
648 { $description "Defines a method, that is, a behavior for the generic word specialized on instances of the class." } ;
649
650 HELP: UNION:
651 { $syntax "UNION: class members... ;" }
652 { $values { "class" "a new class word to define" } { "members" "a list of class words separated by whitespace" } }
653 { $description "Defines a union class. An object is an instance of a union class if it is an instance of one of its members." } ;
654
655 HELP: INTERSECTION:
656 { $syntax "INTERSECTION: class participants... ;" }
657 { $values { "class" "a new class word to define" } { "participants" "a list of class words separated by whitespace" } }
658 { $description "Defines an intersection class. An object is an instance of an intersection class if it is an instance of all of its participants." } ;
659
660 HELP: MIXIN:
661 { $syntax "MIXIN: class" }
662 { $values { "class" "a new class word to define" } }
663 { $description "Defines a mixin class. A mixin is similar to a union class, except it has no members initially, and new members can be added with the " { $link POSTPONE: INSTANCE: } " word." }
664 { $examples "The " { $link sequence } " and " { $link assoc } " mixin classes." } ;
665
666 HELP: INSTANCE:
667 { $syntax "INSTANCE: instance mixin" }
668 { $values { "instance" "a class word" } { "mixin" "a mixin class word" } }
669 { $description "Makes " { $snippet "instance" } " an instance of " { $snippet "mixin" } "." } ;
670
671 HELP: PREDICATE:
672 { $syntax "PREDICATE: class < superclass predicate... ;" }
673 { $values { "class" "a new class word to define" } { "superclass" "an existing class word" } { "predicate" "membership test with stack effect " { $snippet "( superclass -- ? )" } } }
674 { $description
675     "Defines a predicate class deriving from " { $snippet "superclass" } "."
676     $nl
677     "An object is an instance of a predicate class if two conditions hold:"
678     { $list
679         "it is an instance of the predicate's superclass,"
680         "it satisfies the predicate"
681     }
682     "Each predicate must be defined as a subclass of some other class. This ensures that predicates inheriting from disjoint classes do not need to be exhaustively tested during method dispatch."
683 }
684 { $examples
685     { $code "USING: math ;" "PREDICATE: positive < integer 0 > ;" }
686 } ;
687
688 HELP: TUPLE:
689 { $syntax "TUPLE: class slots... ;" "TUPLE: class < superclass slots ... ;" }
690 { $values { "class" "a new tuple class to define" } { "slots" "a list of slot specifiers" } }
691 { $description "Defines a new tuple class."
692 $nl
693 "The superclass is optional; if left unspecified, it defaults to " { $link tuple } "."
694 $nl
695 "Slot specifiers take one of the following three forms:"
696 { $list
697     { { $snippet "name" } " - a slot which can hold any object, with no attributes" }
698     { { $snippet "{ name attributes... }" } " - a slot which can hold any object, with optional attributes" }
699     { { $snippet "{ name class attributes... }" } " - a slot specialized to a specific class, with optional attributes" }
700 }
701 "Slot attributes are lists of slot attribute specifiers followed by values; a slot attribute specifier is one of " { $link initial: } " or " { $link read-only } ". See " { $link "tuple-declarations" } " for details." }
702 { $examples
703     "A simple tuple class:"
704     { $code "TUPLE: color red green blue ;" }
705     "Declaring slots to be integer-valued:"
706     { $code "TUPLE: color" "{ red integer }" "{ green integer }" "{ blue integer } ;" }
707     "An example mixing short and long slot specifiers:"
708     { $code "TUPLE: person" "{ age integer initial: 0 }" "{ department string initial: \"Marketing\" }" "manager ;" }
709 } ;
710
711 HELP: initial:
712 { $syntax "TUPLE: ... { slot initial: value } ... ;" }
713 { $values { "slot" "a slot name" } { "value" "any literal" } }
714 { $description "Specifies an initial value for a tuple slot." } ;
715
716 HELP: read-only
717 { $syntax "TUPLE: ... { slot read-only } ... ;" }
718 { $values { "slot" "a slot name" } }
719 { $description "Defines a tuple slot to be read-only. If a tuple has read-only slots, instances of the tuple should only be created by calling " { $link boa } ", instead of " { $link new } ". Using " { $link boa } " is the only way to set the value of a read-only slot." } ;
720
721 { initial: read-only } related-words
722
723 HELP: SLOT:
724 { $syntax "SLOT: name" }
725 { $values { "name" "a slot name" } }
726 { $description "Defines a protocol slot; that is, defines the accessor words for a slot named " { $snippet "slot" } " without associating it with any specific tuple." } ;
727
728 HELP: ERROR:
729 { $syntax "ERROR: class slots... ;" }
730 { $values { "class" "a new tuple class to define" } { "slots" "a list of slot names" } }
731 { $description "Defines a new tuple class whose class word throws a new instance of the error." }
732 { $notes
733     "The following two snippets are equivalent:"
734     { $code
735         "ERROR: invalid-values x y ;"
736         ""
737         "TUPLE: invalid-values x y ;"
738         ": invalid-values ( x y -- * )"
739         "    \\ invalid-values boa throw ;"
740     }
741 } ;
742
743 HELP: C:
744 { $syntax "C: constructor class" }
745 { $values { "constructor" "a new word to define" } { "class" tuple-class } }
746 { $description "Define a constructor word for a tuple class which simply performs BOA (by order of arguments) construction using " { $link boa } "." }
747 { $examples
748     "Suppose the following tuple has been defined:"
749     { $code "TUPLE: color red green blue ;" }
750     "The following two lines are equivalent:"
751     { $code
752         "C: <color> color"
753         ": <color> color boa ;"
754     }
755     "In both cases, a word " { $snippet "<color>" } " is defined, which reads three values from the stack and creates a " { $snippet "color" } " instance having these values in the " { $snippet "red" } ", " { $snippet "green" } " and " { $snippet "blue" } " slots, respectively."
756 } ;
757
758 HELP: MAIN:
759 { $syntax "MAIN: word" }
760 { $values { "word" word } }
761 { $description "Defines the main entry point for the current vocabulary. This word will be executed when this vocabulary is passed to " { $link run } "." } ;
762
763 HELP: <PRIVATE
764 { $syntax "<PRIVATE ... PRIVATE>" }
765 { $description "Begins a block of private word definitions. Private word definitions are placed in the current vocabulary name, suffixed with " { $snippet ".private" } "." }
766 { $notes
767     "The following is an example of usage:"
768     { $code
769         "IN: factorial"
770         ""
771         "<PRIVATE"
772         ""
773         ": (fac) ( accum n -- n! )"
774         "    dup 1 <= [ drop ] [ [ * ] keep 1 - (fac) ] if ;"
775         ""
776         "PRIVATE>"
777         ""
778         ": fac ( n -- n! ) 1 swap (fac) ;"
779     }
780     "The above is equivalent to:"
781     { $code
782         "IN: factorial.private"
783         ""
784         ": (fac) ( accum n -- n! )"
785         "    dup 1 <= [ drop ] [ [ * ] keep 1 - (fac) ] if ;"
786         ""
787         "IN: factorial"
788         ""
789         ": fac ( n -- n! ) 1 swap (fac) ;"
790     }
791 } ;
792
793 HELP: PRIVATE>
794 { $syntax "<PRIVATE ... PRIVATE>" }
795 { $description "Ends a block of private word definitions." } ;
796
797 { POSTPONE: <PRIVATE POSTPONE: PRIVATE> } related-words
798
799 HELP: <<
800 { $syntax "<< ... >>" }
801 { $description "Evaluates some code at parse time." }
802 { $notes "Calling words defined in the same source file at parse time is prohibited; see compilation unit as where it was defined; see " { $link "compilation-units" } "." } ;
803
804 HELP: >>
805 { $syntax ">>" }
806 { $description "Marks the end of a parse time code block." } ;
807
808 HELP: call-next-method
809 { $syntax "call-next-method" }
810 { $description "Calls the next applicable method. Only valid inside a method definition. The values at the top of the stack are passed on to the next method, and they must be compatible with that method's class specializer." }
811 { $notes "This word looks like an ordinary word but it is a parsing word. It cannot be factored out of a method definition, since the code expansion references the current method object directly." }
812 { $errors
813     "Throws a " { $link no-next-method } " error if this is the least specific method, and throws an " { $link inconsistent-next-method } " error if the values at the top of the stack are not compatible with the current method's specializer."
814 } ;
815
816 { POSTPONE: call-next-method (call-next-method) next-method } related-words
817
818 { POSTPONE: << POSTPONE: >> } related-words
819
820 HELP: call(
821 { $syntax "call( stack -- effect )" }
822 { $description "Calls the quotation on the top of the stack, asserting that it has the given stack effect. The quotation does not need to be known at compile time." }
823 { $examples
824   { $code
825     "TUPLE: action name quot ;"
826     ": perform-action ( action -- )"
827     "    [ name>> print ] [ quot>> call( -- ) ] bi ;"
828   }
829 } ;
830
831 HELP: execute(
832 { $syntax "execute( stack -- effect )" }
833 { $description "Calls the word on the top of the stack, asserting that it has the given stack effect. The word does not need to be known at compile time." } ;
834
835 { POSTPONE: call( POSTPONE: execute( } related-words