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