1 USING: assocs help.markup help.syntax kernel math
2 namespaces.private quotations words words.symbol ;
5 ARTICLE: "namespaces-combinators" "Namespace combinators"
12 ARTICLE: "namespaces-change" "Changing variable values"
23 ARTICLE: "namespaces-global" "Global variables"
33 ARTICLE: "namespaces.private" "Namespace implementation details"
34 "The namestack holds namespaces."
40 "A pair of words push and pop namespaces on the namestack."
46 ARTICLE: "namespaces" "Dynamic variables"
47 "The " { $vocab-link "namespaces" } " vocabulary implements dynamically-scoped variables."
49 "A dynamic variable is an entry in an assoc of bindings, where the assoc is implicit rather than passed on the stack. These assocs are termed " { $emphasis "namespaces" } ". Nesting of scopes is implemented with a search order on namespaces, defined by a " { $emphasis "namestack" } ". Since namespaces are just assocs, any object can be used as a variable. By convention, variables are keyed by " { $link "words.symbol" } "."
51 "The " { $link get } " and " { $link set } " words read and write variable values. The " { $link get } " word searches the chain of nested namespaces, while " { $link set } " always sets variable values in the current namespace only. Namespaces are dynamically scoped; when a quotation is called from a nested scope, any words called by the quotation also execute in that scope."
56 "Various utility words provide common variable access patterns:"
59 "namespaces-combinators"
61 "Implementation details your code probably does not care about:"
62 { $subsections "namespaces.private" }
63 "Dynamic variables complement " { $link "locals" } "." ;
68 { $values { "variable" "a variable, by convention a symbol" } { "value" { $maybe "the value" } } }
69 { $description "Searches the namestack for a namespace containing the variable, and outputs the associated value. If no such namespace is found, outputs " { $link f } "." } ;
72 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
73 { $description "Assigns a value to the variable in the namespace at the top of the namestack." }
74 { $side-effects "variable" } ;
77 { $values { "variable" "a variable, by convention a symbol" } }
78 { $description "Assigns a value of " { $link f } " to the variable." }
79 { $side-effects "variable" } ;
82 { $values { "variable" "a variable, by convention a symbol" } }
83 { $description "Assigns a value of " { $link t } " to the variable." }
84 { $side-effects "variable" } ;
87 { $values { "variable" "a variable, by convention a symbol" } { "quot" { $quotation ( old -- new ) } } }
88 { $description "Applies the quotation to the old value of the variable, and assigns the resulting value to the variable." }
89 { $side-effects "variable" } ;
92 { $values { "variable" "a variable, by convention a symbol" } { "quot" { $quotation ( old -- new ) } } }
93 { $description "Applies the quotation to the old value of the global variable, and assigns the resulting value to the global variable." }
94 { $side-effects "variable" } ;
98 { "variable" "a variable, by convention a symbol" }
100 { $description "Changes the boolean value of a variable to its opposite." } ;
106 { $description "Runs the quotation in the global namespace." } ;
109 { $values { "n" number } { "variable" "a variable, by convention a symbol" } }
110 { $description "Adds " { $snippet "n" } " to the value of the variable. A variable value of " { $link f } " is interpreted as being zero." }
111 { $side-effects "variable" }
113 { $example "USING: namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: foo\n1 foo +@\n10 foo +@\nfoo get ." "11" }
117 { $values { "variable" "a variable, by convention a symbol" } }
118 { $description "Increments the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
119 { $side-effects "variable" } ;
122 { $values { "variable" "a variable, by convention a symbol" } }
123 { $description "Decrements the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
124 { $side-effects "variable" } ;
127 { $values { "variable" "a variable, by convention a symbol" } { "n" integer } }
128 { $description "Increments the value of the variable by 1, and returns its new value." }
129 { $notes "This word is useful for generating (somewhat) unique identifiers. For example, the " { $link gensym } " word uses it." }
130 { $side-effects "variable" } ;
133 { $values { "quot" quotation } }
134 { $description "Calls the quotation in a new namespace. Any variables set by the quotation are discarded when it returns." }
136 { $example "USING: math namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: x" "0 x set" "[ x [ 5 + ] change x get . ] with-scope x get ." "5\n0" }
140 { $values { "value" object } { "key" "a variable, by convention a symbol" } { "quot" quotation } }
141 { $description "Calls the quotation in a new namespace where " { $snippet "key" } " is set to " { $snippet "value" } "." }
142 { $examples "The following two phrases are equivalent:"
143 { $code "[ 3 x set foo ] with-scope" }
144 { $code "3 x [ foo ] with-variable" }
148 { $values { "ns" assoc } { "quot" quotation } }
149 { $description "Calls the quotation in the dynamic scope of " { $snippet "ns" } ". When variables are looked up by the quotation, " { $snippet "ns" } " is checked first, and setting variables in the quotation stores them in " { $snippet "ns" } "." } ;
152 { $values { "namespace" assoc } }
153 { $description "Outputs the current namespace. Calls to " { $link set } " modify this namespace." } ;
156 { $values { "g" assoc } }
157 { $description "Outputs the global namespace. The global namespace is always checked last when looking up variable values." } ;
160 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value" } }
161 { $description "Outputs the value of a variable in the global namespace." } ;
164 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
165 { $description "Assigns a value to the variable in the global namespace." }
166 { $side-effects "variable" } ;
168 HELP: (get-namestack)
169 { $values { "namestack" "a vector of assocs" } }
170 { $description "Outputs the current namestack." } ;
173 { $values { "namestack" "a vector of assocs" } }
174 { $description "Outputs a copy of the current namestack." } ;
177 { $values { "namestack" "a vector of assocs" } }
178 { $description "Replaces the namestack with a copy of the given vector." } ;
181 { $values { "namespace" assoc } }
182 { $description "Pushes a namespace on the namestack." } ;
185 { $description "Pops a namespace from the namestack." } ;
188 { $description "Resets the namestack to its initial state, holding a single copy of the global namespace." }
192 { $values { "variable" symbol } { "quot" quotation } }
193 { $description "If " { $snippet "variable" } " does not have a value in the global namespace, calls " { $snippet "quot" } " and assigns the result to " { $snippet "variable" } " in the global namespace." } ;