New syntax for the <abs> function.
[shapes.git] / doc / parts / syntax / misc.sxml
blob9b94690ee336c60a7cda8541bc3799a3c04bafcc
1 <!-- This file is part of Shapes.                                           -->
2 <!--                                                                        -->
3 <!-- Shapes is free software: you can redistribute it and/or modify         -->
4 <!-- it under the terms of the GNU General Public License as published by   -->
5 <!-- the Free Software Foundation, either version 3 of the License, or      -->
6 <!-- any later version.                                                     -->
7 <!--                                                                        -->
8 <!-- Shapes is distributed in the hope that it will be useful,              -->
9 <!-- but WITHOUT ANY WARRANTY; without even the implied warranty of         -->
10 <!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          -->
11 <!-- GNU General Public License for more details.                           -->
12 <!--                                                                        -->
13 <!-- You should have received a copy of the GNU General Public License      -->
14 <!-- along with Shapes.  If not, see <http://www.gnu.org/licenses/>.        -->
15 <!--                                                                        -->
16 <!-- Copyright 2008 Henrik Tidefelt                                         -->
18 <section id="syntax/misc-expr">
19 <title>Other expressions and expressions in general</title>
20 <top>
21 <p>There are many kinds of expressions.  An expression can be either <em>non-pure</em> or <em>pure</em> depending on whether it interacts with states or not.  An expression may also be <em>immediate</em>, either because it is of a certain kind which is always immediate, or because the user has flagged it to be immediate for some reason.  Expressions that are non-pure or immediate cannot or must not be delayed, and are evaluated in the second phase of code bracket evaluation.  While non-purity is a property that a child can (and generally does) give to its parent in the abstract syntax tree, the immediate flag is never transferred between child and parent.</p>
23 <p>The following breaks down <syntax name="expr" />:</p>
24 <syntax-table>
25 <tr> <td><syntax name="expr" class="new" /></td> <td><bnf>→</bnf></td> <td><inline>( <syntax name="expr" /> )</inline></td></tr>
26 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="constant" /> <bnf>|</bnf> <syntax name="reference" /></inline></td></tr>
27 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="tuple" /> <bnf>|</bnf> <syntax name="structure" /></inline></td></tr>
28 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="function" /> <bnf>|</bnf> <syntax name="procedure" /> <bnf>|</bnf> <syntax name="application" /></inline></td></tr>
29 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="reach-out" /> <bnf>|</bnf> <syntax name="immediate-expr" /> <bnf>|</bnf> <syntax name="tex-expr" /></inline></td></tr>
30 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="dynamic-binding" /> <bnf>|</bnf> <syntax name="with-dynamic" /></inline></td></tr>
31 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="with-escape-continuation" /> <bnf>|</bnf> <syntax name="invoke-escape-continuation" /></inline></td></tr>
32 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="insertion-expr" /> <bnf>|</bnf> <syntax name="empty-expr" /></inline></td></tr>
33 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="unary" /> <bnf>|</bnf> <syntax name="binary-arithmetic" /> <bnf>|</bnf> <syntax name="binary-relational" /></inline></td></tr>
35 <tr> <td><syntax name="reference" class="new" /></td> <td><bnf>→</bnf></td> <td><inline> <syntax name="identifier" /> <bnf>|</bnf> <syntax name="dyn-var" /> <bnf>|</bnf> <syntax name="peek-state" /></inline></td></tr>
36 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="field-ref" /> <bnf>|</bnf> <syntax name="lookup-symbol" /> <bnf>|</bnf> <syntax name="typename" /></inline></td></tr>
38 <tr> <td><syntax name="application" class="new" /></td> <td><bnf>→</bnf></td> <td><inline> <syntax name="basic-call" /> <bnf>|</bnf> <syntax name="proc-call" /> <bnf>|</bnf> <syntax name="mutator-call" /> <bnf>|</bnf> <syntax name="unary-call" /> <bnf>|</bnf> <syntax name="split-call" /></inline></td></tr>
39 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline> <syntax name="basic-cute" /> <bnf>|</bnf> <syntax name="unary-cute" /> <bnf>|</bnf> <syntax name="split-cute" /></inline></td></tr>
41 <tr> <td><syntax name="constant" class="new" /></td> <td><bnf>→</bnf></td> <td><inline><syntax name="float" /> <bnf>|</bnf> <syntax name="length" /> <bnf>|</bnf> <syntax name="string" /> <bnf>|</bnf> <syntax name="boolean" /> <bnf>|</bnf> <syntax name="integer" /> <bnf>|</bnf> <syntax name="symbol" /></inline></td></tr>
43 <tr> <td><syntax name="tuple" class="new" /></td> <td><bnf>→</bnf></td> <td><inline><syntax name="float-pair" /> <bnf>|</bnf> <syntax name="float-triple" /> <bnf>|</bnf> <syntax name="coords-2D" /> <bnf>|</bnf> <syntax name="coords-3D" /></inline></td></tr>
44 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="polar-handle-2D" /> <bnf>|</bnf> <syntax name="corner-point-2D" /></inline></td></tr>
46 <tr> <td><syntax name="unary" class="new" /></td> <td><bnf>→</bnf></td> <td><inline><syntax name="not-expr" /> <bnf>|</bnf> <syntax name="cycle" /> <bnf>|</bnf> <syntax name="abs-call" /></inline></td></tr>
47 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="negation" /> <bnf>|</bnf> <syntax name="relative" /></inline></td></tr>
49 <tr> <td><syntax name="binary-arithmetic" class="new" /></td> <td><bnf>→</bnf></td> <td><inline><syntax name="plus" /> <bnf>|</bnf> <syntax name="minus" /> <bnf>|</bnf> <syntax name="star" /> <bnf>|</bnf> <syntax name="slash" /></inline></td></tr>
50 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="plus-plus" /> <bnf>|</bnf> <syntax name="minus-minus" /></inline></td></tr>
51 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="angle" /> <bnf>|</bnf> <syntax name="projection" /></inline></td></tr>
52 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="ampersand" /> <bnf>|</bnf> <syntax name="compose" /></inline></td></tr>
54 <tr> <td><syntax name="binary-relational" class="new" /></td> <td><bnf>→</bnf></td> <td><inline><syntax name="less" /> <bnf>|</bnf> <syntax name="less-equal" /> <bnf>|</bnf> <syntax name="greater" /> <bnf>|</bnf> <syntax name="greater-equal" /></inline></td></tr>
55 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="equal" /> <bnf>|</bnf> <syntax name="not-equal" /></inline></td></tr>
56 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="and" /> <bnf>|</bnf> <syntax name="or" /> <bnf>|</bnf> <syntax name="xor" /></inline></td></tr>
57 </syntax-table>
58 </top>
60 <section id="syntax/misc-expr/path-constr">
61 <title>Path construction</title>
62 <body>
63 <p>In the end, all paths are piecewise cubic splines.  Parameterizing the splines as Bezier splines, we refer to <em>interpolation points</em> instead of <em>spline coefficients</em>.  In general, the spline has two <em>endpoints</em>, and two <em>intermediate</em> interpolation points.  The two end points may be referred to as the <em>first endpoint</em> and <em>second endpoint</em>, respectively.  The intermediate interpolation points may be referred to as the <em>first intermediate</em> and <em>second intermediate</em> interpolation point, respectively.  The intermediate interpolation points may be omitted as a shorthand for placing them at the neighboring endpoint.</p>
65 <p>The Bezier spline will pass through the enpoints, and be contained in the convex hull of all four interpolation points.  Adjacent splines share one of the endpoints, so that the path becomes continuous.  This makes it convenient to think of the path, not as a sequence of Bezier splines, but as a sequence of points on the path, where each point on the path may have a forward and a backward control point (or <em>handle</em>).  Please refer to the WWW for more details on the splines, for instance <a href="http://en.wikipedia.org/wiki/Bézier_curve">Wikipedia</a>.  Here, we shall focus on how the handles may be laid out.</p>
67 <p>
68         The syntax that constructs path points with handles is:
69         <syntax-table>
70                 <tr> <td><syntax name="path-point-2D" /></td> <td><bnf>::</bnf></td> <td><named-type name="PathPoint2D" /></td></tr>
71                 <tr> <td><syntax name="path-point-2D" class="new" /></td> <td><bnf>→</bnf></td> <td><bnf>&lt;</bnf><named-type name="Coords2D" /><bnf>&gt;</bnf></td></tr>
72                 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><bnf>&lt;</bnf><named-type name="Handle2D" /><bnf>&gt;</bnf> &lt; <bnf>&lt;</bnf><named-type name="PathPoint2D" /><bnf>&gt;</bnf></inline></td></tr>
73                 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><bnf>&lt;</bnf><named-type name="PathPoint2D" /><bnf>&gt;</bnf> &gt; <bnf>&lt;</bnf><named-type name="Handle2D" /><bnf>&gt;</bnf></inline></td></tr>
74                 <tr> <td><named-type name="Handle2D" /></td> <td><bnf>=</bnf></td> <td><named-type name="Coords2D" /> <bnf>|</bnf> <named-type name="PolarHandle2D" /></td></tr>
75         </syntax-table>
76         It is illegal to put a handle on a pathpoint which already has a handle in the same direction.
77 </p>
79 <p>
80         Often, paths are constructed according to the pattern
81 <pre>
82 <syntax name="path-point-2D" /> <bnf>(</bnf> -- <syntax name="path-point-2D" /> <bnf>)*</bnf> <bnf>(</bnf> -- cycle <bnf>)?</bnf>
83 </pre>
84         but this is just a special case of the general
85 </p>
87 <syntax-table>
88 <tr> <td><syntax name="simple-path-2D" /></td> <td><bnf>::</bnf></td> <td><named-type name="SimplePath2D" /></td></tr>
89 <tr> <td><syntax name="simple-path-2D" class="new" /></td> <td><bnf>→</bnf></td> <td><bnf>&lt;</bnf><named-type name="PathPoint2D" /><bnf>&gt;</bnf></td></tr>
90 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><bnf>&lt;</bnf><named-type name="SimplePath2D" /><bnf>&gt;</bnf> -- <bnf>&lt;</bnf><named-type name="SimplePath2D" /><bnf>&gt;</bnf></inline></td></tr>
91 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><syntax name="cycle" /></inline></td></tr>
92 <tr> <td><syntax name="cycle" class="new" /></td> <td><bnf>→</bnf></td> <td><inline><bnf>&lt;</bnf><named-type name="SimplePath2D" /><bnf>&gt;</bnf> -- cycle</inline></td></tr>
93 </syntax-table>
94 <p>It is illegal to close a path that is already closed, or to extend a closed path.</p>
96 <p>What remains to be described regarding simple paths is how the free parts of polar handles are determined.  When all handles have been determined, an <em>elementary</em> path has been obtained, but this concept is insignificant from the user's perspective.  Anyway, most of the computations on paths require an elementary path, and will trigger the following compuation if needed:</p>
97 <ol>
98 <li><p>Find all angles to handles that are determined explicitly.</p></li>
99 <li><p>Propagate all known angles to any free angles on the other side of a path point (taking corner angles into account).</p></li>
100 <li><p>Compute remaining angles based on the path point's position relative its neighboring path points.</p><todo><p>Fill in details!</p></todo></li>
101 <li><p>Compute all distances to handles that are given explicitly.  Note that this requires all angles to be known.</p></li>
102 <li><p>Propagate known distances to any free distances on the other side of a path point.</p></li>
103 <li><p>Use <dynvar name="defaultunit" /> to find one value per remaining free distance.  At path points where only one such value is computed, use it as it is.  At path points where two such values are computed, use the smalles of these values on both sides.</p></li>
104 </ol>
106         Note that the rules above means that there is a subtle difference between the following two path points:
107 <pre>
108 <![CDATA[p1: (1%C^)<(0cm,0cm)>(1%C^)
109 p2: @defaultunit:1%C | (^)<(0cm,0cm)>(^)]]>
110 </pre>
111 </p>
113 <remark>
114 <p>The difference is that <user-binding name="p1" />  will generally get handles of unequal length, while <user-binding name="p2" /> gets handles of equal length.</p>
115 </remark>
117 <example-with-output title="Example" internal-id="example-pathconstruction">
118 <image pdf="features/pathconstruction.pdf" jpg="features/pathconstruction_y_small.jpg" />
119 <source file="features/pathconstruction.shape">
120 <![CDATA[<!--#include depth="0" virtual="$(BUILDDIR)$(EXAMPLES)features/pathconstruction.shape" -->]]>
121 </source>
122 </example-with-output>
124 <p>When it comes to filling paths with color, it becomes necessary to handle collections of simple paths.  Sometimes, composite paths may also serve merely as a container for simple paths.  The ampersand operator is used to construct composite paths:</p>
125 <syntax-table>
126 <tr> <td><syntax name="composite-path-2D" /></td> <td><bnf>::</bnf></td> <td><named-type name="Path2D" /></td></tr>
127 <tr> <td><syntax name="composite-path-2D" class="new" /></td> <td><bnf>→</bnf></td> <td><bnf>&lt;</bnf><named-type name="SimplePath2D" /><bnf>&gt;</bnf></td></tr>
128 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline><bnf>&lt;</bnf><named-type name="CompositePath2D" /><bnf>&gt;</bnf> &amp; <bnf>&lt;</bnf><named-type name="CompositePath2D" /><bnf>&gt;</bnf></inline></td></tr>
129 </syntax-table>
131 <p>Paths in <str-3D /> are constructed in the same way as in <str-2D />, although their path points cannot have polar handles.</p>
133 </body>
134 </section>
136 <section id="syntax/misc-expr/laziness">
137 <title>Laziness control</title>
138 <body>
139 <p>While sequential construction does assure functional binding semantics, even when evaluation is delayed due to laziness, it makes delayed evaluation not completely transparent.  <em>Order of evaluation</em> also becomes important under some circumstances.  Further, prohibiting delayed evaluation may be important for efficiency reasons.  Delayed evaluation is prohibited by flagging an expression as <em>immediate</em>:</p>
140 <syntax-table>
141 <tr> <td><syntax name="immediate-expr" class="new" /></td> <td><bnf>→</bnf></td> <td><inline>!! <syntax name="expr" /></inline></td></tr>
142 </syntax-table>
143 <p>The double exclamation mark is meant to remind that everything involving states — procedures calls (where there is a single exclamation mark) in particular — are never delayed, and therefore does not need to be flagged immediate.</p>
145 <example-with-output title="Example" internal-id="example-evalorder">
146 <source file="features/evalorder.shape">
147 <![CDATA[<!--#include depth="0" virtual="$(BUILDDIR)$(EXAMPLES)features/evalorder.shape" -->]]>
148 </source>
149 <stdout>
150 <![CDATA[<!--#include depth="0" virtual="$(BUILDDIR)$(EXAMPLES)features/evalorder.stdout" -->]]>
151 </stdout>
152 </example-with-output>
154 </body>
155 </section>
157 <section id="syntax/misc-expr/unary">
158 <title>Unary operators</title>
159 <body>
160 <p>The expansions of <syntax name="unary" /> which have not been described elsewhere are given here.  Note that the unary plus and minus signs are completely unrelated operations.  The special syntax for calling the <binding name="abs" /> function is also included here.</p>
161 <syntax-table>
162 <tr> <td><syntax name="negation" class="new" /></td> <td><bnf>→</bnf></td> <td><inline>( - <syntax name="expr" /> )</inline></td></tr>
163 <tr> <td></td> <td><bnf>|</bnf></td> <td><inline>~ <syntax name="expr" /></inline></td></tr>
164 <tr> <td><syntax name="relative" class="new" /></td> <td><bnf>→</bnf></td> <td><inline>( + <syntax name="expr" /> )</inline></td></tr>
165 <tr> <td><syntax name="not-expr" class="new" /></td> <td><bnf>→</bnf></td> <td><inline>not <bnf>&lt;</bnf><named-type name="Boolean" /><bnf>&gt;</bnf></inline></td></tr>
166 <tr> <td><syntax name="abs-call" class="new" /></td> <td><bnf>→</bnf></td> <td><inline>(| <syntax name="expr" /> |)</inline></td></tr>
167 </syntax-table>
168 </body>
169 </section>
171 <section id="syntax/misc-expr/binary">
172 <title>Binary arithmetic operators</title>
173 <body>
174 </body>
175 </section>
177 <section id="syntax/misc-expr/relational">
178 <title>Relational operators</title>
179 <body>
180 </body>
181 </section>
183 </section><!-- end of syntax/misc-expr -->