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