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