Debian Codegraph @

doc/libapl.html

raw symbols SCIP
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html>
3 <!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>libAPL</title>
7
8 <meta name="description" content="libAPL">
9 <meta name="keywords" content="libAPL">
10 <meta name="resource-type" content="document">
11 <meta name="distribution" content="global">
12 <meta name="Generator" content="makeinfo">
13 <meta name="viewport" content="width=device-width,initial-scale=1">
14
15 <link href="#Top" rel="start" title="Top">
16 <link href="#Vague-details-of-the-GNU-APL-implementation" rel="next" title="Vague details of the GNU APL implementation">
17 <style type="text/css">
18 <!--
19 a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
20 a.summary-letter {text-decoration: none}
21 blockquote.indentedblock {margin-right: 0em}
22 div.display {margin-left: 3.2em}
23 div.example {margin-left: 3.2em}
24 kbd {font-style: oblique}
25 pre.display {font-family: inherit}
26 pre.format {font-family: inherit}
27 pre.menu-comment {font-family: serif}
28 pre.menu-preformatted {font-family: serif}
29 span.nolinebreak {white-space: nowrap}
30 span.roman {font-family: initial; font-weight: normal}
31 span.sansserif {font-family: sans-serif; font-weight: normal}
32 span:hover a.copiable-anchor {visibility: visible}
33 ul.no-bullet {list-style: none}
34 -->
35 </style>
36
37
38 </head>
39
40 <body lang="en">
41 <h1 class="settitle" align="center">libAPL</h1>
42
43
44
45 <div class="top" id="Top">
46 <div class="header">
47 <p>
48 Next: <a href="#Vague-details-of-the-GNU-APL-implementation" accesskey="n" rel="next">Vague details of the GNU APL implementation</a> &nbsp; </p>
49 </div>
50 <span id="libAPL"></span><h1 class="top">libAPL</h1>
51
52 <p><b>libapl</b> is a C library that was kindly contributed to the GNU APL project by
53 <b>Dr. Dirk Laurie</b>. <b>libapl</b> gives C programs access to GNU APL.
54 </p>
55 <p>Almost everything that a user can do interactively with GNU APL can also be
56 done programmatically with libapl. You can )LOAD and )SAVE workspaces,
57 evaluate APL expression, create Variables, and even call APL primitives
58 directly with values originating from in your C code.
59 </p>
60 <p>The main facilities provided by libapl are listed in the following.
61 </p>
62 <p>Some functions come in two flavours: one whose argument is a (0-terminated and
63 UTF8-encoded) C-string (that is, a <b>const char *</b>), and one whose argument
64 is a <b>const unsigned int *</b> to a 0-terminated array of Unicodes.
65 </p>
66 <dl compact="compact">
67 <dt><span><code>apl_exec(const char * line_utf8)</code></span></dt>
68 <dt><span><code>apl_exec_ucs(const unsigned int * line_ucs)</code></span></dt>
69 <dd><p>pass a line to the interpreter for immediate execution as APL code. For example,
70 </p>
71 <p>apl_exec(&quot;1 2 3 + 4 5 6&quot;)
72 </p>
73 <p>should return the APL vector 5 7 9, which can then be accessed with other
74 libapl functions such as get_rank(), get_axis(), etc.
75 </p>
76 </dd>
77 <dt><span><code>apl_command(const char * line_utf8)</code></span></dt>
78 <dt><span><code>apl_command_ucs(const unsigned int * line_ucs)</code></span></dt>
79 <dd><p>pass an APL command to the command processor and return its output. For example,
80 </p>
81 <p>apl_command(&quot;)CLEAR&quot;)
82 </p>
83 <p>should clear the current workspace and return &quot;CLEAR WS&quot;.
84 </p>
85 </dd>
86 <dt><span><code>APL_value</code></span></dt>
87 <dd><p>APL_value is a convenience typedef for a pointer to an opaque structure Value, supported by over 30 functions allowing one to construct a new Value and to gain access to its rank, shape and ravel. In this document, the terms &quot;Value&quot; and &quot;APL value&quot; are not interchangeable; they refer respectively to a structure and to a pointer.
88 </p>
89 </dd>
90 <dt><span><code>APL_function</code></span></dt>
91 <dd><p>APL_function is a convenience typedef for a pointer to an opaque structure Function, which is a defined APL function or a built-in function of the interpreter. This pointer provides direct access of the eval__XXX() functions that are impemented by the function. In this document, the terms &quot;Function&quot; and &quot;APL function&quot; are not interchangeable; they refer respectively to a structure and to a pointer. Every function implements a (typically small) subset of eval__XXX() functions that differ by the arguments that the take. The XXX stands for the signature of the function, e.g. eval__fun_B for a monadic function, eval__A_fun_B for a dyafic function, and so on. The possible arguments (in that order are: A (left value), L (left function of a dyadic operator), R (right function of an operator), X (axis argument of a function or operator), and B (right value).
92 </p>
93 </dd>
94 <dt><span><code>eval__fun(APL_function fun)</code></span></dt>
95 <dt><span><code>eval__A_fun_B(APL_function fun, APL_value B)</code></span></dt>
96 <dt><span><code>eval__A_fun_B(APL_value A, APL_function fun, APL_value B)</code></span></dt>
97 <dt><span><code>eval__A_L_oper_B(APL_value A, APL_function L, APL_function fun, APL_value B)</code></span></dt>
98 <dt><span><code>eval__A_fun_X_B(APL_value A, APL_function fun, APL_value X, APL_value B)</code></span></dt>
99 <dt><span><code>eval__A_L_oper_R_B(APL_value A, APL_function L, APL_function fun, APL_function R, APL_value B)</code></span></dt>
100 <dt><span><code>eval__A_L_oper_R_X_B(APL_value A, APL_function L, APL_function fun, APL_function R, APL_value X, APL_value B)</code></span></dt>
101 <dt><span><code>eval__fun_B(APL_function fun, APL_value B)</code></span></dt>
102 <dt><span><code>eval__fun_B(APL_function fun, APL_value B)</code></span></dt>
103 <dt><span><code>eval__L_oper_B(APL_function L, APL_function fun, APL_value B)</code></span></dt>
104 <dt><span><code>eval__fun_X_B(APL_function fun, APL_value X, APL_value B)</code></span></dt>
105 <dt><span><code>eval__L_oper_R_B(APL_value A, APL_function L, APL_function fun, APL_function R, APL_value B)</code></span></dt>
106 <dt><span><code>eval__L_oper_R_X_B(APL_function L, APL_function fun, APL_function R, APL_value X, APL_value B)</code></span></dt>
107 <dd><p>These are the possible eval functions. The APL_value returned by an eval__XXX() function shall be released with release_value() by the caller.
108 </p>
109 </dd>
110 <dt><span><code>fix_function(const char ** function_lines_utf8)</code></span></dt>
111 <dt><span><code>fix_function_NL(const char * function_lines_utf8)</code></span></dt>
112 <dd><p>These functions create a new defined APL function in the workspace, which
113 can then be called in APL, or with suitable eval_XXX() functions in C/C++.
114 fix_function() expects an array of strings, one string per APL line, and
115 terminated with NULL. fix_function() is a front-end for fix_function_NL().
116 fix_function_NL() expects a signle string with function lines separated by
117 newline (line feed, 0x0A). In both cases the first line becomes the function
118 header and the subsequent lines become the function body. Existing functions
119 with the same name in the function header will be replaced without warning.
120 Examples:
121 </p>
122 <pre class="verbatim">const char * foo[] = { &quot;Z←A FOO B&quot;, // function header
123 &quot;Z←A ⍴ B&quot;, // one body line
124 NULL // sentinel
125 };
126 fix_function(foo);
127
128 const char * bar = &quot;Z←A BAR B\n&quot; // function header
129 &quot;Z←A ≠ B\n&quot;; // one body line
130 fix_function_NL(bar);
131 </pre>
132 <p>should create dyadic defined functions FOO and BAR. fix_function_NL() is
133 easier to use with e.g. C string literals, while fix_function() is more
134 convenient if (some of) the function lines are being computed.
135 </p>
136 </dd>
137 <dt><span><code>get_var_value(const char * var_name_utf8, const char * loc)</code></span></dt>
138 <dd><p>return an APL value pointing to the contents of a variable in the current workspace.
139 </p>
140 </dd>
141 <dt><span><code>set_var_value(const char * var_name_utf8, const APL_value new_value, const char * loc)</code></span></dt>
142 <dd><p>set the contents of a variable in the workspace to that of the given APL value.
143 </p>
144 </dd>
145 <dt><span><code>expand_LF_to_CRLF</code></span></dt>
146 <dd><p>this function controls whether linefeed (LF) characters shall be expanded to CR/LF on output. The default is no expansion. Please note that LF expansion may be triggered in other places, therefore expand_LF_to_CRLF(0) alone does not guarantee that no CR characters are being printed.
147 </p>
148 </dd>
149 <dt><span><code>SYNC_APL_SCRIPTS</code> and <code>repl</code></span></dt>
150 <dd><p>See <a href="#g_t_0023non_002datomic_002doperations">Non-atomic Operations</a>
151 </p>
152 </dd>
153 </dl>
154
155 <p>No other GNU APL header is exposed.
156 </p>
157 <p>This document will not tell you much about APL. For more details about the language, consult an APL reference manual such as those recommended in the file <code>README-7-more-info</code>, which are probably installed in <code>/usr/share/doc/apl</code> or <code>/usr/local/share/doc/apl</code>.
158 </p>
159 <ul class="section-toc">
160 <li><a href="#Vague-details-of-the-GNU-APL-implementation" accesskey="1">Vague details of the GNU APL implementation</a></li>
161 <li><a href="#Summary-of-functions" accesskey="2">Summary of functions</a></li>
162 <li><a href="#Programming-notes" accesskey="3">Programming notes</a></li>
163 </ul>
164 <hr>
165 <div class="chapter" id="Vague-details-of-the-GNU-APL-implementation">
166 <div class="header">
167 <p>
168 Next: <a href="#Summary-of-functions" accesskey="n" rel="next">Summary of functions</a>, Previous: <a href="#Top" accesskey="p" rel="prev">libAPL</a>, Up: <a href="#Top" accesskey="u" rel="up">libAPL</a> &nbsp; </p>
169 </div>
170 <span id="Vague-details-of-the-GNU-APL-implementation-1"></span><h2 class="chapter">1 Vague details of the GNU APL implementation</h2>
171 <span id="g_t_0023vague_002ddetails_002dof_002dthe_002dgnu_002dapl_002dimplementation"></span><p>Although the implementation is hidden from the API, the programmer needs to know a little about it.
172 </p>
173 <p>Only one workspace, simply known as &quot;the workspace&quot;, is active at any time. The workspace may be cleared, named, saved and restored by calls to <code>apl_command</code>.
174 </p>
175 <p>The workspace contains a collection of symbols of various kinds. Apart from <code>apl_exec</code> and <code>apl_command</code>, which behave as if entered from the keyboard in an interactive APL session, this API gives access only to APL variables, i.e. symbols associated with Values.
176 </p>
177 <p>A Value is a multidimensional array of cells. It has three visible components: rank, shape and ravel.
178 </p>
179 <p>The shape is a vector of integers, giving the number of elements along each axis of the array. The number of shape items is known as the rank. There is an upper bound to the rank, configurable when GNU APL is built, which is displayed as a message by <code>apl_exec(&quot;⎕SYL[7;]&quot;)</code>.
180 </p>
181 <p>The ravel is a vector of cells, accessed in APL by a multi-index but in the API by a single index starting at 0. As one progresses along the ravel, the multi-index is ordered lexicographically, e.g. in a clear workspace, the multi-index of an array of shape <code>2 3</code> would successively be <code>1 1</code>, <code>1 2</code>, <code>1 3</code>, <code>2 1</code>, <code>2 2</code>, <code>2 3</code>. The index origin in APL may be changed by <code>apl_exec(&quot;⎕IO←0&quot;)</code>, but in the API the ravel is always indexed from 0.
182 </p>
183 <p>The number of elements in the ravel is given by the product of the shape items. An empty product is of course equal to 1, thus this calculation is also valid for a scalar, which has rank 0.
184 </p>
185 <p>A cell can hold any of several kinds of objects:
186 </p>
187 <ol>
188 <li> A scalar, i.e. either a number or a single 32-bit Unicode character. The number may be stored internally as a 64-bit integer, a <code>double</code>, or a <code>complex&lt;double&gt;</code>.
189 </li><li> An APL value. This allows nested arrays to be represented.
190 </li><li> None of the above, i.e. information not accessible from the API.
191 </li></ol>
192
193 <p>The API does not give direct access to cell objects. The user must know what is in a particular cell and retrieve it by supplying its position in the ravel, using a specialized access method for cells of that particular type. To this end, the cell type can be queried. This is an integer treated as a bit string. The bits denoting cells accessible from the API have predefined names.
194 </p>
195 <pre class="verbatim">CCT_CHAR = 0x02
196 CCT_POINTER = 0x04
197 CCT_INT = 0x10
198 CCT_FLOAT = 0x20
199 CCT_COMPLEX = 0x40
200 CCT_NUMERIC = CCT_INT | CCT_FLOAT | CCT_COMPLEX
201 </pre>
202 <p>Attempting to retrieve the contents of a cell by the wrong access method is an error that will crash the program.
203 </p>
204 <ul class="section-toc">
205 <li><a href="#Lifespan-of-Values" accesskey="1">Lifespan of Values</a></li>
206 </ul>
207 <hr>
208 <div class="section" id="Lifespan-of-Values">
209 <div class="header">
210 <p>
211 Up: <a href="#Vague-details-of-the-GNU-APL-implementation" accesskey="u" rel="up">Vague details of the GNU APL implementation</a> &nbsp; </p>
212 </div>
213 <span id="Lifespan-of-Values-1"></span><h3 class="section">1.1 Lifespan of Values</h3>
214 <span id="g_t_0023lifespan_002dof_002dvalues"></span><ol>
215 <li> All Values are invisible to the API. Internally, they contain a reference count, and are scheduled for destruction when the reference count reaches zero. The actual destruction might not happen immediately. The fact that one succeeded in accessing a Value does not prove that it is still alive, it merely means that it has not yet been destructed.
216
217 </li><li> All API functions that return an APL value increment the reference count. It is your responsibility to decrement the reference count using <code>release_value</code> when the Value is no longer needed. Failure to do so will cause memory leaks.
218
219 </li><li> The APL value provided in the argument list of <code>res_callback</code> (see <a href="#g_t_0023interface_002dto_002dapl_002dinterpreter">Interface to APL interpreter</a>) has a particularly brief lifespan. The execution of that function is your only chance of accessing it. Its reference count is not increased before the call, so you must not release it.
220
221 </li><li> The type-specific <code>set_</code> functions change one element only. Other references to the Value concerned will also reflect the change; for example, if the APL value was returned by <code>get_var_value</code>, a following call to <code>get_var_value</code> with the same variable name will show the change.
222
223 </li><li> <code>set_value</code> and <code>set_var_value</code> make a new deep copy of a non-scalar Value. The reference count of the original Value is not increased. Cloning (which is deliberately discouraged in the API by not providing a copy constructor) can be simulated with the aid of either of these. The details are left to the persevering user.
224
225 </li></ol>
226
227 <hr>
228 </div>
229 </div>
230 <div class="chapter" id="Summary-of-functions">
231 <div class="header">
232 <p>
233 Next: <a href="#Programming-notes" accesskey="n" rel="next">Programming notes</a>, Previous: <a href="#Vague-details-of-the-GNU-APL-implementation" accesskey="p" rel="prev">Vague details of the GNU APL implementation</a>, Up: <a href="#Top" accesskey="u" rel="up">libAPL</a> &nbsp; </p>
234 </div>
235 <span id="Summary-of-functions-1"></span><h2 class="chapter">2 Summary of functions</h2>
236 <span id="g_t_0023summary_002dof_002dfunctions"></span><p>This section is an aide-memoire, not a manual: consult the comments preceding each function for details. See <a href="#g_t_0023programming_002dnotes">Programming notes</a> for information on the <code>loc</code> parameter.
237 </p>
238 <p>The other parameter values have the following types:
239 </p>
240 <table>
241 <tr><td><code>val</code></td><td>The main APL value</td></tr>
242 <tr><td><code>pval</code></td><td>A secondary APL value</td></tr>
243 <tr><td><code>cval</code></td><td>A 32-bit Unicode character</td></tr>
244 <tr><td><code>ival</code></td><td>A 64-bit integer</td></tr>
245 <tr><td><code>xval</code>,<code>yval</code></td><td>A 64-bit double</td></tr>
246 <tr><td><code>sval</code></td><td>A UTF-8 encoded <code>char*</code></td></tr>
247 <tr><td><code>i</code></td><td>A 64-bit index</td></tr>
248 <tr><td><code>k</code>,<code>n1</code>,<code>n2</code>,<code>n2</code></td><td>A 32-bit index</td></tr>
249 </table>
250
251
252 <ul class="section-toc">
253 <li><a href="#Library-Initialization" accesskey="1">Library Initialization</a></li>
254 <li><a href="#Constructor-functions" accesskey="2">Constructor functions</a></li>
255 <li><a href="#Read-access-to-Values" accesskey="3">Read access to Values</a></li>
256 <li><a href="#Write-access-to-cells" accesskey="4">Write access to cells</a></li>
257 <li><a href="#Interface-to-APL-interpreter" accesskey="5">Interface to APL interpreter</a></li>
258 <li><a href="#Destructor-function" accesskey="6">Destructor function</a></li>
259 <li><a href="#Print-support" accesskey="7">Print support</a></li>
260 </ul>
261 <hr>
262 <div class="section" id="Library-Initialization">
263 <div class="header">
264 <p>
265 Next: <a href="#Constructor-functions" accesskey="n" rel="next">Constructor functions</a>, Up: <a href="#Summary-of-functions" accesskey="u" rel="up">Summary of functions</a> &nbsp; </p>
266 </div>
267 <span id="Library-Initialization-1"></span><h3 class="section">2.1 Library Initialization</h3>
268 <span id="g_t_0023library_002dinitialization"></span>
269 <p>For the correct operation of <b>libapl</b> it is mandatory to first initialize it.
270 This is accomplished by calling the libapl function
271 <b>init_libapl(const char * prog, int su_log)</b> once, and before any other
272 libapl function.
273 </p>
274 <p>The first argument <b>prog</b> is, by convention, the name of the binary program
275 that uses libapl. This name is used in some of the printouts produced by libapl.
276 </p>
277 <p>The second argument <b>su_log</b> controls whether libapl shall emit startup
278 logging messages (su_log ≠ 0) or not (su_log = 0).
279 </p>
280 <p>For example:
281 </p><pre class="verbatim">#include &quot;apl/libapl.h&quot;
282
283 int
284 main(int argc, char * argv[])
285 {
286 init_libapl(argv[0], /* no startup logging */ 0);
287 return apl_exec(&quot;⎕←2 3⍴⍳6&quot;);
288 }
289 </pre>
290 <p>See also section <b>liba[l</b> in the document
291 <b>HOWTOS/APL-Communication-Cookbook.html</b>
292 </p>
293 <hr>
294 </div>
295 <div class="section" id="Constructor-functions">
296 <div class="header">
297 <p>
298 Next: <a href="#Read-access-to-Values" accesskey="n" rel="next">Read access to Values</a>, Previous: <a href="#Library-Initialization" accesskey="p" rel="prev">Library Initialization</a>, Up: <a href="#Summary-of-functions" accesskey="u" rel="up">Summary of functions</a> &nbsp; </p>
299 </div>
300 <span id="Constructor-functions-1"></span><h3 class="section">2.2 Constructor functions</h3>
301 <span id="g_t_0023constructor_002dfunctions"></span><p>Each of these functions returns an APL value and has a name descriptive of its argument list.
302 </p>
303 <p><code>int_scalar(ival,loc)</code>, <code>double_scalar(xval,loc)</code>, <code>complex_scalar(xval,yval,loc)</code> and <code>char_scalar(cval,loc)</code> initialize to a given C value.
304 </p>
305 <p><code>char_vector(sval,loc)</code> initializes from a UTF-8 encoded C string to an array of rank 1 containing Unicode characters.
306 </p>
307 <p><code>apl_scalar(loc)</code>, <code>apl_vector(n1)</code>, <code>apl_matrix(n1,n2)</code> and <code>apl_cube(n1,n2,n3)</code> initialize to arrays of rank 0,1,2,3; <code>apl_value(shape,loc)</code> initializes to an array of arbitrary shape. All cells in these arrays are initialized to 0.
308 </p>
309 <hr>
310 </div>
311 <div class="section" id="Read-access-to-Values">
312 <div class="header">
313 <p>
314 Next: <a href="#Write-access-to-cells" accesskey="n" rel="next">Write access to cells</a>, Previous: <a href="#Constructor-functions" accesskey="p" rel="prev">Constructor functions</a>, Up: <a href="#Summary-of-functions" accesskey="u" rel="up">Summary of functions</a> &nbsp; </p>
315 </div>
316 <span id="Read-access-to-Values-1"></span><h3 class="section">2.3 Read access to Values</h3>
317 <span id="g_t_0023read_002daccess_002dto_002dvalues"></span><p><code>get_rank(val)</code>, <code>get_axis(val,k)</code> and <code>get_element_count(val)</code> give information about the shape,
318 </p>
319 <p><code>get_type(val,i)</code> returns the cell type of a ravel element. The predefined names can be used in e.g. a <code>switch</code> statement on the cell type.
320 </p>
321 <p><code>is_char(val,i)</code>, <code>is_int(val,i)</code>, <code>is_double(val,i)</code>, <code>is_complex(val,i)</code> and <code>is_value(val,i)</code> are conveniently named front-ends to <code>get_type</code> that do not require the user to examine the cell type.
322 </p>
323 <p><code>is_string(val)</code> tests whether the entire value is a simple character vector. If so, <code>print_value_to_string</code> can be used to convert it to a UTF-8 encoded C string.
324 </p>
325 <p><code>get_char(val,i)</code>, <code>get_int(val,i)</code>, <code>get_real(val,i)</code>, <code>get_imag(val,i)</code> and <code>get_value(val,i)</code> retrieve the actual contents of a cell of which the type is already known, if necessary by having called <code>get_type</code> or one of its front-ends. For example <code>get_real</code> can be used if <code>get_type(val,i) &amp; (CCT_FLOAT&nbsp;| CCT_COMPLEX)</code> is nonzero.
326 </p>
327 <hr>
328 </div>
329 <div class="section" id="Write-access-to-cells">
330 <div class="header">
331 <p>
332 Next: <a href="#Interface-to-APL-interpreter" accesskey="n" rel="next">Interface to APL interpreter</a>, Previous: <a href="#Read-access-to-Values" accesskey="p" rel="prev">Read access to Values</a>, Up: <a href="#Summary-of-functions" accesskey="u" rel="up">Summary of functions</a> &nbsp; </p>
333 </div>
334 <span id="Write-access-to-cells-1"></span><h3 class="section">2.4 Write access to cells</h3>
335 <span id="g_t_0023write_002daccess_002dto_002dcells"></span><p>Cells can be accessed only via an APL value pointing to their containing Value.
336 </p>
337 <p><code>set_char(cval,val,i)</code>, <code>set_int(ival,val,i)</code>, <code>set_real(xval,val,i)</code>, <code>set_imag(yval,val,i)</code> and <code>set_value(pval,val,i)</code> replace the contents of cell <code>i</code> of <code>val</code>.
338 </p>
339 <p>It is not possible to change the shape of an APL value.
340 </p>
341 <hr>
342 </div>
343 <div class="section" id="Interface-to-APL-interpreter">
344 <div class="header">
345 <p>
346 Next: <a href="#Destructor-function" accesskey="n" rel="next">Destructor function</a>, Previous: <a href="#Write-access-to-cells" accesskey="p" rel="prev">Write access to cells</a>, Up: <a href="#Summary-of-functions" accesskey="u" rel="up">Summary of functions</a> &nbsp; </p>
347 </div>
348 <span id="Interface-to-APL-interpreter-1"></span><h3 class="section">2.5 Interface to APL interpreter</h3>
349 <span id="g_t_0023interface_002dto_002dapl_002dinterpreter"></span><p><code>set_var_value(name,val,loc)</code> and <code>get_var_value(name,val,loc)</code> save and retrieve values to the workspace under specified names.
350 </p>
351 <p>An external function pointer <code>res_callback</code> is called just before <code>apl_exec</code> exits. To exploit it, assign a suitable user-written function to it, e.g.
352 </p>
353 <pre class="verbatim">/* callback to print every value */
354 static int always_print(const APL_value apl,int committed) {
355 return 1;
356 }
357
358 /* callback to save a copy in the workspace under the name &quot;_&quot; */
359 static int save_to_workspace(const APL_value apl,int committed) {
360 set_var_value(&quot;_&quot;,apl,LOC);
361 return !committed;
362 }
363
364 /* One-off declaration statement, must not be inside a function */
365 result_callback res_callback = always_print_it;
366 ...
367 /* A later assignment statement may be anywhere */
368 res_callback = save_to_workspace;
369 ...
370 res_callback = NULL; /* disables callback feature */
371 </pre>
372 <p>Here <code>apl</code> is the anonymous value to which the APL expression evaluates. You are granted access to it just before its brief lifespan expires. <code>committed</code> is a C boolean (only 0 is false) reporting whether that value was stored to a variable. Your return value is a C boolean telling whether the value should be printed by the APL interpreter.
373 </p>
374 <p>The value <code>*apl</code> (which the API cannot see) will be scheduled for destruction as soon as you exit <code>res_callback</code>. Don&rsquo;t release it yourself.
375 </p>
376 <hr>
377 </div>
378 <div class="section" id="Destructor-function">
379 <div class="header">
380 <p>
381 Next: <a href="#Print-support" accesskey="n" rel="next">Print support</a>, Previous: <a href="#Interface-to-APL-interpreter" accesskey="p" rel="prev">Interface to APL interpreter</a>, Up: <a href="#Summary-of-functions" accesskey="u" rel="up">Summary of functions</a> &nbsp; </p>
382 </div>
383 <span id="Destructor-function-1"></span><h3 class="section">2.6 Destructor function</h3>
384 <span id="g_t_0023destructor_002dfunction"></span><p><code>release_value(val,loc)</code> decrements the reference count of <code>*val</code> as explained in <a href="#g_t_0023lifespan_002dof_002dvalues">Lifespan of Values</a>/
385 </p>
386 <hr>
387 </div>
388 <div class="section" id="Print-support">
389 <div class="header">
390 <p>
391 Previous: <a href="#Destructor-function" accesskey="p" rel="prev">Destructor function</a>, Up: <a href="#Summary-of-functions" accesskey="u" rel="up">Summary of functions</a> &nbsp; </p>
392 </div>
393 <span id="Print-support-1"></span><h3 class="section">2.7 Print support</h3>
394 <span id="g_t_0023print_002dsupport"></span><p><code>print_value(value,file)</code>, <code>print_value_to_string(value)</code> filter an APL value through the APL formatting routines. Their behaviour depends on several system variables, such as <code>⎕FC</code>, <code>⎕PP</code>, <code>⎕PW</code>.
395 </p>
396 <p><code>UTF8_to_Unicode</code>, <code>Unicode_to_UTF8</code> are provided because <code>get_char</code> and <code>set_char</code> work with 32-bit Unicode, whereas many other user interfaces use UTF-8.
397 </p>
398 <hr>
399 </div>
400 </div>
401 <div class="chapter" id="Programming-notes">
402 <div class="header">
403 <p>
404 Previous: <a href="#Summary-of-functions" accesskey="p" rel="prev">Summary of functions</a>, Up: <a href="#Top" accesskey="u" rel="up">libAPL</a> &nbsp; </p>
405 </div>
406 <span id="Programming-notes-1"></span><h2 class="chapter">3 Programming notes</h2>
407 <span id="g_t_0023programming_002dnotes"></span><p>The typical application would start with:
408 </p>
409 <pre class="verbatim">#include &lt;stdio.h&gt;
410 #include &lt;stdint.h&gt;
411 #include &lt;apl/libapl.h&gt;
412 </pre>
413 <p>This interface can be called from C, but since GNU APL is a C++ package. the C++ library must be explicitly loaded, e.g. in Linux:
414 </p>
415 <pre class="verbatim">cc myprog.c -lapl -lstdc++ -o myprog
416 </pre>
417
418 <ul class="section-toc">
419 <li><a href="#The-loc-parameter-and-LOC-macro" accesskey="1">The <code>loc</code> parameter and <code>LOC</code> macro</a></li>
420 <li><a href="#Non_002datomic-Operations" accesskey="2">Non-atomic Operations</a></li>
421 </ul>
422 <hr>
423 <div class="section" id="The-loc-parameter-and-LOC-macro">
424 <div class="header">
425 <p>
426 Next: <a href="#Non_002datomic-Operations" accesskey="n" rel="next">Non-atomic Operations</a>, Up: <a href="#Programming-notes" accesskey="u" rel="up">Programming notes</a> &nbsp; </p>
427 </div>
428 <span id="The-loc-parameter-and-LOC-macro-1"></span><h3 class="section">3.1 The <code>loc</code> parameter and <code>LOC</code> macro</h3>
429 <span id="g_t_0023the_002dloc_002dparameter_002dand_002dloc_002dmacro"></span><p>All the functions that return APL values, as well as <code>release_value</code> and <code>set_var_value</code>, contain a parameter <code>const char* loc</code>. This parameter is used to keep track of changes to a Value and may be displayed by certain debugging services. You can put in anything you like, but most convenient is <code>LOC</code>, a macro that expands to the file name and line number.
430 </p>
431 <hr>
432 </div>
433 <div class="section" id="Non_002datomic-Operations">
434 <div class="header">
435 <p>
436 Previous: <a href="#The-loc-parameter-and-LOC-macro" accesskey="p" rel="prev">The <code>loc</code> parameter and <code>LOC</code> macro</a>, Up: <a href="#Programming-notes" accesskey="u" rel="up">Programming notes</a> &nbsp; </p>
437 </div>
438 <span id="Non_002datomic-Operations-1"></span><h3 class="section">3.2 Non-atomic Operations</h3>
439 <span id="g_t_0023non_002datomic_002doperations"></span><ul class="section-toc">
440 <li><a href="#Architecture-of-the-GNU-APL-interpreter" accesskey="1">Architecture of the GNU APL interpreter</a></li>
441 <li><a href="#Architecture-of-the-libapl-library" accesskey="2">Architecture of the libapl library</a></li>
442 <li><a href="#Atomic-vs_002e-Non_002dAtomic-Operations" accesskey="3">Atomic vs. Non-Atomic Operations</a></li>
443 </ul>
444 <div class="subsection" id="Architecture-of-the-GNU-APL-interpreter">
445 <h4 class="subsection">3.2.1 Architecture of the GNU APL interpreter</h4>
446
447 <p>The &quot;normal&quot; GNU APL interpreter has, simplified, the following architecture:
448 </p>
449 <pre class="verbatim"> ╔═══════════╗ ╔════════════════╗
450 ║ Frontend ║ ║ File-I/O ║
451 ║ ║ .apl ║ ║
452 stdin ─────╢ ┌────┐ ╠═══════════════╣ .apl Scripts ║
453 stdout ─────╢ │REPL│ ║ ║ .xml Snapshots ║
454 ║ └────┘ ║ ║ ║
455 ╚═════╤═════╝ ╚═══════╦════════╝
456 │ ║
457 ╔═════╧═════╗ ║
458 ║ Backend ║ .xml ║
459 ║───────────╠═══════════════════════╝
460 ║ (APL Core)║
461 ╚═══════════╝
462 </pre>
463 <p>The <b>backend</b> handles most of the APL commands and the interpretation of APL programs. By definition (e.g. in the ISO APL standard) the backend works in a strict line-by-line fashion. <b>REPL</b> is an abbreviation for <b>R</b>ead/<b>E</b>valuate/<b>P</b>rint/<b>L</b>oop, a construct present in almost every interactive interpreter.
464 </p>
465 <p>The <b>frontend</b> is responsible for:
466 </p>
467 <ul>
468 <li> pushing individual lines from <b>stdin</b> into the backend, and/or
469 </li><li> chopping .apl scripts into individual lines and pushing them, line-by-line, into the backend, and after that
470 </li><li> displaying the output from the backend (if any) on <b>stdout</b>.
471 </li></ul>
472
473 <p>In the special case of a <b>)COPY</b> command of an .apl file (as opposed to an .xml file) the backend opens the .apl file whereas the frontend then pushes the content of the file into the backend. For that reason, )COPY of an .xml files is atomic in the sense defined below, while )COPY of an .apl file is not. <i>This subtle difference is sometimes missed by libapl users</i>.
474 </p>
475 </div>
476 <div class="subsection" id="Architecture-of-the-libapl-library">
477 <h4 class="subsection">3.2.2 Architecture of the libapl library</h4>
478
479 <p><b>libapl</b> is a thin API towards the backend. It can be used by applications written in other languages (primarily C/C++) to obtain services from the backend. Every such application replaces the frontend and must therefore follow the same as the (now removed) frontend:
480 </p>
481 <pre class="verbatim"> ╔═════════════╗ ╔════════════════╗
482 ║ Application ║ ← ← ║ File-I/O ║
483 ║──────┬──────║ .apl ║ ║
484 ║ │ ╠═══════════════╣ .apl Scripts ║
485 ║ ┌───┴──┐ ║ ║ .xml Snapshots ║
486 ║ │libapl│ ║ ║ ║
487 ╚══╧═══╤══╧═══╝ ╚═══════╦════════╝
488 │ ║
489 ╔══════╧══════╗ ← → → → ║
490 ║ Backend ║ .xml .apl ║
491 ║─────────────╠═══════════════════════╝
492 ║ (APL Core) ║
493 ╚═════════════╝
494 </pre>
495 <p>In particular, every application may or may not need to provide its own REPL loop. This may be tricky for programmers that are not entirely familiar with the internals of the GNU APL code. Therefore libapl also provides functions that simplify the construction of a suitable REPL loop.
496 </p>
497 </div>
498 <div class="subsection" id="Atomic-vs_002e-Non_002dAtomic-Operations">
499 <h4 class="subsection">3.2.3 Atomic vs. Non-Atomic Operations</h4>
500
501 <p>The vast majority of GNU APL commands are <b>atomic</b>, which shall mean that the command is a single line passed to the APL core (by means of the libapl functions <b>apl_command()</b> or <b>apl_command_ucs()</b>) and the result is a (typically short) response string. An atomic command ends before any other command or APL code is being executed.
502 </p>
503 <p>Also, lines entered in immediate execution mode are atomic (unless, of course, they execute (⍎) a non-atomic GNU APL command). Note that ⍎ of commands is a non-standard GNU APL feature.
504 </p>
505 <p>Some commands, most importantly )COPY of .apl files, are not atomic since the (top-level) )COPY command may invoke other commands before it completes. That is:
506 </p>
507 <ul>
508 <li> )COPY of .apl files is non-atomic,
509 </li><li> )COPY of .xml files is atomic,
510 </li><li> The ∇-editor is is non-atomic,
511 </li><li> ⎕FX is atomic, and
512 </li><li> )SAVE, )DUMP, and )DUMP-HTML are atomic.
513 </li></ul>
514
515 <p>As the list above shows, every non-atomic operation has an atomic alternative.
516 </p>
517 <p>It is essential to keep in mind that, in the case of )COPY with .apl files, the )COPY command itself merely opens the .apl file but does NOT pass the lines of the just opened .apl file to the backend. The REPL loop of a &quot;normal&quot; GNU APL interpreter pushes the lines of .apl files into the backeend and displays their result. In contrast, libapl has no REPL loop itself but instead provides a helper macro and a helper function for constructing proper REPL loops in the application that uses libapl.
518 </p>
519 <p>First of all, libapl.h <b>#define</b>s the convenience macro <b>SYNC_APL_SCRIPTS</b> which is a loop around the more fine-grained libapl function <b>repl()</b>:
520 </p>
521 <pre class="verbatim">#define SYNC_APL_SCRIPTS { do ; while(repl(0, 0, 0, 0, 0)); }
522 </pre>
523 <p>The libapl function <b>repl()</b> fetches one line of APL and passes it to the APL core. It returns non-zero as long as there are more APL lines available. The caller of <b>repl()</b> may, primarily for debugging purposes, provide buffers into which <b>repl()</b> will store the input to and the output of the APL core.
524 </p>
525 <p>Normally REPL loops are flat (non-recursive) and run forever. In contrast, SYNC_APL_SCRIPTS is a per file REPL loop that is recursive. If, for example, a )COPY opens an .apl file that contains another )COPY command, then SYNC_APL_SCRIPTS returns only after both files were processed) and stops at the end of the first (top-level) .apl file.
526 </p>
527 <p>The overall purpose and effect of <b>SYNC_APL_SCRIPTS</b> is that all open APL
528 scripts are being executed line-by-line.
529 </p>
530 <p>The rules for <b>SYNC_APL_SCRIPTS</b> and <b>repl()</b> are:
531 </p>
532 <ul>
533 <li> every non-atomic operation requires SYNC_APL_SCRIPTS immediatly after it. For example:
534 <pre class="verbatim"> apl_command(&quot;)COPY ./foo.apl&quot;); /* open the script foo.apl */
535 SYNC_APL_SCRIPTS /* execute lines in foo.apl */
536 </pre>
537 </li><li> <b>SYNC_APL_SCRIPTS</b> does no harm; using it after an atomic command is far better than not using it after a non-atomic command.
538 </li><li> <b>repl()</b> essentially performs one iteration of a REPL loop and therefore
539 SYNC_APL_SCRIPTS is an entire REPL loop.
540 </li><li> <b>repl()</b> returns 0 if there are no more .apl files to process and otherwise an identifier &gt; 0 that identifies an .apl file (in the order discovered).
541 </li><li> the libapl can avoid the need for <b>SYNC_APL_SCRIPTS</b> entirely by using only .xml workspaces instead of .apl scripts.
542 </li><li> the )LOAD command is essentially )CLEAR followed by )COPY, therefore the difference between .apl and .xml files applies as well.
543 </li></ul>
544
545 <p>The declaration of function <b>repl()</b> is:
546 </p>
547 <pre class="verbatim">extern long repl(char * input_buffer, int * input_bufsize,
548 char * output_buffer, int * output_bufsize,
549 LIBAPL_error * error);
550 </pre>
551 <p>and the following rules apply:
552 </p>
553 <ul>
554 <li> a non-zero <b>input_buffer</b> causes <b>repl()</b> to copy the input line that is passed to the APL core into <b>input_buffer</b>. In that case <b>input_bufsize</b> must also be non-zero and point to an integer that is the size of the <b>input_buffer</b>. Many programmers follow the <b>GNU APL Coding Standard</b> which suggests that source code lines should not have more than 79 characters. In that case an input_buffer of 400 bytes should suffice (considering that the input buffer is UTF8-encoded so that an 80-character line may require more than 80 bytes).
555 </li><li> <b>input_bufsize</b> is initially the size of the <b>input_buffer</b>. Calling <b>repl()</b> will update it to the size of the line that was read from some .apl file and passed to the APL core.
556 </li><li> a non-zero <b>output_buffer</b> causes <b>repl()</b> to copy the output line(s) of the APL core into <b>output_buffer</b>. In that case <b>output_bufsize</b> must also be non-zero and point to an integer that is the size of the <b>output_buffer</b>. If <b>⎕PW</b> is known beforehand then <b>4×⎕PW</b> might be a proper value for the size of the <b>output_buffer</b> (considering that the output buffer is UTF8-encoded).
557 </li><li> after <b>repl()</b> returns, the input and output buffers are 0-terminated and the sizes are the numbers of valid bytes (not characters!) in the respective buffer.
558 </li><li> a non-zero <b>LIBAPL_error * error</b> causes <b>repl()</b> to store an error code in that location. The error (if any) refers to <b>repl()</b> itself (possibly LAE_IN_BUFFER_OVERFLOW or LAE_OUT_BUFFER_OVERFLOW if the buffers were truncated). In that case <b>input_bufsize</b> and <b>output_bufsize</b> are the sizes that, similar to <b>snprinf()</b>, <i>would have been</i> returned if the buffers were large enough.
559 </li><li> Errors detected by the APL core are NOT set in <b>*error</b>; the caller may decode <b>output_buffer</b> in order to detect them.
560 </li></ul>
561
562 </div>
563 </div>
564 </div>
565 </div>
566
567
568
569 </body>
570 </html>