]> gitweb.factorcode.org Git - factor.git/blob - core/definitions/definitions-docs.factor
b1575cc1e4cf249319500aad63bf5d2a108c8dbd
[factor.git] / core / definitions / definitions-docs.factor
1 USING: help.markup help.syntax words math source-files
2 parser quotations compiler.units ;
3 IN: definitions
4
5 ARTICLE: "definition-protocol" "Definition protocol"
6 "A common protocol is used to build generic tools for working with all definitions."
7 $nl
8 "Definitions must know what source file they were loaded from, and provide a way to set this:"
9 { $subsection where }
10 { $subsection set-where }
11 "Definitions can be removed:"
12 { $subsection forget }
13 "Definitions must implement a few operations used for printing them in source form:"
14 { $subsection definer }
15 { $subsection definition }
16 { $see-also "see" } ;
17
18 ARTICLE: "definition-checking" "Definition sanity checking"
19 "When a source file is reloaded, the parser compares the previous list of definitions with the current list; any definitions which are no longer present in the file are removed by a call to " { $link forget } ". A warning message is printed if any other definitions still depend on the removed definitions."
20 $nl
21 "The parser also catches forward references when reloading source files. This is best illustrated with an example. Suppose we load a source file " { $snippet "a.factor" } ":"
22 { $code
23     "USING: io sequences ;"
24     "IN: a"
25     ": hello \"Hello\" ;"
26     ": world \"world\" ;"
27     ": hello-world hello " " world 3append print ;"
28 }
29 "The definitions for " { $snippet "hello" } ", " { $snippet "world" } ", and " { $snippet "hello-world" } " are in the dictionary."
30 $nl
31 "Now, after some heavily editing and refactoring, the file looks like this:"
32 { $code
33     "USING: namespaces ;"
34     "IN: a"
35     ": hello \"Hello\" % ;"
36     ": hello-world [ hello " " % world ] \"\" make ;"
37     ": world \"world\" % ;"
38 }
39 "Note that the developer has made a mistake, placing the definition of " { $snippet "world" } " " { $emphasis "after" } " its usage in " { $snippet "hello-world" } "."
40 $nl
41 "If the parser did not have special checks for this case, then the modified source file would still load, because when the definition of " { $snippet "hello-world" } " on line 4 is being parsed, the " { $snippet "world" } " word is already present in the dictionary from an earlier run. The developer would then not discover this mistake until attempting to load the source file into a fresh image."
42 $nl
43 "Since this is undesirable, the parser explicitly raises a " { $link no-word } " error if a source file refers to a word which is in the dictionary, but defined after it is used."
44 $nl
45 "The parser also catches duplicate definitions. If an artifact is defined twice in the same source file, the earlier definition will never be accessible, and this is almost always a mistake, perhaps due to a bad choice of word names, or a copy and paste error. The parser raises an error in this case."
46 { $subsection redefine-error } ;
47
48 ARTICLE: "definitions" "Definitions"
49 "A " { $emphasis "definition" } " is an artifact read from a source file. Words for working with definitions are found in the " { $vocab-link "definitions" } " vocabulary."
50 $nl
51 "Definitions are defined using parsing words. Examples of definitions together with their defining parsing words are words (" { $link POSTPONE: : } "), methods (" { $link POSTPONE: M: } "), and vocabularies (" { $link POSTPONE: IN: } ")."
52 $nl
53 "All definitions share some common traits:"
54 { $list
55   "There is a word to list all definitions of a given type"
56   "There is a parsing word for creating new definitions"
57   "There is an ordinary word which is the runtime equivalent of the parsing word, for introspection"
58   "Instances of the definition may be introspected and modified with the definition protocol"
59 }
60 "For every source file loaded into the system, a list of definitions is maintained. Pathname objects implement the definition protocol, acting over the definitions their source files contain. See " { $link "source-files" } " for details."
61 { $subsection "definition-protocol" }
62 { $subsection "definition-checking" }
63 { $subsection "compilation-units" }
64 "A parsing word to remove definitions:"
65 { $subsection POSTPONE: FORGET: }
66 { $see-also "see" "parser" "source-files" "words" "generic" "help-impl" } ;
67
68 ABOUT: "definitions"
69
70 HELP: where
71 { $values { "defspec" "a definition specifier" } { "loc" "a " { $snippet "{ path line# }" } " pair" } }
72 { $description "Outputs the location of a definition. If the location is not known, will output " { $link f } "." } ;
73
74 HELP: set-where
75 { $values { "loc" "a " { $snippet "{ path line# }" } " pair" } { "defspec" "a definition specifier" } }
76 { $description "Sets the definition's location." }
77 { $notes "This word is used by the parser." } ;
78
79 HELP: forget
80 { $values { "defspec" "a definition specifier" } }
81 { $description "Forgets about a definition. For example, if it is a word, it will be removed from its vocabulary." }
82 { $notes "This word must be called from inside " { $link with-compilation-unit } "." } ;
83
84 HELP: forget-all
85 { $values { "definitions" "a sequence of definition specifiers" } }
86 { $description "Forgets every definition in a sequence." }
87 { $notes "This word must be called from inside " { $link with-compilation-unit } "." } ;