2 USING: generic help kernel math memory namespaces sequences kernel-internals ;
4 HELP: eq? ( obj1 obj2 -- ? )
5 { $values { "obj1" "an object" } { "obj2" "an object" } }
6 { $description "Tests if two references point at the same object." } ;
8 HELP: drop ( x -- ) $shuffle ;
9 HELP: 2drop ( x y -- ) $shuffle ;
10 HELP: 3drop ( x y z -- ) $shuffle ;
11 HELP: dup ( x -- x x ) $shuffle ;
12 HELP: 2dup ( x y -- x y x y ) $shuffle ;
13 HELP: 3dup ( x y z -- x y z x y z ) $shuffle ;
14 HELP: rot ( x y z -- y z x ) $shuffle ;
15 HELP: -rot ( x y z -- z x y ) $shuffle ;
16 HELP: dupd ( x y -- x x y ) $shuffle ;
17 HELP: swapd ( x y z -- y x z ) $shuffle ;
18 HELP: nip ( x y -- y ) $shuffle ;
19 HELP: 2nip ( x y z -- z ) $shuffle ;
20 HELP: tuck ( x y -- y x y ) $shuffle ;
21 HELP: over ( x y -- x y x ) $shuffle ;
22 HELP: pick ( x y z -- x y z x ) $shuffle ;
23 HELP: swap ( x y -- y x ) $shuffle ;
24 HELP: 2swap ( x y z t -- z t x y ) $shuffle ;
27 { $values { "x" "an object" } } { $description "Moves the top of the data stack to the retain stack." } ;
30 { $values { "x" "an object" } } { $description "Moves the top of the retain stack to the data stack." } ;
32 HELP: datastack ( -- ds )
33 { $values { "ds" "a vector" } }
34 { $description "Outputs the a vector containing a copy of the data stack contents right before the call to this word, with the top of the stack at the end of the vector." } ;
36 HELP: set-datastack ( ds -- )
37 { $values { "ds" "a vector" } }
38 { $description "Replaces the data stack contents with a copy of a vector. The end of the vector becomes the top of the stack." } ;
40 HELP: retainstack ( -- rs )
41 { $values { "rs" "a vector" } }
42 { $description "Outputs the a vector containing a copy of the retain stack contents right before the call to this word, with the top of the stack at the end of the vector." } ;
44 HELP: set-retainstack ( rs -- )
45 { $values { "rs" "a vector" } }
46 { $description "Replaces the retain stack contents with a copy of a vector. The end of the vector becomes the top of the stack." } ;
48 HELP: callstack ( -- cs )
49 { $values { "cs" "a vector" } }
50 { $description "Outputs the a vector containing a copy of the call stack contents right before the call to this word, with the top of the stack at the end of the vector. The call frame of the caller word is " { $emphasis "not" } " included." } ;
52 HELP: set-callstack ( cs -- )
53 { $values { "cs" "a vector" } }
54 { $description "Replaces the call stack contents with a copy of a vector. The end of the vector becomes the top of the stack. The current quotation continues executing. The new callstack takes effect when the current quotation returns, resulting in a callframe being popped." } ;
57 { $description "Clears the data stack." } ;
60 { $values { "obj" "an object" } { "n" "a fixnum" } }
61 { $contract "Outputs the hashcode of the object. The hashcode operation must satisfy the following properties:"
63 "the hashcode should be a fixnum, however returning a bignum will not cause any problems other than potential performance degradation."
64 { "if two objects are equal under " { $link = } ", they must have equal hashcodes" }
65 "the hashcode is only permitted to change if the object is mutated in some way"
67 "If mutable objects are used as hashtable keys, they must not be mutated in such a way that their hashcode changes. Doing so will violate bucket sorting invariants and result in undefined behavior." } ;
70 { $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } }
72 "Tests if two objects are equal. If " { $snippet "obj1" } " and " { $snippet "obj2" } " point to the same object, outputs " { $link t } ". Otherwise, calls the " { $link equal? } " generic word."
76 { $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } }
78 "Tests if two objects are equal."
80 "Method definitions should ensure that this is an equality relation:"
83 { { $snippet "a = b" } " implies " { $snippet "b = a" } }
84 { { $snippet "a = b" } " and " { $snippet "b = c" } " implies " { $snippet "a = c" } }
86 "While user code can define methods for this generic word, it should not call it directly, since it is slightly less efficient than " { $link = } " in the case the two references point to the same object."
89 "The most common reason for defining a method for this generic word to ensure that instances of a specific tuple class are only ever equal to themselves, overriding the default implementation which checks slot values for equality."
90 { $code "TUPLE: foo ;" "M: foo equal? eq? ;" }
94 { $values { "obj1" "an object" } { "obj2" "an object" } { "n" "an integer" } }
96 "Compares two objects using an intrinsic partial order, for example, the natural order for real numbers and lexicographic order for strings."
98 "The output value is one of the following:"
100 { "positive - indicating that " { $snippet "str1" } " follows " { $snippet "str2" } }
101 { "zero - indicating that " { $snippet "str1" } " is equal to " { $snippet "str2" } }
102 { "negative - indicating that " { $snippet "str1" } " precedes " { $snippet "str2" } }
104 "The default implementation treats the two objects as sequences, and recursively compares their elements. So no extra work is required to compare sequences lexicographically."
106 { $see-also natural-sort } ;
109 { $values { "obj" "an object" } { "cloned" "a new object" } }
110 { $contract "Outputs a new object equal to the given object. This is not guaranteed to actually copy the object; it does nothing with immutable objects, and does not copy words either. However, sequences and tuples can be cloned to obtain a shallow copy of the original." } ;
113 { $values { "quot" "a quotation" } }
114 { $description "Sets the initial quotation called by the runtime as the last stage of startup. The image must be saved for changes to the boot quotation to take effect. Usually the boot quotation should not be changed." } ;
117 { $values { "n" "a postiive integer" } }
118 { $description "Outputs one more than the maximum value from the " { $link type } " primitive." } ;
120 DEFER: type ( object -- n )
123 { $values { "object" "an object" } { "n" "a type number" } }
124 { $description "Outputs an object's type number, between zero and one less than " { $link num-types } ". This is implementation detail and user code should call " { $link class } " instead." }
125 { $see-also type>class tag } ;
128 { $values { "cond" "a generalized boolean" } { "true" "an object" } { "false" "an object" } { "true/false" "one two input objects" } }
129 { $description "Chooses between two values depending on the boolean value of " { $snippet "cond" } "." } ;
132 { $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
133 { $description "Convert a generalized boolean into a boolean. That is, " { $link f } " retains its value, whereas anything else becomes " { $link t } "." } ;
135 HELP: not ( obj -- ? )
136 { $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
137 { $description "For " { $link f } " outputs " { $link t } " and for anything else outputs " { $link f } "." } ;
140 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "obj" "a generalized boolean" } }
141 { $description "Tests if neither object is " { $link f } "." } ;
144 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "obj" "a generalized boolean" } }
145 { $description "Tests if at least one object is not " { $link f } "." } ;
148 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "obj" "a generalized boolean" } }
149 { $description "Tests if at exactly one object is not " { $link f } "." } ;
152 { $values { "cpu" "a string" } }
154 "Outputs a string descriptor of the current CPU architecture. Currently, this set of descriptors is:"
155 { $code "amd64" "ppc" "x86" }
159 { $values { "os" "a string" } }
161 "Outputs a string descriptor of the current operating system family. Currently, this set of descriptors is:"
162 { $code "freebsd" "linux" "macosx" "solaris" "win32" "unix" }
166 { $values { "?" "a boolean" } }
167 { $description "Tests if Factor is running on Windows." } ;
170 { $values { "?" "a boolean" } }
171 { $description "Tests if Factor is running on Mac OS X." } ;
173 HELP: call ( quot -- )
174 { $values { "quot" "a quotation" } }
175 { $description "Push the current callframe on the callstack, and set the callframe to the given quotation. Conceptually, calls the quotation, as if its definition was substituted at the location of the call." }
177 "The following two lines are equivalent:"
178 { $code "2 [ 2 + 3 * ] call" "2 2 + 3 *" }
182 { $values { "quot" "a quotation" } { "x" "an object" } }
183 { $description "Calls a quotation while hiding the top of the stack." } ;
186 { $values { "quot" "a quotation" } { "x" "an object" } { "y" "an object" } }
187 { $description "Calls a quotation while hiding the top two stack elements." } ;
190 { $values { "quot" "a quotation with stack effect " { $snippet "( x -- )" } } { "x" "an object" } }
191 { $description "Call a quotation with a value on the stack, restoring the value when the quotation returns." } ;
194 { $values { "quot" "a quotation with stack effect " { $snippet "( x y -- )" } } { "x" "an object" } { "y" "an object" } }
195 { $description "Call a quotation with two values on the stack, restoring the values when the quotation returns." } ;
198 { $values { "quot" "a quotation with stack effect " { $snippet "( x y -- )" } } { "x" "an object" } { "y" "an object" } { "z" "an object" } }
199 { $description "Call a quotation with three values on the stack, restoring the values when the quotation returns." } ;
202 { $values { "quot" "a quotation with stack effect " { $snippet "( obj -- )" } } { "x" "an object" } { "y" "an object" } }
203 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } "." } ;
205 HELP: if ( cond true false -- )
206 { $values { "cond" "a generalized boolean" } { "true" "a quotation" } { "false" "a quotation" } }
207 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation. Otherwise calls the " { $snippet "true" } " quotation."
209 "The " { $snippet "cond" } " value is removed from the stack before either quotation is called." } ;
212 { $values { "cond" "a generalized boolean" } { "true" "a quotation" } }
213 { $description "If " { $snippet "cond" } " is not " { $link f } ", calls the " { $snippet "true" } " quotation."
215 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
218 { $values { "cond" "a generalized boolean" } { "false" "a quotation" } }
219 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation."
221 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
224 { $values { "cond" "a generalized boolean" } { "true" "a quotation with stack effect " { $snippet "( cond -- )" } } { "false" "a quotation" } }
225 { $description "Alternative conditional form that preserves the " { $snippet "cond" } " value if it is true."
227 "If the condition is true, it is retained on the stack before the " { $snippet "true" } " quotation is called. Otherwise, the condition is removed from the stack and the " { $snippet "false" } " quotation is called."
229 "The following two lines are equivalent:"
230 { $code "X [ Y ] [ Z ] if*" "X dup [ Y ] [ drop Z ] if" } } ;
233 { $values { "cond" "a generalized boolean" } { "true" "a quotation with stack effect " { $snippet "( cond -- )" } } }
234 { $description "Variant of " { $link if* } " with no false quotation."
236 "The following two lines are equivalent:"
237 { $code "X [ Y ] when*" "X dup [ Y ] [ drop ] if" } } ;
240 { $values { "cond" "a generalized boolean" } { "false" "a quotation " } }
241 { $description "Variant of " { $link if* } " with no true quotation."
243 "The following two lines are equivalent:"
244 { $code "X [ Y ] unless*" "X dup [ ] [ drop Y ] if" } } ;
247 { $values { "default" "an object" } { "cond" "a generalized boolean" } { "true" "a quotation with stack effect " { $snippet "( cond -- )" } } { "false" "a quotation with stack effect " { $snippet "( default -- )" } } }
248 { $description "If the condition is " { $link f } ", the " { $snippet "false" } " quotation is called with the " { $snippet "default" } " value on the stack. Otherwise, the " { $snippet "true" } " quotation is called with the condition on the stack."
250 "The following two lines are equivalent:"
251 { $code "[ X ] [ Y ] ?if" "dup [ nip X ] [ drop Y ] if" } } ;
254 { $values { "obj" "an object" } { "quot" "a quotation with stack effect " { $snippet "( obj elt -- )" } } { "elt" "an object" } }
255 { $description "Utility word used to implement curried combinators such as " { $link each-with } " and " { $link map-with } "." } ;
258 { $values { "quot" "a quotation" } }
259 { $description "Calls a quotation, saving the data stack before calling it and restoring it after it returns." } ;
262 { $description "Starts the front-end processor (FEP), which is a low-level debugger which can inspect memory addresses and the like. The FEP is also entered when a critical error occurs." } ;
265 { $values { "n" "an integer exit code" } }
266 { $description "Exits the Factor process." } ;
268 HELP: millis ( -- n )
269 { $values { "n" "an integer" } }
270 { $description "Outputs the number of milliseconds ellapsed since midnight January 1, 1970." } ;
272 HELP: os-env ( key -- value )
273 { $values { "key" "a string" } { "value" "a string" } }
274 { $description "Looks up the value of a shell environment variable." }
276 "This is an operating system-specific feature. On Unix, you can do:"
277 { $example "\"USER\" os-env print" "slava" }
281 { $values { "n" "a positive integer" } }
282 { $description "Outputs the pointer size in bytes of the current CPU architecture." } ;
285 { $values { "?" "a boolean" } }
286 { $description "Tests if Factor is running on 32-bit Windows." } ;
289 { $values { "?" "a boolean" } }
290 { $description "Tests if Factor is running on 64-bit Windows." } ;
295 { $values { "n" "a positive integer" } }
296 { $description "Outputs the number of generations partitioning the heap." } ;
299 { $values { "path" "a path name string" } }
300 { $description "Outputs the path name of the currently running Factor image." } ;
302 HELP: save-image ( path -- )
303 { $values { "path" "a path name string" } }
304 { $description "Saves a snapshot of the heap to the given file, overwriting the file if it already exists." } ;
307 { $description "Saves a snapshot of the heap to the current image file." } ;
311 HELP: (clone) ( obj -- newobj )
312 { $values { "obj" "an object" } { "newobj" "a shallow copy" } }
313 { $description "Outputs a byte-by-byte copy of the given object. User code should call " { $link clone } " instead." } ;
316 { $values { "spec" "an array of class words" } }
317 { $description "Declares that the elements at the top of the stack are instances of the classes in " { $snippet "spec" } "." }
318 { $warning "The compiler blindly trusts declarations, and false declarations can lead to crashes, memory corruption and other undesirable behavior." }
320 "The optimizer cannot do anything with the below code as you can verify with " { $link "ui-dataflow" } ":"
322 "However, if we declare that the top of the stack is a " { $link float } ", then type checks and generic dispatch are eliminated, and the compiler can use unsafe intrinsics:"
323 { $code "{ float } declare 2 + 10 *" }
327 { $values { "array" "an array" } { "n" "a non-negative fixnum" } }
328 { $description "Low-level array length accessor." }
329 { $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is unsafe. It does not check types, so improper use can corrupt memory." } ;
332 { $values { "n" "a non-negative fixnum" } { "array" "an array" } { "elt" "an object" } }
333 { $description "Low-level array element accessor." }
334 { $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory." } ;
337 { $values { "elt" "an object" } { "n" "a non-negative fixnum" } { "array" "an array" } }
338 { $description "Low-level array element mutator." }
339 { $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory." } ;
341 HELP: tag ( object -- n )
342 { $values { "object" "an object" } { "n" "a tag number" } }
343 { $description "Outputs an object's tag number, between zero and one less than " { $link num-tags } ". This is implementation detail and user code should call " { $link class } " instead." }
346 HELP: dispatch ( n array -- )
347 { $values { "n" "a fixnum" } { "array" "an array of quotations" } }
348 { $description "Calls the " { $snippet "n" } "th quotation in the array." }
349 { $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is an implementation detail used by the generic word system to accelerate method dispatch. It does not perform type or bounds checks, and user code should not need to call it directly." } ;
351 HELP: getenv ( n -- obj )
352 { $values { "n" "a non-negative integer" } { "obj" "an object" } }
353 { $description "Reads an object from the Factor runtime's environment table. User code never has to read the environment table directly; instead, use one of the callers of this word." } ;
355 HELP: setenv ( obj n -- )
356 { $values { "n" "a non-negative integer" } { "obj" "an object" } }
357 { $description "Writes an object to the Factor runtime's environment table. User code never has to write to the environment table directly; instead, use one of the callers of this word." } ;
359 HELP: slot ( obj m -- value )
360 { $values { "obj" "an object" } { "m" "a non-negative fixnum" } { "value" "an object" } }
361 { $description "Reads the object stored at the " { $snippet "n" } "th slot of " { $snippet "obj" } "." }
362 { $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it does not perform type or bounds checks, and slot numbers are implementation detail." } ;
364 HELP: set-slot ( value obj n -- )
365 { $values { "value" "an object" } { "obj" "an object" } { "m" "a non-negative fixnum" } }
366 { $description "Writes " { $snippet "value" } " to the " { $snippet "n" } "th slot of " { $snippet "obj" } "." }
367 { $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it does not perform type or bounds checks, and slot numbers are implementation detail." } ;