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