]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax-docs.factor
Fixing everything for mandatory stack effects
[factor.git] / core / syntax / syntax-docs.factor
1 USING: generic help.syntax help.markup kernel math parser words
2 effects classes generic.standard classes.tuple generic.math
3 generic.standard arrays io.files vocabs.loader io sequences
4 assocs ;
5 IN: syntax
6
7 ARTICLE: "parser-algorithm" "Parser algorithm"
8 "At the most abstract level, Factor syntax consists of whitespace-separated tokens. The parser tokenizes the input on whitespace boundaries.  The parser is case-sensitive and whitespace between tokens is significant, so the following three expressions tokenize differently:"
9 { $code "2X+\n2 X +\n2 x +" }
10 "As the parser reads tokens it makes a distinction between numbers, ordinary words, and parsing words. Tokens are appended to the parse tree, the top level of which is a quotation returned by the original parser invocation. Nested levels of the parse tree are created by parsing words."
11 $nl
12 "The parser iterates through the input text, checking each character in turn. Here is the parser algorithm in more detail -- some of the concepts therein will be defined shortly:"
13 { $list
14     { "If the current character is a double-quote (\"), the " { $link POSTPONE: " } " parsing word is executed, causing a string to be read." }
15     {
16         "Otherwise, the next token is taken from the input. The parser searches for a word named by the token in the currently used set of vocabularies. If the word is found, one of the following two actions is taken:"
17         { $list
18             "If the word is an ordinary word, it is appended to the parse tree."
19             "If the word is a parsing word, it is executed."
20         }
21     }
22     "Otherwise if the token does not represent a known word, the parser attempts to parse it as a number. If the token is a number, the number object is added to the parse tree. Otherwise, an error is raised and parsing halts."
23 }
24 "Parsing words play a key role in parsing; while ordinary words and numbers are simply added to the parse tree, parsing words execute in the context of the parser, and can do their own parsing and create nested data structures in the parse tree. Parsing words are also able to define new words."
25 $nl
26 "While parsing words supporting arbitrary syntax can be defined, the default set is found in the " { $vocab-link "syntax" } " vocabulary and provides the basis for all further syntactic interaction with Factor." ;
27
28 ARTICLE: "syntax-comments" "Comments"
29 { $subsection POSTPONE: ! }
30 { $subsection POSTPONE: #! } ;
31
32 ARTICLE: "syntax-immediate" "Parse time evaluation"
33 "Code can be evaluated at parse time. This is a rarely-used feature; one use-case is " { $link "loading-libs" } ", where you want to execute some code before the words in a source file are compiled."
34 { $subsection POSTPONE: << }
35 { $subsection POSTPONE: >> } ;
36
37 ARTICLE: "syntax-integers" "Integer syntax"
38 "The printed representation of an integer consists of a sequence of digits, optionally prefixed by a sign."
39 { $code
40     "123456"
41     "-10"
42     "2432902008176640000"
43 }
44 "Integers are entered in base 10 unless prefixed with a base change parsing word."
45 { $subsection POSTPONE: BIN: }
46 { $subsection POSTPONE: OCT: }
47 { $subsection POSTPONE: HEX: }
48 "More information on integers can be found in " { $link "integers" } "." ;
49
50 ARTICLE: "syntax-ratios" "Ratio syntax"
51 "The printed representation of a ratio is a pair of integers separated by a slash (/), prefixed by an optional whole number part followed by a plus (+). No intermediate whitespace is permitted. Here are some examples:"
52 { $code
53     "75/33"
54     "1/10"
55     "-5/-6"
56     "1+1/3"
57     "-10+1/7"
58 }
59 "More information on ratios can be found in " { $link "rationals" } ;
60
61 ARTICLE: "syntax-floats" "Float syntax"
62 "Floating point literals must contain a decimal point, and may contain an exponent:"
63 { $code
64     "10.5"
65     "-3.1456"
66     "7.e13"
67     "1.0e-5"
68 }
69 "More information on floats can be found in " { $link "floats" } "." ;
70
71 ARTICLE: "syntax-complex-numbers" "Complex number syntax"
72 "A complex number is given by two components, a ``real'' part and ''imaginary'' part. The components must either be integers, ratios or floats."
73 { $code
74     "C{ 1/2 1/3 }   ! the complex number 1/2+1/3i"
75     "C{ 0 1 }       ! the imaginary unit"
76 }
77 { $subsection POSTPONE: C{ }
78 "More information on complex numbers can be found in " { $link "complex-numbers" } "." ;
79
80 ARTICLE: "syntax-numbers" "Number syntax"
81 "If a vocabulary lookup of a token fails, the parser attempts to parse it as a number."
82 { $subsection "syntax-integers" }
83 { $subsection "syntax-ratios" }
84 { $subsection "syntax-floats" }
85 { $subsection "syntax-complex-numbers" } ;
86
87 ARTICLE: "syntax-words" "Word syntax"
88 "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" } ")."
89 { $subsection POSTPONE: \ }
90 { $subsection POSTPONE: POSTPONE: }
91 "The implementation of the " { $link POSTPONE: \ } " word is discussed in detail in " { $link "reading-ahead" } ". Words are documented in " { $link "words" } "." ;
92
93 ARTICLE: "escape" "Character escape codes"
94 { $table
95     { "Escape code" "Meaning" }
96     { { $snippet "\\\\" } { $snippet "\\" } }
97     { { $snippet "\\s" } "a space" }
98     { { $snippet "\\t" } "a tab" }
99     { { $snippet "\\n" } "a newline" }
100     { { $snippet "\\r" } "a carriage return" }
101     { { $snippet "\\0" } "a null byte (ASCII 0)" }
102     { { $snippet "\\e" } "escape (ASCII 27)" }
103     { { $snippet "\\\"" } { $snippet "\"" } }
104     { { $snippet "\\u" { $emphasis "xxxxxx" } } { "The Unicode code point with hexadecimal number " { $snippet { $emphasis "xxxxxx" } } } }
105     { { $snippet "\\u{" { $emphasis "name" } "}" } { "The Unicode code point named " { $snippet { $emphasis "name" } } } }
106 } ;
107
108 ARTICLE: "syntax-strings" "Character and string syntax"
109 "Factor has no distinct character type, however Unicode character value integers can be read by specifying a literal character, or an escaped representation thereof."
110 { $subsection POSTPONE: CHAR: }
111 { $subsection POSTPONE: " }
112 { $subsection "escape" }
113 "Strings are documented in " { $link "strings" } "." ;
114
115 ARTICLE: "syntax-sbufs" "String buffer syntax"
116 { $subsection POSTPONE: SBUF" }
117 "String buffers are documented in " { $link "sbufs" } "." ;
118
119 ARTICLE: "syntax-arrays" "Array syntax"
120 { $subsection POSTPONE: { }
121 { $subsection POSTPONE: } }
122 "Arrays are documented in " { $link "arrays" } "." ;
123
124 ARTICLE: "syntax-vectors" "Vector syntax"
125 { $subsection POSTPONE: V{ }
126 "Vectors are documented in " { $link "vectors" } "." ;
127
128 ARTICLE: "syntax-hashtables" "Hashtable syntax"
129 { $subsection POSTPONE: H{ }
130 "Hashtables are documented in " { $link "hashtables" } "." ;
131
132 ARTICLE: "syntax-tuples" "Tuple syntax"
133 { $subsection POSTPONE: T{ }
134 "Tuples are documented in " { $link "tuples" } "."  ;
135
136 ARTICLE: "syntax-quots" "Quotation syntax"
137 { $subsection POSTPONE: [ }
138 { $subsection POSTPONE: ] }
139 "Quotations are documented in " { $link "quotations" } "." ;
140
141 ARTICLE: "syntax-bit-arrays" "Bit array syntax"
142 { $subsection POSTPONE: ?{ }
143 "Bit arrays are documented in " { $link "bit-arrays" } "." ;
144
145 ARTICLE: "syntax-float-arrays" "Float array syntax"
146 { $subsection POSTPONE: F{ }
147 "Float arrays are documented in " { $link "float-arrays" } "." ;
148
149 ARTICLE: "syntax-byte-arrays" "Byte array syntax"
150 { $subsection POSTPONE: B{ }
151 "Byte arrays are documented in " { $link "byte-arrays" } "." ;
152
153 ARTICLE: "syntax-pathnames" "Pathname syntax"
154 { $subsection POSTPONE: P" }
155 "Pathnames are documented in " { $link "pathnames" } "." ;
156
157 ARTICLE: "syntax-literals" "Literals"
158 "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."
159 $nl
160 "If a quotation contains a literal object, the same literal object instance is used each time the quotation executes; that is, literals are ``live''."
161 $nl
162 "Using mutable object literals in word definitions requires care, since if those objects are mutated, the actual word definition will be changed, which is in most cases not what you would expect. Literals should be " { $link clone } "d before being passed to word which may potentially mutate them."
163 { $subsection "syntax-numbers" }
164 { $subsection "syntax-words" }
165 { $subsection "syntax-quots" }
166 { $subsection "syntax-arrays" }
167 { $subsection "syntax-strings" }
168 { $subsection "syntax-bit-arrays" }
169 { $subsection "syntax-byte-arrays" }
170 { $subsection "syntax-float-arrays" }
171 { $subsection "syntax-vectors" }
172 { $subsection "syntax-sbufs" }
173 { $subsection "syntax-hashtables" }
174 { $subsection "syntax-tuples" }
175 { $subsection "syntax-pathnames" } ;
176
177 ARTICLE: "syntax" "Syntax"
178 "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" } "."
179 { $subsection "parser-algorithm" }
180 { $subsection "syntax-comments" }
181 { $subsection "syntax-literals" }
182 { $subsection "syntax-immediate" } ;
183
184 ABOUT: "syntax"
185
186 HELP: delimiter
187 { $syntax ": foo ... ; delimiter" }
188 { $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." } ;
189
190 HELP: parsing
191 { $syntax ": foo ... ; parsing" }
192 { $description "Declares the most recently defined word as a parsing word." }
193 { $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" "<< : hello \"Hello parser!\" print ; parsing >>\n: world hello ;" "Hello parser!" } } ;
194
195 HELP: inline
196 { $syntax ": foo ... ; inline" }
197 { $description
198     "Declares the most recently defined word as an inline word. The optimizing compiler copies definitions of inline words when compiling calls to them."
199     $nl
200     "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."
201     $nl
202     "The non-optimizing quotation compiler ignores inlining declarations."
203 } ;
204
205 HELP: foldable
206 { $syntax ": foo ... ; foldable" }
207 { $description
208     "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:"
209     { $list
210         "foldable words must not have any observable side effects,"
211         "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."
212         "both inputs and outputs of foldable words must be immutable."
213     }
214     "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."
215 }
216 { $notes
217     "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" } "."
218 }
219 { $examples "Most operations on numbers are foldable. For example, " { $snippet "2 2 +" } " compiles to a literal 4, since " { $link + } " is declared foldable." } ;
220
221 HELP: flushable
222 { $syntax ": foo ... ; flushable" }
223 { $description
224     "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."
225     $nl
226     "Note that many words are flushable but not foldable, for example " { $link clone } " and " { $link <array> } "."
227 } ;
228
229 HELP: t
230 { $syntax "t" }
231 { $values { "t" "the canonical truth value" } }
232 { $class-description "The canonical truth value, which is an instance of itself." } ;
233
234 HELP: f
235 { $syntax "f" }
236 { $values { "f" "the singleton false value" } }
237 { $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:"
238 { $code "f    ! the singleton f object denoting falsity\n\\ f  ! the f class word" } } ;
239
240 HELP: [
241 { $syntax "[ elements... ]" }
242 { $description "Marks the beginning of a literal quotation." }
243 { $examples { $code "[ 1 2 3 ]" } } ;
244
245 { POSTPONE: [ POSTPONE: ] } related-words
246
247 HELP: ]
248 { $syntax "]" }
249 { $description "Marks the end of a literal quotation."
250 $nl
251 "Parsing words can use this word as a generic end delimiter." } ;
252
253 HELP: }
254 { $syntax "}" }
255 { $description "Marks the end of an array, vector, hashtable, complex number, tuple, or wrapper."
256 $nl
257 "Parsing words can use this word as a generic end delimiter." } ;
258
259 { POSTPONE: { POSTPONE: V{ POSTPONE: H{ POSTPONE: C{ POSTPONE: T{ POSTPONE: W{ POSTPONE: } } related-words
260
261 HELP: {
262 { $syntax "{ elements... }" }
263 { $values { "elements" "a list of objects" } }
264 { $description "Marks the beginning of a literal array. Literal arrays are terminated by " { $link POSTPONE: } } "." } 
265 { $examples { $code "{ 1 2 3 }" } } ;
266
267 HELP: V{
268 { $syntax "V{ elements... }" }
269 { $values { "elements" "a list of objects" } }
270 { $description "Marks the beginning of a literal vector. Literal vectors are terminated by " { $link POSTPONE: } } "." } 
271 { $examples { $code "V{ 1 2 3 }" } } ;
272
273 HELP: B{
274 { $syntax "B{ elements... }" }
275 { $values { "elements" "a list of integers" } }
276 { $description "Marks the beginning of a literal byte array. Literal byte arrays are terminated by " { $link POSTPONE: } } "." } 
277 { $examples { $code "B{ 1 2 3 }" } } ;
278
279 HELP: ?{
280 { $syntax "?{ elements... }" }
281 { $values { "elements" "a list of booleans" } }
282 { $description "Marks the beginning of a literal bit array. Literal bit arrays are terminated by " { $link POSTPONE: } } "." } 
283 { $examples { $code "?{ t f t }" } } ;
284
285 HELP: F{
286 { $syntax "F{ elements... }" }
287 { $values { "elements" "a list of real numbers" } }
288 { $description "Marks the beginning of a literal float array. Literal float arrays are terminated by " { $link POSTPONE: } } "." } 
289 { $examples { $code "F{ 1.0 2.0 3.0 }" } } ;
290
291 HELP: H{
292 { $syntax "H{ { key value }... }" }
293 { $values { "key" "an object" } { "value" "an object" } }
294 { $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: } } "." } 
295 { $examples { $code "H{ { \"tuna\" \"fish\" } { \"jalapeno\" \"vegetable\" } }" } } ;
296
297 HELP: C{
298 { $syntax "C{ real-part imaginary-part }" }
299 { $values { "real-part" "a real number" } { "imaginary-part" "a real number" } }
300 { $description "Parses a complex number given in rectangular form as a pair of real numbers. Literal complex numbers are terminated by " { $link POSTPONE: } } "." }  ;
301
302 HELP: T{
303 { $syntax "T{ class slots... }" }
304 { $values { "class" "a tuple class word" } { "slots" "list of objects" } }
305 { $description "Marks the beginning of a literal tuple. Literal tuples are terminated by " { $link POSTPONE: } } "."
306 $nl
307 "The class word must always be specified. If an insufficient number of values is given after the class word, the remaining slots of the tuple are set to " { $link f } ". If too many values are given, they are ignored." } ;
308
309 HELP: W{
310 { $syntax "W{ object }" }
311 { $values { "object" "an object" } }
312 { $description "Marks the beginning of a literal wrapper. Literal wrappers are terminated by " { $link POSTPONE: } } "." }  ;
313
314 HELP: POSTPONE:
315 { $syntax "POSTPONE: word" }
316 { $values { "word" "a word" } }
317 { $description "Reads the next word from the input string and appends the word to the parse tree, even if it is a parsing word." }
318 { $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." }
319 { $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." } ;
320
321 HELP: :
322 { $syntax ": word ( stack -- effect ) definition... ;" }
323 { $values { "word" "a new word to define" } { "definition" "a word definition" } }
324 { $description "Defines a word with the given stack effect in the current vocabulary. The stack effect is optional for words which only push literals on the stack." }
325 { $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 ;" } } ;
326
327 { POSTPONE: : POSTPONE: ; define } related-words
328
329 HELP: ;
330 { $syntax ";" }
331 { $description
332     "Marks the end of a definition."
333     $nl
334     "Parsing words can use this word as a generic end delimiter."
335 } ;
336
337 HELP: SYMBOL:
338 { $syntax "SYMBOL: word" }
339 { $values { "word" "a new word to define" } }
340 { $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" } ")." }
341 { $examples { $example "USE: prettyprint" "IN: scratchpad" "SYMBOL: foo\nfoo ." "foo" } } ;
342
343 { define-symbol POSTPONE: SYMBOL: } related-words
344
345 HELP: \
346 { $syntax "\\ word" }
347 { $values { "word" "a word" } }
348 { $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." }
349 { $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" } } ;
350
351 HELP: DEFER:
352 { $syntax "DEFER: word" }
353 { $values { "word" "a new word to define" } }
354 { $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." }
355 { $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." }
356 { $examples { $code "DEFER: foe\n: fie ... foe ... ;\n: foe ... fie ... ;" } } ;
357
358 HELP: FORGET:
359 { $syntax "FORGET: word" }
360 { $values { "word" "a word" } }
361 { $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." } ;
362
363 HELP: USE:
364 { $syntax "USE: vocabulary" }
365 { $values { "vocabulary" "a vocabulary name" } }
366 { $description "Adds a new vocabulary at the front of the search path. Subsequent word lookups by the parser will search this vocabulary first." }
367 { $errors "Throws an error if the vocabulary does not exist." } ;
368
369 HELP: USING:
370 { $syntax "USING: vocabularies... ;" }
371 { $values { "vocabularies" "a list of vocabulary names" } }
372 { $description "Adds a list of vocabularies to the front of the search path, with later vocabularies taking precedence." }
373 { $errors "Throws an error if one of the vocabularies does not exist." } ;
374
375 HELP: IN:
376 { $syntax "IN: vocabulary" }
377 { $values { "vocabulary" "a new vocabulary name" } }
378 { $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." } ;
379
380 HELP: CHAR:
381 { $syntax "CHAR: token" }
382 { $values { "token" "a literal character, escape code, or Unicode character name" } }
383 { $description "Adds a Unicode code point to the parse tree." }
384 { $examples
385     { $code
386         "CHAR: x"
387         "CHAR: \\u000032"
388         "CHAR: \\u{exclamation-mark}"
389         "CHAR: exclamation-mark"
390         "CHAR: ugaritic-letter-samka"
391     }
392 } ;
393
394 HELP: "
395 { $syntax "\"string...\"" }
396 { $values { "string" "literal and escaped characters" } }
397 { $description "Reads from the input string until the next occurrence of " { $link POSTPONE: " } ", and appends the resulting string to the parse tree. String literals cannot span multiple lines. Strings containing the " { $link POSTPONE: " } " character and various other special characters can be read by inserting escape sequences." }
398 { $examples { $example "USE: io" "\"Hello\\nworld\" print" "Hello\nworld" } } ;
399
400 HELP: SBUF"
401 { $syntax "SBUF\" string... \"" }
402 { $values { "string" "literal and escaped characters" } }
403 { $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." }
404 { $examples { $example "USING: io strings ;" "SBUF\" Hello world\" >string print" "Hello world" } } ;
405
406 HELP: P"
407 { $syntax "P\" pathname\"" }
408 { $values { "pathname" "a pathname string" } }
409 { $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." }
410 { $examples { $example "USING: io io.files ;" "P\" foo.txt\" pathname-string print" "foo.txt" } } ;
411
412 HELP: (
413 { $syntax "( inputs -- outputs )" }
414 { $values { "inputs" "a list of tokens" } { "outputs" "a list of tokens" } }
415 { $description "Declares the stack effect of the most recently defined word, storing a new " { $link effect } " instance in the " { $snippet "\"declared-effect\"" } " word property." }
416 { $notes "All words except those only pushing literals on the stack must have a stack effect declaration. See " { $link "effect-declaration" } " for details." } ;
417
418 HELP: ((
419 { $syntax "(( inputs -- outputs ))" }
420 { $values { "inputs" "a list of tokens" } { "outputs" "a list of tokens" } }
421 { $description "Literal stack effect syntax." }
422 { $notes "Useful for meta-programming with " { $link define-declared } "." }
423 { $examples
424     { $code
425         "SYMBOL: my-dynamic-word"
426         "USING: math random words ;"
427         "3 { [ + ] [ - ] [ * ] [ / ] } random curry"
428         "(( x -- y )) define-declared"
429     }
430 } ;
431
432 HELP: !
433 { $syntax "! comment..." }
434 { $values { "comment" "characters" } }
435 { $description "Discards all input until the end of the line." } ;
436
437 { POSTPONE: ! POSTPONE: #! } related-words
438
439 HELP: #!
440 { $syntax "#! comment..." }
441 { $values { "comment" "characters" } }
442 { $description "Discards all input until the end of the line." } ;
443
444 HELP: HEX:
445 { $syntax "HEX: integer" }
446 { $values { "integer" "hexadecimal digits (0-9, a-f, A-F)" } }
447 { $description "Adds an integer read from a hexadecimal literal to the parse tree." }
448 { $examples { $example "USE: prettyprint" "HEX: ff ." "255" } } ;
449
450 HELP: OCT:
451 { $syntax "OCT: integer" }
452 { $values { "integer" "octal digits (0-7)" } }
453 { $description "Adds an integer read from an octal literal to the parse tree." }
454 { $examples { $example "USE: prettyprint" "OCT: 31337 ." "13023" } } ;
455
456 HELP: BIN:
457 { $syntax "BIN: integer" }
458 { $values { "integer" "binary digits (0 and 1)" } }
459 { $description "Adds an integer read from an binary literal to the parse tree." }
460 { $examples { $example "USE: prettyprint" "BIN: 100 ." "4" } } ;
461
462 HELP: GENERIC:
463 { $syntax "GENERIC: word" }
464 { $values { "word" "a new word to define" } }
465 { $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." } ;
466
467 HELP: GENERIC#
468 { $syntax "GENERIC# word n" }
469 { $values { "word" "a new word to define" } { "n" "the stack position to dispatch on, either 0, 1 or 2" } }
470 { $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." }
471 { $notes
472     "The following two definitions are equivalent:"
473     { $code "GENERIC: foo" }
474     { $code "GENERIC# foo 0" }
475 } ;
476
477 HELP: MATH:
478 { $syntax "MATH: word" }
479 { $values { "word" "a new word to define" } }
480 { $description "Defines a new generic word which uses the " { $link math-combination } " method combination." } ;
481
482 HELP: HOOK:
483 { $syntax "HOOK: word variable" }
484 { $values { "word" "a new word to define" } { "variable" word } }
485 { $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." }
486 { $examples
487     { $example
488         "USING: io namespaces ;"
489         "IN: scratchpad"
490         "SYMBOL: transport"
491         "TUPLE: land-transport ;"
492         "TUPLE: air-transport ;"
493         "HOOK: deliver transport ( destination -- )"
494         "M: land-transport deliver \"Land delivery to \" write print ;"
495         "M: air-transport deliver \"Air delivery to \"  write print ;"
496         "T{ air-transport } transport set"
497         "\"New York City\" deliver"
498         "Air delivery to New York City"
499     }
500 }
501 { $notes
502     "Hook words are really just generic words with a custom method combination (see " { $link "method-combination" } ")."
503 } ;
504
505 HELP: M:
506 { $syntax "M: class generic definition... ;" }
507 { $values { "class" "a class word" } { "generic" "a generic word" } { "definition" "a method definition" } }
508 { $description "Defines a method, that is, a behavior for the generic word specialized on instances of the class." } ;
509
510 HELP: UNION:
511 { $syntax "UNION: class members... ;" }
512 { $values { "class" "a new class word to define" } { "members" "a list of class words separated by whitespace" } }
513 { $description "Defines a union class. An object is an instance of a union class if it is an instance of one of its members." } ;
514
515 HELP: INTERSECTION:
516 { $syntax "INTERSECTION: class participants... ;" }
517 { $values { "class" "a new class word to define" } { "participants" "a list of class words separated by whitespace" } }
518 { $description "Defines an intersection class. An object is an instance of a union class if it is an instance of all of its participants." } ;
519
520 HELP: MIXIN:
521 { $syntax "MIXIN: class" }
522 { $values { "class" "a new class word to define" } }
523 { $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." }
524 { $examples "The " { $link sequence } " and " { $link assoc } " mixin classes." } ;
525
526 HELP: INSTANCE:
527 { $syntax "INSTANCE: instance mixin" }
528 { $values { "instance" "a class word" } { "instance" "a class word" } }
529 { $description "Makes " { $snippet "instance" } " an instance of " { $snippet "mixin" } "." } ;
530
531 HELP: PREDICATE:
532 { $syntax "PREDICATE: class < superclass predicate... ;" }
533 { $values { "class" "a new class word to define" } { "superclass" "an existing class word" } { "predicate" "membership test with stack effect " { $snippet "( superclass -- ? )" } } }
534 { $description
535     "Defines a predicate class deriving from " { $snippet "superclass" } "."
536     $nl
537     "An object is an instance of a predicate class if two conditions hold:"
538     { $list
539         "it is an instance of the predicate's superclass,"
540         "it satisfies the predicate"
541     }
542     "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."
543 }
544 { $examples
545     { $code "USING: math ;" "PREDICATE: positive < integer 0 > ;" }
546 } ;
547
548 HELP: TUPLE:
549 { $syntax "TUPLE: class slots... ;" "TUPLE: class < superclass slots ... ;" }
550 { $values { "class" "a new tuple class to define" } { "slots" "a list of slot names" } }
551 { $description "Defines a new tuple class. The superclass is optional; if left unspecified, it defaults to " { $link tuple } "." } ;
552
553 HELP: ERROR:
554 { $syntax "ERROR: class slots... ;" }
555 { $values { "class" "a new tuple class to define" } { "slots" "a list of slot names" } }
556 { $description "Defines a new tuple class whose class word throws a new instance of the error." }
557 { $notes
558     "The following two snippets are equivalent:"
559     { $code
560         "ERROR: invalid-values x y ;"
561         ""
562         "TUPLE: invalid-values x y ;"
563         ": invalid-values ( x y -- * )"
564         "    \\ invalid-values boa throw ;"
565     }
566 } ;
567
568 HELP: C:
569 { $syntax "C: constructor class" }
570 { $values { "constructor" "a new word to define" } { "class" tuple-class } }
571 { $description "Define a constructor word for a tuple class which simply performs BOA (by order of arguments) construction using " { $link boa } "." }
572 { $examples
573     "Suppose the following tuple has been defined:"
574     { $code "TUPLE: color red green blue ;" }
575     "The following two lines are equivalent:"
576     { $code
577         "C: <color> color"
578         ": <color> color boa ;"
579     }
580     "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."
581 } ;
582
583 HELP: MAIN:
584 { $syntax "MAIN: word" }
585 { $values { "word" word } }
586 { $description "Defines the main entry point for the current vocabulary. This word will be executed when this vocabulary is passed to " { $link run } "." } ;
587
588 HELP: <PRIVATE
589 { $syntax "<PRIVATE ... PRIVATE>" }
590 { $description "Marks the start of a block of private word definitions. Private word definitions are placed in a vocabulary named by suffixing the current vocabulary with " { $snippet ".private" } "." }
591 { $notes
592     "The following is an example of usage:"
593     { $code
594         "IN: factorial"
595         ""
596         "<PRIVATE"
597         ""
598         ": (fac) ( accum n -- n! )"
599         "    dup 1 <= [ drop ] [ [ * ] keep 1- (fac) ] if ;"
600         ""
601         "PRIVATE>"
602         ""
603         ": fac ( n -- n! ) 1 swap (fac) ;"
604     }
605     "The above is equivalent to:"
606     { $code
607         "IN: factorial.private"
608         ""
609         ": (fac) ( accum n -- n! )"
610         "    dup 1 <= [ drop ] [ [ * ] keep 1- (fac) ] if ;"
611         ""
612         "IN: factorial"
613         ""
614         ": fac ( n -- n! ) 1 swap (fac) ;"
615     }
616 } ;
617
618 HELP: PRIVATE>
619 { $syntax "<PRIVATE ... PRIVATE>" }
620 { $description "Marks the end of a block of private word definitions." } ;
621
622 { POSTPONE: <PRIVATE POSTPONE: PRIVATE> } related-words
623
624 HELP: <<
625 { $syntax "<< ... >>" }
626 { $description "Evaluates some code at parse time." }
627 { $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" } "." } ;
628
629 HELP: >>
630 { $syntax ">>" }
631 { $description "Marks the end of a parse time code block." } ;
632
633 HELP: call-next-method
634 { $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." }
635 { $notes "This is syntax sugar around " { $link (call-next-method) } ". The following two lines are equivalent:"
636     { $code
637         "M: my-class my-generic ... call-next-method ... ;"
638         "M: my-class my-generic ... \\ my-class \\ my-generic (call-next-method) ... ;"
639     }
640 "In most cases, this word should be called with the original input values on the stack. Calling it with other values is usually a sign of poor design." }
641 { $errors
642     "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."
643 } ;
644
645 { POSTPONE: call-next-method (call-next-method) next-method } related-words
646
647 { POSTPONE: << POSTPONE: >> } related-words