]> gitweb.factorcode.org Git - factor.git/blob - core/effects/effects-docs.factor
Fixing everything for mandatory stack effects
[factor.git] / core / effects / effects-docs.factor
1 USING: help.markup help.syntax math strings words ;
2 IN: effects
3
4 ARTICLE: "effect-declaration" "Stack effect declaration"
5 "Stack effects of words must be declared, with the exception of words which only push literals on the stack."
6 $nl
7 "Stack effects are declared with the following syntax:"
8 { $code ": sq ( x -- y ) dup * ;" }
9 "A stack effect declaration is written in parentheses and lists word inputs and outputs, separated by " { $snippet "--" } ". Stack effect declarations are read in using a parsing word:"
10 { $subsection POSTPONE: ( }
11 "Stack elements in a stack effect are ordered so that the top of the stack is on the right side. Each value can be named by a data type or description. The following are some examples of value names:"
12 { $table
13     { { { $snippet "?" } } "a boolean" }
14     { { { $snippet "elt" } } "an object which is an element of a sequence" }
15     { { { $snippet "m" } ", " { $snippet "n" } } "an integer" }
16     { { { $snippet "obj" } } "an object" }
17     { { { $snippet "quot" } } "a quotation" }
18     { { { $snippet "seq" } } "a sequence" }
19     { { { $snippet "assoc" } } "an associative mapping" }
20     { { { $snippet "str" } } "a string" }
21     { { { $snippet "x" } ", " { $snippet "y" } ", " { $snippet "z" } } "a number" }
22     { { $snippet "loc" } "a screen location specified as a two-element array holding x and y co-ordinates" }
23     { { $snippet "dim" } "a screen dimension specified as a two-element array holding width and height values" }
24     { { $snippet "*" } "when this symbol appears by itself in the list of outputs, it means the word unconditionally throws an error" }
25 }
26 "The stack effect inferencer verifies stack effect comments to ensure the correct number of inputs and outputs is listed. Value names are ignored; only their number matters. An error is thrown if a word's declared stack effect does not match its inferred stack effect."
27 $nl
28 "Recursive words must declare a stack effect in order to compile. This includes all generic words, due to how delegation is implemented." ;
29
30 ARTICLE: "effects" "Stack effects"
31 "A " { $emphasis "stack effect declaration" } ", for example " { $snippet "( x y -- z )" } " denotes that an operation takes two inputs, with " { $snippet "y" } " at the top of the stack, and returns one output."
32 $nl
33 "Stack effects of words can be declared."
34 { $subsection "effect-declaration" }
35 "Stack effects are first-class, and words for working with them are found in the " { $vocab-link "effects" } " vocabulary."
36 { $subsection effect }
37 { $subsection effect? }
38 "There is a literal syntax for stack objects. It is most often used with " { $link define-declared } "."
39 { $subsection POSTPONE: (( }
40 "Getting a word's declared stack effect:"
41 { $subsection stack-effect }
42 "Converting a stack effect to a string form:"
43 { $subsection effect>string }
44 "Comparing effects:"
45 { $subsection effect-height }
46 { $subsection effect<= }
47 { $see-also "inference" } ;
48
49 ABOUT: "effects"
50
51 HELP: effect
52 { $class-description "An object representing a stack effect. Holds a sequence of inputs, a sequence of outputs and a flag indicating if an error is thrown unconditionally." } ;
53
54 HELP: effect-height
55 { $values { "effect" effect } { "n" integer } }
56 { $description "Outputs the number of objects added to the data stack by the stack effect. This will be negative if the stack effect only removes objects from the stack." } ;
57
58 HELP: effect<=
59 { $values { "eff1" effect } { "eff2" effect } { "?" "a boolean" } }
60 { $description "Tests if " { $snippet "eff1" } " is substitutable for " { $snippet "eff2" } ". What this means is that both stack effects change the stack height by the same amount, the first takes a smaller or equal number of inputs as the second, and either both or neither one terminate execution by throwing an error." } ;
61
62 HELP: effect>string
63 { $values { "effect" effect } { "string" string } }
64 { $description "Turns a stack effect object into a string mnemonic." }
65 { $examples
66     { $example "USING: effects io ;" "1 2 <effect> effect>string print" "( object -- object object )" }
67 } ;
68
69 HELP: stack-effect
70 { $values { "word" word } { "effect/f" "an " { $link effect } " or " { $link f } } }
71 { $description "Outputs the stack effect of a word; either a stack effect declared with " { $link POSTPONE: ( } ", or an inferred stack effect (see " { $link "inference" } "." } ;