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.pathnames vocabs.loader io sequences
4 assocs words.symbol words.alias words.constant ;
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."
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:"
14 { "If the current character is a double-quote (\"), the " { $link POSTPONE: " } " parsing word is executed, causing a string to be read." }
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:"
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."
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."
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."
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." ;
28 ARTICLE: "syntax-comments" "Comments"
29 { $subsection POSTPONE: ! }
30 { $subsection POSTPONE: #! } ;
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: >> } ;
37 ARTICLE: "syntax-integers" "Integer syntax"
38 "The printed representation of an integer consists of a sequence of digits, optionally prefixed by a sign."
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" } "." ;
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:"
59 "More information on ratios can be found in " { $link "rationals" } ;
61 ARTICLE: "syntax-floats" "Float syntax"
62 "Floating point literals must contain a decimal point, and may contain an exponent:"
69 "More information on floats can be found in " { $link "floats" } "." ;
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."
74 "C{ 1/2 1/3 } ! the complex number 1/2+1/3i"
75 "C{ 0 1 } ! the imaginary unit"
77 { $subsection POSTPONE: C{ }
78 "More information on complex numbers can be found in " { $link "complex-numbers" } "." ;
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" } ;
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" } "." ;
93 ARTICLE: "escape" "Character escape codes"
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" } } } }
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" } "." ;
115 ARTICLE: "syntax-sbufs" "String buffer syntax"
116 { $subsection POSTPONE: SBUF" }
117 "String buffers are documented in " { $link "sbufs" } "." ;
119 ARTICLE: "syntax-arrays" "Array syntax"
120 { $subsection POSTPONE: { }
121 { $subsection POSTPONE: } }
122 "Arrays are documented in " { $link "arrays" } "." ;
124 ARTICLE: "syntax-vectors" "Vector syntax"
125 { $subsection POSTPONE: V{ }
126 "Vectors are documented in " { $link "vectors" } "." ;
128 ARTICLE: "syntax-hashtables" "Hashtable syntax"
129 { $subsection POSTPONE: H{ }
130 "Hashtables are documented in " { $link "hashtables" } "." ;
132 ARTICLE: "syntax-tuples" "Tuple syntax"
133 { $subsection POSTPONE: T{ }
134 "Tuples are documented in " { $link "tuples" } "." ;
136 ARTICLE: "syntax-quots" "Quotation syntax"
137 { $subsection POSTPONE: [ }
138 { $subsection POSTPONE: ] }
139 "Quotations are documented in " { $link "quotations" } "." ;
141 ARTICLE: "syntax-byte-arrays" "Byte array syntax"
142 { $subsection POSTPONE: B{ }
143 "Byte arrays are documented in " { $link "byte-arrays" } "." ;
145 ARTICLE: "syntax-pathnames" "Pathname syntax"
146 { $subsection POSTPONE: P" }
147 "Pathnames are documented in " { $link "io.pathnames" } "." ;
149 ARTICLE: "syntax-literals" "Literals"
150 "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."
152 "If a quotation contains a literal object, the same literal object instance is used each time the quotation executes; that is, literals are ``live''."
154 "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."
155 { $subsection "syntax-numbers" }
156 { $subsection "syntax-words" }
157 { $subsection "syntax-quots" }
158 { $subsection "syntax-arrays" }
159 { $subsection "syntax-strings" }
160 { $subsection "syntax-byte-arrays" }
161 { $subsection "syntax-vectors" }
162 { $subsection "syntax-sbufs" }
163 { $subsection "syntax-hashtables" }
164 { $subsection "syntax-tuples" }
165 { $subsection "syntax-pathnames" } ;
167 ARTICLE: "syntax" "Syntax"
168 "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" } "."
169 { $subsection "parser-algorithm" }
170 { $subsection "syntax-comments" }
171 { $subsection "syntax-literals" }
172 { $subsection "syntax-immediate" } ;
177 { $syntax ": foo ... ; delimiter" }
178 { $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." } ;
181 { $syntax ": foo ... ; parsing" }
182 { $description "Declares the most recently defined word as a parsing word." }
183 { $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!" } } ;
186 { $syntax ": foo ... ; inline" }
188 "Declares the most recently defined word as an inline word. The optimizing compiler copies definitions of inline words when compiling calls to them."
190 "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."
192 "The non-optimizing quotation compiler ignores inlining declarations."
196 { $syntax ": foo ... ; recursive" }
197 { $description "Declares the most recently defined word as a recursive word." }
198 { $notes "This declaration is only required for " { $link POSTPONE: inline } " words which call themselves. See " { $link "inference-recursive-combinators" } "." } ;
201 { $syntax ": foo ... ; foldable" }
203 "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:"
205 "foldable words must not have any observable side effects,"
206 "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."
207 "both inputs and outputs of foldable words must be immutable."
209 "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."
212 "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" } "."
214 { $examples "Most operations on numbers are foldable. For example, " { $snippet "2 2 +" } " compiles to a literal 4, since " { $link + } " is declared foldable." } ;
217 { $syntax ": foo ... ; flushable" }
219 "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."
221 "Note that many words are flushable but not foldable, for example " { $link clone } " and " { $link <array> } "."
226 { $values { "t" "the canonical truth value" } }
227 { $class-description "The canonical truth value, which is an instance of itself." } ;
231 { $values { "f" "the singleton false value" } }
232 { $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:"
233 { $code "f ! the singleton f object denoting falsity\n\\ f ! the f class word" } } ;
236 { $syntax "[ elements... ]" }
237 { $description "Marks the beginning of a literal quotation." }
238 { $examples { $code "[ 1 2 3 ]" } } ;
240 { POSTPONE: [ POSTPONE: ] } related-words
244 { $description "Marks the end of a literal quotation."
246 "Parsing words can use this word as a generic end delimiter." } ;
250 { $description "Marks the end of an array, vector, hashtable, complex number, tuple, or wrapper."
252 "Parsing words can use this word as a generic end delimiter." } ;
254 { POSTPONE: { POSTPONE: V{ POSTPONE: H{ POSTPONE: C{ POSTPONE: T{ POSTPONE: W{ POSTPONE: } } related-words
257 { $syntax "{ elements... }" }
258 { $values { "elements" "a list of objects" } }
259 { $description "Marks the beginning of a literal array. Literal arrays are terminated by " { $link POSTPONE: } } "." }
260 { $examples { $code "{ 1 2 3 }" } } ;
263 { $syntax "V{ elements... }" }
264 { $values { "elements" "a list of objects" } }
265 { $description "Marks the beginning of a literal vector. Literal vectors are terminated by " { $link POSTPONE: } } "." }
266 { $examples { $code "V{ 1 2 3 }" } } ;
269 { $syntax "B{ elements... }" }
270 { $values { "elements" "a list of integers" } }
271 { $description "Marks the beginning of a literal byte array. Literal byte arrays are terminated by " { $link POSTPONE: } } "." }
272 { $examples { $code "B{ 1 2 3 }" } } ;
275 { $syntax "H{ { key value }... }" }
276 { $values { "key" "an object" } { "value" "an object" } }
277 { $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: } } "." }
278 { $examples { $code "H{ { \"tuna\" \"fish\" } { \"jalapeno\" \"vegetable\" } }" } } ;
281 { $syntax "C{ real-part imaginary-part }" }
282 { $values { "real-part" "a real number" } { "imaginary-part" "a real number" } }
283 { $description "Parses a complex number given in rectangular form as a pair of real numbers. Literal complex numbers are terminated by " { $link POSTPONE: } } "." } ;
286 { $syntax "T{ class slots... }" }
287 { $values { "class" "a tuple class word" } { "slots" "slot values" } }
288 { $description "Marks the beginning of a literal tuple."
290 "Three literal syntax forms are recognized:"
292 { "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" } ")." }
293 { "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." }
294 { "assoc-form: otherwise, " { $snippet "slots" } " is interpreted as a sequence of " { $snippet "{ slot-name value }" } " pairs. The " { $snippet "slot-name" } " should not be quoted." }
296 "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."
298 "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." }
300 "An empty tuple; since vectors have their own literal syntax, the above is equivalent to " { $snippet "V{ }" } ""
301 { $code "T{ vector }" }
305 "T{ rgba f 1.0 0.0 0.5 }"
307 "An assoc-form tuple equal to the above:"
310 "T{ rgba { red 1.0 } { green 0.0 } { blue 0.5 } }"
314 { $syntax "W{ object }" }
315 { $values { "object" "an object" } }
316 { $description "Marks the beginning of a literal wrapper. Literal wrappers are terminated by " { $link POSTPONE: } } "." } ;
319 { $syntax "POSTPONE: word" }
320 { $values { "word" "a word" } }
321 { $description "Reads the next word from the input string and appends the word to the parse tree, even if it is a parsing word." }
322 { $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." }
323 { $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." } ;
326 { $syntax ": word ( stack -- effect ) definition... ;" }
327 { $values { "word" "a new word to define" } { "definition" "a word definition" } }
328 { $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." }
329 { $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 ;" } } ;
331 { POSTPONE: : POSTPONE: ; define } related-words
336 "Marks the end of a definition."
338 "Parsing words can use this word as a generic end delimiter."
342 { $syntax "SYMBOL: word" }
343 { $values { "word" "a new word to define" } }
344 { $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" } ")." }
345 { $examples { $example "USE: prettyprint" "IN: scratchpad" "SYMBOL: foo\nfoo ." "foo" } } ;
347 { define-symbol POSTPONE: SYMBOL: POSTPONE: SYMBOLS: } related-words
350 { $syntax "SYMBOLS: words... ;" }
351 { $values { "words" "a sequence of new words to define" } }
352 { $description "Creates a new symbol for every token until the " { $snippet ";" } "." }
353 { $examples { $example "USING: prettyprint ;" "IN: scratchpad" "SYMBOLS: foo bar baz ;\nfoo . bar . baz ." "foo\nbar\nbaz" } } ;
356 { $syntax "SINGLETONS: words... ;" }
357 { $values { "words" "a sequence of new words to define" } }
358 { $description "Creates a new singleton for every token until the " { $snippet ";" } "." } ;
361 { $syntax "ALIAS: new-word existing-word" }
362 { $values { "new-word" word } { "existing-word" word } }
363 { $description "Creates a new inlined word that calls the existing word." }
365 { $example "USING: prettyprint sequences ;"
367 "ALIAS: sequence-nth nth"
368 "0 { 10 20 30 } sequence-nth ."
373 { define-alias POSTPONE: ALIAS: } related-words
376 { $syntax "CONSTANT: word value" }
377 { $values { "word" word } { "value" object } }
378 { $description "Creates a word which pushes a value on the stack." }
379 { $examples { $code "CONSTANT: magic 1" "CONSTANT: science HEX: ff0f" } } ;
381 { define-constant POSTPONE: CONSTANT: } related-words
384 { $syntax "\\ word" }
385 { $values { "word" "a word" } }
386 { $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." }
387 { $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" } } ;
390 { $syntax "DEFER: word" }
391 { $values { "word" "a new word to define" } }
392 { $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." }
393 { $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." }
394 { $examples { $code "DEFER: foe\n: fie ... foe ... ;\n: foe ... fie ... ;" } } ;
397 { $syntax "FORGET: word" }
398 { $values { "word" "a word" } }
399 { $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." } ;
402 { $syntax "USE: vocabulary" }
403 { $values { "vocabulary" "a vocabulary name" } }
404 { $description "Adds a new vocabulary at the front of the search path. Subsequent word lookups by the parser will search this vocabulary first." }
405 { $errors "Throws an error if the vocabulary does not exist." } ;
408 { $syntax "USING: vocabularies... ;" }
409 { $values { "vocabularies" "a list of vocabulary names" } }
410 { $description "Adds a list of vocabularies to the front of the search path, with later vocabularies taking precedence." }
411 { $errors "Throws an error if one of the vocabularies does not exist." } ;
414 { $syntax "QUALIFIED: vocab" }
415 { $description "Similar to " { $link POSTPONE: USE: } " but loads vocabulary with prefix." }
416 { $examples { $example
417 "USING: prettyprint ;"
422 HELP: QUALIFIED-WITH:
423 { $syntax "QUALIFIED-WITH: vocab word-prefix" }
424 { $description "Works like " { $link POSTPONE: QUALIFIED: } " but uses " { $snippet "word-prefix" } " as prefix." }
426 "USING: prettyprint ;"
427 "QUALIFIED-WITH: math m"
433 { $syntax "FROM: vocab => words ... ;" }
434 { $description "Imports " { $snippet "words" } " from " { $snippet "vocab" } "." }
436 "FROM: math.parser => bin> hex> ; ! imports only bin> and hex>" } } ;
439 { $syntax "EXCLUDE: vocab => words ... ;" }
440 { $description "Imports everything from " { $snippet "vocab" } " excluding " { $snippet "words" } "." }
442 "EXCLUDE: math.parser => bin> hex> ; ! imports everything but bin> and hex>" } } ;
445 { $syntax "RENAME: word vocab => newname" }
446 { $description "Imports " { $snippet "word" } " from " { $snippet "vocab" } ", but renamed to " { $snippet "newname" } "." }
447 { $examples { $example
448 "USING: prettyprint ;"
449 "RENAME: + math => -"
455 { $syntax "IN: vocabulary" }
456 { $values { "vocabulary" "a new vocabulary name" } }
457 { $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." } ;
460 { $syntax "CHAR: token" }
461 { $values { "token" "a literal character, escape code, or Unicode character name" } }
462 { $description "Adds a Unicode code point to the parse tree." }
467 "CHAR: \\u{exclamation-mark}"
468 "CHAR: exclamation-mark"
469 "CHAR: ugaritic-letter-samka"
474 { $syntax "\"string...\"" }
475 { $values { "string" "literal and escaped characters" } }
476 { $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." }
477 { $examples { $example "USE: io" "\"Hello\\nworld\" print" "Hello\nworld" } } ;
480 { $syntax "SBUF\" string... \"" }
481 { $values { "string" "literal and escaped characters" } }
482 { $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." }
483 { $examples { $example "USING: io strings ;" "SBUF\" Hello world\" >string print" "Hello world" } } ;
486 { $syntax "P\" pathname\"" }
487 { $values { "pathname" "a pathname string" } }
488 { $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." }
489 { $examples { $example "USING: accessors io io.files ;" "P\" foo.txt\" string>> print" "foo.txt" } } ;
492 { $syntax "( inputs -- outputs )" }
493 { $values { "inputs" "a list of tokens" } { "outputs" "a list of tokens" } }
494 { $description "Declares the stack effect of the most recently defined word, storing a new " { $link effect } " instance in the " { $snippet "\"declared-effect\"" } " word property." }
495 { $notes "All words except those only pushing literals on the stack must have a stack effect declaration. See " { $link "effect-declaration" } " for details." } ;
498 { $syntax "(( inputs -- outputs ))" }
499 { $values { "inputs" "a list of tokens" } { "outputs" "a list of tokens" } }
500 { $description "Literal stack effect syntax." }
501 { $notes "Useful for meta-programming with " { $link define-declared } "." }
504 "SYMBOL: my-dynamic-word"
505 "USING: math random words ;"
506 "3 { [ + ] [ - ] [ * ] [ / ] } random curry"
507 "(( x -- y )) define-declared"
512 { $syntax "! comment..." }
513 { $values { "comment" "characters" } }
514 { $description "Discards all input until the end of the line." } ;
516 { POSTPONE: ! POSTPONE: #! } related-words
519 { $syntax "#! comment..." }
520 { $values { "comment" "characters" } }
521 { $description "Discards all input until the end of the line." } ;
524 { $syntax "HEX: integer" }
525 { $values { "integer" "hexadecimal digits (0-9, a-f, A-F)" } }
526 { $description "Adds an integer read from a hexadecimal literal to the parse tree." }
527 { $examples { $example "USE: prettyprint" "HEX: ff ." "255" } } ;
530 { $syntax "OCT: integer" }
531 { $values { "integer" "octal digits (0-7)" } }
532 { $description "Adds an integer read from an octal literal to the parse tree." }
533 { $examples { $example "USE: prettyprint" "OCT: 31337 ." "13023" } } ;
536 { $syntax "BIN: integer" }
537 { $values { "integer" "binary digits (0 and 1)" } }
538 { $description "Adds an integer read from an binary literal to the parse tree." }
539 { $examples { $example "USE: prettyprint" "BIN: 100 ." "4" } } ;
542 { $syntax "GENERIC: word" }
543 { $values { "word" "a new word to define" } }
544 { $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." } ;
547 { $syntax "GENERIC# word n" }
548 { $values { "word" "a new word to define" } { "n" "the stack position to dispatch on, either 0, 1 or 2" } }
549 { $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." }
551 "The following two definitions are equivalent:"
552 { $code "GENERIC: foo" }
553 { $code "GENERIC# foo 0" }
557 { $syntax "MATH: word" }
558 { $values { "word" "a new word to define" } }
559 { $description "Defines a new generic word which uses the " { $link math-combination } " method combination." } ;
562 { $syntax "HOOK: word variable" }
563 { $values { "word" "a new word to define" } { "variable" word } }
564 { $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." }
567 "USING: io namespaces ;"
570 "TUPLE: land-transport ;"
571 "TUPLE: air-transport ;"
572 "HOOK: deliver transport ( destination -- )"
573 "M: land-transport deliver \"Land delivery to \" write print ;"
574 "M: air-transport deliver \"Air delivery to \" write print ;"
575 "T{ air-transport } transport set"
576 "\"New York City\" deliver"
577 "Air delivery to New York City"
581 "Hook words are really just generic words with a custom method combination (see " { $link "method-combination" } ")."
585 { $syntax "M: class generic definition... ;" }
586 { $values { "class" "a class word" } { "generic" "a generic word" } { "definition" "a method definition" } }
587 { $description "Defines a method, that is, a behavior for the generic word specialized on instances of the class." } ;
590 { $syntax "UNION: class members... ;" }
591 { $values { "class" "a new class word to define" } { "members" "a list of class words separated by whitespace" } }
592 { $description "Defines a union class. An object is an instance of a union class if it is an instance of one of its members." } ;
595 { $syntax "INTERSECTION: class participants... ;" }
596 { $values { "class" "a new class word to define" } { "participants" "a list of class words separated by whitespace" } }
597 { $description "Defines an intersection class. An object is an instance of an intersection class if it is an instance of all of its participants." } ;
600 { $syntax "MIXIN: class" }
601 { $values { "class" "a new class word to define" } }
602 { $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." }
603 { $examples "The " { $link sequence } " and " { $link assoc } " mixin classes." } ;
606 { $syntax "INSTANCE: instance mixin" }
607 { $values { "instance" "a class word" } { "instance" "a class word" } }
608 { $description "Makes " { $snippet "instance" } " an instance of " { $snippet "mixin" } "." } ;
611 { $syntax "PREDICATE: class < superclass predicate... ;" }
612 { $values { "class" "a new class word to define" } { "superclass" "an existing class word" } { "predicate" "membership test with stack effect " { $snippet "( superclass -- ? )" } } }
614 "Defines a predicate class deriving from " { $snippet "superclass" } "."
616 "An object is an instance of a predicate class if two conditions hold:"
618 "it is an instance of the predicate's superclass,"
619 "it satisfies the predicate"
621 "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."
624 { $code "USING: math ;" "PREDICATE: positive < integer 0 > ;" }
628 { $syntax "TUPLE: class slots... ;" "TUPLE: class < superclass slots ... ;" }
629 { $values { "class" "a new tuple class to define" } { "slots" "a list of slot specifiers" } }
630 { $description "Defines a new tuple class."
632 "The superclass is optional; if left unspecified, it defaults to " { $link tuple } "."
634 "Slot specifiers take one of the following three forms:"
636 { { $snippet "name" } " - a slot which can hold any object, with no attributes" }
637 { { $snippet "{ name attributes... }" } " - a slot which can hold any object, with optional attributes" }
638 { { $snippet "{ name class attributes... }" } " - a slot specialized to a specific class, with optional attributes" }
640 "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." }
642 "A simple tuple class:"
643 { $code "TUPLE: color red green blue ;" }
644 "Declaring slots to be integer-valued:"
645 { $code "TUPLE: color" "{ red integer }" "{ green integer }" "{ blue integer } ;" }
646 "An example mixing short and long slot specifiers:"
647 { $code "TUPLE: person" "{ age integer initial: 0 }" "{ department string initial: \"Marketing\" }" "manager ;" }
651 { $syntax "TUPLE: ... { slot initial: value } ... ;" }
652 { $values { "slot" "a slot name" } { "value" "any literal" } }
653 { $description "Specifies an initial value for a tuple slot." } ;
656 { $syntax "TUPLE: ... { slot read-only } ... ;" }
657 { $values { "slot" "a slot name" } }
658 { $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." } ;
660 { initial: read-only } related-words
663 { $syntax "SLOT: name" }
664 { $values { "name" "a slot name" } }
665 { $description "Defines a protocol slot; that is, defines the accessor words for a slot named " { $snippet "slot" } " without associating it with any specific tuple." } ;
668 { $syntax "ERROR: class slots... ;" }
669 { $values { "class" "a new tuple class to define" } { "slots" "a list of slot names" } }
670 { $description "Defines a new tuple class whose class word throws a new instance of the error." }
672 "The following two snippets are equivalent:"
674 "ERROR: invalid-values x y ;"
676 "TUPLE: invalid-values x y ;"
677 ": invalid-values ( x y -- * )"
678 " \\ invalid-values boa throw ;"
683 { $syntax "C: constructor class" }
684 { $values { "constructor" "a new word to define" } { "class" tuple-class } }
685 { $description "Define a constructor word for a tuple class which simply performs BOA (by order of arguments) construction using " { $link boa } "." }
687 "Suppose the following tuple has been defined:"
688 { $code "TUPLE: color red green blue ;" }
689 "The following two lines are equivalent:"
692 ": <color> color boa ;"
694 "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."
698 { $syntax "MAIN: word" }
699 { $values { "word" word } }
700 { $description "Defines the main entry point for the current vocabulary. This word will be executed when this vocabulary is passed to " { $link run } "." } ;
703 { $syntax "<PRIVATE ... PRIVATE>" }
704 { $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" } "." }
706 "The following is an example of usage:"
712 ": (fac) ( accum n -- n! )"
713 " dup 1 <= [ drop ] [ [ * ] keep 1- (fac) ] if ;"
717 ": fac ( n -- n! ) 1 swap (fac) ;"
719 "The above is equivalent to:"
721 "IN: factorial.private"
723 ": (fac) ( accum n -- n! )"
724 " dup 1 <= [ drop ] [ [ * ] keep 1- (fac) ] if ;"
728 ": fac ( n -- n! ) 1 swap (fac) ;"
733 { $syntax "<PRIVATE ... PRIVATE>" }
734 { $description "Marks the end of a block of private word definitions." } ;
736 { POSTPONE: <PRIVATE POSTPONE: PRIVATE> } related-words
739 { $syntax "<< ... >>" }
740 { $description "Evaluates some code at parse time." }
741 { $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" } "." } ;
745 { $description "Marks the end of a parse time code block." } ;
747 HELP: call-next-method
748 { $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." }
749 { $notes "This is syntax sugar around " { $link (call-next-method) } ". The following two lines are equivalent:"
751 "M: my-class my-generic ... call-next-method ... ;"
752 "M: my-class my-generic ... \\ my-class \\ my-generic (call-next-method) ... ;"
754 "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." }
756 "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."
759 { POSTPONE: call-next-method (call-next-method) next-method } related-words
761 { POSTPONE: << POSTPONE: >> } related-words