Debian Codegraph @

HOWTOs/GNU-APL-Designers-Guide.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
5 <head>
6 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
7 <meta name="generator" content="AsciiDoc 10.1.2" />
8 <title>An Introduction for Designers and Contributors to the GNU APL Interpreter</title>
9 <style type="text/css">
10 /* Shared CSS for AsciiDoc xhtml11 and html5 backends */
11
12 /* Default font. */
13 body {
14 font-family: Georgia,serif;
15 }
16
17 /* Title font. */
18 h1, h2, h3, h4, h5, h6,
19 div.title, caption.title,
20 thead, p.table.header,
21 #toctitle,
22 #author, #revnumber, #revdate, #revremark,
23 #footer {
24 font-family: Arial,Helvetica,sans-serif;
25 }
26
27 body {
28 margin: 1em 5% 1em 5%;
29 }
30
31 a {
32 color: blue;
33 text-decoration: underline;
34 }
35 a:visited {
36 color: fuchsia;
37 }
38
39 em {
40 font-style: italic;
41 color: navy;
42 }
43
44 strong {
45 font-weight: bold;
46 color: #083194;
47 }
48
49 h1, h2, h3, h4, h5, h6 {
50 color: #527bbd;
51 margin-top: 1.2em;
52 margin-bottom: 0.5em;
53 line-height: 1.3;
54 }
55
56 h1, h2, h3 {
57 border-bottom: 2px solid silver;
58 }
59 h2 {
60 padding-top: 0.5em;
61 }
62 h3 {
63 float: left;
64 }
65 h3 + * {
66 clear: left;
67 }
68 h5 {
69 font-size: 1.0em;
70 }
71
72 div.sectionbody {
73 margin-left: 0;
74 }
75
76 hr {
77 border: 1px solid silver;
78 }
79
80 p {
81 margin-top: 0.5em;
82 margin-bottom: 0.5em;
83 }
84
85 ul, ol, li > p {
86 margin-top: 0;
87 }
88 ul > li { color: #aaa; }
89 ul > li > * { color: black; }
90
91 .monospaced, code, pre {
92 font-family: "Courier New", Courier, monospace;
93 font-size: inherit;
94 color: navy;
95 padding: 0;
96 margin: 0;
97 }
98 pre {
99 white-space: pre-wrap;
100 }
101
102 #author {
103 color: #527bbd;
104 font-weight: bold;
105 font-size: 1.1em;
106 }
107 #email {
108 }
109 #revnumber, #revdate, #revremark {
110 }
111
112 #footer {
113 font-size: small;
114 border-top: 2px solid silver;
115 padding-top: 0.5em;
116 margin-top: 4.0em;
117 }
118 #footer-text {
119 float: left;
120 padding-bottom: 0.5em;
121 }
122 #footer-badges {
123 float: right;
124 padding-bottom: 0.5em;
125 }
126
127 #preamble {
128 margin-top: 1.5em;
129 margin-bottom: 1.5em;
130 }
131 div.imageblock, div.exampleblock, div.verseblock,
132 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
133 div.admonitionblock {
134 margin-top: 1.0em;
135 margin-bottom: 1.5em;
136 }
137 div.admonitionblock {
138 margin-top: 2.0em;
139 margin-bottom: 2.0em;
140 margin-right: 10%;
141 color: #606060;
142 }
143
144 div.content { /* Block element content. */
145 padding: 0;
146 }
147
148 /* Block element titles. */
149 div.title, caption.title {
150 color: #527bbd;
151 font-weight: bold;
152 text-align: left;
153 margin-top: 1.0em;
154 margin-bottom: 0.5em;
155 }
156 div.title + * {
157 margin-top: 0;
158 }
159
160 td div.title:first-child {
161 margin-top: 0.0em;
162 }
163 div.content div.title:first-child {
164 margin-top: 0.0em;
165 }
166 div.content + div.title {
167 margin-top: 0.0em;
168 }
169
170 div.sidebarblock > div.content {
171 background: #ffffee;
172 border: 1px solid #dddddd;
173 border-left: 4px solid #f0f0f0;
174 padding: 0.5em;
175 }
176
177 div.listingblock > div.content {
178 border: 1px solid #dddddd;
179 border-left: 5px solid #f0f0f0;
180 background: #f8f8f8;
181 padding: 0.5em;
182 }
183
184 div.quoteblock, div.verseblock {
185 padding-left: 1.0em;
186 margin-left: 1.0em;
187 margin-right: 10%;
188 border-left: 5px solid #f0f0f0;
189 color: #888;
190 }
191
192 div.quoteblock > div.attribution {
193 padding-top: 0.5em;
194 text-align: right;
195 }
196
197 div.verseblock > pre.content {
198 font-family: inherit;
199 font-size: inherit;
200 }
201 div.verseblock > div.attribution {
202 padding-top: 0.75em;
203 text-align: left;
204 }
205 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
206 div.verseblock + div.attribution {
207 text-align: left;
208 }
209
210 div.admonitionblock .icon {
211 vertical-align: top;
212 font-size: 1.1em;
213 font-weight: bold;
214 text-decoration: underline;
215 color: #527bbd;
216 padding-right: 0.5em;
217 }
218 div.admonitionblock td.content {
219 padding-left: 0.5em;
220 border-left: 3px solid #dddddd;
221 }
222
223 div.exampleblock > div.content {
224 border-left: 3px solid #dddddd;
225 padding-left: 0.5em;
226 }
227
228 div.imageblock div.content { padding-left: 0; }
229 span.image img { border-style: none; vertical-align: text-bottom; }
230 a.image:visited { color: white; }
231
232 dl {
233 margin-top: 0.8em;
234 margin-bottom: 0.8em;
235 }
236 dt {
237 margin-top: 0.5em;
238 margin-bottom: 0;
239 font-style: normal;
240 color: navy;
241 }
242 dd > *:first-child {
243 margin-top: 0.1em;
244 }
245
246 ul, ol {
247 list-style-position: outside;
248 }
249 ol.arabic {
250 list-style-type: decimal;
251 }
252 ol.loweralpha {
253 list-style-type: lower-alpha;
254 }
255 ol.upperalpha {
256 list-style-type: upper-alpha;
257 }
258 ol.lowerroman {
259 list-style-type: lower-roman;
260 }
261 ol.upperroman {
262 list-style-type: upper-roman;
263 }
264
265 div.compact ul, div.compact ol,
266 div.compact p, div.compact p,
267 div.compact div, div.compact div {
268 margin-top: 0.1em;
269 margin-bottom: 0.1em;
270 }
271
272 tfoot {
273 font-weight: bold;
274 }
275 td > div.verse {
276 white-space: pre;
277 }
278
279 div.hdlist {
280 margin-top: 0.8em;
281 margin-bottom: 0.8em;
282 }
283 div.hdlist tr {
284 padding-bottom: 15px;
285 }
286 dt.hdlist1.strong, td.hdlist1.strong {
287 font-weight: bold;
288 }
289 td.hdlist1 {
290 vertical-align: top;
291 font-style: normal;
292 padding-right: 0.8em;
293 color: navy;
294 }
295 td.hdlist2 {
296 vertical-align: top;
297 }
298 div.hdlist.compact tr {
299 margin: 0;
300 padding-bottom: 0;
301 }
302
303 .comment {
304 background: yellow;
305 }
306
307 .footnote, .footnoteref {
308 font-size: 0.8em;
309 }
310
311 span.footnote, span.footnoteref {
312 vertical-align: super;
313 }
314
315 #footnotes {
316 margin: 20px 0 20px 0;
317 padding: 7px 0 0 0;
318 }
319
320 #footnotes div.footnote {
321 margin: 0 0 5px 0;
322 }
323
324 #footnotes hr {
325 border: none;
326 border-top: 1px solid silver;
327 height: 1px;
328 text-align: left;
329 margin-left: 0;
330 width: 20%;
331 min-width: 100px;
332 }
333
334 div.colist td {
335 padding-right: 0.5em;
336 padding-bottom: 0.3em;
337 vertical-align: top;
338 }
339 div.colist td img {
340 margin-top: 0.3em;
341 }
342
343 @media print {
344 #footer-badges { display: none; }
345 }
346
347 #toc {
348 margin-bottom: 2.5em;
349 }
350
351 #toctitle {
352 color: #527bbd;
353 font-size: 1.1em;
354 font-weight: bold;
355 margin-top: 1.0em;
356 margin-bottom: 0.1em;
357 }
358
359 div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
360 margin-top: 0;
361 margin-bottom: 0;
362 }
363 div.toclevel2 {
364 margin-left: 2em;
365 font-size: 0.9em;
366 }
367 div.toclevel3 {
368 margin-left: 4em;
369 font-size: 0.9em;
370 }
371 div.toclevel4 {
372 margin-left: 6em;
373 font-size: 0.9em;
374 }
375
376 span.aqua { color: aqua; }
377 span.black { color: black; }
378 span.blue { color: blue; }
379 span.fuchsia { color: fuchsia; }
380 span.gray { color: gray; }
381 span.green { color: green; }
382 span.lime { color: lime; }
383 span.maroon { color: maroon; }
384 span.navy { color: navy; }
385 span.olive { color: olive; }
386 span.purple { color: purple; }
387 span.red { color: red; }
388 span.silver { color: silver; }
389 span.teal { color: teal; }
390 span.white { color: white; }
391 span.yellow { color: yellow; }
392
393 span.aqua-background { background: aqua; }
394 span.black-background { background: black; }
395 span.blue-background { background: blue; }
396 span.fuchsia-background { background: fuchsia; }
397 span.gray-background { background: gray; }
398 span.green-background { background: green; }
399 span.lime-background { background: lime; }
400 span.maroon-background { background: maroon; }
401 span.navy-background { background: navy; }
402 span.olive-background { background: olive; }
403 span.purple-background { background: purple; }
404 span.red-background { background: red; }
405 span.silver-background { background: silver; }
406 span.teal-background { background: teal; }
407 span.white-background { background: white; }
408 span.yellow-background { background: yellow; }
409
410 span.big { font-size: 2em; }
411 span.small { font-size: 0.6em; }
412
413 span.underline { text-decoration: underline; }
414 span.overline { text-decoration: overline; }
415 span.line-through { text-decoration: line-through; }
416
417 div.unbreakable { page-break-inside: avoid; }
418
419
420 /*
421 * xhtml11 specific
422 *
423 * */
424
425 div.tableblock {
426 margin-top: 1.0em;
427 margin-bottom: 1.5em;
428 }
429 div.tableblock > table {
430 border: 3px solid #527bbd;
431 }
432 thead, p.table.header {
433 font-weight: bold;
434 color: #527bbd;
435 }
436 p.table {
437 margin-top: 0;
438 }
439 /* Because the table frame attribute is overridden by CSS in most browsers. */
440 div.tableblock > table[frame="void"] {
441 border-style: none;
442 }
443 div.tableblock > table[frame="hsides"] {
444 border-left-style: none;
445 border-right-style: none;
446 }
447 div.tableblock > table[frame="vsides"] {
448 border-top-style: none;
449 border-bottom-style: none;
450 }
451
452
453 /*
454 * html5 specific
455 *
456 * */
457
458 table.tableblock {
459 margin-top: 1.0em;
460 margin-bottom: 1.5em;
461 }
462 thead, p.tableblock.header {
463 font-weight: bold;
464 color: #527bbd;
465 }
466 p.tableblock {
467 margin-top: 0;
468 }
469 table.tableblock {
470 border-width: 3px;
471 border-spacing: 0px;
472 border-style: solid;
473 border-color: #527bbd;
474 border-collapse: collapse;
475 }
476 th.tableblock, td.tableblock {
477 border-width: 1px;
478 padding: 4px;
479 border-style: solid;
480 border-color: #527bbd;
481 }
482
483 table.tableblock.frame-topbot {
484 border-left-style: hidden;
485 border-right-style: hidden;
486 }
487 table.tableblock.frame-sides {
488 border-top-style: hidden;
489 border-bottom-style: hidden;
490 }
491 table.tableblock.frame-none {
492 border-style: hidden;
493 }
494
495 th.tableblock.halign-left, td.tableblock.halign-left {
496 text-align: left;
497 }
498 th.tableblock.halign-center, td.tableblock.halign-center {
499 text-align: center;
500 }
501 th.tableblock.halign-right, td.tableblock.halign-right {
502 text-align: right;
503 }
504
505 th.tableblock.valign-top, td.tableblock.valign-top {
506 vertical-align: top;
507 }
508 th.tableblock.valign-middle, td.tableblock.valign-middle {
509 vertical-align: middle;
510 }
511 th.tableblock.valign-bottom, td.tableblock.valign-bottom {
512 vertical-align: bottom;
513 }
514
515
516 /*
517 * manpage specific
518 *
519 * */
520
521 body.manpage h1 {
522 padding-top: 0.5em;
523 padding-bottom: 0.5em;
524 border-top: 2px solid silver;
525 border-bottom: 2px solid silver;
526 }
527 body.manpage h2 {
528 border-style: none;
529 }
530 body.manpage div.sectionbody {
531 margin-left: 3em;
532 }
533
534 @media print {
535 body.manpage div#toc { display: none; }
536 }
537
538
539 </style>
540 <script type="text/javascript">
541 /*<![CDATA[*/
542 var asciidoc = { // Namespace.
543
544 /////////////////////////////////////////////////////////////////////
545 // Table Of Contents generator
546 /////////////////////////////////////////////////////////////////////
547
548 /* Author: Mihai Bazon, September 2002
549 * http://students.infoiasi.ro/~mishoo
550 *
551 * Table Of Content generator
552 * Version: 0.4
553 *
554 * Feel free to use this script under the terms of the GNU General Public
555 * License, as long as you do not remove or alter this notice.
556 */
557
558 /* modified by Troy D. Hanson, September 2006. License: GPL */
559 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
560
561 // toclevels = 1..4.
562 toc: function (toclevels) {
563
564 function getText(el) {
565 var text = "";
566 for (var i = el.firstChild; i != null; i = i.nextSibling) {
567 if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
568 text += i.data;
569 else if (i.firstChild != null)
570 text += getText(i);
571 }
572 return text;
573 }
574
575 function TocEntry(el, text, toclevel) {
576 this.element = el;
577 this.text = text;
578 this.toclevel = toclevel;
579 }
580
581 function tocEntries(el, toclevels) {
582 var result = new Array;
583 var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
584 // Function that scans the DOM tree for header elements (the DOM2
585 // nodeIterator API would be a better technique but not supported by all
586 // browsers).
587 var iterate = function (el) {
588 for (var i = el.firstChild; i != null; i = i.nextSibling) {
589 if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
590 var mo = re.exec(i.tagName);
591 if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
592 result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
593 }
594 iterate(i);
595 }
596 }
597 }
598 iterate(el);
599 return result;
600 }
601
602 var toc = document.getElementById("toc");
603 if (!toc) {
604 return;
605 }
606
607 // Delete existing TOC entries in case we're reloading the TOC.
608 var tocEntriesToRemove = [];
609 var i;
610 for (i = 0; i < toc.childNodes.length; i++) {
611 var entry = toc.childNodes[i];
612 if (entry.nodeName.toLowerCase() == 'div'
613 && entry.getAttribute("class")
614 && entry.getAttribute("class").match(/^toclevel/))
615 tocEntriesToRemove.push(entry);
616 }
617 for (i = 0; i < tocEntriesToRemove.length; i++) {
618 toc.removeChild(tocEntriesToRemove[i]);
619 }
620
621 // Rebuild TOC entries.
622 var entries = tocEntries(document.getElementById("content"), toclevels);
623 for (var i = 0; i < entries.length; ++i) {
624 var entry = entries[i];
625 if (entry.element.id == "")
626 entry.element.id = "_toc_" + i;
627 var a = document.createElement("a");
628 a.href = "#" + entry.element.id;
629 a.appendChild(document.createTextNode(entry.text));
630 var div = document.createElement("div");
631 div.appendChild(a);
632 div.className = "toclevel" + entry.toclevel;
633 toc.appendChild(div);
634 }
635 if (entries.length == 0)
636 toc.parentNode.removeChild(toc);
637 },
638
639
640 /////////////////////////////////////////////////////////////////////
641 // Footnotes generator
642 /////////////////////////////////////////////////////////////////////
643
644 /* Based on footnote generation code from:
645 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
646 */
647
648 footnotes: function () {
649 // Delete existing footnote entries in case we're reloading the footnodes.
650 var i;
651 var noteholder = document.getElementById("footnotes");
652 if (!noteholder) {
653 return;
654 }
655 var entriesToRemove = [];
656 for (i = 0; i < noteholder.childNodes.length; i++) {
657 var entry = noteholder.childNodes[i];
658 if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
659 entriesToRemove.push(entry);
660 }
661 for (i = 0; i < entriesToRemove.length; i++) {
662 noteholder.removeChild(entriesToRemove[i]);
663 }
664
665 // Rebuild footnote entries.
666 var cont = document.getElementById("content");
667 var spans = cont.getElementsByTagName("span");
668 var refs = {};
669 var n = 0;
670 for (i=0; i<spans.length; i++) {
671 if (spans[i].className == "footnote") {
672 n++;
673 var note = spans[i].getAttribute("data-note");
674 if (!note) {
675 // Use [\s\S] in place of . so multi-line matches work.
676 // Because JavaScript has no s (dotall) regex flag.
677 note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
678 spans[i].innerHTML =
679 "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
680 "' title='View footnote' class='footnote'>" + n + "</a>]";
681 spans[i].setAttribute("data-note", note);
682 }
683 noteholder.innerHTML +=
684 "<div class='footnote' id='_footnote_" + n + "'>" +
685 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
686 n + "</a>. " + note + "</div>";
687 var id =spans[i].getAttribute("id");
688 if (id != null) refs["#"+id] = n;
689 }
690 }
691 if (n == 0)
692 noteholder.parentNode.removeChild(noteholder);
693 else {
694 // Process footnoterefs.
695 for (i=0; i<spans.length; i++) {
696 if (spans[i].className == "footnoteref") {
697 var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
698 href = href.match(/#.*/)[0]; // Because IE return full URL.
699 n = refs[href];
700 spans[i].innerHTML =
701 "[<a href='#_footnote_" + n +
702 "' title='View footnote' class='footnote'>" + n + "</a>]";
703 }
704 }
705 }
706 },
707
708 install: function(toclevels) {
709 var timerId;
710
711 function reinstall() {
712 asciidoc.footnotes();
713 if (toclevels) {
714 asciidoc.toc(toclevels);
715 }
716 }
717
718 function reinstallAndRemoveTimer() {
719 clearInterval(timerId);
720 reinstall();
721 }
722
723 timerId = setInterval(reinstall, 500);
724 if (document.addEventListener)
725 document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
726 else
727 window.onload = reinstallAndRemoveTimer;
728 }
729
730 }
731 asciidoc.install(4);
732 /*]]>*/
733 </script>
734 </head>
735 <body class="article">
736 <div id="header">
737 <h1>An Introduction for Designers and Contributors to the GNU APL Interpreter</h1>
738 <span id="author">Dr. Jürgen Sauermann, GNU APL</span><br />
739 <div id="toc">
740 <div id="toctitle">Table of Contents</div>
741 </div>
742 </div>
743 <div id="content">
744 <div class="sect1">
745 <h2 id="_abstract">Abstract</h2>
746 <div class="sectionbody">
747 <div class="paragraph"><p>This document contains a number of unrelated hints that may be useful to
748 understanding the GNU APL source code. Its primary purpose is to support
749 the fading memory of the author, but it may be interesting for others as well.
750 In addition to the hints there are also a number of rules. These rules
751 should be observed by those who contribute C/C++ source code to the
752 GNU APL project (as opposed to those that contribute APL code).</p></div>
753 <div class="paragraph"><p>This document is <strong>NOT</strong> aimed at the normal GNU APL user, i.e. at the APL
754 programmer. Those users are referred to the various <strong>README</strong> files in the
755 top-level GNU APL directory.</p></div>
756 </div>
757 </div>
758 <div class="sect1">
759 <h2 id="_the_gnu_apl_build_system">The GNU APL Build System</h2>
760 <div class="sectionbody">
761 <div class="paragraph"><p>All commands described below shall executed in the top-level GNU APL directory.
762 The top-level GNU APL directory is the directory that contains - after
763 unpacking a GNU APL tar file like <strong>apl-2.0.tar.gz</strong> or after fetching the GNU
764 APL sourced with SVN or with git - the file named <strong>configure.ac</strong>.</p></div>
765 <div class="paragraph"><p>GNU APL uses <strong>automake</strong> and <strong>autoconf</strong> to build the interpreter. The build
766 system performs a number of major steps in typically different places and
767 by different persons (C/C++ designer/maintainer vs. user):</p></div>
768 <div class="sect2">
769 <h3 id="_step_1_generate_configure_and_a_makefile_in_for_every_makefile_am">Step 1: Generate ./configure and a Makefile.in for every Makefile.am</h3>
770 <div class="paragraph"><p>Every directory of the GNU APL source tree contains a file named <strong>Makefile.am</strong>.
771 The command <strong>autoreconf</strong>, performed in top-level directory of the GNU APL
772 source tree:</p></div>
773 <div class="listingblock">
774 <div class="content">
775 <pre>
776
777 <code>$ autoreconf</code>
778
779 </pre>
780 </div></div>
781 <div class="paragraph"><p>generates a file named <strong>Makefile.in</strong> for every <strong>Makefile.am</strong>. In addition it
782 creates a script named <strong>configure</strong>. This is normally done by the maintainer
783 and the <strong>Makefile.in</strong> files produced are shipped with GNU APL (tar file or
784 svn/git repository).</p></div>
785 <div class="paragraph"><p>The absolutely cool thing about <strong>autoconf</strong> (as opposed to e.g. <strong>Cmake</strong> or GUI
786 based IDEs) is that <strong>autoconf</strong> and <strong>automake</strong> themselves need not be installed
787 by the end users. The <strong>configure</strong> script produced by <strong>autoreconf</strong> is
788 self-contained and can be executed by the end user to configure different
789 aspects of the GNU APL interpreter. To accommodate this, the different
790 <strong>Makefile.in</strong> files are shipped with GNU APL even though they are somewhat
791 redundant (and, compared to the <strong>Makefile.am</strong>, pretty large).</p></div>
792 <div class="paragraph"><p>In essence <strong>Makefile.am</strong> files are easy to read and maintain by humans, while
793 <strong>Makefile.in*s are easy to decode by machines, in particular by the *configure</strong>
794 script that is produced along with them. In GNU APL the <strong>autoreconf</strong> command
795 produces a more than 1,000 line top-level <strong>Makefile.in</strong> from a 150 line
796 <strong>Makefile.am</strong> and a 25,000 line ./configure script from its 1,000 line
797 <strong>configure.ac</strong></p></div>
798 <div class="admonitionblock">
799 <table><tr>
800 <td class="icon">
801 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
802 </td>
803 <td class="content">If a maintainer adds a new directory to the source tree, then she needs
804 to add the final <strong>Makefile</strong> in the top-level file <strong>configure.ac</strong> (in m4 macro
805 <strong>AC_CONFIG_FILES), otherwise the *Makefile.am</strong> will be ignored.</td>
806 </tr></table>
807 </div>
808 <div class="paragraph"><p>The most important take-away here is that the user or maintainer should never
809 edit <strong>Makefile</strong>s or <strong>Makefile.in</strong>s because doing so is cumbersome,
810 error-prone, and useless since <strong>autoreconf</strong> will overwrite any changes made.</p></div>
811 </div>
812 <div class="sect2">
813 <h3 id="_step_2_generate_a_makefile_for_every_makefile_in">Step 2: Generate a Makefile for every Makefile.in</h3>
814 <div class="paragraph"><p>The next step is to run the <strong>configure</strong> script produced in the previous step.
815 It is performed by the end user (or by the system administrator where
816 appropriate) in the top-level directory of the GNU APL source tree:</p></div>
817 <div class="listingblock">
818 <div class="content">
819 <pre>
820
821 <code>$ ./configure</code>
822
823 </pre>
824 </div></div>
825 <div class="paragraph"><p>The typical end user will run <strong>./configure</strong> without any arguments, while
826 experts may run it with additional arguments (see README-2-configure
827 for details). The primary task of <strong>./configure</strong> is to convert every
828 <strong>Makefile.in</strong> into a corresponding <strong>Makefile</strong>. In doing so, the script tries
829 to determine which and where the libraries and header files used by GNU APL
830 are installed on the user&#8217;s system. The system functions and variables of the
831 final GNU APL interpreter behaves differently depending on the libraries and
832 header files present. For example, <strong>⎕FFT</strong> (Fast Fourier Transforms) depends
833 on library <strong>libfftw3</strong> and if that library (or its header files) are missing
834 then ⎕FFT will raise a syntax error instead of computing the FFT.</p></div>
835 <div class="admonitionblock">
836 <table><tr>
837 <td class="icon">
838 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
839 </td>
840 <td class="content">On typical Debian systems, libraries and header files are often
841 contained in different packages that should all be installed.</td>
842 </tr></table>
843 </div>
844 <div class="paragraph"><p>For example, in the ⎕FFT case there are 3 packages:</p></div>
845 <div class="ulist"><ul>
846 <li>
847 <p>
848 libfftw3-bin # the library itself
849 </p>
850 </li>
851 <li>
852 <p>
853 libfftw3-dev # header files for the library, and
854 </p>
855 </li>
856 <li>
857 <p>
858 libfftw3-doc # documentation related to the library
859 </p>
860 </li>
861 </ul></div>
862 <div class="paragraph"><p>For ⎕FFT to work the first two are needed and the third does not hurt.
863 Most of the other libraries follow the same naming scheme for Debian packages.</p></div>
864 <div class="paragraph"><p>The configure script produces a lot of output that is useful if a library or
865 its header file(s) are missing. The final result of the decisions made by
866 configure is collected in file <strong>config.h</strong> so that the compiler can adapt
867 itself to the presence or absence of libraries and their header files.</p></div>
868 <div class="paragraph"><p>If a library is not detected even though it is installed then either:</p></div>
869 <div class="ulist"><ul>
870 <li>
871 <p>
872 the test performed by ./configure is faulty and then either configure.ac
873 or else a more complex test file in sub-directory <strong>m4</strong> need to be fixed, or
874 </p>
875 </li>
876 <li>
877 <p>
878 the library only lives in a non-standard location (e.g. below the user&#8217;s
879 home and then ./configure hopefully provides ./configure option to
880 specify that location.
881 </p>
882 </li>
883 </ul></div>
884 </div>
885 <div class="sect2">
886 <h3 id="_step_3_compile_and_link_the_gnu_apl_interpreter">Step 3: Compile and Link the GNU APL Interpreter</h3>
887 <div class="paragraph"><p>The final step is to compile the interpreter:</p></div>
888 <div class="listingblock">
889 <div class="content">
890 <pre>
891
892 <code>$ make</code>
893
894 </pre>
895 </div></div>
896 <div class="paragraph"><p>The code base is quite large, therefore one may prefer to compile several
897 sources in parallel. For example, on an 8 core CPU:</p></div>
898 <div class="listingblock">
899 <div class="content">
900 <pre>
901
902 <code>make -j 7</code>
903
904 </pre>
905 </div></div>
906 <div class="paragraph"><p>leaving one core for other purposes. If the compilation is successful then the
907 interpreter is named <strong>apl</strong> in directory <strong>src</strong>, i.e. <strong>src/apl</strong>. During
908 development, the interpreter can be started from there without installing it:</p></div>
909 <div class="listingblock">
910 <div class="content">
911 <pre>
912
913 <code>$ src/apl</code>
914
915 </pre>
916 </div></div>
917 <div class="paragraph"><p>After the development is done, the interpreter is installed properly.</p></div>
918 <div class="admonitionblock">
919 <table><tr>
920 <td class="icon">
921 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
922 </td>
923 <td class="content">The steps described only require write permission in and below the
924 top-level directory, for instance below the user&#8217;s $HOME directory.
925 The next step requires write permissions in directories that are normally not
926 writable by ordinary users.</td>
927 </tr></table>
928 </div>
929 </div>
930 <div class="sect2">
931 <h3 id="_step_4_install_the_gnu_apl_interpreter">Step 4: Install the GNU APL Interpreter</h3>
932 <div class="paragraph"><p>Installation of the interpreter must be performed by root:</p></div>
933 <div class="listingblock">
934 <div class="content">
935 <pre>
936
937 <code>$ sudo make install</code>
938
939 </pre>
940 </div></div>
941 <div class="admonitionblock">
942 <table><tr>
943 <td class="icon">
944 <img src="/usr/share/asciidoc/icons/caution.png" alt="Caution" />
945 </td>
946 <td class="content">A common mistake (by the author) is to try to install the interpreter
947 after the previous step has failed. The build system will then try to
948 recompile missing files, but the resulting files will now be owned by root
949 and will no longer be writable by the user. To fix this:</td>
950 </tr></table>
951 </div>
952 <div class="listingblock">
953 <div class="content">
954 <pre>
955
956 <code>$ sudo chown -R username.username . # assume user is 'username'</code>
957
958 </pre>
959 </div></div>
960 <div class="paragraph"><p>The <strong>make install</strong> installs a few files (all owned by root, but with read
961 permissions for everybody):</p></div>
962 <div class="ulist"><ul>
963 <li>
964 <p>
965 The APL interpreter itself in <strong>/usr/local/bin/apl</strong>,
966 </p>
967 </li>
968 <li>
969 <p>
970 interpreter-related header files <strong>/usr/local/include/apl</strong>,
971 </p>
972 </li>
973 <li>
974 <p>
975 interpreter-related libraries <strong>/usr/local/lib/apl</strong>,
976 </p>
977 </li>
978 <li>
979 <p>
980 an info file in <strong>/usr/local/share/info/apl.info</strong>,
981 </p>
982 </li>
983 <li>
984 <p>
985 a man page in <strong>/usr/local/share/man/man1/apl.1</strong>
986 </p>
987 </li>
988 </ul></div>
989 <div class="paragraph"><p>The default installation prefix <strong>/usr/local</strong> can be changed with <strong>./configure</strong>
990 options.</p></div>
991 </div>
992 <div class="sect2">
993 <h3 id="_summary">Summary</h3>
994 <div class="paragraph"><p>The following picture illustrates the different steps to build the GNU APL
995 interpreter and the files involved:</p></div>
996 <div class="listingblock">
997 <div class="content">
998 <pre>
999
1000 <code> ┌─────────────┐
1001 ┌┴────────────┐│ ┌──────────────┐
1002 ┌┴────────────┐├┘ │ configure.ac │ autoreconf (GNU APL designer)
1003 │ Makefile.am ├┘ └──────────────┘
1004 └─────────────┘ ↓
1005 ↓ ↓ ──────────────────────────
1006 ┌─────────────┐ ↓
1007 ┌┴────────────┐│ ┌───────────┐
1008 ┌┴────────────┐├┘ │ configure │
1009 │ Makefile.in ├┘ └───────────┘
1010 └─────────────┘
1011 ↓ ./configure (User)
1012 ┌──────────┐ ┌──────────┐
1013 ┌┴─────────┐│ ┌┴─────────┐│
1014 ┌┴─────────┐├┘ ┌┴─────────┐├┘
1015 │ Makefile ├┘ │ src/*.cc ├┘
1016 └──────────┘ └──────────┘
1017 ↓ ──────────────────────────
1018 ┌─────────┐
1019 │ src/apl │ make (User)
1020 └─────────┘
1021 ↓ ──────────────────────────
1022 ╔════════════════════╗
1023 ║ /usr/local/bin/apl ║ make install (root)
1024 ╚════════════════════╝
1025 ↓ ──────────────────────────</code>
1026
1027 </pre>
1028 </div></div>
1029 </div>
1030 <div class="sect2">
1031 <h3 id="_special_make_targets">Special make targets</h3>
1032 <div class="paragraph"><p>The typical user shall simply run <em>make</em> in the top-level directory.</p></div>
1033 <div class="paragraph"><p>For experts there are some more make targets (defined in <strong>Makefile.incl</strong>),
1034 again with <strong>make</strong> run in the top-level directory, unless noted otherwise.</p></div>
1035 </div>
1036 <div class="sect2">
1037 <h3 id="_make_help">make help</h3>
1038 <div class="paragraph"><p>Shows all top-level make targets:</p></div>
1039 <div class="listingblock">
1040 <div class="content">
1041 <pre>
1042
1043 <code>In addition to the standard make targets (all, install, clean, ...),
1044 the following make targets may be supported (at this level):
1045
1046 make help - print this text
1047 make DIST - dist + a Makefile that calls ./configure
1048 make DOXY - create Doxygen documentation
1049 make RPM - create (source and binary) RPMs
1050 make DEB - create Debian packages
1051
1052 make SVNUP - update from SVN and ./configure
1053
1054 NOTE: The RPM and DEB targets may fail because they require additional tools
1055 that may not be present on your machine. Don't worry if that happens, unless
1056 you really need the RPM and/or Debian packages.
1057
1058 The following targets are shortcuts for lazy developers (like the GNU APL
1059 author) and are not very useful for normal users:
1060
1061 make develop - enable full dependency tracking
1062 make gprof - make develop + enable gprof profiling
1063 make python - make develop + build python module
1064 make parallel - enable multi-core APL (buggy and experimental!)
1065 make parallel1 - make parallel for benchmarking
1066 make VPATH_clean - prepare for VPATH build</code>
1067
1068 </pre>
1069 </div></div>
1070 </div>
1071 <div class="sect2">
1072 <h3 id="_make_test">make test</h3>
1073 <div class="paragraph"><p>This make target is only available in sub-directory <strong>src</strong>. It builds the
1074 interpreter and then runs all testcases (= files with extension <strong>.tc</strong>)
1075 that are present in directory <strong>src/testcases</strong>. The total result of all
1076 testcases is stored in <strong>src/testcases/summary.log</strong>, and every testcase
1077 <strong>x</strong> produces a log file <strong>src/testcases/x.tc.log</strong> with details of the test
1078 execution.</p></div>
1079 </div>
1080 <div class="sect2">
1081 <h3 id="_make_apl_lines">make apl.lines</h3>
1082 <div class="paragraph"><p>This make target is only available in sub-directory <strong>src</strong>. It produces a file
1083 named <strong>src/apl.lines</strong> and must be run right after building the interpreter.
1084 When the interpreter produces a stack trace (i.e. it lists the C++ call stack)
1085 then:</p></div>
1086 <div class="ulist"><ul>
1087 <li>
1088 <p>
1089 If the <strong>apl.lines</strong> file is absent or older than the interpreter, the stack
1090 trace will show function names and code offsets in hex. For short functions
1091 that is sort of OK, but for longer ones the hex numbers can make it difficult
1092 to find the exact location of the fault that has triggered the stack trace.
1093 </p>
1094 </li>
1095 <li>
1096 <p>
1097 If the <strong>apl.lines</strong> file is up-to-date (i.e. <strong>make apl.lines</strong> was run after
1098 the interpreter binary was produced), then the stack traces will show source
1099 file line numbers instead of hex numbers. Producing <strong>apl.lines</strong> takes quite
1100 a while, therefore it is not built automatically.
1101 </p>
1102 </li>
1103 </ul></div>
1104 </div>
1105 <div class="sect2">
1106 <h3 id="_make_gen_gen2">make gen / gen2</h3>
1107 <div class="paragraph"><p>These make targets are only available in sub-directory <strong>tools</strong>. Each produces
1108 the file <strong>src/Prefix.def</strong>. After every new token it reads, the interpreter has
1109 to decide if (and which) a new reducible phrase is present. This check is
1110 executed in the inner loop of the interpreter, therefore the efficiency
1111 of this check is crucial for the performance of the interpreter.</p></div>
1112 <div class="ulist"><ul>
1113 <li>
1114 <p>
1115 GNU APL uses <strong>tools/phrase_gen</strong> to produce <strong>src/Prefix.def</strong>.
1116 </p>
1117 </li>
1118 <li>
1119 <p>
1120 <strong>src/Prefix.def</strong> is therefore automatically generated and <strong>#included</strong> in
1121 <strong>Prefix.hh</strong> and in <strong>Prefix.cc</strong> (= the interpreter source code that performs
1122 the phrase matching).
1123 </p>
1124 </li>
1125 <li>
1126 <p>
1127 The <strong>phrase_gen</strong> tool can produce two types of code:
1128 </p>
1129 <div class="ulist"><ul>
1130 <li>
1131 <p>
1132 either a collision-free hash table (the default),
1133 </p>
1134 </li>
1135 <li>
1136 <p>
1137 or else a search tree.
1138 </p>
1139 </li>
1140 </ul></div>
1141 </li>
1142 <li>
1143 <p>
1144 After a <strong>make gen</strong> the next compiler build will use the hash table, while
1145 after *make gen2 it will use a search tree. Performance tests of both
1146 alternatives have shown only a marginal performance difference between
1147 both methods, but the technique as such might be of interest.
1148 </p>
1149 </li>
1150 </ul></div>
1151 </div>
1152 </div>
1153 </div>
1154 <div class="sect1">
1155 <h2 id="_source_code_documentation">Source Code Documentation</h2>
1156 <div class="sectionbody">
1157 <div class="paragraph"><p>GNU APL is fully documented with <strong>Doxygen</strong>. The term <strong>fully</strong> means that every
1158 class member has a (typically rather brief) Doxygen description and <strong>not</strong> that
1159 the description is exhaustive. The purpose is primarily to be a memory aid for
1160 browsing the source code.</p></div>
1161 <div class="paragraph"><p>The entire <strong>Doxygen</strong> documentation of GNU APL can, provided that <strong>Doxygen</strong>
1162 itself is installed, be produced like this:</p></div>
1163 <div class="listingblock">
1164 <div class="content">
1165 <pre>
1166
1167 <code>make DOXY</code>
1168
1169 </pre>
1170 </div></div>
1171 <div class="paragraph"><p>The result is around 6,000 files in directory <strong>html</strong>. After generating them,
1172 the files can be browsed with e.g.</p></div>
1173 <div class="listingblock">
1174 <div class="content">
1175 <pre>
1176
1177 <code>firefox html/index.html</code>
1178
1179 </pre>
1180 </div></div>
1181 <div class="paragraph"><p>or with some other browser. The <strong>Doxygen</strong> documentation is a good starting
1182 point for those that want to get a quick overview of the GNU APL source code.</p></div>
1183 </div>
1184 </div>
1185 <div class="sect1">
1186 <h2 id="_source_code_file_types">Source Code File Types</h2>
1187 <div class="sectionbody">
1188 <div class="paragraph"><p>The <strong>Makefile</strong> files belong to the build system and were explained above.
1189 What remains are:</p></div>
1190 <div class="ulist"><ul>
1191 <li>
1192 <p>
1193 C source files (file extension <strong>.c</strong>) are almost never used,
1194 </p>
1195 </li>
1196 <li>
1197 <p>
1198 C header files (file extension <strong>.h</strong>) are almost never used, a noteworthy
1199 exception is <strong>config.h</strong> which is produced by the <strong>./configure</strong> script,
1200 </p>
1201 </li>
1202 <li>
1203 <p>
1204 C++ source files for inline functions (file extension <strong>.icc</strong>),
1205 </p>
1206 </li>
1207 <li>
1208 <p>
1209 C++ source files (file extension <strong>.cc</strong>),
1210 </p>
1211 </li>
1212 <li>
1213 <p>
1214 C++ header files (file extension <strong>.hh</strong>), and
1215 </p>
1216 </li>
1217 <li>
1218 <p>
1219 pure macro files (file extension <strong>.def</strong>).
1220 </p>
1221 </li>
1222 </ul></div>
1223 <div class="paragraph"><p>By and large, the rules for C and C++ source and header files are:</p></div>
1224 <div class="ulist"><ul>
1225 <li>
1226 <p>
1227 Small and many rather than large and few (aka. modularity). Early versions
1228 of GNU APL did not follow this rule and some left-overs still do not (but
1229 those will be further modularized in the future).
1230 </p>
1231 </li>
1232 <li>
1233 <p>
1234 One class per file; the file name reflects the class name. Exceptions are:
1235 </p>
1236 </li>
1237 <li>
1238 <p>
1239 classes that are closely related (e.g. classes <strong>Token</strong> and <strong>Token_string</strong>).
1240 </p>
1241 </li>
1242 <li>
1243 <p>
1244 The .cc and .hh file names are the same, except for the file name extension
1245 and classes with a small (say, &lt; 100 lines) source footprint (e.g. those in
1246 PrimitiveFunction.hh and PrimitiveFunction.cc).
1247 </p>
1248 </li>
1249 <li>
1250 <p>
1251 C++ declarations can be made in the .cc file if their scope is local to the
1252 .cc file (like static declarations in C), otherwise they go into the
1253 corresponding .hh file.
1254 </p>
1255 </li>
1256 </ul></div>
1257 <div class="sect2">
1258 <h3 id="_def_files">.def Files</h3>
1259 <div class="paragraph"><p>C/C++ source files primarily contain C/C++ code and occasionally declarations.
1260 C/C++ header files primarily contain C/C++ declarations and often (inline)
1261 C/C++ code. In contrast, .def files contain neither C/C++ code nor
1262 C/C++ declarations. They only contain macro calls whereby the definition of
1263 the macro is defined outside the .def file. Currently there are about 20 .def
1264 files in the GNU APL source code so that they deserve an explanation.</p></div>
1265 <div class="sect3">
1266 <h4 id="_why_def_files">Why .def Files?</h4>
1267 <div class="paragraph"><p>Suppose we have a number of integer error codes:</p></div>
1268 <div class="listingblock">
1269 <div class="content">
1270 <pre>
1271
1272 <code> enum ErrorCode
1273 {
1274 NO_ERROR = 0,
1275 SYNTAX_ERROR = 1,
1276 RANK_ERROR = 2,
1277 LENGTH_ERROR = 4
1278 };</code>
1279
1280 </pre>
1281 </div></div>
1282 <div class="paragraph"><p>Sooner or later we need a string that describes the error in a human readable
1283 format. This requires a mapping between integer error codes and string
1284 literals. There are 2 standard methods in C/C++ to define such a mapping.</p></div>
1285 </div>
1286 <div class="sect3">
1287 <h4 id="_method_1_struct_definition">Method 1: struct definition</h4>
1288 <div class="listingblock">
1289 <div class="content">
1290 <pre>
1291
1292 <code>struct _error_map
1293 {
1294 ErrorCode ecode;
1295 const char * ename;
1296 } error_map =
1297 {
1298 { NO_ERROR, "OK" },
1299 { SYNTAX_ERROR, "Syntax Error" },
1300 { RANK_ERROR, "Rank Error" },
1301 { LENGTH_ERROR, "Length Error" }
1302 } error_map;
1303
1304 const char *
1305 get_error_string(ErrorCode ec)
1306 {
1307 return error_map[ec];
1308 }</code>
1309
1310 </pre>
1311 </div></div>
1312 </div>
1313 <div class="sect3">
1314 <h4 id="_method_2_switch_statement">Method 2: switch statement</h4>
1315 <div class="listingblock">
1316 <div class="content">
1317 <pre>
1318
1319 <code>const char *
1320 get_error_string(ErrorCode ec)
1321 {
1322 switch(ec)
1323 {
1324 case NO_ERROR: return "OK";
1325 case SYNTAX_ERROR: return "Syntax Error";
1326 case RANK_ERROR: return "Rank Error";
1327 case LENGTH_ERROR: return "Length Error";
1328 }
1329 return "Unknown error";
1330 }</code>
1331
1332 </pre>
1333 </div></div>
1334 <div class="paragraph"><p>Now suppose we add another ErrorCode <strong>DOMAIN_ERROR</strong> to the <strong>enum ErrorCode</strong>.
1335 Suppose further that we add the new error code to the enum but not to the
1336 <strong>get_error_string()</strong> function. This is a fairly common mistake, in particular
1337 because <strong>ErrorCode</strong> is usually declared in a <strong>.hh</strong> file and
1338 <strong>get_error_string()</strong> is defined in the corresponding <strong>.cc</strong> file. In that case
1339 Method 1 will most likely produce a segmentation fault at runtime, which is
1340 about the worst case that may happen. &lt;Method 2 is a little better because a
1341 smart compiler like g++ will issue a warning in this case. Both methods are,
1342 however, quite inconvenient for the C/C++ programmer, in particular if many
1343 such mappings are needed in a larger program.</p></div>
1344 <div class="paragraph"><p>In GNU APL this problem is solved in the following way:</p></div>
1345 <div class="ulist"><ul>
1346 <li>
1347 <p>
1348 #define a macro that expresses the relationship (between integer error codes
1349 and C/C++ literals for the corresponding error text in our example), and
1350 </p>
1351 </li>
1352 <li>
1353 <p>
1354 call that macro, typically in several different files with the macro
1355 defined differently. Calling the macro is performed by #including
1356 the .def file. In our example:
1357 </p>
1358 </li>
1359 </ul></div>
1360 <div class="paragraph"><p>In e.g. <strong>errors.def</strong>:</p></div>
1361 <div class="listingblock">
1362 <div class="content">
1363 <pre>
1364
1365 <code> error_def(NO_ERROR , 0, OK )
1366 error_def(SYNTAX_ERROR , 1, Syntax Error )
1367 error_def(RANK_ERROR: 2, Rank Error )
1368 error_def(LENGTH_ERROR: 3, Length Error )
1369
1370 #undef error_def /* to detect missing definitions of the error_def() macro */</code>
1371
1372 </pre>
1373 </div></div>
1374 <div class="paragraph"><p>In, say, <strong>errors.hh</strong>:</p></div>
1375 <div class="listingblock">
1376 <div class="content">
1377 <pre>
1378
1379 <code> enum ErrorCode
1380 {
1381 #define error_def(enum_name, enum_value, _error_string) \
1382 enum_name = enum_value,
1383 #include "errors.def"
1384 };</code>
1385
1386 </pre>
1387 </div></div>
1388 <div class="paragraph"><p>And finally in <strong>errors.cc</strong>:</p></div>
1389 <div class="listingblock">
1390 <div class="content">
1391 <pre>
1392
1393 <code>const char *
1394 get_error_string(ErrorCode ec)
1395 {
1396 switch(ec)
1397 {
1398 #define error_def(enum_name, _enum_value, error_string) \
1399 case enum_name: return #error_string;
1400 #include "errors.def"
1401 }
1402 return "Unknown error";
1403 }</code>
1404
1405 </pre>
1406 </div></div>
1407 <div class="paragraph"><p>Note the following:</p></div>
1408 <div class="ulist"><ul>
1409 <li>
1410 <p>
1411 the macro invocations of <strong>error_def()</strong> (in the <strong>.def</strong> file) have more
1412 arguments than are used in their macro definitions (in the <strong>.hh</strong> or <strong>.cc</strong>
1413 files). GNU APL has a naming convention that unused macro arguments of
1414 macros (which are typical for the <strong>.cc</strong> and <strong>.hh</strong> files) start with
1415 underscore (_). For this reason the argument lists of the #defines above
1416 differ slightly.
1417 </p>
1418 </li>
1419 <li>
1420 <p>
1421 <strong>error_string</strong> is not quoted in <strong>errors.def</strong> even though <strong>get_error_string()</strong>
1422 returns a const char *. The quoting is performed by # in #error_string
1423 (a preprocessor feature).
1424 </p>
1425 </li>
1426 <li>
1427 <p>
1428 the example above uses a simple 1:1 relation between two types. Some <strong>.def</strong>
1429 files use more than two sides, e.g. numeric IDs, string names, and help
1430 texts.
1431 </p>
1432 </li>
1433 </ul></div>
1434 </div>
1435 </div>
1436 <div class="sect2">
1437 <h3 id="_rules_for_def_files">Rules for .def Files</h3>
1438 <div class="ulist"><ul>
1439 <li>
1440 <p>
1441 The <strong>.def</strong> file should <strong>#undef</strong> all macros that it uses. This is a safeguard
1442 against forgetting to <strong>#define</strong> a macro before #including the <strong>.def</strong> file
1443 (this may or may not be detectable by the compiler).
1444 </p>
1445 </li>
1446 <li>
1447 <p>
1448 A <strong>.def</strong> file should normally not use more than one macro. An exception
1449 occurs when macros are very similar (e.g. SystemVariable.def with macro
1450 </p>
1451 </li>
1452 <li>
1453 <p>
1454 ro_sv_def* (for read-only system variables), <strong>rw_sv_def</strong> (for read/write
1455 system variables), and <strong>sf_def</strong> for system functions).
1456 </p>
1457 </li>
1458 <li>
1459 <p>
1460 item separators such as , in enums or ; in case statements are contained in
1461 the macro definitions (i.e. in the <strong>.hh</strong> or <strong>.cc</strong> file) and not in the macro
1462 call (i.e. not in the <strong>.def</strong> file). This is because the separators may differ
1463 for different macros.
1464 </p>
1465 </li>
1466 </ul></div>
1467 <div class="paragraph"><p>The most important take-aways here are:</p></div>
1468 <div class="ulist"><ul>
1469 <li>
1470 <p>
1471 <strong>.def</strong> files contain only macro invocations,
1472 </p>
1473 </li>
1474 <li>
1475 <p>
1476 <strong>.def</strong> files are #included in several files and almost certainly the macro
1477 definitions (before #including a <strong>.def</strong> file) differ between the files,
1478 </p>
1479 </li>
1480 <li>
1481 <p>
1482 the macros are #defined outside the <strong>.def</strong> file but #undef&#8217;ed inside the
1483 <strong>.def</strong> file. (This is normally a bad programming practice, but on purpose
1484 here).
1485 </p>
1486 </li>
1487 </ul></div>
1488 </div>
1489 </div>
1490 </div>
1491 <div class="sect1">
1492 <h2 id="_coding_style">Coding Style</h2>
1493 <div class="sectionbody">
1494 <div class="paragraph"><p>GNU APL is a GNU project. For the most part it therefore follows the GNU
1495 coding standard, see: <a href="https://www.gnu.org/prep/standards/standards.html">https://www.gnu.org/prep/standards/standards.html</a>
1496 With some minor deviations which attempt to further improve the readability
1497 of the C/C++ code.</p></div>
1498 <div class="sect2">
1499 <h3 id="_general">General</h3>
1500 <div class="ulist"><ul>
1501 <li>
1502 <p>
1503 keep lines short (≤ 78 characters) where possible
1504 </p>
1505 </li>
1506 <li>
1507 <p>
1508 NEVER use tabs (ASCII 0x09) in the source code
1509 </p>
1510 </li>
1511 </ul></div>
1512 </div>
1513 <div class="sect2">
1514 <h3 id="_indentation_and_blanks">Indentation and Blanks</h3>
1515 <div class="sect3">
1516 <h4 id="_function_bodies_cc_files">Function bodies (.cc files)</h4>
1517 <div class="ulist"><ul>
1518 <li>
1519 <p>
1520 Variable declarations at top-level scope are <strong><em>not</em></strong> indented:
1521 </p>
1522 </li>
1523 <li>
1524 <p>
1525 C++ code at the top-level scope is indented with 3 blanks:
1526 </p>
1527 </li>
1528 </ul></div>
1529 <div class="listingblock">
1530 <div class="content">
1531 <pre>
1532
1533 <code>void
1534 foo()
1535 {
1536 int x = 0; // declaration: not indented!
1537
1538 printf("%d:, x); // code: indented by 3 spaces
1539
1540 int y = 1; // another declaration.
1541 ...
1542 }</code>
1543
1544 </pre>
1545 </div></div>
1546 <div class="ulist"><ul>
1547 <li>
1548 <p>
1549 Variable declarations below top-level scope are indented with 2 (from the {
1550 starting the scope
1551 </p>
1552 </li>
1553 <li>
1554 <p>
1555 C++ code below the top-level scope are also indented with 2 blanks:
1556 </p>
1557 </li>
1558 </ul></div>
1559 <div class="listingblock">
1560 <div class="content">
1561 <pre>
1562
1563 <code>void
1564 foo()
1565 {
1566 int x = 0; // declaration: not indented!
1567 int y = 1; // another declaration.
1568
1569 if (x)
1570 { // start of sub-scope
1571 printf("positive x=%d", x); // code: indented by 2 spaces
1572 }
1573 else
1574 { // start of another sub-scope
1575 printf("zero x"); // code: indented by 2 spaces
1576 }
1577 }</code>
1578
1579 </pre>
1580 </div></div>
1581 </div>
1582 <div class="sect3">
1583 <h4 id="_conditionals">Conditionals</h4>
1584 <div class="ulist"><ul>
1585 <li>
1586 <p>
1587 conditionals are not functions! put 1 blank between the keyword and the
1588 condition
1589 </p>
1590 </li>
1591 <li>
1592 <p>
1593 the body of the conditional is indented according to the opening <strong>(</strong> of the
1594 condition (for short ones like if, else, or for), but no more than 2 blanks:
1595 </p>
1596 </li>
1597 </ul></div>
1598 <div class="listingblock">
1599 <div class="content">
1600 <pre>
1601
1602 <code>void
1603 foo()
1604 {
1605 int x = 0;
1606
1607 // DO NOT:
1608 if(i == 0)
1609
1610 // DO:
1611 if (i == 0)
1612 {
1613 ...
1614 }
1615
1616 for (int i = 0; i &lt; N; ++i)
1617 {
1618 ...
1619 }
1620
1621 while (i == 0)
1622 {
1623 ...
1624 }
1625 }</code>
1626
1627 </pre>
1628 </div></div>
1629 <div class="ulist"><ul>
1630 <li>
1631 <p>
1632 you MAY put single statements or very short bodies onto the condition line,
1633 </p>
1634 </li>
1635 <li>
1636 <p>
1637 but NOT mix single- and multi-line styles for <strong>if</strong> and <strong>else</strong> clauses, and
1638 </p>
1639 </li>
1640 <li>
1641 <p>
1642 place the { and } of multi-line bodies on the same column, that is:
1643 </p>
1644 </li>
1645 <li>
1646 <p>
1647 you MUST NEVER NEVER NEVER use K&amp;R style:
1648 </p>
1649 </li>
1650 </ul></div>
1651 <div class="listingblock">
1652 <div class="content">
1653 <pre>
1654
1655 <code> // DO:
1656
1657 if (x &gt;= 0) return "positive";
1658 else return "negative";
1659
1660 // DO NOT:
1661
1662 if (x &gt;= 0) { return "positive"; } // redundant { }
1663 else { return "negative"; } // redundant { }
1664
1665 if (x &gt;= 0) return "positive"; // single-line style
1666 else // multi-line style
1667 {
1668 error = true;
1669 return "negative";
1670 }
1671
1672 // HEAVEN FORBID (aka. K&amp;R style)
1673 if (x &gt;= 0) {
1674 return "positive";
1675 } else {
1676 return "negative";
1677 }</code>
1678
1679 </pre>
1680 </div></div>
1681 </div>
1682 <div class="sect3">
1683 <h4 id="_function_arguments">Function arguments</h4>
1684 <div class="ulist"><ul>
1685 <li>
1686 <p>
1687 one blank after every comma and between tokens:
1688 </p>
1689 </li>
1690 </ul></div>
1691 <div class="listingblock">
1692 <div class="content">
1693 <pre>
1694
1695 <code> // DO:
1696
1697 int
1698 main(int argc, char * argv[])
1699 {
1700 ...
1701 }
1702
1703 // DO NOT ANY OF THE FOLLOWING...
1704
1705 int
1706 main (int argc, char * argv[]) // extra space before arguments
1707 {
1708 ...
1709 }
1710
1711 int
1712 main( int argc, char * argv[] ) // extra spaces around arguments
1713 {
1714 ...
1715 }
1716
1717 int
1718 main(int argc, char *argv[]) // no space after *
1719 {
1720 ...
1721 }
1722
1723 int
1724 main(int argc, char* argv[]) // no space before *
1725 {
1726 ...
1727 }</code>
1728
1729 </pre>
1730 </div></div>
1731 </div>
1732 </div>
1733 <div class="sect2">
1734 <h3 id="_naming_conventions">Naming Conventions</h3>
1735 <div class="sect3">
1736 <h4 id="_cpp_class_names">C++ Class Names</h4>
1737 <div class="paragraph"><p>Class names should be in <strong>CamelCase</strong>. That is:</p></div>
1738 <div class="ulist"><ul>
1739 <li>
1740 <p>
1741 No <em>_</em> between multiple words like in <strong>CamelCase</strong>,
1742 </p>
1743 </li>
1744 <li>
1745 <p>
1746 First letter of each word shall be uppercase, the rest lowercase,
1747 </p>
1748 </li>
1749 <li>
1750 <p>
1751 Abbreviations shall be all uppercase (in that case _ is allowed if it
1752 improves readability or groups classes). E.g. Quad_XML.
1753 </p>
1754 </li>
1755 </ul></div>
1756 </div>
1757 <div class="sect3">
1758 <h4 id="_cpp_file_names">C++ File Names</h4>
1759 <div class="ulist"><ul>
1760 <li>
1761 <p>
1762 class name and file names shall be the same (except for the .cc and .hh
1763 extensions).
1764 </p>
1765 </li>
1766 <li>
1767 <p>
1768 file names: always use .cc (not .cxx or .cpp) and .hh (not .hxx or hpp).
1769 </p>
1770 </li>
1771 <li>
1772 <p>
1773 Pure C (as opposed to C++) files should use .c and .h as usual.
1774 </p>
1775 </li>
1776 </ul></div>
1777 </div>
1778 </div>
1779 <div class="sect2">
1780 <h3 id="_some_coding_conventions">Some Coding Conventions</h3>
1781 <div class="ulist"><ul>
1782 <li>
1783 <p>
1784 Use <strong>(const) char <strong> for ASCII characters (i.e. 0x00&#8230;0x7F)
1785 and *(const) UTF </strong> for UTF8 encoded strings. Keep in mind that
1786 C++ literals with APL characters (e.g. "A∆B") are automatically
1787 UTF8 encoded by the compiler even though their type is *const char *</strong>.
1788 </p>
1789 </li>
1790 <li>
1791 <p>
1792 Use <strong>basic_string&lt;typename&gt;</strong> for simple C types (char, int, char *, &#8230;) and
1793 <strong>vector&lt;typename&gt;</strong> for structs and classes, in particular for those with
1794 constructors.
1795 </p>
1796 </li>
1797 <li>
1798 <p>
1799 Use <strong>const</strong> where possible.
1800 </p>
1801 </li>
1802 </ul></div>
1803 <div class="paragraph"><p>These conventions have developed over time. Some old code does not follow them,
1804 but new or updated code should.</p></div>
1805 <div class="sect3">
1806 <h4 id="_class_declarations_hh_files">Class declarations (.hh files)</h4>
1807 <div class="paragraph"><p>To the extent possible:</p></div>
1808 <div class="ulist"><ul>
1809 <li>
1810 <p>
1811 declare public members (= the external interface) before protected
1812 members (= the class implementation),
1813 </p>
1814 </li>
1815 <li>
1816 <p>
1817 declare members before static members,
1818 </p>
1819 </li>
1820 <li>
1821 <p>
1822 declare member functions before data members,
1823 </p>
1824 </li>
1825 <li>
1826 <p>
1827 keep the following declaration order:
1828 </p>
1829 <div class="ulist"><ul>
1830 <li>
1831 <p>
1832 constructors,
1833 </p>
1834 </li>
1835 <li>
1836 <p>
1837 destructor,
1838 </p>
1839 </li>
1840 <li>
1841 <p>
1842 inline functions,
1843 </p>
1844 </li>
1845 <li>
1846 <p>
1847 non-inline functions
1848 </p>
1849 </li>
1850 </ul></div>
1851 </li>
1852 <li>
1853 <p>
1854 always use <strong>protected:</strong> rather than <strong>private:</strong> for the members of a class.
1855 GNU APL uses the convention that <strong>private:</strong> members are used to prevent the
1856 accidental function calls that are sometimes generated by the C++ compiler
1857 (implicit conversion). In GNU APL, such members are declared, but not
1858 defined (so that the linker will complain if the compiler should generate
1859 them).
1860 </p>
1861 </li>
1862 </ul></div>
1863 <div class="paragraph"><p>Indentation:</p></div>
1864 <div class="ulist"><ul>
1865 <li>
1866 <p>
1867 indent all members with 2 spaces,
1868 </p>
1869 </li>
1870 <li>
1871 <p>
1872 indent inline function bodies by 3 more spaces
1873 </p>
1874 </li>
1875 <li>
1876 <p>
1877 do this recursively (nested classes)
1878 </p>
1879 </li>
1880 </ul></div>
1881 <div class="paragraph"><p>And finally:</p></div>
1882 <div class="paragraph"><p>have a look at the existing <strong>.cc</strong> and <strong>.hh</strong> files. Even though this will
1883 cause some extra work on your part, it simplifies the reading of your code
1884 for many others. A project with different styles in different files looks
1885 rather unprofessional, so please respect the style rules above even if you
1886 do not like them.</p></div>
1887 </div>
1888 </div>
1889 </div>
1890 </div>
1891 <div class="sect1">
1892 <h2 id="_debugging">Debugging</h2>
1893 <div class="sectionbody">
1894 <div class="paragraph"><p>GNU APL has a number of built-in means to locate faults. In a code base of
1895 around 120,000 LOC, locating the fault in the source file takes most of the
1896 time required to fix it. Many of the GNU APL debugging facilities have
1897 performance impacts; they need to be enabled. The make target <strong>develop</strong>, i.e.</p></div>
1898 <div class="listingblock">
1899 <div class="content">
1900 <pre>
1901
1902 <code>make develop</code>
1903
1904 </pre>
1905 </div></div>
1906 <div class="paragraph"><p>will enable a useful set of debugging facilities. <strong>make develop</strong> re-runs
1907 <strong>./configure</strong>, therefore a normal build must have been performed beforehand
1908 (otherwise the top-level Makefile would be missing).</p></div>
1909 <div class="sect2">
1910 <h3 id="_assertions">Assertions</h3>
1911 <div class="paragraph"><p>GNU APL has more 600 Assertions spread throughout the source code. The
1912 assertions check internal assumptions that the designer has made. If such
1913 an assumption turns out to be false then a stack dump is produced which
1914 tells which assumption (and where) was wrong.</p></div>
1915 <div class="paragraph"><p>Even though every single assertion has a negligible performance impact, the
1916 accumulation of all assertions might become noticeable. It is therefore
1917 possible to enable or disable some or all of them. This is done via the
1918 ./configure argument ASSERT_LEVEL (see also <strong>README-2-configure</strong>):</p></div>
1919 <div class="listingblock">
1920 <div class="content">
1921 <pre>
1922
1923 <code>.configure ASSERT_LEVEL_WANTED=0 (the default, best performance)
1924 .configure ASSERT_LEVEL_WANTED=1 disable trivial assertions, enable others
1925 .configure ASSERT_LEVEL_WANTED=2 enable all assertions (worst performance)</code>
1926
1927 </pre>
1928 </div></div>
1929 <div class="paragraph"><p>The <strong>make develop</strong> target sets, among other things, <strong>ASSERT_LEVEL_WANTED=2</strong>.</p></div>
1930 </div>
1931 <div class="sect2">
1932 <h3 id="_logging_facilities">Logging Facilities</h3>
1933 <div class="paragraph"><p>Failed assertions primarily reveal major programming mistakes, such as
1934 changing the code in one place and not considering the impact in other places.
1935 In many cases the source code is faulty even though no assertion fails.
1936 To isolate a fault one can enable one or more of almost 50 logging facilities
1937 from the APL command line. Each logging facility addresses a functional area
1938 of interest in the source code. When the code comes across such an area then a
1939 printout is produced which provides additional information for the
1940 troubleshooter. As of this writing, the following logging facilities exist:</p></div>
1941 <div class="listingblock">
1942 <div class="content">
1943 <pre>
1944
1945 <code> ]log
1946 1: (OFF) AV details
1947 2: (OFF) new and delete calls
1948 3: (OFF) input from user or testcase file
1949 4: (OFF) parser: parsing
1950 5: (OFF) ... function find_closing()
1951 6: (OFF) ... tokenization
1952 7: (OFF) ... function collect_constants()
1953 8: (OFF) ... create value()
1954 9: (OFF) defined function: fix()
1955 10: (OFF) ... set_line()
1956 11: (OFF) ... load()
1957 12: (OFF) ... execute()
1958 13: (OFF) ... enter/leave
1959 14: (OFF) State indicator: enter/leave
1960 15: (OFF) ... push/pop
1961 16: (OFF) Symbol: lookup
1962 17: (OFF) ... push/pop and )ERASE
1963 18: (OFF) ... resolve
1964 19: (OFF) Value: glue()
1965 20: (OFF) ... erase_stale()
1966 21: (OFF) APL primitive function format
1967 22: (OFF) character conversions
1968 23: (OFF) APL system function Quad-FX
1969 24: (OFF) commands )LOAD, )SAVE, )IN, and )OUT
1970 25: (OFF) more verbose errors
1971 26: (OFF) details of error throwing
1972 27: (OFF) nabla editor
1973 28: (OFF) execute(): state changes
1974 29: (OFF) PrintBuffer: align() function
1975 30: (OFF) Output: cork() functions
1976 31: (OFF) Details of test execution
1977 32: (OFF) Prefix parser
1978 33: (OFF) ... location information
1979 34: (OFF) FunOper1 and FunOper2 functions
1980 35: (OFF) Shared Variable operations
1981 36: (OFF) command line arguments (argc/argv)
1982 37: (OFF) interpreter start-up messages
1983 38: (OFF) optimization messages
1984 39: (OFF) )LOAD and )SAVE details
1985 40: (OFF) Svar_DB signals
1986 41: (OFF) Parallel (multi-core) execution
1987 42: (OFF) EOC handler functionality
1988 43: (OFF) ⎕DLX details
1989 44: (OFF) command ]DOXY
1990 45: (OFF) details of Value allocation
1991 46: (OFF) ⎕TF details
1992 47: (OFF) ⎕PLOT details</code>
1993
1994 </pre>
1995 </div></div>
1996 <div class="paragraph"><p>Like assertions, debugging facilities may have a performance impact. There are
1997 almost 400 places in the source code where a decision is made about if and what
1998 debug output shall be produced. For this reason, the logging facilities can
1999 either be enabled/disabled at runtime from the APL command line (called
2000 <strong>dynamic logging</strong>) or else at compile time (called <strong>static logging</strong>). <strong>dynamic
2001 logging</strong> is the method of choice for trouble shooting and is therefore chosen
2002 by <strong>make develop</strong>. In contrast <strong>static logging</strong> is the default and the
2003 logging facilities that shall be enabled can be set in file <strong>src/Logging.def</strong>
2004 (first argument of macro <strong>log_def()</strong>) before compiling the interpreter.</p></div>
2005 <div class="paragraph"><p>The logging Facility 37, <em>interpreter start-up messages</em> is somewhat special.
2006 Logging messages produced in the start-up of the interpreter occur before
2007 the user has a chance to enter a <strong>]LOG 37</strong> command that enables them. For that
2008 reason, the logging of start-up messages can be enabled from the command
2009 line (option <strong>-l 37</strong>). None of the other logging facility can be enabled
2010 from the command line. In the supposedly rare cases where other facilities
2011 are needed before the interpreter has fully initialized itself, static
2012 logging can be used instead.</p></div>
2013 </div>
2014 <div class="sect2">
2015 <h3 id="_the_check_command">The )CHECK Command</h3>
2016 <div class="paragraph"><p>GNU APL has adopted the )CHECK command from IBM APL2. The check command
2017 performs an internal check of all data structures inside the interpreter.
2018 The primary purpose is to find memory leaks:</p></div>
2019 <div class="listingblock">
2020 <div class="content">
2021 <pre>
2022
2023 <code> )CHECK
2024 OK - no stale functions
2025 OK - no stale values
2026 OK - no stale indices
2027 OK - no duplicate parents</code>
2028
2029 </pre>
2030 </div></div>
2031 <div class="paragraph"><p>A value or function is <strong>stale</strong> if the current workspace cannot reach it
2032 anymore. The interpreter does a sort of double-entry accounting of APL values
2033 and defined functions, and the )CHECK triggers a consistency check of that
2034 accounting. The runtime overhead is low therefore the double-entry accounting
2035 cannot be disabled.</p></div>
2036 </div>
2037 <div class="sect2">
2038 <h3 id="_value_tracing">Value Tracing</h3>
2039 <div class="paragraph"><p>While every logging facility focuses on a particular functional area of
2040 interest, value tracing focuses on the life-cycle of individual APL values.
2041 If )CHECK finds a stale value (stale functions are rather rare) then the
2042 next question is where the value was lost. An APL value sees a lot of places
2043 in the source code and the value tracing keeps track of the major steps
2044 in the lifetime of a value (creation, copying, destruction, etc). Value tracing
2045 is disabled by default (Performance!) but enabled by <strong>make develop</strong>.</p></div>
2046 </div>
2047 <div class="sect2">
2048 <h3 id="_debug_macros">Debug macros</h3>
2049 <div class="paragraph"><p>If all debug means above fail, then the last resort is to either start a
2050 debugger (like <strong>gdb</strong>) or to enter checkpoints in the source code and print the
2051 information needed to isolate the fault. For this purpose (the author is using
2052 debuggers only for analyzing core dump files) there are some macros that
2053 simplify such printouts.</p></div>
2054 <div class="sect3">
2055 <h4 id="_the_loc_macro">The LOC macro</h4>
2056 <div class="paragraph"><p>The macro <strong>LOC</strong> expands to the current source file name and line number. Many
2057 C++ functions use it for non-debug purposes to see from where they were
2058 called. The expanded LOC macro is a string literal, therefore the overhead is
2059 minimal:</p></div>
2060 <div class="listingblock">
2061 <div class="content">
2062 <pre>
2063
2064 <code>#define STR(x) #x
2065 #define Loc(f, l) f ":" STR(l)
2066 #define LOC Loc(__FILE__, __LINE__)</code>
2067
2068 </pre>
2069 </div></div>
2070 </div>
2071 <div class="sect3">
2072 <h4 id="_the_q_macro">The Q() macro</h4>
2073 <div class="paragraph"><p>The macro <strong>Q(x)</strong> prints the value of <strong>x</strong>, source file name and line number
2074 at which the value of <strong>x</strong> was printed:</p></div>
2075 <div class="listingblock">
2076 <div class="content">
2077 <pre>
2078
2079 <code>/// print x and its source code location
2080 #define Q(x) get_CERR() &lt;&lt; std::left &lt;&lt; setw(20) &lt;&lt; #x ":" \
2081 &lt;&lt; " '" &lt;&lt; x &lt;&lt; "' at " LOC &lt;&lt; endl;</code>
2082
2083 </pre>
2084 </div></div>
2085 <div class="paragraph"><p>The other debug means are somewhat coarse-grained. Dividing around <strong>120,000
2086 LOC</strong> (lines of source code) by 400 places where logging facilities may
2087 provide some output yields an average of 300 lines of code between logging
2088 outputs. The typical troubleshooting process is this:</p></div>
2089 <div class="olist arabic"><ol class="arabic">
2090 <li>
2091 <p>
2092 find an easy way to reproduce the fault (testcase file, APL script, etc.),
2093 then
2094 </p>
2095 </li>
2096 <li>
2097 <p>
2098 use logging facilities and/or value tracing to narrow the possible locations
2099 of the fault (to typically one source code file), and finally
2100 </p>
2101 </li>
2102 <li>
2103 <p>
2104 insert <strong>Q()</strong> macros if needed to further isolate the fault.
2105 </p>
2106 </li>
2107 </ol></div>
2108 <div class="paragraph"><p>The build system checks that all <strong>Q()</strong> macros were removed before checking the
2109 code into the Savannah SVN repository. This is because forgetting that used to
2110 be a frequent mistake.</p></div>
2111 </div>
2112 <div class="sect3">
2113 <h4 id="_the_q1_macro">The Q1() macro</h4>
2114 <div class="paragraph"><p>The <strong>Q1()</strong> macro does the same as <strong>Q()</strong> but shall remain in the code. It is
2115 (rarely) used in places of which the designer believes that they cannot be
2116 reached but has no proof that this is the case. Similar to assertions, but
2117 only printing values of interest without terminating with a stack dump.</p></div>
2118 </div>
2119 </div>
2120 </div>
2121 </div>
2122 <div class="sect1">
2123 <h2 id="_top_down_description_of_the_gnu_apl_source_code">Top-Down Description of the GNU APL Source Code</h2>
2124 <div class="sectionbody">
2125 <div class="paragraph"><p>The APL interpreter is, like almost any other interpreter, a program that
2126 reads one line of input after another. For every line read (the <strong>input</strong>)
2127 it produces zero or more lines of <strong>output</strong>. In doing so, the interpreter may
2128 or may not change its internal state.</p></div>
2129 <div class="paragraph"><p>In the context of APL the internal state of the interpreter is called (the
2130 current) <strong>workspace</strong>; it can be <strong>)SAVE</strong>d into a file and later <strong>)LOAD</strong>ed
2131 or <strong>)COPY</strong>ed into another interpreter instance.</p></div>
2132 <div class="paragraph"><p>The top-level loop of the interpreter described above is commonly referred
2133 to as <strong>REPL</strong>, an abbreviation for <strong>R</strong>ead-<strong>E</strong>valuate-<strong>P</strong>rint-<strong>L</strong>oop:</p></div>
2134 <div class="listingblock">
2135 <div class="content">
2136 <pre>
2137
2138 <code> ┌──────┐
2139 │ INIT │
2140 └──────┘
2141 ↓
2142 ○← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ← ←
2143 ↓ ↑
2144 ┌──────┐ ┌──────────┐ ┌───────┐ ┌──────┐
2145 │ Read │→ → →│ Evaluate │→ → →│ Print │→ → →│ Loop │
2146 └──────┘ └──────────┘ └───────┘ └──────┘
2147 ↓
2148 )OFF
2149 ↓
2150 ┌──────┐
2151 │ EXIT │
2152 └──────┘</code>
2153
2154 </pre>
2155 </div></div>
2156 <div class="paragraph"><p>The <strong>REPL</strong> of GNU APL is located at the end of function <strong>main()</strong> in source
2157 file <strong>main.cc</strong>:</p></div>
2158 <div class="listingblock">
2159 <div class="content">
2160 <pre>
2161
2162 <code> for (const bool exit_on_error = IO_Files::exit_on_error();;)
2163 {
2164 const Token tok = Workspace::immediate_execution(exit_on_error);
2165 if (tok.get_tag() == TOK_OFF) Command::cmd_OFF(0);
2166 }</code>
2167
2168 </pre>
2169 </div></div>
2170 <div class="paragraph"><p>The <strong>R</strong>ead, <strong>E</strong>valuate, and <strong>P</strong>rint parts of the <strong>REPL</strong> are performed
2171 in function <strong>immediate_execution()</strong> which returns <strong>TOK_OFF</strong> if the APL
2172 command <strong>)OFF</strong> was processed and something else otherwise.</p></div>
2173 <div class="sect2">
2174 <h3 id="_the_workspace_immediate_execution_function">The Workspace::immediate_execution() Function</h3>
2175 <div class="paragraph"><p>Function <strong>Workspace::immediate_execution()</strong> processes one line of input,
2176 in source file <strong>Workspace.cc</strong>. It simply calls <strong>Command::process_line()</strong>.
2177 More importantly, it <strong>catch</strong>es C++ errors that may be thrown and processes
2178 the errors:</p></div>
2179 <div class="listingblock">
2180 <div class="content">
2181 <pre>
2182
2183 <code>Token
2184 Workspace::immediate_execution(bool exit_on_error)
2185 {
2186 for (;;)
2187 {
2188 try
2189 {
2190 Command::process_line();
2191 }
2192 catch (Error &amp; err)
2193 ...</code>
2194
2195 </pre>
2196 </div></div>
2197 <div class="admonitionblock">
2198 <table><tr>
2199 <td class="icon">
2200 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
2201 </td>
2202 <td class="content">Some lines, notably the ∇-editor and, in certain cases, the <strong>)COPY</strong> and
2203 <strong>)LOAD</strong> commands have their own sub-REPL. They loop themselves until a
2204 particular condition (the closing ∇ for the ∇-editor or end-of-file for the
2205 <strong>)COPY</strong> and <strong>)LOAD</strong> commands) has occurred. Therefore:</td>
2206 </tr></table>
2207 </div>
2208 <div class="ulist"><ul>
2209 <li>
2210 <p>
2211 process_line() is <strong>atomic</strong> (in the sense that it processes only one input
2212 line), but
2213 </p>
2214 </li>
2215 <li>
2216 <p>
2217 the functions called by process_line() will be called repeatedly until an
2218 error is returned. The close of the ∇-editor is also considered an error
2219 condition here).
2220 </p>
2221 </li>
2222 </ul></div>
2223 <div class="paragraph"><p>This fact is sometimes missed by programmers using <strong>libapl</strong>. <strong>libapl</strong> is a
2224 library that contains most of the normal interpreter code, <strong>but not</strong> the REPL
2225 loops of the interpreter. The purpose of <strong>libapl</strong> is to provide a C-API to
2226 other front-ends like <strong>Erlang</strong>, <strong>Python</strong>, or <strong>C++</strong> programs. For example,
2227 if a front end opens a defined function with a line that starts with ∇, then
2228 the front-end is responsible for providing the subsequent (defined function-)
2229 lines including the final ∇ that closes the function definition. The same
2230 holds for <strong>)LOAD</strong> and <strong>)COPY</strong> of <strong>.apl</strong> scripts, but not for <strong>)LOAD</strong> and
2231 <strong>)COPY</strong> of workspace snapshots (<strong>.xml</strong> files). To summarize:</p></div>
2232 <div class="tableblock">
2233 <table rules="all"
2234 width="30%"
2235 frame="border"
2236 cellspacing="0" cellpadding="4">
2237 <col width="50%" />
2238 <col width="50%" />
2239 <thead>
2240 <tr>
2241 <th align="left" valign="top"> Operation </th>
2242 <th align="left" valign="top"> Atomicity</th>
2243 </tr>
2244 </thead>
2245 <tbody>
2246 <tr>
2247 <td align="left" valign="top"><p class="table">∇-editor</p></td>
2248 <td align="left" valign="top"><p class="table">non-atomic</p></td>
2249 </tr>
2250 <tr>
2251 <td align="left" valign="top"><p class="table">)SAVE</p></td>
2252 <td align="left" valign="top"><p class="table">atomic</p></td>
2253 </tr>
2254 <tr>
2255 <td align="left" valign="top"><p class="table">)LOAD .apl</p></td>
2256 <td align="left" valign="top"><p class="table">non-atomic</p></td>
2257 </tr>
2258 <tr>
2259 <td align="left" valign="top"><p class="table">)LOAD .xml</p></td>
2260 <td align="left" valign="top"><p class="table">atomic</p></td>
2261 </tr>
2262 <tr>
2263 <td align="left" valign="top"><p class="table">)COPY .apl</p></td>
2264 <td align="left" valign="top"><p class="table">non-atomic</p></td>
2265 </tr>
2266 <tr>
2267 <td align="left" valign="top"><p class="table">)COPY .xml</p></td>
2268 <td align="left" valign="top"><p class="table">atomic</p></td>
2269 </tr>
2270 <tr>
2271 <td align="left" valign="top"><p class="table">multi-line string</p></td>
2272 <td align="left" valign="top"><p class="table">non-atomic</p></td>
2273 </tr>
2274 </tbody>
2275 </table>
2276 </div>
2277 </div>
2278 <div class="sect2">
2279 <h3 id="_the_command_process_line_functions">The Command::process_line() Functions</h3>
2280 <div class="paragraph"><p>Function <strong>Command::process_line()</strong> reads the next input line
2281 (<strong>InputMux::get_line()</strong>), removes leading white-space (blanks) and then
2282 decides how to proceed. The first non-blank character determines the
2283 subsequent processing as follows:</p></div>
2284 <div class="olist arabic"><ol class="arabic">
2285 <li>
2286 <p>
2287 empty line: return
2288 </p>
2289 </li>
2290 <li>
2291 <p>
2292 ⍝: APL comment: return
2293 </p>
2294 </li>
2295 <li>
2296 <p>
2297 #: script comment: return
2298 </p>
2299 </li>
2300 <li>
2301 <p>
2302 ): (regular) APL command: <strong>Command::process_line(line&#8230;)</strong>
2303 </p>
2304 </li>
2305 <li>
2306 <p>
2307 ]: (debug) APL command: <strong>Command::process_line(line&#8230;)</strong>
2308 </p>
2309 </li>
2310 <li>
2311 <p>
2312 """|«««: start of a multi-line string: loop until end of string
2313 </p>
2314 </li>
2315 <li>
2316 <p>
2317 otherwise: APL expression: <strong>Command::process_line(line)</strong>
2318 </p>
2319 </li>
2320 </ol></div>
2321 <div class="paragraph"><p>Function <strong>Command::process_line()</strong> is overloaded; the variant without arguments
2322 fetches a line while the variant with a line argument processes the fetched
2323 line. The decision about the further processing is done in the latter, i.e. in
2324 <strong>process_line(line&#8230;I)</strong>:</p></div>
2325 <div class="listingblock">
2326 <div class="content">
2327 <pre>
2328
2329 <code>void
2330 Command::process_line(UCS_string &amp; line, ostream * out)
2331 {
2332 line.remove_leading_white spaces();
2333 if (line.size() == 0) return; // empty input line
2334
2335 switch(line[0])
2336 {
2337 case UNI_R_PARENT: // regular command, e.g. )SI
2338 if (out == 0) out = &amp;COUT;
2339 do_APL_command(*out, line);
2340 if (line.size()) break;
2341 return;
2342
2343 case UNI_R_BRACK: // debug command, e.g. ]LOG
2344 if (out == 0) out = &amp;CERR;
2345 do_APL_command(*out, line);
2346 if (line.size()) break;
2347 return;
2348
2349 case UNI_NABLA: // e.g. ∇FUN
2350 Nabla::edit_function(line);
2351 return;
2352
2353 case UNI_NUMBER_SIGN: // e.g. # comment
2354 case UNI_COMMENT: // e.g. ⍝ comment
2355 return;
2356
2357 default: break;
2358 }
2359
2360 ++APL_expression_count;
2361 do_APL_expression(line);
2362 }</code>
2363
2364 </pre>
2365 </div></div>
2366 <div class="paragraph"><p>By default the output of process_line() is printed to the console, but a
2367 non-zero <strong>out</strong> redirects the output to <strong>out</strong>. This is used by functions that
2368 need to capture the output of a command, e.g. ⍎")COMMAND" (a GNU APL extension,
2369 normally APL commands cannot be used by ⍎).</p></div>
2370 <div class="paragraph"><p>As explained, further processing depends on the first character of the line:</p></div>
2371 <div class="listingblock">
2372 <div class="content">
2373 <pre>
2374
2375 <code> ┌──────────────┐
2376 │ process_line │
2377 └──────┬───────┘
2378 │
2379 ┌───────────────┼──────────────────┐
2380 │ │ │
2381 ∇ ),] other
2382 │ │ │
2383 ┌────┴─────┐ ┌────┴─────┐ ┌───────┴────────┐
2384 │ ∇-editor │ │ )COMMAND │ │ APL expression │
2385 └──────────┘ │ ]COMMAND │ └────────────────┘
2386 └──────────┘</code>
2387
2388 </pre>
2389 </div></div>
2390 <div class="paragraph"><p>Strictly speaking the ∇-editor and APL commands are not part of the APL
2391 language (they have their own syntax). At this point it should suffice that
2392 the implementation of the ∇-editor is found in source file <strong>Nabla</strong>.cc
2393 (nabla is the name for ∇ in the Greek alphabet) while the implementations
2394 of most commands are found in <strong>Command.cc</strong>. Most of the functions in
2395 <strong>Command.cc</strong> are concerned with the decoding of command arguments and
2396 are not further discussed here. Most commands have a simple implementation
2397 which is also contained in <strong>Command.cc</strong>. The others have their own source
2398 file:</p></div>
2399 <div class="tableblock">
2400 <table rules="all"
2401 width="40%"
2402 frame="border"
2403 cellspacing="0" cellpadding="4">
2404 <col width="50%" />
2405 <col width="50%" />
2406 <thead>
2407 <tr>
2408 <th align="left" valign="top"> Command </th>
2409 <th align="left" valign="top"> Implementation</th>
2410 </tr>
2411 </thead>
2412 <tbody>
2413 <tr>
2414 <td align="left" valign="top"><p class="table">)CLEAR</p></td>
2415 <td align="left" valign="top"><p class="table">Workspace.cc</p></td>
2416 </tr>
2417 <tr>
2418 <td align="left" valign="top"><p class="table">)MORE</p></td>
2419 <td align="left" valign="top"><p class="table">Workspace.cc</p></td>
2420 </tr>
2421 <tr>
2422 <td align="left" valign="top"><p class="table">)SAVE .xml</p></td>
2423 <td align="left" valign="top"><p class="table">Archive.cc</p></td>
2424 </tr>
2425 <tr>
2426 <td align="left" valign="top"><p class="table">)COPY .xml</p></td>
2427 <td align="left" valign="top"><p class="table">Archive.cc</p></td>
2428 </tr>
2429 <tr>
2430 <td align="left" valign="top"><p class="table">)LOAD .xml</p></td>
2431 <td align="left" valign="top"><p class="table">Archive.cc</p></td>
2432 </tr>
2433 </tbody>
2434 </table>
2435 </div>
2436 <div class="paragraph"><p>Consult source file <strong>Command.def</strong> to find the implementation of a particular
2437 command. Most commands support TAB-completion: the user hits TAB and the
2438 possible completions are are displayed. Implemented in <strong>TabExpansion.cc</strong>.</p></div>
2439 </div>
2440 <div class="sect2">
2441 <h3 id="_apl_expressions">APL Expressions</h3>
2442 <div class="paragraph"><p>The most interesting case occurs if the user enters an APL expression, from
2443 simple ones like <strong>+/⍳10</strong> or complex ones like calling a defined function. The
2444 behavior in this case is specified in the ISO APL standard and GNU APL
2445 follows this standard, at least for the most part. The processing of the input
2446 line is <em>essentially</em> the same for the lines of defined APL functions and for
2447 lines entered in immediate execution. The minor differences, e.g. APL labels
2448 are not allowed in immediate execution, statement separators are not allowed
2449 in ⍎-strings, etc. are handled via a <strong>ParseMode</strong> declared and defined in
2450 <strong>APL_types.hh</strong>:</p></div>
2451 <div class="listingblock">
2452 <div class="content">
2453 <pre>
2454
2455 <code>/// What is being parsed (defined function, immediate execution statements,
2456 /// or ⍎expr)
2457 enum ParseMode
2458 {
2459 PM_FUNCTION = 0, ///&lt; defined function
2460 PM_STATEMENT_LIST = 1, ///&lt; immediate execution
2461 PM_EXECUTE = 2, ///&lt; execute (⍎)
2462 };</code>
2463
2464 </pre>
2465 </div></div>
2466 <div class="paragraph"><p>The <strong>ParseMode</strong> defines the origin of a particular line; the options are:</p></div>
2467 <div class="ulist"><ul>
2468 <li>
2469 <p>
2470 The line was entered in immediate execution (as described above), or
2471 </p>
2472 </li>
2473 <li>
2474 <p>
2475 The line is a line of a defined function (see later), or
2476 </p>
2477 </li>
2478 <li>
2479 <p>
2480 The line was an APL string (⍎, ⎕EA, ⎕ES &#8230;).
2481 </p>
2482 </li>
2483 </ul></div>
2484 <div class="paragraph"><p>The next steps in the processing of a line are tokenization and pre-parsing:</p></div>
2485 <div class="listingblock">
2486 <div class="content">
2487 <pre>
2488
2489 <code> ┌───────────────┐
2490 │ Tokenization │
2491 └───────┬───────┘
2492 ┌───────┴──────┐
2493 │ Pre-Parsing │
2494 ├──────────────┤
2495 │ Optimization │
2496 └──────────────┘</code>
2497
2498 </pre>
2499 </div></div>
2500 </div>
2501 <div class="sect2">
2502 <h3 id="_tokenization">Tokenization</h3>
2503 <div class="paragraph"><p>Tokenization splits the (APL-) characters of a line into small fixed-sized
2504 units called *Token*s. Tokens are the machine-readable unit equivalents of the
2505 human readable input characters. For example, a possibly lengthy string like
2506 "3.142.0" is converted into a Token holding a C <em>double</em>. The whole purpose of
2507 tokenization is to convert the APL input into an internal representation that
2508 can be interpreted significantly faster than the APL characters enters by the
2509 APL programmer. For example:</p></div>
2510 <div class="listingblock">
2511 <div class="content">
2512 <pre>
2513
2514 <code> A←1 2 3 ◊ B←'abc' ⍝ APL input string</code>
2515
2516 </pre>
2517 </div></div>
2518 <div class="paragraph"><p>is tokenized into:</p></div>
2519 <div class="listingblock">
2520 <div class="content">
2521 <pre>
2522
2523 <code> ┌───┐ ┌───┐ ┌───────┐ ┌───┐ ┌───┐ ┌───┐ ┌─────┐ ┌─────┐
2524 │ A │ │ ← │ │ 1 2 3 │ │ ◊ │ │ B │ │ ← │ │ abc │ │ EOL │
2525 └───┘ └───┘ └───────┘ └───┘ └───┘ └───┘ └─────┘ └─────┘</code>
2526
2527 </pre>
2528 </div></div>
2529 <div class="paragraph"><p>The function that performs the tokenization is:</p></div>
2530 <div class="listingblock">
2531 <div class="content">
2532 <pre>
2533
2534 <code> ErrorCode tokenize(const UCS_string &amp; input, Token_string &amp; tos) const;</code>
2535
2536 </pre>
2537 </div></div>
2538 <div class="paragraph"><p>which is a member of <strong>class Tokenizer</strong>, see <strong>Tokenizer.hh</strong>. The result is
2539 a Token_string:</p></div>
2540 <div class="listingblock">
2541 <div class="content">
2542 <pre>
2543
2544 <code>// A sequence of Tokens
2545 class Token_string : public std::vector&lt;Token&gt;
2546 {
2547 ...
2548 };</code>
2549
2550 </pre>
2551 </div></div>
2552 <div class="paragraph"><p>see file <strong>Token.hh</strong>.</p></div>
2553 </div>
2554 <div class="sect2">
2555 <h3 id="_pre_parsing">Pre-parsing</h3>
2556 <div class="paragraph"><p>Pre-parsing is primarily an optimization step that performs a number of
2557 sub-steps, such as:</p></div>
2558 <div class="ulist"><ul>
2559 <li>
2560 <p>
2561 splitting multiple statements (separated by ◊ Token) into individual
2562 statements (at ◊),
2563 </p>
2564 </li>
2565 <li>
2566 <p>
2567 grouping multiple scalars into single APL values,
2568 </p>
2569 </li>
2570 <li>
2571 <p>
2572 reversing the order of individual statements, and
2573 </p>
2574 </li>
2575 <li>
2576 <p>
2577 performing some optimizations
2578 </p>
2579 </li>
2580 </ul></div>
2581 <div class="paragraph"><p>In the example above, the token string</p></div>
2582 <div class="listingblock">
2583 <div class="content">
2584 <pre>
2585
2586 <code> ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌─────┐ ┌─────┐
2587 │ A │ │ ← │ │ 1 │ │ 2 │ │ 3 │ │ ◊ │ │ B │ │ ← │ │ abc │ │ EOL │
2588 └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └─────┘ └─────┘</code>
2589
2590 </pre>
2591 </div></div>
2592 <div class="paragraph"><p>is first split into 2 statements:</p></div>
2593 <div class="listingblock">
2594 <div class="content">
2595 <pre>
2596
2597 <code> ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐
2598 │ A │ │ ← │ │ 1 │ │ 2 │ │ 3 │
2599 └───┘ └───┘ └───┘ └───┘ └───┘
2600 ┌───┐ ┌───┐ ┌─────┐
2601 │ B │ │ ← │ │ abc │
2602 └───┘ └───┘ └─────┘</code>
2603
2604 </pre>
2605 </div></div>
2606 <div class="paragraph"><p>The scalars 1, 2, and 3 are then combined into a single APL value (the APL
2607 value <em>abc</em> is already a single value after tokenization):</p></div>
2608 <div class="listingblock">
2609 <div class="content">
2610 <pre>
2611
2612 <code> ┌───┐ ┌───┐ ┌───────┐
2613 │ A │ │ ← │ │ 1 2 3 │
2614 └───┘ └───┘ └───────┘
2615 ┌───┐ ┌───┐ ┌─────┐
2616 │ B │ │ ← │ │ abc │
2617 └───┘ └───┘ └─────┘</code>
2618
2619 </pre>
2620 </div></div>
2621 <div class="paragraph"><p>Next, the individual statements are reversed:</p></div>
2622 <div class="listingblock">
2623 <div class="content">
2624 <pre>
2625
2626 <code> ┌───────┐ ┌───┐ ┌───┐
2627 │ 1 2 3 │ │ ← │ │ A │
2628 └───────┘ └───┘ └───┘
2629 ┌─────┐ ┌───┐ ┌───┐
2630 │ abc │ │ ← │ │ B │
2631 └─────┘ └───┘ └───┘</code>
2632
2633 </pre>
2634 </div></div>
2635 <div class="paragraph"><p>and finally combined again:</p></div>
2636 <div class="listingblock">
2637 <div class="content">
2638 <pre>
2639
2640 <code> ┌───────┐ ┌───┐ ┌───┐ ┌───┐ ┌─────┐ ┌───┐ ┌───┐ ┌────┐
2641 │ 1 2 3 │ │ ← │ │ A │ │ ◊ │ │ abc │ │ ← │ │ B │ │ EOL│
2642 └───────┘ └───┘ └───┘ └───┘ └─────┘ └───┘ └───┘ └────┘</code>
2643
2644 </pre>
2645 </div></div>
2646 <div class="paragraph"><p>The reason for reversing the tokens of every statement is somewhat subtle: APL
2647 statements are executed from left to right, but the tokens inside a statement
2648 are processed from right to left. Without the reversal the tokens would need to
2649 be processed like this:</p></div>
2650 <div class="listingblock">
2651 <div class="content">
2652 <pre>
2653
2654 <code> → → → → → → → → → → → → → → → → → → → → → → → →
2655 ↓ ↑ ↓
2656 ┌───┐ ┌───┐ ┌───────┐ ┌───┐ ┌───┐ ┌───┐ ┌─────┐ ┌─────┐
2657 │ A │←│ ← │←│ 1 2 3 │ │ ◊ │←│ B │←│ ← │←│ abc │ │ EOL │
2658 └───┘ └───┘ └───────┘ └───┘ └───┘ └───┘ └─────┘ └─────┘
2659 ↓ ↑
2660 → → → → → → → → → → → → → → → → → → → →</code>
2661
2662 </pre>
2663 </div></div>
2664 <div class="paragraph"><p>By reversing every statement the same processing order now becomes:</p></div>
2665 <div class="listingblock">
2666 <div class="content">
2667 <pre>
2668
2669 <code> ┌───────┐ ┌───┐ ┌───┐ ┌───┐ ┌─────┐ ┌───┐ ┌───┐ ┌─────┐
2670 │ 1 2 3 │→│ ← │→│ A │→│ ◊ │→│ abc │→│ ← │→│ B │→│ EOL │
2671 └───────┘ └───┘ └───┘ └───┘ └─────┘ └───┘ └───┘ └─────┘</code>
2672
2673 </pre>
2674 </div></div>
2675 <div class="paragraph"><p><strong>DEFINITION</strong>: in source code comments, we refer to the initial token order
2676 (statement tokens from left to right, statements from left to right,
2677 and lines from top to bottom) as the <strong>APL order</strong> and to the order after
2678 reversing the tokens of every statement (statement tokens from right to left,
2679 statements from left to right, and lines from top to bottom) as the
2680 <strong>reversed order</strong>. The reversal is performed in <strong>Executable::parse_body_line().
2681 In other words, function *Tokenizer::tokenize()</strong> produces a <strong>Token_string tos</strong>
2682 in APL order, which is later reversed into the final function body
2683 <strong>Token_string Executable::body</strong>.</p></div>
2684 <div class="paragraph"><p>The reversed order can be processed much faster since the order is now linear
2685 and no more back and forth jumps are needed when the token string is
2686 interpreted.</p></div>
2687 <div class="paragraph"><p>Another trick in the pre-parsing phase is to store the distance between
2688 opening and closing parentheses and square brackets (curly brackets are removed
2689 entirely in this phase) in the token itself. Sometimes the prefix-parser
2690 (<strong>Prefix.cc</strong>) that finally performs the interpretation has to jump
2691 over a pair of parentheses or brackets to find the token to their left.
2692 Instead of scanning the token string at runtime until a matching parenthesis
2693 or bracket is found, one can compute its location directly (i.e. in time
2694 O(1) rather than O(∆) for distance ∆ between the matching parentheses
2695 or brackets):</p></div>
2696 <div class="listingblock">
2697 <div class="content">
2698 <pre>
2699
2700 <code> ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
2701 │ (,∆ │ ... │ ),∆ │ or │ [,∆ │ ... │ ],∆ │
2702 └─────┘ └─────┘ └─────┘ └─────┘
2703 │← ← ← ∆ → → →│ │← ← ← ∆ → → →│</code>
2704
2705 </pre>
2706 </div></div>
2707 <div class="paragraph"><p>The Pre-parser function returns an error code and fills its argument <strong>tos</strong>
2708 as a side effect:</p></div>
2709 <div class="listingblock">
2710 <div class="content">
2711 <pre>
2712
2713 <code> Parser.hh:
2714
2715 /// Parse token string \b input into token string \b tos.
2716 ErrorCode parse(const Token_string &amp; input, Token_string &amp; tos) const;</code>
2717
2718 </pre>
2719 </div></div>
2720 <div class="paragraph"><p>The entire conversion (i.e. tokenization and pre-parsing
2721 happens in function <strong>Executable::parse_body_line()</strong>; the result is stored in
2722 member <strong>body</strong> of class Executable:</p></div>
2723 <div class="listingblock">
2724 <div class="content">
2725 <pre>
2726
2727 <code> Executable.hh:
2728
2729 /// The tokens to be executed. They are organized line by line and
2730 /// statement by statement, but the tokens within a statement are
2731 /// reversed due to the right-to-left execution of APL.
2732 Token_string body;</code>
2733
2734 </pre>
2735 </div></div>
2736 <div class="paragraph"><p>Every defined function, every line entered in immediate execution, and every
2737 statement from a ⍎ expression is converted into an object of base
2738 class <strong>Executable</strong>. That object contains some more information such as
2739 the original line(s) from which it was created, a reference counter to
2740 delete it if no longer used, etc.</p></div>
2741 <div class="paragraph"><p>From base class Executable three different classes for the different
2742 ParseModes are derived:</p></div>
2743 <div class="listingblock">
2744 <div class="content">
2745 <pre>
2746
2747 <code>class ExecuteList : public Executable { ...} // in Executable.hh
2748 class StatementList : public Executable { ... } // in Executable.hh
2749 class UserFunction : public Function, public Executable // in UserFunction.hh</code>
2750
2751 </pre>
2752 </div></div>
2753 <div class="paragraph"><p>This is because the minor differences between the ParseModes not only affect
2754 the parsing of the input line but also some runtime aspects. The base class
2755 Executable contains the common functionality while the derived classes
2756 contain the specific functionalities related to the different ParseModes.</p></div>
2757 <div class="paragraph"><p>Some debug printouts display the ParseMode, like this:</p></div>
2758 <div class="ulist"><ul>
2759 <li>
2760 <p>
2761 ∇ for defined functions,
2762 </p>
2763 </li>
2764 <li>
2765 <p>
2766 ◊ for a list of statements (i.e. immediate execution), and
2767 </p>
2768 </li>
2769 <li>
2770 <p>
2771 ⍎ for executed APL strings (⍎, ⎕EA, ⎕EB, ⎕ES).
2772 </p>
2773 </li>
2774 </ul></div>
2775 <div class="paragraph"><p>The difference between <strong>◊</strong> and <strong>⍎</strong> is mostly resolved during pre-parsing,
2776 therefore classes <strong>StatementList</strong> and <strong>ExecuteList</strong> are almost the same as
2777 class <strong>Executable</strong>. Both pertain to a single line of APL code and their
2778 implementation can be found in <strong>Executable.cc</strong>.</p></div>
2779 <div class="paragraph"><p>In contrast, defined functions are far more complex (multiple lines, labels,
2780 parameter passing) and have their own implementation file <strong>UserFunction.cc</strong>.</p></div>
2781 </div>
2782 <div class="sect2">
2783 <h3 id="_naming_of_apl_objects">Naming of APL objects</h3>
2784 <div class="paragraph"><p>To conclude the discussion of pre-parsing we should mention the role of names.
2785 Ignoring comments and white-space (which are discarded rather early), the
2786 lines of every APL program consists of 3 types of objects:</p></div>
2787 <div class="olist arabic"><ol class="arabic">
2788 <li>
2789 <p>
2790 literals (constants) like 1 2 3 or <em>abc</em>, and
2791 </p>
2792 </li>
2793 <li>
2794 <p>
2795 names of built-in primitives like + and -, system functions like ⎕UCS, and
2796 system variables like ⎕IO
2797 </p>
2798 </li>
2799 <li>
2800 <p>
2801 (user-defined) names of variables and defined functions, in the following
2802 called <strong>symbols</strong>.
2803 </p>
2804 </li>
2805 </ol></div>
2806 <div class="paragraph"><p>These objects are processed by the pre-parser according to the following
2807 rules:</p></div>
2808 <div class="olist arabic"><ol class="arabic">
2809 <li>
2810 <p>
2811 literals are converted to APL values,
2812 </p>
2813 </li>
2814 <li>
2815 <p>
2816 every primitive, system function, or system variable has an integer ID which
2817 uniquely identifies it, and
2818 </p>
2819 </li>
2820 <li>
2821 <p>
2822 every user defined name is entered into a symbol table, which is a hash
2823 table that contains all user defined names. The conversion from a name
2824 (i.e. string) to a user defined object happens during the pre-parsing (and
2825 therefore has no runtime overhead).
2826 </p>
2827 </li>
2828 </ol></div>
2829 <div class="paragraph"><p>The IDs of all primitives are <strong>#defined</strong> in <strong>Id.def</strong>. The Ids are sorted by
2830 the first character in their name (the character after ⎕ for system functions
2831 and variables) or e.g. "P" for Plus, "M" for Minus etc. These IDs are not only
2832 used in the body tokens of executables but also in )SAVEd .xml snapshots. It
2833 is therefore essential that new IDs are added at the end of the ID group that
2834 starts with the same character, and that obsolete IDs are kept (or at least
2835 their IDs are never reused). The first character (upper 8 bits of the ID are
2836 chosen in a way that simplifies debugging at little or no cost.</p></div>
2837 <div class="paragraph"><p>All user defined symbols have the ID 0x0000 and their token contains a
2838 unique <strong>Symbol <strong> pointer. The symbol table (class *SymbolTable</strong> in file
2839 <strong>SymbolTable.hh</strong>) is only used by the pre-parser (to ensure that all symbols
2840 have the same <strong>Symbol </strong>) and by some APL commands like *)FNS</strong>, <strong>)VARS</strong>,
2841 <strong>)OPS</strong>, etc.</p></div>
2842 <div class="paragraph"><p>The Id.def file classifies the around 200 IDs via 5 macros that indicate their
2843 purpose or usage:</p></div>
2844 <div class="ulist"><ul>
2845 <li>
2846 <p>
2847 macro <strong>pp(&#8230;)</strong>: <strong>p</strong>retty <strong>p</strong>rint. For constructing the name (usually
2848 "---") of a token that has no permanent role, e.g. pass C function arguments.
2849 E.g. NO_VALUE, VARIABLE, &#8230;
2850 </p>
2851 </li>
2852 <li>
2853 <p>
2854 macro <strong>qf(&#8230;)</strong>: <strong>q</strong>uad <strong>f</strong>unction. Quad function,
2855 e.g. ⎕DL, ⎕FX, &#8230;
2856 </p>
2857 </li>
2858 <li>
2859 <p>
2860 macro <strong>qv(&#8230;)</strong>: <strong>q</strong>uad <strong>v</strong>ariable. Quad variable,
2861 e.g. ⎕CT, ⎕IO, &#8230;
2862 </p>
2863 </li>
2864 <li>
2865 <p>
2866 macro <strong>sf(&#8230;)</strong>: <strong>s</strong>ystem <strong>f</strong>unction. An APL primitive,
2867 e.g. ⍴, ⌽, &#8230;
2868 &#8230;
2869 </p>
2870 </li>
2871 <li>
2872 <p>
2873 macro <strong>st(&#8230;)</strong>: <strong>s</strong>ystem <strong>t</strong>oken. An ID for other token,
2874 e.g. ⍺, →, &#8230;
2875 </p>
2876 </li>
2877 </ul></div>
2878 </div>
2879 <div class="sect2">
2880 <h3 id="_token_structure">Token Structure</h3>
2881 <div class="paragraph"><p>With the prerequisites above we can define the structure of a <strong>Token</strong>:</p></div>
2882 <div class="listingblock">
2883 <div class="content">
2884 <pre>
2885
2886 <code> Token.hh:
2887
2888 class Token
2889 {
2890 ...
2891
2892 /// the (optional) value of the token.
2893 union sval
2894 {
2895 Unicode char_val; ///&lt; the Unicode for TV_CHAR
2896 APL_Integer int_vals[2]; ///&lt; the integer for TV_INT
2897 APL_Float_Base float_vals[2]; ///&lt; the doubles for TV_FLT and TV_CPX
2898 Symbol * sym_ptr; ///&lt; the symbol for TV_SYM
2899 Function_Line fun_line; ///&lt; the function line for TV_LIN
2900 IndexExpr * index_val; ///&lt; the index for TV_INDEX
2901 Function_P function; ///&lt; the function for TV_FUN
2902 Value_P_Base apl_val; ///&lt; the APL value for TV_VAL
2903
2904 /// a shortcut for accessing apl_val
2905 Value_P &amp; _apl_val() const
2906 { return reinterpret_cast&lt;Value_P &amp;&gt;
2907 (const_cast&lt;Value_P_Base &amp;&gt;(apl_val)); }
2908 };
2909
2910 ...
2911
2912 protected:
2913
2914 /// The tag indicating the type of \b this token
2915 TokenTag tag;
2916
2917 /// The value of \b this token
2918 sval value;
2919
2920 ...
2921 }</code>
2922
2923 </pre>
2924 </div></div>
2925 <div class="paragraph"><p>In short this means that:</p></div>
2926 <div class="ulist"><ul>
2927 <li>
2928 <p>
2929 a token is a tagged union (named <strong>sval</strong>), and
2930 </p>
2931 </li>
2932 <li>
2933 <p>
2934 the tag (an <strong>enum TokenTag</strong>) determines which member (if any) of <strong>union sval</strong>
2935 a particular token holds.
2936 </p>
2937 </li>
2938 </ul></div>
2939 <div class="paragraph"><p>The APL interpreter can be understood as virtual machine whose opcodes are
2940 Tokens. In this context the attribute <strong>virtual</strong> of <em>virtual machine</em> must not
2941 be confused with the attribute <strong>virtual</strong> of <em>virtual C++ functions</em> or
2942 <em>classes</em>. The tokenization and pre-parsing is then the "compilation" of APL
2943 source code into machine code of this virtual APL machine.
2944 The rest of <strong>Token.hh</strong> defines quite a number of constructors and access
2945 functions of class Token, most of them <em>inline</em> because the manipulation
2946 of Token is usually simple but performed frequently.</p></div>
2947 <div class="paragraph"><p>The <strong>TokenTag</strong> is a 32-bit (4 byte) number which has 3 sub-fields:</p></div>
2948 <div class="listingblock">
2949 <div class="content">
2950 <pre>
2951
2952 <code> ------16 bit------ ---8 bit--- ---8 bit---
2953 ┌────────┬─────────┬────────────┬─────────────┐
2954 Token Tag: │ ID │ Value-type │ Token-class │
2955 └────────┴─────────┴────────────┴─────────────┘</code>
2956
2957 </pre>
2958 </div></div>
2959 <div class="sect3">
2960 <h4 id="_the_id_of_a_token">The ID of a Token</h4>
2961 <div class="paragraph"><p>The 16 bit <strong>ID</strong> is the integer ID of system functions and variables explained
2962 above. The ID (i.e. its access function Token::get_Id()) is not used during
2963 the normal interpretation of APL code but only in error messages and for
2964 loading and saving of workspaces.</p></div>
2965 </div>
2966 <div class="sect3">
2967 <h4 id="_the_value_type_of_a_token">The Value-Type of a token</h4>
2968 <div class="paragraph"><p>The value type simply defines which of the members of <strong>union sval</strong> the token
2969 holds. The value type is only used when a Token is copied or moved around
2970 (most likely from the body of a defined function into the prefix parser as
2971 will be explained below). The knowledge of the type is needed because some
2972 the <strong>union sval</strong> members have constructors.</p></div>
2973 <div class="paragraph"><p>The value types are defined in <strong>TokenEnums.hh</strong>:</p></div>
2974 <div class="listingblock">
2975 <div class="content">
2976 <pre>
2977
2978 <code>TokenEnums.hh:
2979
2980 /// The value type of a token
2981
2982 enum TokenValueType
2983 {
2984 // token value types. The token value type defines the type of the
2985 // token in the union 'value'.
2986 // Type union member
2987 TV_MASK = 0xFF00,
2988 TV_NONE = 0x0000, // value not used
2989 TV_CHAR = 0x0100, // Unicode .char_val
2990 TV_INT = 0x0200, // uint64_t .int_val;
2991 TV_FLT = 0x0300, // APL_Float .flt_val;
2992 TV_CPX = 0x0400, // cdouble .complex_val;
2993 TV_SYM = 0x0500, // Symbol * .sym_ptr;
2994 TV_LIN = 0x0600, // Function_Line .fun_line;
2995 TV_VAL = 0x0700, // Value_P .apl_val;
2996 TV_INDEX = 0x0800, // IndexExpr * .index_val;
2997 TV_FUN = 0x0900, // Function_P .function;
2998 };</code>
2999
3000 </pre>
3001 </div></div>
3002 <div class="paragraph"><p>The typical (inline) function that copies a Token is:</p></div>
3003 <div class="listingblock">
3004 <div class="content">
3005 <pre>
3006
3007 <code>Token.hh:
3008
3009 Token::copy_N(const Token &amp; src)
3010 {
3011 tag = src.tag;
3012 switch(src.get_ValueType())
3013 {
3014 case TV_NONE: value.int_vals[0] = 0;
3015 value.int_vals[1] = 0; break;
3016 case TV_CHAR: value.char_val = src.value.char_val; break;
3017 case TV_INT: value.int_vals[0] = src.value.int_vals[0];
3018 value.int_vals[1] = src.value.int_vals[1]; break;
3019 case TV_FLT: value.float_vals[0] = src.value.float_vals[0]; break;
3020 case TV_CPX: value.float_vals[0] = src.value.float_vals[0];
3021 value.float_vals[1] = src.value.float_vals[1]; break;
3022 case TV_SYM: value.sym_ptr = src.value.sym_ptr; break;
3023 case TV_LIN: value.fun_line = src.value.fun_line; break;
3024 case TV_VAL: value._apl_val() = src.value._apl_val(); break;
3025 case TV_INDEX: value.index_val = src.value.index_val; break;
3026 case TV_FUN: value.function = src.value.function; break;
3027 default: FIXME;
3028 }
3029 }</code>
3030
3031 </pre>
3032 </div></div>
3033 <div class="paragraph"><p>In <strong>copy_N()</strong>, <strong>src</strong> is a token that is copied into token <strong>this</strong>, and <strong>value</strong>
3034 is the member (of <strong>union sval</strong>) that is being copied.</p></div>
3035 </div>
3036 <div class="sect3">
3037 <h4 id="_the_tokenclass_type_of_a_token">The TokenClass-Type of a token</h4>
3038 <div class="paragraph"><p>The most important part of a <strong>TokenTag</strong> is its class (not to be confused with
3039 C++ classes). Like the token value types they are defined in <strong>TokenEnums.hh</strong>.
3040 There are two different kinds of token classes: <strong>permanent token class</strong>es and
3041 <strong>temporary token class</strong>es. The permanent classes are:</p></div>
3042 <div class="listingblock">
3043 <div class="content">
3044 <pre>
3045
3046 <code>TokenEnums.hh:
3047
3048 enum TokenClass
3049 {
3050 // token classes.
3051 //
3052
3053 // permanent token classes. Only Token of these classes can appear in
3054 // the body of a defined function.
3055 //
3056 TC_ASSIGN = 0x01, ///&lt; ←
3057 TC_R_ARROW = 0x02, ///&lt; →N
3058 TC_L_BRACK = 0x03, ///&lt; [ or ;
3059 TC_R_BRACK = 0x04, ///&lt; ]
3060 TC_END = 0x05, ///&lt; left end of statement
3061 TC_FUN0 = 0x06, ///&lt; niladic function
3062 TC_FUN12 = 0x07, ///&lt; ambivalent function
3063 TC_INDEX = 0x08, ///&lt; [...]
3064 TC_OPER1 = 0x09, ///&lt; monadic operator
3065 TC_OPER2 = 0x0A, ///&lt; dyadic operator
3066 TC_L_PARENT = 0x0B, ///&lt; (
3067 TC_R_PARENT = 0x0C, ///&lt; )
3068 TC_RETURN = 0x0D, ///&lt; return from defined function
3069 TC_SYMBOL = 0x0E, ///&lt; user defined name
3070 TC_VALUE = 0x0F, ///&lt; APL value
3071
3072 TC_MAX_PERM, ///&lt; permanent token are &lt; TC_MAX_PERM
3073 ...</code>
3074
3075 </pre>
3076 </div></div>
3077 <div class="paragraph"><p>In contrast, temporary tokens are never used in the body of an <strong>Executable</strong>.
3078 They are short-lived and vanish after some permanent token was created from
3079 them. For example, prefix parser creates a token of class <strong>TC_PINDEX</strong> (for
3080 partial index) when it sees the closing <strong>]</strong> of an APL index (remember:
3081 right-to-left) and creates a permanent token of class TC_INDEX when it sees
3082 the corresponding opening <strong>[</strong>. Likewise, a defined function without result
3083 returns a token of class TC_VOID which is passed from one prefix parser to
3084 another to indicate the result of a defined function.</p></div>
3085 <div class="paragraph"><p>In short, temporary token classes are only created and consumed inside the
3086 prefix parser. The temporary token classes are:</p></div>
3087 <div class="listingblock">
3088 <div class="content">
3089 <pre>
3090
3091 <code>TokenEnums.hh:
3092 ...
3093 // temporary Token classes. Token of these classes only appear as
3094 // intermediate results during tokenization and prefix parsing
3095 //
3096 TC_PINDEX = 0x10, ///&lt; partial index
3097 TC_VOID = 0x11,
3098 TC_MAX_PHRASE, ///&lt; token in phrases are &lt; TC_MAX_PHRASE
3099 TC_MAX_PHRASE_2 = TC_MAX_PHRASE*TC_MAX_PHRASE, // TC_MAX_PHRASE ^ 2
3100 TC_MAX_PHRASE_3 = TC_MAX_PHRASE*TC_MAX_PHRASE_2, // TC_MAX_PHRASE ^ 3
3101 TC_MAX_PHRASE_4 = TC_MAX_PHRASE*TC_MAX_PHRASE_3, // TC_MAX_PHRASE ^ 4
3102
3103 TC_OFF = 0x12,
3104 TC_SI_LEAVE = 0x13,
3105 TC_LINE = 0x14,
3106 TC_DIAMOND = 0x15, // ◊
3107 TC_NUMERIC = 0x16, // 0-9, ¯
3108 TC_SPACE = 0x17, // space, tab, CR (but not LF)
3109 TC_NEWLINE = 0x18, // LF
3110 TC_COLON = 0x19, // :
3111 TC_QUOTE = 0x1A, // ' or "
3112 TC_L_CURLY = 0x1B, // {
3113 TC_R_CURLY = 0x1C, // }</code>
3114
3115 </pre>
3116 </div></div>
3117 <div class="paragraph"><p>The phrase table (or phrase tree) described in
3118 <a href="#_the_phrase_table">The Phrase Table</a>
3119 below maps particular sequences of token classes
3120 to reduce functions. To make the phrase table a little more readable,
3121 we define some shorter names (SN_xxx) for token classes that better reflect
3122 the relation between the APL point of view and the C++ representation of
3123 it, and/or abbreviates them:</p></div>
3124 <div class="listingblock">
3125 <div class="content">
3126 <pre>
3127
3128 <code>TokenEnums.hh:
3129 ...
3130 // shorter token class aliases for the phrase table
3131 //
3132 SN_A = TC_VALUE,
3133 SN_ASS = TC_ASSIGN,
3134 SN_B = TC_VALUE,
3135 SN_C = TC_INDEX,
3136 SN_D = TC_OPER2,
3137 SN_END = TC_END,
3138 SN_F = TC_FUN12,
3139 SN_G = TC_FUN12,
3140 SN_GOTO = TC_R_ARROW,
3141 SN_I = TC_PINDEX,
3142 SN_LBRA = TC_L_BRACK,
3143 SN_LPAR = TC_L_PARENT,
3144 SN_M = TC_OPER1,
3145 SN_N = TC_FUN0,
3146 SN_RETC = TC_RETURN,
3147 SN_RBRA = TC_R_BRACK,
3148 SN_RPAR = TC_R_PARENT,
3149 SN_V = TC_SYMBOL,
3150 SN_VOID = TC_VOID,
3151 SN_ = TC_INVALID</code>
3152
3153 </pre>
3154 </div></div>
3155 <div class="paragraph"><p>For example: the ISO APL standard defines a phrase table of 39 phrases. In
3156 particular phrase 7 is defined as:</p></div>
3157 <div class="paragraph"><p><em>7 EVALUATE DYADIC FUNCTION (&lt;A F B&gt; R)</em></p></div>
3158 <div class="paragraph"><p>In GNU APL code the ISO phrase <strong>&lt;A F B&gt;</strong> is represented as 3 short-names
3159 <strong>SN_A, SN_F, SN_B,</strong>. To further simplify matters, the <strong>SN_</strong> prefix is hidden
3160 by means of some macro magic, so that the same ISO phrase in the GNU APL
3161 phrase table (in <strong>Prefix.def</strong>) becomes:</p></div>
3162 <div class="listingblock">
3163 <div class="content">
3164 <pre>
3165
3166 <code> Prefix.def :
3167
3168 PH( A F B , A_F_B_ , 0x03CEF , 33 , 0 , 3), // [EA]</code>
3169
3170 </pre>
3171 </div></div>
3172 <div class="paragraph"><p>Given that <strong>SN_A</strong> and <strong>SN B</strong> are both short names for <strong>TC_VALUE</strong>, and that
3173 <strong>SN_F</strong> the short name for <strong>TC_FUN12</strong>, after expanding macro PH() in the C++
3174 preprocessor this translated to something like:</p></div>
3175 <div class="paragraph"><p><em>If a sequence of token classes TC_VALUE TC_FUN12 TC_VALUE is discovered, then
3176 call C++ function reduce_A_F_B().</em></p></div>
3177 </div>
3178 </div>
3179 </div>
3180 </div>
3181 <div class="sect1">
3182 <h2 id="_evaluation_of_apl_expression">Evaluation of APL Expression</h2>
3183 <div class="sectionbody">
3184 <div class="sect2">
3185 <h3 id="_the_phrase_table">The Phrase Table</h3>
3186 <div class="paragraph"><p>The IBM APL Language Reference Manual describes the fundamental rule for the
3187 evaluation of APL expressions as follows:</p></div>
3188 <div class="paragraph"><p><em>All functions execute according to their position within an expression.
3189 The rightmost function whose arguments are available is evaluated first.</em></p></div>
3190 <div class="paragraph"><p>In parser terms, one could just as well formulate this as:</p></div>
3191 <div class="ulist"><ul>
3192 <li>
3193 <p>
3194 execution of an APL expression starts with an empty sequence of token
3195 classes,
3196 </p>
3197 </li>
3198 <li>
3199 <p>
3200 the tokens in the sequence are then, from right to left, prepended to the
3201 sequence, and
3202 </p>
3203 </li>
3204 <li>
3205 <p>
3206 whenever a prefix of the current sequence is found in the phrase table:
3207 </p>
3208 <div class="ulist"><ul>
3209 <li>
3210 <p>
3211 the reduce function in the phrase table entry is called with the token of
3212 the prefix as arguments,
3213 </p>
3214 </li>
3215 <li>
3216 <p>
3217 the prefix in the sequence is replaced with the result of the reduce
3218 function.
3219 </p>
3220 </li>
3221 </ul></div>
3222 </li>
3223 </ul></div>
3224 <div class="paragraph"><p>Since GNU APL stores the token in reverse order as explained earlier, from
3225 right to left actually means left to right for the token string that is being
3226 interpreted. The rules above amount to what is commonly called a LALR(1)
3227 parser (short for <strong>L</strong>ook<strong>A</strong>head-<strong>L</strong>eft<strong>R</strong>ight-Parser with 1 lookahead
3228 token). In GNU APL terminology it is the <strong>Prefix parser</strong> in file <strong>Prefix.cc</strong>.
3229 The somewhat simplified algorithm of the GNU APL prefix parser is:</p></div>
3230 <div class="olist arabic"><ol class="arabic">
3231 <li>
3232 <p>
3233 Every Prefix parser starts execution at the beginning of some token string,
3234 say <strong>body</strong>, which is in most cases the body of a defined function.
3235 </p>
3236 <div class="olist loweralpha"><ol class="loweralpha">
3237 <li>
3238 <p>
3239 Its integer variable <strong>PC</strong> (actually of type <strong>enum Function_PC</strong>) is
3240 initialized to 0 (start of function), and
3241 </p>
3242 </li>
3243 <li>
3244 <p>
3245 the sequence of tokens read so far is cleared. Then:
3246 </p>
3247 </li>
3248 </ol></div>
3249 </li>
3250 <li>
3251 <p>
3252 The next token is read and PC is incremented.
3253 </p>
3254 </li>
3255 <li>
3256 <p>
3257 If the token class is <strong>TC_SYMBOL</strong> (i.e. a name defined by the APL programmer)
3258 then the new token is replaced by the current value of the symbol, which is
3259 one of:
3260 </p>
3261 <div class="olist loweralpha"><ol class="loweralpha">
3262 <li>
3263 <p>
3264 <strong>TC_VOID</strong> - the symbol name was used (tokenized) but not assigned (e.g.
3265 the initial values of local variables of a defined function),
3266 </p>
3267 </li>
3268 <li>
3269 <p>
3270 <strong>TC_VALUE</strong> - an APL variable,
3271 </p>
3272 </li>
3273 <li>
3274 <p>
3275 <strong>TC_FUN0</strong> - a defined niladic function,
3276 </p>
3277 </li>
3278 <li>
3279 <p>
3280 <strong>TC_FUN12</strong> - a defined monadic or dyadic function,
3281 </p>
3282 </li>
3283 <li>
3284 <p>
3285 <strong>TC_OPER1</strong> - a defined monadic operator (i.e. 1 function argument), or
3286 </p>
3287 </li>
3288 <li>
3289 <p>
3290 <strong>TC_OPER2</strong> - a defined dyadic operator (i.e. 2 function argument).
3291 This name resolution step happens in function
3292 </p>
3293 <div class="ulist"><ul>
3294 <li>
3295 <p>
3296 <strong>Symbol::resolve_right()</strong> for symbols right of <strong>←</strong> or in
3297 </p>
3298 </li>
3299 <li>
3300 <p>
3301 <strong>Symbol::resolve_left()</strong> for symbols left of <strong>←</strong>. The case <strong>TC_VOID</strong> happens
3302 when a name was tokenized, but no value or function was assigned to it.
3303 </p>
3304 </li>
3305 </ul></div>
3306 </li>
3307 <li>
3308 <p>
3309 If the new token together with the sequence matches a phrase (as discussed
3310 above) then the phrase is reduced (called REDUCE in parser terminology).
3311 </p>
3312 </li>
3313 </ol></div>
3314 </li>
3315 <li>
3316 <p>
3317 Otherwise (no phrase matches) the new token is prepended to the sequence and
3318 the next token is read (called SHIFT in parser terminology). In this case
3319 execution continues with step 2. above.
3320 </p>
3321 </li>
3322 <li>
3323 <p>
3324 If a phrase with an APL jump, such as <strong>→N</strong> (i.e. to line <strong>N</strong> of a defined
3325 function) is matched, then a new value for the <strong>PC</strong> is computed so
3326 that <strong>body[PC]</strong> is the first token of line N. If no such line exists,
3327 then the Prefix parser returns its result (either TOK_VOID for functions
3328 that return no result or TOK_VALUE otherwise.
3329 </p>
3330 </li>
3331 <li>
3332 <p>
3333 If the parser sees a closing parenthesis <strong>)</strong> or bracket <strong>]</strong> then the parser
3334 creates a sub-parser and execution is continued (with the current PC) in
3335 the sub-parser until the corresponding opening parenthesis <strong>(</strong> or bracked
3336 <strong>[</strong> is reached. The result of the sub-parser, typically a token with class
3337 <strong>TC_VALUE</strong> for parentheses and <strong>TC_INDEX</strong> for brackets is returned to the
3338 calling parser,
3339 </p>
3340 </li>
3341 <li>
3342 <p>
3343 an end of statement token (class TC_END) does not stop the parser. The
3344 necessary actions, e,g, printing of the value for non-committed APL values
3345 is performed in the reduce function (which calls
3346 <strong>StateIndicator::statement_result()</strong>), and the parser simply continues after
3347 the reduce function returns. Only the end of the <strong>body</strong> stops the parser,
3348 and the last token in the <strong>body</strong> performs the transfer of the value computed
3349 by a defined APL function to its caller.
3350 </p>
3351 </li>
3352 </ol></div>
3353 <div class="paragraph"><p>The tokens in the current sequence are of C++ type <strong>Token_loc</strong> instead of
3354 type <strong>Token</strong>. <strong>Token_loc</strong> is a Token and a range in the <strong>body</strong>. When a
3355 token is first fetched from, say, <strong>body[PC]</strong>) then the range of its
3356 <strong>Token_loc</strong> is <strong>[PC:PC+1]</strong>. As more and more consecutive tokens are being
3357 reduced, that range grows and contains all tokens that have contributed to
3358 the creation of every token. The range in the body of a defined function
3359 is needed for proper error reporting.</p></div>
3360 <div class="paragraph"><p>The case 3. above is the most tricky one and deserves a closer look at it.</p></div>
3361 <div class="sect3">
3362 <h4 id="_ambiguity_of_apl_symbols">Ambiguity of APL symbols.</h4>
3363 <div class="paragraph"><p>In compiled lanuages like C/C++ every name defined by the user must be
3364 <em>declared</em>. Any use of a name without a prior declaration is an error detected
3365 by the compiler. The declaration determines, among other things, whether the
3366 name refers to a variable, or to a function, and in that latter case the arity
3367 (number of arguments) of a function.</p></div>
3368 <div class="paragraph"><p>In contrast, in APL the role of a user defined name is not defined at
3369 ⎕FX-time but at runtime. Due to the declarations in e.g. C/C++ the scope
3370 of a name (and from that the relevant declaration) can be determined
3371 statically (i.e. at compile time) while in APL the role of a name must
3372 be inevitably determined at runtime. In both cases (APL vs. C/C++) the
3373 same name may have different roles at different times, but the point in
3374 time where the role is determined varies. As a consequence, the compiler uses
3375 its symbol table only at compile time while APL also uses it at runtime.
3376 Consider the following example:</p></div>
3377 <div class="listingblock">
3378 <div class="content">
3379 <pre>
3380
3381 <code> )CLEAR
3382
3383 ⍝ define function LEAF (for a function that does not call others)
3384 ⍝
3385 ∇Z←LEAF
3386 Z←'LEAF is a niladic defined function.'
3387 ∇
3388
3389 ⍝ define function FOO, which localizes symbol LEAF
3390 ⍝
3391 ∇Z←FOO;LEAF
3392 LEAF←'LEAF is a (localized) variable.'
3393 BAR
3394 ∇
3395
3396 ⍝ define function BAR which calls LEAF.
3397 ⍝
3398 ∇Z←BAR
3399 Z←LEAF
3400 ∇
3401
3402 ⍝ then see what happens
3403 ⍝
3404 FOO ◊ BAR
3405 LEAF is a (localized) variable.
3406 LEAF is a niladic defined function.</code>
3407
3408 </pre>
3409 </div></div>
3410 <div class="paragraph"><p>In the simple example above, <strong>BAR</strong> is called twice: first from <strong>FOO</strong> and then
3411 directly from immediate execution (to simplify the example; the same
3412 would happen if <strong>BAR</strong> were called from some other defined function). The
3413 problem in the example above is that <strong>BAR</strong> cannot statically decide whether
3414 the symbol <strong>LEAF</strong> in its function body relates to the niladic function <strong>LEAF</strong>
3415 or to the local variable <strong>LEAF</strong> of function <strong>FOO</strong>. One can even construct
3416 more complex examples where <em>the same statement</em> of a defined function
3417 must, according to the evaluation rules of APL, produce <em>different results</em>
3418 depending how it was called. As a</p></div>
3419 <div class="paragraph"><p><em>Corollary:</em> The parse tree of an APL statement is only unique when the
3420 statement contains no user defined symbols. And therefore:</p></div>
3421 <div class="paragraph"><p><em>Corollary:</em> In general, every APL statement yields a (possibly degenerated)
3422 parse tree, where each APL symbol in the statement is a branching point at
3423 which it has to be decided (at runtime) which branch down the tree shall be
3424 chosen.</p></div>
3425 <div class="paragraph"><p>The degenerated case is the one where all symbols can be resolved statically
3426 (such as labels in a defined function), and then the tree becomes a linear
3427 sequence. In real-life APL programs this degenerated case is fairly rare. It
3428 could be optimized in a simple manner, but its rarity seems not be worth the
3429 effort. A more frequent exception is non-conditional branches like <strong>→LABEL</strong>
3430 which are optimized in GNU APL.</p></div>
3431 <div class="paragraph"><p>Now, back to the <strong>TC_SYMBOL</strong>. If the prefix parser reads a symbol, then
3432 it consults the symbol table for the current meaning of the symbol.
3433 There are quite a few different cases:</p></div>
3434 <div class="ulist"><ul>
3435 <li>
3436 <p>
3437 The symbol is undefined, for example a local variable before a value is
3438 assigned to it or a defined function with that name was created. In this
3439 case:
3440 </p>
3441 <div class="ulist"><ul>
3442 <li>
3443 <p>
3444 If the symbol is referenced (i.e. on the right of →) then a SYNTAX ERROR
3445 is raised, otherwise
3446 </p>
3447 </li>
3448 <li>
3449 <p>
3450 The symbol is (the name of) a variable that is being assigned (either
3451 completely, or indexed, or selectively).
3452 </p>
3453 </li>
3454 </ul></div>
3455 </li>
3456 <li>
3457 <p>
3458 The symbol is a defined function, in that case the <strong>TC_SYMBOL</strong> token is,
3459 depending on the signature of the defined function, replaced with a token
3460 of class <strong>TC_FUN0</strong>, <strong>TC_FUN12</strong>, <strong>TC_OPER1</strong>, or <strong>TC_OPER2</strong>. The token value
3461 then points to a <strong>UserFunction</strong>. Note that APL primitives and APL system
3462 functions are not user-defined, and therefore no symbol table lookup is
3463 required. The pre-parser can therefore produce the corresponding tokens
3464 directly.
3465 </p>
3466 </li>
3467 <li>
3468 <p>
3469 The symbol is a variable.
3470 </p>
3471 <div class="ulist"><ul>
3472 <li>
3473 <p>
3474 A variable right of ← is referenced and the <strong>TC_SYMBOL</strong> token is replaced
3475 by a <strong>TC_VALUE</strong> token pointing to the current value of the variable.
3476 Otherwise the symbol is subject to an assignment and is, for the moment,
3477 left unchanged. At a later time one of the assignment phrases is supposed
3478 to match:
3479 </p>
3480 <div class="ulist"><ul>
3481 <li>
3482 <p>
3483 <strong>A←VALUE</strong> ⍝ normal assignment (symbol <strong>A</strong> may or may not exist),
3484 </p>
3485 </li>
3486 <li>
3487 <p>
3488 <strong>(A B &#8230;)←VALUE</strong> ⍝ vector assignment (symbols <strong>A</strong>, <strong>B</strong>&#8230; may or may
3489 not exist),
3490 </p>
3491 </li>
3492 <li>
3493 <p>
3494 <strong>A[X]←VALUE</strong> ⍝ indexed assignment (<strong>A</strong> must be a variable),
3495 </p>
3496 </li>
3497 <li>
3498 <p>
3499 <strong>(f&#8230; A)←VALUE</strong> ⍝ selective assignment (<strong>A</strong> must be a variable),
3500 </p>
3501 </li>
3502 </ul></div>
3503 </li>
3504 </ul></div>
3505 </li>
3506 </ul></div>
3507 <div class="paragraph"><p>All this happens in <strong>Prefix::push_Symbol()</strong> which delegates most of its work
3508 to either <strong>Symbol::resolve_left()</strong> or <strong>Symbol::resolve_right()</strong>, depending
3509 on the position of the symbol in the statement. This position is tracked
3510 with <strong>Prefix::set_assign_state()</strong>, which is one of:</p></div>
3511 <div class="listingblock">
3512 <div class="content">
3513 <pre>
3514
3515 <code>APL_types.hh:
3516 ...
3517 // the state of an assignment
3518 enum Assign_state
3519 {
3520 ASS_none = 0, ///&lt; no assignment (right of ←)
3521 ASS_arrow_seen = 1, ///&lt; ← seen but no variable yet
3522 ASS_var_seen = 2, ///&lt; var and ← seen
3523 ASS_unknown = 3, ///&lt; not known (too much effort to determine)
3524 };
3525 ...</code>
3526
3527 </pre>
3528 </div></div>
3529 <div class="admonitionblock">
3530 <table><tr>
3531 <td class="icon">
3532 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
3533 </td>
3534 <td class="content"><strong>Terminology:</strong> The GNU APL prefixes are called <strong>pattern</strong> or <strong>phrase</strong>
3535 in the ISO standard. The token sequence above is called <strong>stack</strong> in the ISO
3536 standard and also in some of GNU APL debug printouts.</td>
3537 </tr></table>
3538 </div>
3539 <div class="paragraph"><p>The main implementation of the algorithm above is C++ function
3540 <strong>Prefix::reduce_statements()</strong>. It returns a <strong>Token</strong>. The function
3541 uses C/C++ goto statements because that leads to much clearer code than the
3542 so called "structured programming" would. The goto labels in this function
3543 are:</p></div>
3544 <div class="ulist"><ul>
3545 <li>
3546 <p>
3547 <strong>grow:</strong> do a SHIFT (if no phrase has matched),
3548 </p>
3549 </li>
3550 <li>
3551 <p>
3552 <strong>again:</strong> repeat phrase matching (after a reduce function was called), and
3553 </p>
3554 </li>
3555 <li>
3556 <p>
3557 <strong>done:</strong> the parser returns.
3558 </p>
3559 </li>
3560 </ul></div>
3561 <div class="paragraph"><p>The point of <strong>again:</strong> is that the reduction of some phrases, for example an
3562 expression in parentheses, may produce a stack that could not be reduced
3563 before but now can without reading another token.</p></div>
3564 </div>
3565 </div>
3566 <div class="sect2">
3567 <h3 id="_an_example">An Example</h3>
3568 <div class="paragraph"><p>Consider the APL code from our example above, for simplicity in immediate
3569 execution:</p></div>
3570 <div class="literalblock">
3571 <div class="content">
3572 <pre>
3573
3574 <code>A←1 2 3</code>
3575
3576 </pre>
3577 </div></div>
3578 <div class="paragraph"><p>The tokenizer translates the string "A←1 2 3" into a 4-item <strong>Token_string</strong>:</p></div>
3579 <div class="literalblock">
3580 <div class="content">
3581 <pre>
3582
3583 <code>┌───────┐ ┌───┐ ┌───┐ ┌────────┐
3584 │ 1 2 3 │ │ ← │ │ A │ │ RETURN │
3585 └───────┘ └───┘ └───┘ └────────┘</code>
3586
3587 </pre>
3588 </div></div>
3589 <div class="paragraph"><p>To be precise, the <strong>RETURN</strong> token is actually appended by function
3590 <strong>StatementList::fix()</strong> and not by the Tokenizer itself. Then:</p></div>
3591 <div class="olist arabic"><ol class="arabic">
3592 <li>
3593 <p>
3594 The prefix parser reads (the single) token <strong>1 2 3</strong> and shifts.
3595 Pattern is now: <strong>value(1 2 3)</strong>.
3596 </p>
3597 </li>
3598 <li>
3599 <p>
3600 The prefix parser reads <strong>←</strong> and shifts. Pattern is now: <strong>← value(1 2 3)</strong>.
3601 </p>
3602 </li>
3603 <li>
3604 <p>
3605 The prefix parser reads <strong>A</strong>. The pattern matches and the parser
3606 reduces with <strong>Prefix::reduce_A_ASS_B_()</strong>. The result is
3607 <strong>committed_value(1 2 3)</strong> and the new pattern is also:
3608 <strong>committed_value(1 2 3)</strong>
3609 </p>
3610 </li>
3611 <li>
3612 <p>
3613 The prefix parser reads <strong>RETURN</strong>. The pattern matches and the parser
3614 reduces with <strong>Prefix::reduce_END_B__()</strong>, which calls
3615 <strong>StateIndicator::statement_result()</strong> and clears the pattern to empty.
3616 </p>
3617 </li>
3618 <li>
3619 <p>
3620 The parser has no more token to read and returns.
3621 </p>
3622 </li>
3623 </ol></div>
3624 </div>
3625 <div class="sect2">
3626 <h3 id="_another_example">Another Example</h3>
3627 <div class="literalblock">
3628 <div class="content">
3629 <pre>
3630
3631 <code>(1 + 2) FOO 4 ⍝ with dyadic defined function FOO</code>
3632
3633 </pre>
3634 </div></div>
3635 <div class="paragraph"><p>The tokenizer produces:</p></div>
3636 <div class="literalblock">
3637 <div class="content">
3638 <pre>
3639
3640 <code>┌───┐ ┌─────┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌────────┐
3641 │ 4 │ │ FOO │ │ ) │ │ 2 │ │ + │ │ 1 │ │ ( │ │ RETURN │
3642 └───┘ └─────┘ └───┘ └───┘ └───┘ └───┘ └───┘ └────────┘</code>
3643
3644 </pre>
3645 </div></div>
3646 <div class="olist arabic"><ol class="arabic">
3647 <li>
3648 <p>
3649 The parser shifts <strong>4</strong> and <strong>FOO</strong> and creates a sub-parser for ).
3650 </p>
3651 </li>
3652 <li>
3653 <p>
3654 The sub-parser reduces <strong>(1+2)</strong> and returns the value <strong>3</strong>.
3655 </p>
3656 </li>
3657 <li>
3658 <p>
3659 The parser reads the result <strong>3</strong> and computes <strong>3 FOO 4</strong>.
3660 </p>
3661 </li>
3662 </ol></div>
3663 <div class="sect3">
3664 <h4 id="_properties_of_the_reduce_xxx_functions">Properties of the reduce_XXX() functions</h4>
3665 <div class="ulist"><ul>
3666 <li>
3667 <p>
3668 In GNU APL there is a total of 67 phrases and consequently 67 reduce
3669 functions.
3670 </p>
3671 </li>
3672 <li>
3673 <p>
3674 reduce functions are typically short. Nevertheless they cannot be in-lined
3675 since they are being called via the function pointer
3676 <strong>Prefix::Phrase::reduce_fun</strong> in the phrase table (or tree).
3677 </p>
3678 </li>
3679 <li>
3680 <p>
3681 reduce functions have return type <strong>void</strong> and no arguments. They access the
3682 token which they reduce directly from a ring-buffer named <strong>Prefix::content</strong>
3683 and also store the result there (function <strong>Prefix::pop_args_push_result()</strong>)
3684 </p>
3685 </li>
3686 <li>
3687 <p>
3688 accessing the token is performed via functions <strong>at0()</strong>, <strong>at1()</strong>, <strong>at2()</strong>, or
3689 <strong>at3()</strong>, where <strong>at0()</strong> is the leftmost (in APL code order) and the rightmost
3690 (in reversed order) token. For example, in <strong>reduce_V_ASS_B()</strong> corresponds <strong>V</strong>
3691 to <strong>at0()</strong>, <strong>ASS</strong> to <strong>at1()</strong>, and <strong>B</strong> to <strong>at2()</strong>.
3692 </p>
3693 </li>
3694 <li>
3695 <p>
3696 the lookahead token of the LALR(1) parser is always a token in the body of a
3697 defined function. It therefore needs not (yet) to be copied at the time of
3698 the lookahead. It is instead accessed with <strong>Prefix::lookahead()</strong>, which
3699 returns <strong>TOK_VOID</strong> if no lookahead token exists.
3700 </p>
3701 </li>
3702 </ul></div>
3703 <div class="paragraph"><p>The principal structure of a reduce function is always:</p></div>
3704 <div class="olist arabic"><ol class="arabic">
3705 <li>
3706 <p>
3707 obtain the arguments (<strong>at0()</strong>, <strong>at1()</strong>, &#8230;)
3708 </p>
3709 </li>
3710 <li>
3711 <p>
3712 compute the desired side effect (assign a variable, evaluate a function,
3713 perform a branch, etc.)
3714 </p>
3715 </li>
3716 <li>
3717 <p>
3718 remove (pop) the arguments from the stack and push the result on the stack,
3719 in most cases with <strong>Prefix::pop_args_push_result()</strong>, and finally
3720 </p>
3721 </li>
3722 <li>
3723 <p>
3724 set member <strong>Prefix::action</strong> to the next action that the prefix parser shall
3725 perform. The possible actions are (enum <strong>R_action</strong> in <strong>Prefix.hh</strong>):
3726 </p>
3727 <div class="olist loweralpha"><ol class="loweralpha">
3728 <li>
3729 <p>
3730 <strong>RA_CONTINUE</strong>: repeat the phrase matching with the current stack (after a
3731 reduce_XXX() has modified it, e.g. with <strong>pop_args_push_result()</strong>.
3732 </p>
3733 </li>
3734 <li>
3735 <p>
3736 <strong>RA_PUSH_NEXT</strong>: push the next token onto the stack (aka. SHIFT)
3737 </p>
3738 </li>
3739 <li>
3740 <p>
3741 <strong>RA_RETURN</strong>: return from the current (sub-) parser and push its result onto
3742 the stack of the calling parser, or
3743 </p>
3744 </li>
3745 <li>
3746 <p>
3747 <strong>RA_SI_PUSHED</strong>: like <strong>RA_RETURN</strong> but without a result (called a <strong>future</strong>
3748 in esoteric computer languages). It indicates that a new parser with a new
3749 defined function was started and will or will not return a result. This
3750 case is slightly different from simply calling a sub-parser, because a
3751 sub-parser (e.g. for parentheses) remains in the same function body, while
3752 a new defined function has its own body.
3753 </p>
3754 </li>
3755 </ol></div>
3756 </li>
3757 </ol></div>
3758 </div>
3759 </div>
3760 <div class="sect2">
3761 <h3 id="_construction_of_the_phrase_table_or_tree">Construction of the phrase table (or tree)</h3>
3762 <div class="paragraph"><p>The large number of phrases makes it error prone and cumbersome to handle
3763 them. For that reason the declaration of all <strong>reduce_XXX()</strong> functions (in
3764 <strong>Prefix.hh</strong>, as well as the instantiation of a hash table for fast lookup of
3765 phrases (or, alternatively a search tree) in <strong>Prefix.cc</strong> are realized by
3766 including <strong>src/Prefix.def</strong>. That is, <strong>Prefix.def</strong> is, with the help of the C++
3767 preprocessor, a code generator for <strong>Prefix.hh</strong> and <strong>Prefix.cc</strong>. <strong>Prefix.def</strong>
3768 itself is also generated (with <strong>tools/phrase_gen</strong>). And <strong>tools/phrase_gen</strong>
3769 itself <strong>#includes</strong> file <strong>tools/phrase_gen</strong>.def which contains the primary
3770 definition of all phrases (similar to the phrase table in the ISO standard.
3771 This machinery is hidden behind <strong>tools/makefile</strong> with targets: <strong>gen</strong> (for a
3772 hash table) and <strong>gen2</strong> (for a search tree). Note the lowercase <strong>makefile</strong>; the
3773 tools directory also contains an (uppercase) <strong>Makefile</strong> which belongs to the
3774 build system. The full picture is this:</p></div>
3775 <div class="listingblock">
3776 <div class="content">
3777 <pre>
3778
3779 <code> ╔═════════════════════╗ ╔══════════════════════╗
3780 ║ tools/phrase_gen.cc ║ ║ tools/phrase_gen.def ║
3781 ╚══════════╤══════════╝ ╚══════════╤═══════════╝
3782 │ │
3783 ┌────────┴───────┐ │
3784 │ tools/Makefile │ ↓
3785 └────────┬───────┘ │
3786 │ │
3787 g++ ─────── #include ───────┘
3788 │
3789 ╔════════╧═════════╗
3790 ║ tools/phrase_gen ║ ./configure
3791 ╚═════════╤════════╝
3792 │ ══════════════════
3793 ┌────────┴───────┐
3794 │ tools/makefile │ make
3795 └────────┬───────┘
3796 │
3797 write
3798 │
3799 ┌────────┴────────┐ ┌───────────────┐
3800 │ src/Prefix.def ├───────── #include ─── → ───┤ src/Prefix.hh │
3801 └────────┬────────┘ └───────┬───────┘
3802 │ │
3803 ↓ ↓
3804 │ ┌───────────────┐ │
3805 └── #include ──┤ src/Prefix.cc ├── #include ──┘
3806 └───────┬───────┘
3807 │
3808 g++
3809 │
3810 ┌─────────┴────────┐
3811 │ src/apl-Prefix.o │
3812 └──────────────────┘</code>
3813
3814 </pre>
3815 </div></div>
3816 <div class="admonitionblock">
3817 <table><tr>
3818 <td class="icon">
3819 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
3820 </td>
3821 <td class="content">GNU APL is shipped with <strong>src/Prefix.def</strong> already produced, so that the
3822 "normal" designer is rarely concerned with the above. Unless she needs to
3823 change the GNU APL phrase table.</td>
3824 </tr></table>
3825 </div>
3826 <div class="paragraph"><p>If, however, <strong>tools/phrase_gen.def</strong> was modified, then</p></div>
3827 <div class="listingblock">
3828 <div class="content">
3829 <pre>
3830
3831 <code> make gen # generate new src/Prefix.def with a phrase table, or
3832 make gen2 # generate new src/Prefix.def with a phrase tree</code>
3833
3834 </pre>
3835 </div></div>
3836 <div class="paragraph"><p>needs to be performed once. The build system will then note the changed
3837 <strong>src/Prefix.def</strong> and a top-level make will recompile the affected files in
3838 directory <strong>src</strong>.</p></div>
3839 </div>
3840 </div>
3841 </div>
3842 <div class="sect1">
3843 <h2 id="_noteworthy_classes">Noteworthy Classes</h2>
3844 <div class="sectionbody">
3845 <div class="sect2">
3846 <h3 id="_string_classes">String Classes</h3>
3847 <div class="paragraph"><p>The GNU APL source code has more than 1000 places where strings are used.
3848 These strings come in 2 flavors:</p></div>
3849 <div class="olist arabic"><ol class="arabic">
3850 <li>
3851 <p>
3852 strings of 32-bit Unicodes (sometimes referred to as UCS-32),
3853 </p>
3854 </li>
3855 <li>
3856 <p>
3857 strings of bytes in UTF8 encoding.
3858 </p>
3859 </li>
3860 </ol></div>
3861 <div class="paragraph"><p>GNU APL uses 2 classes for these strings: class <strong>UCS_string</strong> for 32-bit
3862 Unicodes and <strong>UTF8_string</strong> for UTF8-encoded strings. In general:</p></div>
3863 <div class="ulist"><ul>
3864 <li>
3865 <p>
3866 UCS_strings are far easier to use than UTF8_strings because UTF8 is a
3867 variable-length encoding, which makes indexing or iterating over them
3868 cumbersome and error-prone. For this reason all strings used internally
3869 in GNU APL are UCS_strings.
3870 </p>
3871 </li>
3872 <li>
3873 <p>
3874 UTF8_strings are commonly used between GNU APL and its environment. They
3875 are shorter when transported over the Internet, therefore most web pages
3876 are UTF8-encoded. Also, filenames in operating systems are UTF8 encoded.
3877 Finally, most terminal emulators, in particular those that support APL in
3878 some way, send and expect all characters to be UTF8-encoded.
3879 </p>
3880 </li>
3881 </ul></div>
3882 <div class="sect3">
3883 <h4 id="_ucs_string">UCS_string</h4>
3884 <div class="paragraph"><p>The starting point is:</p></div>
3885 <div class="listingblock">
3886 <div class="content">
3887 <pre>
3888
3889 <code> Unicode.hh:
3890
3891 enum Unicode
3892 {
3893 #define char_def(name, uni, _tag, _flags, _av_pos) UNI_ ## name = uni,
3894 #define char_uni(name, uni, _tag, _flags) UNI_ ## name = uni,
3895 #include "Avec.def"
3896
3897 Unicode_0 = 0, ///&lt; End of unicode string
3898 Invalid_Unicode = 0x55AA55AA, ///&lt; An invalid Unicode.
3899 };</code>
3900
3901 </pre>
3902 </div></div>
3903 <div class="paragraph"><p>where:</p></div>
3904 <div class="ulist"><ul>
3905 <li>
3906 <p>
3907 The <strong>char_def()</strong> macro defines all Unicodes that are contained in the 256
3908 character <strong>⎕AV</strong> of GNU APL. In old APL1 the ⎕AV and its derivates (⎕FC, ⎕A,
3909 ⎕D, &#8230;). These days, the ⎕AV is only maintained for backward compatibility
3910 with APL1. In APL2 it has been entirely obsoleted by <strong>⎕UCS</strong>. Keep in mind,
3911 however, that:
3912 </p>
3913 <div class="ulist"><ul>
3914 <li>
3915 <p>
3916 The encoding of files produced with )OUT or read with )IN are still based
3917 on ⎕AV. To be precise: based on some IBM ⎕AV defined in the IBM APL2
3918 reference manual, not on the GNU APL ⎕AV. Generally ⎕AVs as well as their
3919 encodings are machine specific, some mainframes even used EBCDIC and older
3920 PC operating systems used special APL code pages which corresponded to the
3921 ⎕AV.
3922 </p>
3923 </li>
3924 <li>
3925 <p>
3926 GNU APL has two helpers for such conversions, see below.
3927 </p>
3928 </li>
3929 <li>
3930 <p>
3931 <strong>class *Avec</strong> contains numerous static functions for handling and
3932 classifying of Unicodes contained in the <strong>⎕AV</strong>.
3933 </p>
3934 </li>
3935 </ul></div>
3936 </li>
3937 <li>
3938 <p>
3939 In contrast, the <strong>char_uni()</strong> macro defines Unicodes that are used in
3940 the GNU APL source code but are not contained in the <strong>⎕AV</strong> of GNU APL.
3941 </p>
3942 </li>
3943 <li>
3944 <p>
3945 In short:
3946 </p>
3947 <div class="ulist"><ul>
3948 <li>
3949 <p>
3950 any Unicode with APL significance (primitives etc.) are (and
3951 must be) contained in the ⎕AV of the interpreter. These Unicodes are
3952 defined with macro <strong>char_def()</strong> in file <strong>Avec.def</strong>, while
3953 </p>
3954 </li>
3955 <li>
3956 <p>
3957 any Unicode used in the GNU APL C++ source code is defined with
3958 macro <strong>char_uni()</strong>.
3959 </p>
3960 </li>
3961 <li>
3962 <p>
3963 Noteworthy examples of the latter are the sub- and superscripts
3964 (⁰, ¹, ², &#8230;, ⁹, and ₀, ₁, ₂, &#8230;, ₉). They are not contained in the ⎕AV
3965 of GNU APL but occur in the .xml files produced by GNU APL (and not only
3966 in the source code).
3967 </p>
3968 </li>
3969 </ul></div>
3970 </li>
3971 </ul></div>
3972 <div class="paragraph"><p>The helpers mentioned above are:</p></div>
3973 <div class="listingblock">
3974 <div class="content">
3975 <pre>
3976
3977 <code> Avec.hh:
3978 /// a pointer to 256 Unicode characters that are exactly the APL2 character
3979 /// set (⎕AV) shown in LRM Appendix A, page 470. The ⎕AV of GNU APL is
3980 /// similar, but contains characters like ≢ that are not in IBM's ⎕AV
3981 /// IBM's ⎕AV is used in the )IN command
3982 static const Unicode * IBM_quad_AV();
3983
3984 /// recompute \b inverse_ibm_av from \b ibm_av and print it
3985 static void print_inverse_IBM_quad_AV();</code>
3986
3987 </pre>
3988 </div></div>
3989 <div class="paragraph"><p>After this digression into Unicodes we can now:</p></div>
3990 <div class="listingblock">
3991 <div class="content">
3992 <pre>
3993
3994 <code> UCS_string.hh:
3995
3996 class UCS_string : public std::basic_string&lt;Unicode&gt;
3997 {
3998 ...
3999 UCS_string(const UTF8_string &amp; utf);
4000 ...
4001 }</code>
4002
4003 </pre>
4004 </div></div>
4005 <div class="paragraph"><p>UCS_string has many constructors, most importantly the constructor from a
4006 UTF8_string which converts the UTF8_string into a UCS_string. In addition the
4007 UCS_string string class defines numerous convenience functions for the
4008 processing of UCS_strings.</p></div>
4009 </div>
4010 <div class="sect3">
4011 <h4 id="_utf8_string">UTF8_string</h4>
4012 <div class="paragraph"><p>In good old C, there were C literals like <strong>"Hello, world"</strong>. At that time the
4013 only relevant encoding of such literals was the 7-bit ASCII code and all
4014 literals contained only positive (signed) characters.</p></div>
4015 <div class="paragraph"><p>Next, C++ defined a template class <strong>std::basic_string&lt;&gt;</strong> which provides
4016 numerous string processing methods (indexing, iteration) for <strong>basic_string</strong>
4017 that are also used in our derived classes <strong>UCS_string</strong> and <strong>UTF8_string</strong>.
4018 So far so good.</p></div>
4019 <div class="paragraph"><p>But now comes the problem. The C++ standard string library says:</p></div>
4020 <div class="listingblock">
4021 <div class="content">
4022 <pre>
4023
4024 <code> class string : public basic_string&lt;char&gt; { }</code>
4025
4026 </pre>
4027 </div></div>
4028 <div class="paragraph"><p>On almost all platforms (read: compilers), a <strong>char</strong> is <strong>signed</strong> as
4029 opposed to type <strong>unsigned char</strong>. Often this does not hurt because C and C++
4030 automatically convert between the signed and unsigned variants and not every
4031 programmer is always aware of the dire consequences of signedness. Like, for
4032 example:</p></div>
4033 <div class="listingblock">
4034 <div class="content">
4035 <pre>
4036
4037 <code> !
4038 char(-56) = char(200) &lt; char(100) = unsigned char(100) &lt; unsigned char(200).</code>
4039
4040 </pre>
4041 </div></div>
4042 <div class="paragraph"><p>To make things worse, UTF8 encoding works on (unsigned) bytes while C/C++
4043 literals in compilers are, without specific qualifiers, signed. As a
4044 consequence, the APL character <strong><em>←</em></strong> has Unicode 8592 aka. 0x2190, and a UTF8
4045 encoding of (hex) <strong>E2 86 90</strong> which makes the string literal "→" a
4046 (0-terminated) sequence of 3 signed (!) and unfortunately negative (!)
4047 characters. Early versions of GNU APL used <strong>std::string</strong> instead of
4048 <strong>UTF8_strings</strong>, and the problems caused by that were numerous. These days
4049 GNU APL itself is clean when it comes to signed vs. unsigned chars, but some
4050 older contributions may still suffer. Comparisons for equality work well for
4051 mixed signed/unsigned operands, but &lt; and &gt; do not, and, as a consequence,
4052 <strong>strcmp()</strong> and friends frequently return the wrong order when used for sorting
4053 UTF-encoded strings.</p></div>
4054 <div class="paragraph"><p>Like <strong>UCS_string</strong> has a constructor from a <strong>UTF8_string</strong>, <strong>UTF8_string</strong> has a
4055 constructor from a <strong>UCS_string</strong>:</p></div>
4056 <div class="listingblock">
4057 <div class="content">
4058 <pre>
4059
4060 <code> UTF8_string.hh:
4061
4062 /// one byte (not character)! of a UTF8 encoded Unicode (RFC 3629) string
4063 typedef uint8_t UTF8;
4064
4065 class UTF8_string : public std::basic_string&lt;UTF8&gt;
4066 {
4067 ...
4068 /// constructor: copy of UCS string. The Unicodes in ucs
4069 /// will be UTF8-encoded
4070 UTF8_string(const UCS_string &amp; ucs);
4071 };</code>
4072
4073 </pre>
4074 </div></div>
4075 <div class="paragraph"><p>The take-aways for designers and contributors are:</p></div>
4076 <div class="ulist"><ul>
4077 <li>
4078 <p>
4079 prefer UCS_string over UTF8_string where possible (efficiency),
4080 </p>
4081 </li>
4082 <li>
4083 <p>
4084 never use std::string, but
4085 </p>
4086 </li>
4087 <li>
4088 <p>
4089 instead use UTF8_string and
4090 </p>
4091 </li>
4092 <li>
4093 <p>
4094 do not write your own conversion functions but use the constructors instead.
4095 </p>
4096 </li>
4097 </ul></div>
4098 </div>
4099 </div>
4100 <div class="sect2">
4101 <h3 id="_apl_values">APL Values</h3>
4102 <div class="paragraph"><p>The primary business of an APL interpreter is to manipulate APL values.
4103 An APL value is simply a <strong>Shape</strong> and a <strong>Ravel</strong>, although simplicity ends here.</p></div>
4104 <div class="paragraph"><p>In that, <strong>Shape</strong> is a class of its own, defined in <strong>Shape.hh</strong>. It has a bunch
4105 of constructors for various purposes and also numerous inline convenience
4106 functions that make the code that deals with shapes more readable.</p></div>
4107 <div class="paragraph"><p>Many of the Shape methods have trivial wrappers in <strong>class Value</strong>, because
4108 almost every shape is accessed in the context of a value. For example:</p></div>
4109 <div class="listingblock">
4110 <div class="content">
4111 <pre>
4112
4113 <code> Shape.hh:
4114
4115 ...
4116 /// return the rank
4117 uRank get_rank() const
4118 { return rho_rho; }
4119
4120 Value.hh:
4121
4122 ...
4123 /// return the rank of \b this value
4124 uRank get_rank() const
4125 { return shape.get_rank(); }
4126 ...
4127 Shape shape;
4128 ...</code>
4129
4130 </pre>
4131 </div></div>
4132 <div class="paragraph"><p>The ravel is simply a C++ array of type <strong>Cell</strong>:</p></div>
4133 <div class="listingblock">
4134 <div class="content">
4135 <pre>
4136
4137 <code> Value.hh:
4138
4139 ...
4140 /// The ravel of \b this value.
4141 Cell * ravel;
4142
4143 /// the cells of a short (i.e. ⍴,value ≤ cfg_SHORT_VALUE_LENGTH_WANTED) value
4144 Cell short_value[cfg_SHORT_VALUE_LENGTH_WANTED];</code>
4145
4146 </pre>
4147 </div></div>
4148 <div class="paragraph"><p>Many real-life APL values are short. To avoid unnecessary memory allocations,
4149 GNU APL allocates short ravels inside the class instance and longer ravels
4150 outside (which then requires an additional new/delete for the ravel). Put
4151 differently, a short value has <strong>ravel == &amp;short_value</strong>, while <strong>ravel</strong> in
4152 long values point to a separately allocated memory area. The border between
4153 short and long values can be changed with <strong>./configure</strong>; larger values of
4154 <strong>SHORT_VALUE_LENGTH_WANTED</strong> have (marginally) better performance, but the
4155 price is a (less marginal) increase in memory consumption.</p></div>
4156 <div class="paragraph"><p>Similar to <strong>class Shape</strong>, there are numerous constructors and (mostly trivial)
4157 access functions for various purposes. For example, a Value can be constructed
4158 from a UCS_string (and conversely a UCS_string can be constructed from a Value
4159 (provided that its rank and Cell types are correct). Most values are
4160 constructed sequentially and to simplify that, every value has a built-in
4161 iterator <strong>Value::next_ravel()</strong> which returns a pointer to the next
4162 not-yet-initialized Cell (if any) and 0 after the last Cell of the ravel
4163 was returned.</p></div>
4164 <div class="admonitionblock">
4165 <table><tr>
4166 <td class="icon">
4167 <img src="/usr/share/asciidoc/icons/tip.png" alt="Tip" />
4168 </td>
4169 <td class="content">A frequent pitfall related to the <strong>Value::next_ravel()</strong> iterator is to
4170 (mistakenly) use it for empty values.</td>
4171 </tr></table>
4172 </div>
4173 <div class="paragraph"><p>Empty APL values like <strong>0⍴0</strong> are never empty at the C++ level because every APL
4174 value <strong>V</strong> has a <em>prototype</em> <strong>↑0⍴V</strong> whose ravel has one item stored in
4175 <strong>ravel[0]</strong>. An old APL joke was, <em>How many empty arrays does it take to fill
4176 an APL workspace? One, if it&#8217;s big enough.</em> If a value <strong>V</strong> is empty, then <strong>⍴V</strong>
4177 is 0 (i.e. its ravel has length 0) but length 1 at C++ level (due to its
4178 prototype). For this reason, a <strong>Value</strong> has a <strong>Value::element_count()</strong>, which
4179 is the ravel length in APL, and a <strong>Value::nz_element_count()</strong> which is the
4180 ravel length in C++. The <strong>Value::next_ravel()</strong> iterator uses the former, which
4181 causes a segfault when used with an empty value. In short this means that APL
4182 values may be empty, but their C++ implementations are never empty <em>and need
4183 to be properly initialized</em>.</p></div>
4184 <div class="admonitionblock">
4185 <table><tr>
4186 <td class="icon">
4187 <img src="/usr/share/asciidoc/icons/caution.png" alt="Caution" />
4188 </td>
4189 <td class="content">This implies that a proper source code <strong>loop()</strong> over a ravel MUST
4190 either use <strong>Value::nz_element_count()</strong> (so that the prototype is initialized
4191 inside the loop), or else must have ruled out the empty case beforehand (and
4192 may then use <strong>Value::element_count()</strong> safely).</td>
4193 </tr></table>
4194 </div>
4195 <div class="paragraph"><p>The typical design patterns are:</p></div>
4196 <div class="listingblock">
4197 <div class="content">
4198 <pre>
4199
4200 <code>Value_P Z(...); // may be empty
4201
4202 loop(z, Z-&gt;nz_element_count()) // not element_count() !!!
4203 {
4204 new (Z-&gt;get_wravel(z)) XxxCell(...)); // safe, but (next_ravel_XXX() is not)
4205 }</code>
4206
4207 </pre>
4208 </div></div>
4209 <div class="paragraph"><p>and:</p></div>
4210 <div class="listingblock">
4211 <div class="content">
4212 <pre>
4213
4214 <code>Value_P Z(...); // may be empty
4215
4216 if (Z-&gt;element_count()) // not Z-&gt;nz_element_count() !!!
4217 {
4218 // Z is not empty
4219 loop(z, Z-&gt;element_count()) Z-&gt;next_ravel_XXX(...)
4220 }
4221 else
4222 {
4223 new (Z-&gt;get_wproto()) XxxCell(...)); // init prototype
4224 }</code>
4225
4226 </pre>
4227 </div></div>
4228 <div class="paragraph"><p><strong>Z&#8594;get_wproto()</strong> is the same as <strong>Z&#8594;get_wravel(0)</strong> and is used here to make
4229 the role of <strong>get_wravel(0)</strong> as the prototype of Z explicit.</p></div>
4230 <div class="sect3">
4231 <h4 id="_class_value_p">class Value_P</h4>
4232 <div class="paragraph"><p>APL values are rarely copied but pointers to APL values are passed back and
4233 forth a lot in the interpreter (in more than 1200 source code locations),
4234 It is therefore easy to loose a Value, which then causes a memory leak. Early
4235 GNU APL versions were plagued by these memory leaks, but are now fixed
4236 using several counter-measures:</p></div>
4237 <div class="ulist"><ul>
4238 <li>
4239 <p>
4240 C++ overloading of operators ::new() and ::delete(). Every newly allocated
4241 Value, no matter if long or short, is tracked in a link list called
4242 <strong>DynamicObject::all_values</strong>, see <strong>DynamicObject.hh</strong>
4243 </p>
4244 </li>
4245 <li>
4246 <p>
4247 The APL command )CHECK compares the values that are are reachable from APL,
4248 e.g. via a token of <strong>class TC_Value</strong>, with the values in
4249 <strong>DynamicObject::all_values</strong> and reports any mismatches as <strong>stale values</strong>.
4250 </p>
4251 </li>
4252 <li>
4253 <p>
4254 Every value tracks itself with a <strong>ValueHistory</strong> which is, for performance
4255 reasons, disabled by default but can be enabled with <strong>./configure</strong>,
4256 or with <strong>make develop</strong>.
4257 </p>
4258 </li>
4259 <li>
4260 <p>
4261 A reference counter named <strong>Value::owner_count</strong> in combination with <strong>class
4262 Value_P</strong>. A value is automatically deleted when its last owner (typically
4263 a <strong>Token</strong> is deleted.
4264 </p>
4265 </li>
4266 <li>
4267 <p>
4268 Member <strong>Value::check_ptr</strong> to detect incorrect
4269 </p>
4270 </li>
4271 <li>
4272 <p>
4273 overwriting or double deletion of a value. Every <strong>Value</strong> constructor sets
4274 <strong>check_ptr</strong> to <strong>(this * * 7)</strong> which points into the middle of a <strong>uint64</strong>
4275 (of member Value::shape). The destructor later checks that <strong>check_ptr</strong>
4276 is still <strong>(this * * 7)</strong> (to detect overwriting) and clears it (to detect
4277 double deletion). The <strong>check_ptr</strong> is:
4278 </p>
4279 <div class="ulist"><ul>
4280 <li>
4281 <p>
4282 is different for every Value,
4283 </p>
4284 </li>
4285 <li>
4286 <p>
4287 is a pointer that cannot occur otherwise,
4288 </p>
4289 </li>
4290 <li>
4291 <p>
4292 becomes incorrect when a Value is incorrectly moved (e.g. with <strong>memcpy()</strong>)
4293 </p>
4294 </li>
4295 </ul></div>
4296 </li>
4297 </ul></div>
4298 <div class="paragraph"><p>Value_P is essentially a smart pointer (aka. shared pointer). A "normal" smart
4299 pointer typically consists of 2 pointers:
4300 * one pointing to the object itself, and
4301 * one to the reference counter that keeps track of the object&#8217;s ownership
4302 (aka. the reference counter)</p></div>
4303 <div class="paragraph"><p>In contrast, the <strong>Value_P</strong> is a single pointer (to a Value), and the reference
4304 counter is allocated inside the value rather than somewhere else. The small
4305 advantage of a "normal" smart pointer is that they are not intrusive, which
4306 means that they can be used without any modifications of the object that they
4307 point to. But since we are in control of the Value class, this benefit does
4308 not really count and the smaller size of a <strong>Value_P</strong> outweighs the benefit of
4309 standard smart pointers. The Value_Ps are also decoupled from each other;
4310 one can always construct a Value_P from a Value * (and vice versa). The
4311 most important member functions of Value_P are:</p></div>
4312 <div class="ulist"><ul>
4313 <li>
4314 <p>
4315 <strong>Value_P()</strong> : default constructor (points to no value)
4316 </p>
4317 </li>
4318 <li>
4319 <p>
4320 <strong>Value_P(Value * val, const char * loc)</strong> : construct Value_P from a Value *
4321 </p>
4322 </li>
4323 <li>
4324 <p>
4325 <strong>Value_P(const Value_P &amp; other, const char * loc)</strong> : construct from <strong>other</strong>
4326 </p>
4327 </li>
4328 <li>
4329 <p>
4330 <strong>Value_P(const Shape &amp; sh, const char * loc)</strong> : construct Value_P to a new
4331 value with shape <strong>sh</strong>. The ravel of that value is not (yet) initialized.
4332 </p>
4333 </li>
4334 <li>
4335 <p>
4336 <strong>inline Value_P(const UCS_string &amp; ucs, const char * loc)</strong> : construct an APL
4337 text vector whose ravel Cells are initialized from the UCS_string,
4338 </p>
4339 </li>
4340 <li>
4341 <p>
4342 <strong>inline Value_P(const UTF8_string &amp; utf, const char * loc)</strong> : construct an APL
4343 text vector whose ravel Cells are initialized from the UTF8_string. ⍴Value is
4344 <strong>utf.size()</strong>, i.e. the UTF8 bytes are kept and no decoding into Unicodes is
4345 performed
4346 </p>
4347 </li>
4348 <li>
4349 <p>
4350 <strong>void reset()</strong> : decrement the owner-count and clear the pointer
4351 </p>
4352 </li>
4353 <li>
4354 <p>
4355 <strong>init_pointer()</strong> reset() without decrementing the owner-count
4356 </p>
4357 </li>
4358 <li>
4359 <p>
4360 <strong>void clear()</strong> : reset and add an event to the ValueHistory (if enabled)
4361 </p>
4362 </li>
4363 <li>
4364 <p>
4365 <strong>bool operator +()</strong> : <strong>true</strong> iff the pointer is valie (non-zero)
4366 </p>
4367 </li>
4368 <li>
4369 <p>
4370 <strong>bool operator !()</strong> : <strong>true</strong> iff the pointer is invalie (zero)
4371 </p>
4372 </li>
4373 <li>
4374 <p>
4375 <strong>Value * operator&#8594;()</strong> : return the Value * (to the Value)
4376 </p>
4377 </li>
4378 <li>
4379 <p>
4380 <strong>Value &amp; operator *()</strong> : return the Value &amp; reference (of the Value)
4381 </p>
4382 </li>
4383 <li>
4384 <p>
4385 <strong>void increment_owner_count()</strong> : dito.
4386 </p>
4387 </li>
4388 <li>
4389 <p>
4390 <strong>void decrement_owner_count()</strong> : dito.
4391 </p>
4392 </li>
4393 <li>
4394 <p>
4395 <strong>void isolate(const char * loc)</strong> : if the value has more than one owner then
4396 clone (deep copy) the value and set pointer to the new copy.
4397 </p>
4398 </li>
4399 <li>
4400 <p>
4401 <strong>void move(Value_P_Base &amp; other, const char * loc)</strong> :
4402 </p>
4403 <div class="ulist"><ul>
4404 <li>
4405 <p>
4406 release ownership of the current pointer,
4407 </p>
4408 </li>
4409 <li>
4410 <p>
4411 take ownership of other&#8217;s pointer, and
4412 </p>
4413 </li>
4414 <li>
4415 <p>
4416 reset other&#8217;s pointer. This operation does not change the owner_count of
4417 the object.
4418 </p>
4419 </li>
4420 </ul></div>
4421 </li>
4422 </ul></div>
4423 <div class="paragraph"><p>All functions are inline and some can throw exceptions (primarily <strong>WS FULL</strong>).
4424 Note that some <strong>Value_P</strong> constructors above (e.g.
4425 <strong>Value_P(const UCS_string &amp; ucs, &#8230;)</strong> initialize the entire Value (shape
4426 and ravel) while others (e.g. <strong>Value_P(const Shape &amp; sh)</strong>) only initialize the
4427 shape and leave the initialization of the ravel to the caller. In fact, the
4428 construction of a Value is always a two-step process (initializations of the
4429 shape followed by the initializations of the ravel) and the constructors that
4430 do both are mere convenience constructors for frequent cases. The following
4431 design pattern can found in many places:</p></div>
4432 <div class="listingblock">
4433 <div class="content">
4434 <pre>
4435
4436 <code>Value_P Z(...); // construct Z (with un-initialized ravel)
4437 loop(x, z.element_count()) Z-&gt;next_ravel_XXX(...); // initialize the ravel
4438 Z-&gt;check_value(LOC) // set checked flag, detect un-initialized ravel items
4439 return Z;</code>
4440
4441 </pre>
4442 </div></div>
4443 <div class="paragraph"><p>The constructors for <strong>Value</strong>s are protected (with friend <strong>Value_P</strong>) to
4444 enforce that values are always constructed via <strong>Value_P::Value_P(&#8230;)</strong> rather
4445 than the corresponding <strong>Value::Value(&#8230;) <strong>. On the other hand, many
4446 functions use *const Value </strong>" (note the const!) instead of *Value_P</strong>,
4447 primarily for passing function arguments. This is safe and slightly more
4448 efficient if the called function does not modify the Value (and hence its
4449 ownership) passed as an argument.</p></div>
4450 </div>
4451 <div class="sect3">
4452 <h4 id="_cloning_of_values">Cloning of Values</h4>
4453 <div class="paragraph"><p>The cloning (deep aka. recursive copy) of a Value is somewhat expensive in
4454 terms of performance. Cloning is, however, only needed if the clone is
4455 modified later (which would corrupt the original). When a <strong>Value</strong> is assigned
4456 to a Symbol (i.e. a variable) then it must be cloned because the variable may
4457 or may not be changed later. Think of a literal in a function body:</p></div>
4458 <div class="listingblock">
4459 <div class="content">
4460 <pre>
4461
4462 <code> ∇Z←FOO
4463 Z←1 2 3
4464 ∇
4465
4466 X←FOO ◊ X←4 5 6</code>
4467
4468 </pre>
4469 </div></div>
4470 <div class="paragraph"><p>If we do not clone the result of FOO, not cloned before assigning it to
4471 X, then the subsequent assignment to X would modify the body of FOO and the
4472 next call of FOO would return 4 5 6.</p></div>
4473 <div class="paragraph"><p>On the other hand:</p></div>
4474 <div class="listingblock">
4475 <div class="content">
4476 <pre>
4477
4478 <code> ∇Z←FOO
4479 Z←1 2 3
4480 ∇
4481
4482 X←1 + FOO ◊ X←4 5 6</code>
4483
4484 </pre>
4485 </div></div>
4486 <div class="paragraph"><p>is safe because the addition + does not modify the result of FOO but rather
4487 discards it immediately. GNU APL versions have always cloned values before
4488 assigning them (to be on the safe side). Current versions check how many
4489 owners a value has before coning them:</p></div>
4490 <div class="listingblock">
4491 <div class="content">
4492 <pre>
4493
4494 <code> Value.hh
4495
4496 #define NEW_CLONE
4497
4498 #ifdef NEW_CLONE /* new clone() scheme */
4499
4500 /// clone, given a Value_P. Result is a Value_P.
4501 # define CLONE_P(B_P, L) (B_P)
4502
4503 /// clone, given a const Value *. Result is a Value_P.
4504 # define CLONE(pB, L) Value_P(const_cast&lt;Value *&gt;(pB), L)
4505
4506 #else /* old clone() scheme */
4507
4508 /// clone, given a Value_P. Result is a Value_P.
4509 # define CLONE_P(B_P, L) (B_P).get()-&gt;clone(L)
4510
4511 /// clone, given a Value *. Result is a Value_P.
4512 # define CLONE(pB, L) (pB)-&gt;clone(L)
4513
4514 #endif</code>
4515
4516 </pre>
4517 </div></div>
4518 <div class="paragraph"><p>The rules are:</p></div>
4519 <div class="ulist"><ul>
4520 <li>
4521 <p>
4522 If a C++ function knows from its context that cloning is not needed (like in
4523 the second example above) then it may CLONE() and CLONE_P(). Determining if
4524 the cloning is actually performed, then depends on whether <strong>NEW_CLONE</strong>
4525 is <strong>#defined</strong> or not. This could be a mistake and then <em>not</em> <strong>#define</strong>ing
4526 <strong>NEW_CLONE</strong> is a quick way to fall back to the old scheme.
4527 </p>
4528 </li>
4529 <li>
4530 <p>
4531 Otherwise, <em>in particular if</em> <strong>#define NEW_CLONE</strong> <em>makes a difference</em>, the
4532 function shall use <strong>Value&#8594;clone() *</strong> which clones unconditionally.
4533 </p>
4534 </li>
4535 </ul></div>
4536 </div>
4537 <div class="sect3">
4538 <h4 id="_ravel_cells">Ravel Cells</h4>
4539 <div class="paragraph"><p>The ravel of every <strong>Value</strong> is a simple C array and not a C++
4540 <strong>std::vector&lt;Cell&gt;</strong>. The alternative <strong>std::vector&lt;Cell&gt;</strong> has some performance
4541 penalties when used for the ravel of a <strong>Value</strong>:</p></div>
4542 <div class="ulist"><ul>
4543 <li>
4544 <p>
4545 destructor calls for every Cell when the vector&lt;&gt; is destructed, and
4546 </p>
4547 </li>
4548 <li>
4549 <p>
4550 no support for short values.
4551 </p>
4552 </li>
4553 </ul></div>
4554 <div class="paragraph"><p>The other alternative, linked list of <strong>Cell</strong>s, was also not an option due
4555 to their overhead in memory allocation (one <strong>new()</strong> <strong>delete()</strong> per Cell. The
4556 huge number of Cells in a typical APL program on the one hand and the typical
4557 operations performed with the Cells on the other result in the following,
4558 somewhat conflicting, requirements:</p></div>
4559 <div class="ulist"><ul>
4560 <li>
4561 <p>
4562 fixed <strong>Cell</strong> size
4563 </p>
4564 </li>
4565 <li>
4566 <p>
4567 different cell contents (character, integer, float, complex, and nested) to
4568 support mixed APL values. In the old APL1 all values were homogeneous (and
4569 the type was a property of the value and its ravel). In APL2 the type
4570 became a property of the ravel Cells. Some APL interpreters optimize for
4571 the case of homogeneous ravels (probably a fair guess for APL2 programs
4572 inherited from an APL1 code base) while GNU APL assumes mixed APL values as
4573 the default. Both assumptions (homogeneous vs. mixed) have advantages and
4574 drawbacks:
4575 </p>
4576 <div class="ulist"><ul>
4577 <li>
4578 <p>
4579 Homogeneous arrays are faster to process, but this backfires when the
4580 automatic type conversion of APL (bool to integer, integer to float, float
4581 to complex takes place.
4582 </p>
4583 </li>
4584 <li>
4585 <p>
4586 Loops in the mixed case for scalar functions become much simpler because
4587 the different combinations of types (e.g. Boolean + Boolean,
4588 Boolean + integer, integer + integer, &#8230;). Making e.g. dyadic <strong>&#43;(A, B)</strong>
4589 a virtual member function <strong>B.&#43;(A)</strong> of <strong>B</strong> and monadic <strong>&#43;(B)</strong> a
4590 virtual member function <strong>(B.&#43;()</strong> of <strong>B</strong>, removes two case statements
4591 (with the dyadic one being quite ugly).
4592 </p>
4593 </li>
4594 </ul></div>
4595 </li>
4596 </ul></div>
4597 <div class="paragraph"><p>The base class <strong>Cell</strong> therefore defines a pretty large number of virtual
4598 functions (a monadic and a dyadic one for each of the scalar APL functions:</p></div>
4599 <div class="listingblock">
4600 <div class="content">
4601 <pre>
4602
4603 <code> Cell.hh:
4604
4605 ...
4606 virtual ErrorCode bif_ceiling(Cell * Z) const
4607 virtual ErrorCode bif_conjugate(Cell * Z) const
4608 virtual ErrorCode bif_direction(Cell * Z) const
4609 ...
4610 virtual ErrorCode bif_add(Cell * Z, const Cell * A) const
4611 virtual ErrorCode bif_and(Cell * Z, const Cell * A) const
4612 virtual ErrorCode bif_and_bitwise(Cell * Z, const Cell * A) const
4613 ...</code>
4614
4615 </pre>
4616 </div></div>
4617 <div class="paragraph"><p>The base class <strong>Cell</strong> is inherited by the following derived classes:</p></div>
4618 <div class="ulist"><ul>
4619 <li>
4620 <p>
4621 <strong>CharCell</strong>, their relevant content is a single 32-bit Unicode,
4622 </p>
4623 </li>
4624 <li>
4625 <p>
4626 <strong>IntCell</strong>, their relevant content is a single 64-bit signed integer,
4627 </p>
4628 </li>
4629 <li>
4630 <p>
4631 <strong>RealCell</strong>, their relevant content is a 64-bit floating point number,
4632 </p>
4633 </li>
4634 <li>
4635 <p>
4636 <strong>ComplexCell</strong>, their relevant content is two 64-bit floating point numbers
4637 </p>
4638 </li>
4639 <li>
4640 <p>
4641 <strong>NumericCell</strong>, the base class of <strong>IntCell</strong>, <strong>RealCell</strong>, and <strong>ComplexCell</strong>.
4642 Many Cell functions are almost identical for these base classes and these
4643 functions are implemented in <strong>NumericCell</strong> rather than implementing a
4644 copy in each base class. The simple trick that replaces hundreds of
4645 C/C++ <strong>switch</strong> statements and <strong>case</strong>s is this:
4646 </p>
4647 <div class="ulist"><ul>
4648 <li>
4649 <p>
4650 base class <strong>Cell</strong> implements virtual functions that either throws a
4651 <strong>DOMAIN ERROR</strong> (e.g. for a character to integer conversion) or else
4652 return the desired type:
4653 </p>
4654 <div class="ulist"><ul>
4655 <li>
4656 <p>
4657 virtual Unicode get_char_value() const { DOMAIN_ERROR; }
4658 </p>
4659 </li>
4660 <li>
4661 <p>
4662 virtual APL_Integer get_int_value() const { DOMAIN_ERROR; }
4663 </p>
4664 </li>
4665 <li>
4666 <p>
4667 virtual APL_Float get_real_value() const { DOMAIN_ERROR; }
4668 </p>
4669 </li>
4670 <li>
4671 <p>
4672 virtual APL_Float get_imag_value() const { DOMAIN_ERROR; }
4673 </p>
4674 </li>
4675 <li>
4676 <p>
4677 virtual Value_P get_pointer_value() const { DOMAIN_ERROR }
4678 </p>
4679 </li>
4680 <li>
4681 <p>
4682 virtual Cell * get_lval_value() const { LEFT_SYNTAX_ERROR; }
4683 </p>
4684 </li>
4685 </ul></div>
4686 </li>
4687 <li>
4688 <p>
4689 the derived classes then overload all access functions that they support.
4690 For example,
4691 </p>
4692 <div class="ulist"><ul>
4693 <li>
4694 <p>
4695 <strong>CharCell</strong> overload only <strong>get_char_value()</strong>
4696 </p>
4697 </li>
4698 <li>
4699 <p>
4700 all <strong>NumericCell</strong>s overload <strong>get_int_value()</strong>, <strong>get_real_value()</strong>,
4701 and <strong>get_imag_value()</strong> but not <strong>get_char_value()</strong>,
4702 </p>
4703 </li>
4704 <li>
4705 <p>
4706 etc.
4707 </p>
4708 </li>
4709 </ul></div>
4710 </li>
4711 </ul></div>
4712 </li>
4713 <li>
4714 <p>
4715 <strong>PointerCell</strong>, their relevant content is a (nested) APL sub-value.
4716 </p>
4717 </li>
4718 <li>
4719 <p>
4720 <strong>LvalCell</strong>, their relevant content is a <strong>Cell</strong> pointer. These <strong>Cell</strong>s are
4721 used to implement "selective specification". A selective specification like
4722 <strong>(2⍴1↓A)←B</strong> starts with (all <strong>Cell</strong>s of <strong>A</strong>) the rightmost variable <strong>A</strong>,
4723 which is then more and more reduced (by <strong>1↓</strong> and <strong>2⍴</strong>) before the new
4724 value <strong>B</strong> can be assigned to the Cells that survive.
4725 </p>
4726 </li>
4727 </ul></div>
4728 <div class="paragraph"><p>Instead of declaring the data members of Cells in the derived class (the
4729 normal approach, i.e. APL_Integers as member of IntCells, APL_Float members
4730 in FloatCells, etc.) they are declared as a union in the base Class Cell:</p></div>
4731 <div class="listingblock">
4732 <div class="content">
4733 <pre>
4734
4735 <code> Cell.hh:
4736 ...
4737 /// A union containing all possible cell values for the different Cell types
4738 union SomeValue
4739 {
4740 Unicode aval; ///&lt; for CharCell
4741 APL_Float_Base cval[2]; ///&lt; for ComplexCell
4742 ErrorCode eval; ///&lt; an error code
4743 APL_Integer ival; ///&lt; for IntCell
4744 Cell *lval; ///&lt; for LvalCell (selective assignment)
4745 ...
4746 };</code>
4747
4748 </pre>
4749 </div></div>
4750 <div class="paragraph"><p>This union ensures that all derived classes have the same size.</p></div>
4751 <div class="paragraph"><p>Now back to the Cell functions. We take dyadic <strong>A + B</strong> and monadic <strong>+ B</strong>
4752 as an example, all other scalar APL functions follow suit:</p></div>
4753 <div class="olist arabic"><ol class="arabic">
4754 <li>
4755 <p>
4756 Every scalar APL function is implemented in a separate associated class,
4757 derived from base <strong>class ScalarFunction</strong>. GNU APL uses the naming convention
4758 that the class name starts with <strong>Bif_</strong>, followed by the function arity
4759 (<strong>F12_</strong> for nomadic functions, <strong>F2_</strong> for purely dyadic functions).
4760 </p>
4761 </li>
4762 <li>
4763 <p>
4764 The interpreter creates a single static instance of each function (such
4765 static instances are initialized by the compiler before main() is executed).
4766 The instance is always called <strong>fun</strong>, here <strong>Bif_F12_PLUS::fun</strong>. At the same
4767 time, a pointer <strong>_fun</strong> is created.
4768 </p>
4769 </li>
4770 <li>
4771 <p>
4772 The single instance of the class is needed because the base class
4773 <strong>Function</strong> of class <strong>ScalarFunction</strong> has virtual functions (which is
4774 impossible for fully static classes). See <a href="#_apl_functions">APL Functions</a>
4775 below for details.
4776 </p>
4777 </li>
4778 <li>
4779 <p>
4780 The <strong>eval_XXX()</strong> functions like <strong>Bif_F12_PLUS::eval_AB()</strong> are implemented
4781 in inline function calls to <strong>ScalarFunction::eval_scalar_AB()</strong> which takes
4782 the Cell function <strong>Cell::bif_add</strong> that distinguishes the behaviors of the
4783 (in total many) single instances of the classes derived from
4784 <strong>ScalarFunction</strong>.
4785 </p>
4786 </li>
4787 <li>
4788 <p>
4789 The naming convention is that virtual <strong>Cell</strong> functions are
4790 <strong>Cell::bif_YYY</strong>.
4791 </p>
4792 </li>
4793 <li>
4794 <p>
4795 In short: base class <strong>ScalarFunction</strong> is essentially an iterator over the
4796 ravels the value arguments provided, and the difference between the derived
4797 functions are handled at the Cell level by means of different Cell
4798 functions.
4799 </p>
4800 </li>
4801 <li>
4802 <p>
4803 While this may look a little complicated at the first glance, it is fairly
4804 efficient at runtime and saves a lot of mindless work for the GNU APL
4805 designer.
4806 </p>
4807 </li>
4808 <li>
4809 <p>
4810 To summarize the example:
4811 </p>
4812 </li>
4813 </ol></div>
4814 <div class="listingblock">
4815 <div class="content">
4816 <pre>
4817
4818 <code> ScalarFunction.hh:
4819 ...
4820 ///
4821 class Bif_F12_PLUS : public ScalarFunction
4822 {
4823 ...
4824 /// overloaded Function::eval_B().
4825 virtual Token eval_B(Value_P B) const
4826 { return eval_scalar_B(B, &amp;Cell::bif_conjugate); }
4827
4828 /// overloaded Function::eval_AB().
4829 virtual Token eval_AB(Value_P A, Value_P B) const
4830 { return eval_scalar_AB(A, B,
4831 inverse ? &amp;Cell::bif_add_inverse : &amp;Cell::bif_add); }
4832 ...
4833 static Bif_F12_PLUS * fun; ///&lt; Built-in function.
4834 static Bif_F12_PLUS _fun; ///&lt; Built-in function.
4835 };</code>
4836
4837 </pre>
4838 </div></div>
4839 </div>
4840 </div>
4841 <div class="sect2">
4842 <h3 id="_class_indexexpr">Class IndexExpr</h3>
4843 <div class="paragraph"><p>Class <strong>IndexExpr</strong> is a collection of 0 or more <strong>Value_P</strong>s or elided
4844 indices, where the <strong>Value_P</strong> 0 denotes an <em>elided index</em> (= all
4845 indices along an axis). <strong>IndexExpr</strong> objects are created and deleted by
4846 the prefix parser <strong>Prefix..cc</strong>. More precisely, a TOK_R_BRACK (for <strong>]</strong>),
4847 starts a new IndexExpr, a <strong>TOK_SEMICOL</strong> (for <strong>;</strong> append new values (axes) to
4848 an IndexExpr, and TOK_L_BRACK finalizes the creation of the <strong>IndexExpr</strong> .
4849 The next token (i.e. the token left of the <strong>IndexExpr</strong>) then calls
4850 the appropriate function (such as <strong>Value::index()</strong> or
4851 <strong>Symbol::assign_indexed()</strong> and deletes the <strong>IndexExpr</strong> again.</p></div>
4852 </div>
4853 <div class="sect2">
4854 <h3 id="_classes_bif_xxx_and_quad_xxx">Classes Bif_XXX and Quad_XXX</h3>
4855 <div class="paragraph"><p>APL offers a rather large number of primitives. In GNU APL, every primitive
4856 is implemented in its own class and according to the following rules:</p></div>
4857 <div class="ulist"><ul>
4858 <li>
4859 <p>
4860 The class name has a prefix that indicates the arity of the primitive and
4861 is one of:
4862 </p>
4863 <div class="ulist"><ul>
4864 <li>
4865 <p>
4866 <strong>Bif_F0</strong> for niladic primitives; there is only one: <strong>Bif_F0_ZILDE</strong> (for ⍬),
4867 </p>
4868 </li>
4869 <li>
4870 <p>
4871 <strong>Bif_F1_</strong> for strictly monadic primitives; there is only one:
4872 <strong>*Bif_F1_EXECUTE</strong> (for ⍎),
4873 </p>
4874 </li>
4875 <li>
4876 <p>
4877 <strong>Bif_F2_</strong> for strictly dyadic primitives,
4878 </p>
4879 </li>
4880 <li>
4881 <p>
4882 <strong>Bif_F12_</strong> for strictly nomadic primitives,
4883 </p>
4884 </li>
4885 <li>
4886 <p>
4887 <strong>Bif_OPER1_</strong> for monadic operators,
4888 </p>
4889 </li>
4890 <li>
4891 <p>
4892 <strong>Bif_OPER2_</strong> for dyadic operators, and
4893 </p>
4894 </li>
4895 <li>
4896 <p>
4897 <strong>Quad_</strong> for system functions. These are derived from class <strong>QuadFunction</strong>,
4898 primarily for grouping purposes. Note that the <strong>Quad_</strong> is also used for
4899 system variables. Similar to primitives, system functions with a large
4900 footprint have their own <strong>.hh</strong> and <strong>.cc</strong> files, while the remaining ones
4901 are collected in <strong>QuadFunction.hh/QuadFunction.cc</strong>.
4902 </p>
4903 </li>
4904 </ul></div>
4905 </li>
4906 <li>
4907 <p>
4908 classes with a large code footprint have their own implementation files
4909 (followed by <strong>.hh</strong> or <strong>.cc</strong>), where the filename is the class name,
4910 </p>
4911 </li>
4912 <li>
4913 <p>
4914 the remaining classes (with a small code footprint) are declared/implemented
4915 in files <strong>Primitivefunction.hh/Primitivefunction.cc</strong>
4916 </p>
4917 </li>
4918 <li>
4919 <p>
4920 Every class is, directly or indirectly, derived from base class <strong>Function</strong>.
4921 Class <strong>FunctionC</strong> defines all <strong>virtual eval_XXX()</strong> so that they thow a
4922 <strong>VALENCE_ERROR</strong>.
4923 </p>
4924 </li>
4925 <li>
4926 <p>
4927 Every class then overrides (only) those <strong>virtual eval_XXX()</strong> whose
4928 signatures it supports. For example, the strictly monadic primitive ⍎
4929 (execute) overrides <strong>virtual eval_B()</strong>, the strictly dyadic primitive ⌷
4930 overrides <strong>virtual eval_AB()</strong>. Most primitives are nomadic and therefore
4931 override (at least) <strong>virtual eval_B()</strong> <em>and</em> <strong>virtual eval_AB()</strong>. With this
4932 technique arity mismatches between the runtime parser and the supported
4933 signatures of a function (for example: the parser calls the stricly dyadic
4934 function ≠ monadically) are resolved without any runtime overhead.
4935 </p>
4936 </li>
4937 <li>
4938 <p>
4939 Due to the <strong>virtual eval_XXX()</strong> functions, every class needs a single
4940 (class static) instance which is called <strong>fun</strong> and every such instance of
4941 <strong>fun</strong> has a (static) pointer named <strong>_fun</strong> pointing to it.
4942 </p>
4943 </li>
4944 </ul></div>
4945 <div class="paragraph"><p>Except for the APL name (i.e. a single APL character for APL primitives and
4946 APL character ⎕ followed by 0 or more letters for APL system functions)
4947 there is no major difference between APL primitives and APL system functions.
4948 The different names are handled in <strong>Tokenizer.cc</strong>; the other noteworthy
4949 difference is that <em>all APL primitives</em>, but only a rather small <em>subset of
4950 APL system functions</em> (and, for that matter, <em>system variables</em>) were
4951 standardized in the ISO standard for APL2.</p></div>
4952 </div>
4953 <div class="sect2">
4954 <h3 id="_symbols">Symbols</h3>
4955 <div class="paragraph"><p>A symbol identifies an APL object created by the user, i.e. a defined
4956 function or a user defined variable. In GNU APL, system functions (⎕-functions,
4957 e.g. ⎕FX) are <em>NOT</em> symbols but <strong>APL primitives</strong>. The commonality between
4958 ⎕-functions and APL primitives is greater than the similarity
4959 between ⎕-variables and ⎕-functions. The only noteworthy difference between
4960 APL primitives and ⎕-functions is their name and the fact that the
4961 APL primitives are defined in the APL standard, while most ⎕-functions
4962 and ⎕-variables are not. They are used by APL interpreter providers to extend
4963 their functionality (and to lock-in their customers). The C++ class hierarchy
4964 in GNU APL (excerpted, the full picture is shown in the .png files) are:</p></div>
4965 <div class="listingblock">
4966 <div class="content">
4967 <pre>
4968
4969 <code> html/classSystemVariable__inherit__graph.png:
4970
4971 ┌─────────────┐
4972 │ NamedObject │
4973 └─────────────┘
4974 ↑
4975 ┌────────┐
4976 │ Symbol │
4977 └────────┘
4978 ↑
4979 ┌────────────────┐
4980 │ SystemVariable │
4981 └────────────────┘
4982 ↑
4983 ┌───────────────────┐
4984 │ NL-SystemVariable │
4985 └───────────────────┘
4986 ↑
4987 ╔═══════════════════╗
4988 ║ RO-SystemVariable ║
4989 ╚═══════════════════╝</code>
4990
4991 </pre>
4992 </div></div>
4993 <div class="paragraph"><p>and:</p></div>
4994 <div class="listingblock">
4995 <div class="content">
4996 <pre>
4997
4998 <code> html/classFunction__inherit__graph.png:
4999
5000 ┌─────────────────────────────────┐
5001 │ NamedObject │
5002 └─────────────────────────────────┘
5003 ↑ ↑
5004 ┌────────────────────────────────────┐ ┌────────┐
5005 │ Function │ │ Symbol │
5006 └────────────────────────────────────┘ └────────┘
5007 ↑ ↑ ↑
5008 ┌─────────────────────────────────────────────────────┐ ┌──────────────┐
5009 │ PrimitiveFunction │ │ UserFunction │
5010 └─────────────────────────────────────────────────────┘ └──────────────┘
5011 ↑ ↑ ↑ ↑
5012 ╔══════════════╗┌────────────────┐┌───────────────────┐ ┌───────┐
5013 ║ QuadFunction ║│ ScalarFunction ││ NonscalarFunction │ │ Macro │
5014 ╚══════════════╝└────────────────┘└───────────────────┘ └───────┘</code>
5015
5016 </pre>
5017 </div></div>
5018 <div class="paragraph"><p>Some explanations might be helpful:</p></div>
5019 <div class="ulist"><ul>
5020 <li>
5021 <p>
5022 the common base class <strong>NamedObject</strong>:
5023 </p>
5024 <div class="ulist"><ul>
5025 <li>
5026 <p>
5027 provides <strong>virtual UCS_string get_name()</strong> to obtain the name of the object,
5028 </p>
5029 </li>
5030 <li>
5031 <p>
5032 <strong>virtual const Function * NamedObject::get_function()</strong> returns a
5033 valid <strong>Function *</strong> for functions, otherwise 0,
5034 </p>
5035 </li>
5036 <li>
5037 <p>
5038 <strong>virtual Symbol * NamedObject::get_symbol()</strong> returns a valid <strong>Symbol *</strong> for
5039 symbols, otherwise 0.
5040 </p>
5041 </li>
5042 </ul></div>
5043 </li>
5044 <li>
5045 <p>
5046 <strong>SystemVariable</strong>s are variables like <strong>⎕IO</strong>, <strong>⎕FC</strong>, &#8230;
5047 </p>
5048 </li>
5049 <li>
5050 <p>
5051 <strong>NL_SystemVariable</strong>s are <strong>N</strong>on-<strong>L</strong>ocal system variables (for which
5052 localizing is permitted, but has no effect): <strong>⎕L</strong>, <strong>⎕R</strong>, &#8230;
5053 </p>
5054 </li>
5055 <li>
5056 <p>
5057 <strong>RO_SystemVariable</strong>s are <strong>R</strong>ead-<strong>O</strong>nly global system variables
5058 (for which localizing makes no sense): <strong>⎕AI</strong>, <strong>⎕ARG</strong>, &#8230;
5059 </p>
5060 </li>
5061 <li>
5062 <p>
5063 base class <strong>Function</strong> defines default behavior of (<strong>virtual eval_B()</strong>,
5064 <strong>eval_XB()</strong>, <strong>eval_AB()</strong>, <strong>eval_AXB()</strong> &#8230;). The default behavior is to
5065 throw a <strong>VALENCE ERROR</strong>, and derived classes simply overload the eval_XXX()
5066 functions that they implement.
5067 </p>
5068 </li>
5069 <li>
5070 <p>
5071 <strong>QuadFunction</strong>, <strong>ScalarFunction</strong>, and <strong>NonscalarFunction</strong> are groupings
5072 that simplify the implementation. For example, all scalar functions are
5073 implemented in the same way and only differ by their details at the Cell
5074 level.
5075 </p>
5076 </li>
5077 <li>
5078 <p>
5079 <strong>UserFunction</strong>s are the only functions that are also Symbols (so that
5080 their name appears in the symbol table. The system functions get their
5081 name via their ID.
5082 </p>
5083 </li>
5084 <li>
5085 <p>
5086 <strong>Macro</strong> is a <strong>UserFunction</strong> that is defined and can only be used by the
5087 interpreter itself. It cannot be called from APL programs and simplifies
5088 built-in operators when they are called with <strong>UserFunction</strong> arguments
5089 considerably.
5090 </p>
5091 </li>
5092 </ul></div>
5093 <div class="paragraph"><p>Symbols are created when the tokenizer detects a name for the first time
5094 <strong>SymbolTable::lookup_symbol()</strong>, <strong>SymbolTable::lookup_existing_symbol()</strong>,
5095 and <strong>SymbolTable::add_symbol()</strong>. Every symbol has a stack
5096 <strong>Symbol::value_stack</strong> of <strong>ValueStackItem</strong>s, which is pushed when the
5097 symbol is localized (at the beginning of a defined function) and popped
5098 when the execution of the defined function ends. The <strong>ValueStackItem</strong> at the
5099 top of the <strong>Symbol::value_stack</strong> defines the role (scope) that the symbol
5100 currently has. The possibilities are:</p></div>
5101 <div class="listingblock">
5102 <div class="content">
5103 <pre>
5104
5105 <code> NamedObject.hh:
5106
5107 enum NameClass
5108 {
5109 NC_INVALID = 0x0100, ///&lt; invalid name class.
5110 NC_UNUSED_USER_NAME = 0x0200, ///&lt; unused user name, not yet assigned
5111 NC_LABEL = 0x0401, ///&lt; Label.
5112 NC_VARIABLE = 0x0802, ///&lt; (assigned) variable.
5113 NC_FUNCTION = 0x1003, ///&lt; (defined) function.
5114 NC_OPERATOR = 0x2004, ///&lt; (defined) operator.
5115 NC_SYSTEM_VAR = 0x4005, ///&lt; system variable.
5116 NC_SYSTEM_FUN = 0x8006, ///&lt; system function.
5117 NC_case_mask = 0x00FF, ///&lt; almost ⎕NC
5118 NC_bool_mask = 0xFF00, ///&lt; for fast selection
5119
5120 NC_FUN_OPER = (NC_FUNCTION | NC_OPERATOR) &amp; NC_bool_mask,
5121 NC_left = (NC_VARIABLE |
5122 NC_UNUSED_USER_NAME |
5123 NC_SYSTEM_VAR |
5124 NC_INVALID // ⎕, ⍞, ⎕xx
5125 ) &amp; NC_bool_mask
5126 };</code>
5127
5128 </pre>
5129 </div></div>
5130 <div class="paragraph"><p>The <strong>NameClass</strong> is equally well suited for C/C++ <strong>switch</strong> statements (by using
5131 the lower byte) and for testing groups of <strong>NameClass</strong>es (by using the upper
5132 byte). After the tokenization (or localization) of a symbol it has the
5133 NameClass <strong>NC_UNUSED_USER_NAME</strong>; this changes to e.g. <strong>NC_VARIABLE</strong> when a
5134 value is assigned to the symbol, or to <strong>NC_FUNCTION</strong> when a defined function
5135 with the name of the symbol is defined with <strong>⎕FX</strong> or the ∇-editor.</p></div>
5136 <div class="paragraph"><p>The actual role of a user defined symbol can change over time, while the role
5137 of a system name is fixed and cannot be changed. E.g. <strong>⎕IO</strong> is always a
5138 variable and <strong>⎕UCS</strong> is always a function.</p></div>
5139 </div>
5140 <div class="sect2">
5141 <h3 id="_classes_symboltable_and_systemsymtab">Classes SymbolTable and SystemSymTab</h3>
5142 <div class="paragraph"><p>Every Symbol is contained in one of two symbol tables:</p></div>
5143 <div class="ulist"><ul>
5144 <li>
5145 <p>
5146 User-defined symbols are stored in a static instance of class <strong>SymbolTable</strong>,
5147 while
5148 </p>
5149 </li>
5150 <li>
5151 <p>
5152 system defined symbols (for system functions and system variables) are
5153 stored in a static instance of class <strong>SystemSymTab</strong>.
5154 </p>
5155 </li>
5156 </ul></div>
5157 <div class="paragraph"><p>Both symbol tables are contained in the base class <strong>Workspace_0</strong> of the
5158 static instance <strong>Workspace::the_workspace</strong>. The classes <strong>SymbolTable</strong> and
5159 <strong>SystemSymTab</strong> have a lot in common, but also differences. The common
5160 functionality is implemented in their template base class <strong>SymbolTableBase</strong>:</p></div>
5161 <div class="listingblock">
5162 <div class="content">
5163 <pre>
5164
5165 <code>SymbolTable.hh:
5166 ...
5167 /// common part of user-defined names and distinguished names
5168 template &lt;typename T, size_t SYMBOL_COUNT&gt;
5169 class SymbolTableBase
5170 {
5171 ...
5172 };</code>
5173
5174 </pre>
5175 </div></div>
5176 <div class="paragraph"><p>The differences are:</p></div>
5177 <div class="ulist"><ul>
5178 <li>
5179 <p>
5180 the table size (256 entries for <strong>SystemSymTab</strong> and 65536 entries for user
5181 defined names (constant <strong>SYMBOL_HASH_TABLE_SIZE</strong> in file <strong>SystemLimits.def</strong>
5182 </p>
5183 </li>
5184 <li>
5185 <p>
5186 On success the lookup of a user defined name returns a <strong>Symbol <strong>,
5187 while a lookup of a system name returns a *SystemName </strong>. Symbols are
5188 somewhat more complicated than SystemName because their role (like function
5189 or variable) can change over time while every SystemName has a fixed role
5190 (either function or variable) cannot. Also, every SystemName has a unique
5191 Id (defined in Id.def) while <em>all</em> user defined symbols have the same Id
5192 named *ID_USER_SYMBOL</strong>.
5193 </p>
5194 </li>
5195 <li>
5196 <p>
5197 In a way <strong>SystemName</strong>s are more lightweight versions of <strong>Symbol</strong>s.
5198 </p>
5199 </li>
5200 <li>
5201 <p>
5202 Symbol lookup. A lookup function has a single UCS_string argument with the
5203 name of a symbol. It returns a valid pointer on success and 0 otherwise.
5204 </p>
5205 <div class="ulist"><ul>
5206 <li>
5207 <p>
5208 The base class <strong>SymbolTableBase</strong> has a lookup function
5209 <strong>SymbolTableBase::lookup_existing_symbol()</strong> which returns a valid (non-zero)
5210 pointer.
5211 </p>
5212 </li>
5213 <li>
5214 <p>
5215 The derived class <strong>SymbolTable</strong> has another lookup function
5216 <strong>SymbolTable::lookup_symbol()</strong> which always succeeds. Instead of returning
5217 0 to indicate that a given symbol name is not in the table, it creates a
5218 new symbol with the name given.
5219 </p>
5220 </li>
5221 <li>
5222 <p>
5223 In contrast, class <strong>SystemSymTab</strong> has functions <strong>add_function()</strong> and
5224 <strong>add_variable()</strong> to add system functions and system varibles (whose role
5225 is, unlike for user defined names, implied by their Id).
5226 </p>
5227 </li>
5228 </ul></div>
5229 </li>
5230 <li>
5231 <p>
5232 Both tables use a hash function <strong>SymbolTableBase::function compute_hash()</strong>
5233 to determine the position of a name in the table, from which a linked
5234 list of colliding names starts.
5235 </p>
5236 </li>
5237 <li>
5238 <p>
5239 Erased names are not removed from a symbol table, but rather marked erased
5240 in the Symbol itself, while the symbol name remains in the table. This is to
5241 avoid searching for a user defined symbol in all existing tokens. Also, a
5242 symbol name could be )ERASEd at the top level but still exist as the name of
5243 a local in some defined function.
5244 </p>
5245 </li>
5246 </ul></div>
5247 <div class="paragraph"><p>The lookup of (and possibly the insertion of a new) Symbol uses the following
5248 algorithm (<strong>SymbolTable::lookup_symbol(const UCS_string &amp; sym_name)</strong>):</p></div>
5249 <div class="olist arabic"><ol class="arabic">
5250 <li>
5251 <p>
5252 compute a hash value from <strong>sym_name</strong>
5253 </p>
5254 </li>
5255 <li>
5256 <p>
5257 if <strong>symbol_table[hash]</strong> is <strong>0</strong> then the name is the first symbol with that
5258 hash (unsuccessful lookup case 1.)
5259 </p>
5260 <div class="olist loweralpha"><ol class="loweralpha">
5261 <li>
5262 <p>
5263 Create a <strong>new Symbol</strong> with the name <strong>sym_name</strong>,
5264 </p>
5265 </li>
5266 <li>
5267 <p>
5268 Store the new symbol in <strong>symbol_table[hash]</strong>, and
5269 </p>
5270 </li>
5271 <li>
5272 <p>
5273 return the new symbol.
5274 </p>
5275 </li>
5276 </ol></div>
5277 </li>
5278 <li>
5279 <p>
5280 Otherwise one or more symbols with the same hash already exists and the
5281 name may or may not already exist in the symbol table.
5282 </p>
5283 <div class="olist loweralpha"><ol class="loweralpha">
5284 <li>
5285 <p>
5286 Chase the linked list from <strong>symbol_table[hash]</strong> to its end (via pointer
5287 <strong>Symbol::next</strong>).
5288 </p>
5289 </li>
5290 <li>
5291 <p>
5292 If a symbol with name <strong>sym_name</strong> is detected on the way then return that
5293 symbol (successful lookup).
5294 </p>
5295 </li>
5296 </ol></div>
5297 </li>
5298 <li>
5299 <p>
5300 Otherwise <strong>sym_name</strong> was not in the table. Chase the linked list again.
5301 </p>
5302 <div class="olist loweralpha"><ol class="loweralpha">
5303 <li>
5304 <p>
5305 If a symbol is detected on the way with:
5306 </p>
5307 <div class="olist lowerroman"><ol class="lowerroman">
5308 <li>
5309 <p>
5310 an empty value stack, or
5311 </p>
5312 </li>
5313 <li>
5314 <p>
5315 a value stack with 1 item and name class <strong>NC_UNUSED_USER_NAME</strong>,
5316 </p>
5317 </li>
5318 <li>
5319 <p>
5320 then re-initialize that symbol with a new name <strong>sym_name</strong> and return the
5321 symbol (unsuccessful lookup case 2). The condition 4.a.i. or ii. is
5322 returned by <strong>Symbol::is_erased()</strong>.
5323 </p>
5324 </li>
5325 </ol></div>
5326 </li>
5327 </ol></div>
5328 </li>
5329 <li>
5330 <p>
5331 Otherwise (unsuccessful lookup case 3), the linked list contains no erased
5332 symbols that could be re-used. Append a new Symbol at the end of a list,
5333 like in (unsuccessful lookup case 1.) above.
5334 </p>
5335 </li>
5336 </ol></div>
5337 <div class="paragraph"><p>This algorithm</p></div>
5338 <div class="ulist"><ul>
5339 <li>
5340 <p>
5341 avoids unnecessary creation of new symbols by re-using formerly erased
5342 symbols (step 4.a.i above), and
5343 </p>
5344 </li>
5345 <li>
5346 <p>
5347 avoids unnecessary removals and insertions into the linked list by not
5348 removing symbols that are (temporarily) not used, e.g. after an )ERASE
5349 command.
5350 </p>
5351 </li>
5352 </ul></div>
5353 <div class="paragraph"><p>The case 4.a.i is not (or no longer) supposed to happen since all Symbol
5354 constructors now <strong>push()</strong> an item with name class <strong>NC_UNUSED_USER_NAME</strong> onto
5355 the Symbol&#8217;s value stack and the <strong>pop()</strong> function asserts if the last item
5356 of the value stack would be removed,</p></div>
5357 <div class="paragraph"><p>The primary user of the symbol tables is the tokenizer (which translates human
5358 readable names into <strong>Symbol</strong>s. The vast majority of symbol table lookups
5359 occur when APL code is tokenized (∇-editor, ⎕FX) along with only a few system
5360 functions (those that use variable or function names or APL code strings at
5361 runtime).</p></div>
5362 </div>
5363 <div class="sect2">
5364 <h3 id="_executables">Executables</h3>
5365 <div class="paragraph"><p>Every APL input line is first tokenized, and the result of this tokenization
5366 is an <strong>Executable</strong>. The primary content of any <strong>Executable</strong> is a <strong>TokenString</strong>
5367 named <strong>body</strong>:</p></div>
5368 <div class="listingblock">
5369 <div class="content">
5370 <pre>
5371
5372 <code> Token.hh:
5373
5374 class Token_string : public std::vector&lt;Token&gt; { ... }
5375
5376 Executable.hh:
5377
5378 // Base class for ExecuteList, StatementList, and UserFunction
5379 class Executable
5380 {
5381 ...
5382 Token_string body;
5383 ...
5384 }</code>
5385
5386 </pre>
5387 </div></div>
5388 <div class="paragraph"><p>The rest of class <strong>Executable</strong> are helper functions that support the
5389 execution of the raw <strong>body</strong>.</p></div>
5390 <div class="paragraph"><p>There are 3 cases where <strong>Executable</strong>s are produced (in order of decreasing
5391 relevance):</p></div>
5392 <div class="olist arabic"><ol class="arabic">
5393 <li>
5394 <p>
5395 The definition of a defined function, either interactively with the
5396 ∇-editor, or programmatically in APL with <strong>⎕FX</strong>.
5397 </p>
5398 </li>
5399 <li>
5400 <p>
5401 The evaluation of a single input line in immediate execution, and
5402 </p>
5403 </li>
5404 <li>
5405 <p>
5406 The evaluation of an APL string with APL primitive ⍎ (aka. Execute).
5407 </p>
5408 </li>
5409 </ol></div>
5410 <div class="paragraph"><p>For each of these 3 cases (which also correspond to the <strong>ParseMode</strong> mentioned
5411 above), a corresponding class for that case is derived:</p></div>
5412 <div class="listingblock">
5413 <div class="content">
5414 <pre>
5415
5416 <code> Executable.hh:
5417 ...
5418 class UserFunction : public Function, public Executable { ... }
5419 class StatementList : public Executable { ... }
5420 class ExecuteList : public Executable { ... }
5421 ....</code>
5422
5423 </pre>
5424 </div></div>
5425 <div class="paragraph"><p>In some debug outputs, as well as in some commands and in ⎕SI, the derived
5426 class of a particular <strong>Executable</strong> is displayed as a single character like
5427 this:</p></div>
5428 <div class="ulist"><ul>
5429 <li>
5430 <p>
5431 class UserFunction: ∇
5432 </p>
5433 </li>
5434 <li>
5435 <p>
5436 class StatementList: ◊
5437 </p>
5438 </li>
5439 <li>
5440 <p>
5441 class ExecuteList: ⍎
5442 </p>
5443 </li>
5444 </ul></div>
5445 <div class="paragraph"><p>Each of a derived class has a static function <strong>fix()</strong> which creates an
5446 instance of that class. The <strong>fix()</strong> function acts as a constructor for
5447 the derived class. Unlike a normal constructor (which either returns a
5448 valid object of the class or else throws an execption, the <strong>fix()</strong> function
5449 returns 0 if the construction fails (typically due to some SYNTAX ERROR in
5450 the input line(s) passed to it. The <strong>fix()</strong> functon of class <strong>UserFunction</strong>
5451 is essentially the implementation of <strong>monadic ⎕FX</strong>, and is also called after
5452 the ∇-editor finalizes a function definition (trailing ∇).</p></div>
5453 <div class="paragraph"><p>The main differences between the 3 classes are:</p></div>
5454 <div class="tableblock">
5455 <table rules="all"
5456 frame="border"
5457 cellspacing="0" cellpadding="4">
5458 <col />
5459 <col />
5460 <col />
5461 <col />
5462 <col />
5463 <col />
5464 <thead>
5465 <tr>
5466 <th align="left" valign="top"> Derived Class </th>
5467 <th align="center" valign="top"> Number of lines </th>
5468 <th align="center" valign="top"> Statements/Line </th>
5469 <th align="center" valign="top"> Jumps/Labels<br />
5470 </th>
5471 <th align="center" valign="top"> Arguments </th>
5472 <th align="center" valign="top"> Result</th>
5473 </tr>
5474 </thead>
5475 <tbody>
5476 <tr>
5477 <td align="left" valign="top"><p class="table">ExecuteList</p></td>
5478 <td align="center" valign="top"><p class="table">single line</p></td>
5479 <td align="center" valign="top"><p class="table">one</p></td>
5480 <td align="center" valign="top"><p class="table">no</p></td>
5481 <td align="center" valign="top"><p class="table">no</p></td>
5482 <td align="center" valign="top"><p class="table">yes</p></td>
5483 </tr>
5484 <tr>
5485 <td align="left" valign="top"><p class="table">StatementList</p></td>
5486 <td align="center" valign="top"><p class="table">single line</p></td>
5487 <td align="center" valign="top"><p class="table">several</p></td>
5488 <td align="center" valign="top"><p class="table">no</p></td>
5489 <td align="center" valign="top"><p class="table">no</p></td>
5490 <td align="center" valign="top"><p class="table">no</p></td>
5491 </tr>
5492 <tr>
5493 <td align="left" valign="top"><p class="table">UserFunction ∇, ⎕FX</p></td>
5494 <td align="center" valign="top"><p class="table">multiple lines</p></td>
5495 <td align="center" valign="top"><p class="table">several</p></td>
5496 <td align="center" valign="top"><p class="table">yes</p></td>
5497 <td align="center" valign="top"><p class="table">yes</p></td>
5498 <td align="center" valign="top"><p class="table">yes</p></td>
5499 </tr>
5500 <tr>
5501 <td align="left" valign="top"><p class="table">UserFunction { &#8230; }</p></td>
5502 <td align="center" valign="top"><p class="table">single line</p></td>
5503 <td align="center" valign="top"><p class="table">one</p></td>
5504 <td align="center" valign="top"><p class="table">no</p></td>
5505 <td align="center" valign="top"><p class="table">yes</p></td>
5506 <td align="center" valign="top"><p class="table">yes</p></td>
5507 </tr>
5508 </tbody>
5509 </table>
5510 </div>
5511 <div class="sect3">
5512 <h4 id="_classes_executelist_and_statementlist">Classes ExecuteList and StatementList</h4>
5513 <div class="paragraph"><p>The execution of <strong>ExecuteList</strong>s and <strong>StatementList</strong>s is rather simple:
5514 the tokens of their body are intepreted from the start of the body to its end,
5515 no arguments can be passed to the body, no change of the execution with
5516 branches (→N), etc.</p></div>
5517 </div>
5518 <div class="sect3">
5519 <h4 id="_class_userfunction">Class UserFunction</h4>
5520 <div class="paragraph"><p>Defined functions (class UserFunction) are far more complex. As can be seen
5521 above, class UserFunction inherits from the two classes <strong>Function</strong> and
5522 <strong>Executable</strong>.</p></div>
5523 <div class="paragraph"><p>The inheritance from <strong>Function</strong> overrides its various virtual <strong>eval_XXX()</strong>
5524 functions, so that a defined function can be called like an APL primitive,
5525 The arity of a defined function (monadic, dyadic, operator, etc.) is
5526 determined by the function header and since class UserFunction cannot "know"
5527 it before the header is parsed, it must overload all possible virtual
5528 <strong>eval_XXX()</strong> declared in base class <strong>Function</strong>.</p></div>
5529 <div class="paragraph"><p>The inheritance from <strong>Executable</strong> provides the construction of the body
5530 (tokenization of the function lines (in class <strong>Tokenizer</strong>), pre-parsing (in
5531 class <strong>Parser</strong>), and some optimizations (also in class <strong>Parser</strong>).</p></div>
5532 <div class="paragraph"><p>There are two aspects of class <strong>UserFunction</strong> that are not considered in
5533 either of its base classes:</p></div>
5534 <div class="ulist"><ul>
5535 <li>
5536 <p>
5537 Local variables, and
5538 </p>
5539 </li>
5540 <li>
5541 <p>
5542 Branches (→N)
5543 </p>
5544 </li>
5545 </ul></div>
5546 <div class="paragraph"><p>These aspects are implemented by means of two member variables:</p></div>
5547 <div class="ulist"><ul>
5548 <li>
5549 <p>
5550 the variable <strong>vector&lt;Function_PC&gt; UserFunction::line_starts</strong> is a jump table
5551 that tells, for every function line N, which token is the start of line N
5552 in the (linear) body of the executable. For an APL jump statement, say <strong>→N</strong>,
5553 <strong>line_starts[N]</strong> is the first token in line N and its execution simply
5554 continues at token <strong>body[line_starts[N]]</strong>.
5555 </p>
5556 </li>
5557 <li>
5558 <p>
5559 when a defined function is called, its arguments (if any) are assigned
5560 to the formal arguments declared in the function header and the local
5561 variables in the header are pushed. This happens in member
5562 <strong>UserFunction::UserFunction_header header</strong>, which is a functional grouping
5563 of the somewhat large class <strong>UserFunction</strong>.
5564 </p>
5565 </li>
5566 </ul></div>
5567 <div class="paragraph"><p>Let:</p></div>
5568 <div class="listingblock">
5569 <div class="content">
5570 <pre>
5571
5572 <code>∇Z←A SUM B;C;D
5573 Z←A + B
5574 ∇</code>
5575
5576 </pre>
5577 </div></div>
5578 <div class="paragraph"><p>The typical life-cycle of Userfunction SUM is then:</p></div>
5579 <div class="olist arabic"><ol class="arabic">
5580 <li>
5581 <p>
5582 The <strong>Userfunction</strong> named <strong>SUM</strong>, is created with the ∇-editor or with <strong>⎕FX</strong>
5583 (which calls <strong>Userfunction::fix()</strong>).
5584 </p>
5585 </li>
5586 <li>
5587 <p>
5588 The APL pattern matching (<strong>Prefix.cc</strong>) detects a call of <strong>SUM</strong>,
5589 say pattern <strong>3 SUM 42</strong>.
5590 </p>
5591 </li>
5592 <li>
5593 <p>
5594 The function is called:
5595 </p>
5596 <div class="olist loweralpha"><ol class="loweralpha">
5597 <li>
5598 <p>
5599 Symbols <strong>Z</strong>, <strong>A</strong>, <strong>B</strong>, <strong>C</strong>, and <strong>D</strong> are pushed (and become, at this
5600 point in time, temporarily undefined).
5601 </p>
5602 </li>
5603 <li>
5604 <p>
5605 The actual arguments <strong>2</strong> and <strong>42</strong> of <strong>SUM</strong> are assigned to the formal
5606 arguments <strong>A</strong> and <strong>B</strong> of <strong>SUM</strong>. that is, <strong>A←3 ◊ B←42</strong>; <strong>C</strong>, <strong>D</strong> and <strong>Z</strong>
5607 remain undefined.
5608 </p>
5609 </li>
5610 <li>
5611 <p>
5612 The <strong>Executable::body</strong> is executed. This computes <strong>Z←45</strong>; <strong>Z</strong> is now
5613 defined.
5614 </p>
5615 </li>
5616 </ol></div>
5617 </li>
5618 <li>
5619 <p>
5620 At some point in time <strong>SUM</strong> returns (either when the end of the body is
5621 reached, or the when execution is stopped explitly (e.g. <strong>→0</strong>).
5622 </p>
5623 <div class="olist loweralpha"><ol class="loweralpha">
5624 <li>
5625 <p>
5626 The current value of <strong>Z</strong> (if any) is set aside (and will later be
5627 returned to the caller).
5628 </p>
5629 </li>
5630 <li>
5631 <p>
5632 The Symbols <strong>Z</strong>, <strong>A</strong>, <strong>B</strong>, <strong>C</strong>, and <strong>D</strong> are popped again (restored).
5633 </p>
5634 </li>
5635 <li>
5636 <p>
5637 The pattern matched above (<strong>3 SUM 42</strong>) is replaced by formerly defined
5638 value of <strong>Z</strong> (i.e. <strong>45</strong>).
5639 </p>
5640 </li>
5641 </ol></div>
5642 </li>
5643 </ol></div>
5644 </div>
5645 </div>
5646 <div class="sect2">
5647 <h3 id="_class_stateindicator">Class StateIndicator</h3>
5648 <div class="paragraph"><p>An <strong>Executable</strong>, as described above, is a static object that is essentially
5649 the tokenization of some APL source code string(s). An Executable is created
5650 once and deleted if no longer needed. Between the creation and the deletion of
5651 an Executable it remains unchanged (aka. static). An Executable is therefore
5652 comparable to the object code (binary) of a compiled language.</p></div>
5653 <div class="paragraph"><p>To produce a result, an Executable must be <em>executed</em>. While the Executable
5654 itself remains unchanged, the different executions of the same Executable
5655 can have rather different results, e.g. an APL value in one execution and an
5656 error in another.</p></div>
5657 <div class="paragraph"><p>Every execution of an Executable is performed in a context which tracks the
5658 state and the progress of the execution. This context is an object of class
5659 <strong>StateIndicator</strong>. The complete execution of an Executable consists of the
5660 following principle steps:</p></div>
5661 <div class="olist arabic"><ol class="arabic">
5662 <li>
5663 <p>
5664 Creation of a new StateIndicator, then
5665 </p>
5666 </li>
5667 <li>
5668 <p>
5669 Execution of the Tokens in the body of the executable, then
5670 </p>
5671 </li>
5672 <li>
5673 <p>
5674 returning the result of the execution (if any) to the initiator of the
5675 execution, and finally
5676 </p>
5677 </li>
5678 <li>
5679 <p>
5680 Destruction of the StateIndicator.
5681 </p>
5682 </li>
5683 </ol></div>
5684 <div class="paragraph"><p>In step 2. above, the execution of an executable (the same or a different) is
5685 started, i.e. before the current execution has completed. To accomplish this,
5686 the different contexts of different executions, regardless of whether the
5687 execution concerns the same Executable (direct recursion) or different ones
5688 (function call, possibly indirect recursion), are organized as a stack, known
5689 as the <em>)SI stack</em>. In GNU APL the )SI stack is a singly-linked list of
5690 StateIndicator objects, the link between adjacent StateIndicator objects
5691 in the )SI stack is member <strong>StateIndicator::parent</strong>.</p></div>
5692 <div class="paragraph"><p>The constructor of StateIndicator has two arguments: the executable to be
5693 executed and the parent that has initiated the execution:</p></div>
5694 <div class="listingblock">
5695 <div class="content">
5696 <pre>
5697
5698 <code>StateIndicator.hh:
5699 ...
5700 /// constructor
5701 StateIndicator(const Executable * exec, StateIndicator * _par);
5702 ...</code>
5703
5704 </pre>
5705 </div></div>
5706 <div class="paragraph"><p>The constructor initializes <strong>StateIndicator::parent</strong>, which does not change
5707 thereafter. Since the )SI stack is a linked list, it can be represented as a
5708 single pointer to the most recent StateIndicator object. This pointer is
5709 stored in class <strong>Workspace</strong>:</p></div>
5710 <div class="listingblock">
5711 <div class="content">
5712 <pre>
5713
5714 <code>Workspace.hh:
5715 ...
5716 /// the SI stack. Initially top_SI is 0 (empty stack)
5717 StateIndicator * top_SI;</code>
5718
5719 </pre>
5720 </div></div>
5721 <div class="paragraph"><p>The full picture is something like this:</p></div>
5722 <div class="listingblock">
5723 <div class="content">
5724 <pre>
5725
5726 <code>╔═══ Workspace ═══╗ ╔═════ StateIndicator ═══╗
5727 ║ ┌──────┐ ║ ║┌──────┐ ║
5728 ║ │top_SI├─╫─ → ─╢│ 0 │ ║ (latest )SI entry)
5729 ║ └──────┘ ║ ║└──────┘ ║
5730 ╚═════════════════╝ ╚╤═══════════════════════╝
5731 ↑
5732 ╔═══╪═ StateIndicator ═══╗
5733 ║┌──┴───┐ ║
5734 ║│parent│ ║
5735 ║└──────┘ ║
5736 ╚╤═══════════════════════╝
5737 ↑
5738 ...
5739 ↑
5740 ╔═══╪═ StateIndicator ═══╗
5741 ║┌──┴───┐ ║
5742 ║│parent│ ║
5743 ║└──────┘ ║
5744 ╚╤═══════════════════════╝
5745 ↑
5746 ╔═══╪═ StateIndicator ═══╗
5747 ║┌──┴───┐ ║
5748 ║│parent│ ║ (oldest )SI entry)
5749 ║└──────┘ ║
5750 ╚════════════════════════╝</code>
5751
5752 </pre>
5753 </div></div>
5754 <div class="paragraph"><p>By and large the only active StateIndicator is the topmost one. The
5755 StateIndicators below it are relatively passive and only come back
5756 to life when the topmost StateIndicator, sometimes referred to as <strong>TOS</strong>
5757 (for <strong>T</strong>op <strong>O</strong>f <strong>S</strong>tack) has finalized step 4. above. An error in the
5758 execution of TOS normally does not end it, but another StateIndicator is
5759 pushed (with mode immediate execution) onto the stack. The only cases that
5760 remove (pop) one or more items from the SI stack are:</p></div>
5761 <div class="ulist"><ul>
5762 <li>
5763 <p>
5764 the executable in TOS reaches the end of the body of its Executable.
5765 This is the normal case,
5766 </p>
5767 </li>
5768 <li>
5769 <p>
5770 the user issues )SIC command (in immediate execution mode). This clears
5771 the entire )SI stack,
5772 </p>
5773 </li>
5774 <li>
5775 <p>
5776 the executable in TOS performs a → statement (aka. Escape), which rolls back
5777 the stack until the pop&#8217;ed StateIndicator was an Executable with mode
5778 immediate execution. That Executable was the one pushed above (i.e. due to
5779 an error). This pops the topmost item(s) until the TOS is an immediate
5780 execution context,
5781 </p>
5782 </li>
5783 <li>
5784 <p>
5785 the execution of TOS had an error and TOS was under the control of
5786 ⎕EA, ⎕EB, or ⎕EC. This pops the topmost item(s) until TOS has the
5787 ⎕EA, ⎕EB, or ⎕EC that catches the error (as opposed to the normal
5788 error handling which would have pushed another StateIndicator onto
5789 the stack rather than popping items off it).
5790 </p>
5791 </li>
5792 </ul></div>
5793 <div class="paragraph"><p>The TOS is needed in many different situations, therefore class <strong>Workspace</strong>
5794 provides, for convenience, a number of static wrapper functions that access
5795 frequently used members of its <strong>top_SI</strong>.</p></div>
5796 <div class="admonitionblock">
5797 <table><tr>
5798 <td class="icon">
5799 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
5800 </td>
5801 <td class="content">Strictly speaking the <em>APL )SI stack</em> refers to the entire linked
5802 list of contexts, i.e. to <strong>Workspace::top_SI</strong>. In GNU APL, though, the
5803 StateIndicator class implements an individual entry in that linked list.</td>
5804 </tr></table>
5805 </div>
5806 <div class="paragraph"><p>The Class has two more members of interest:</p></div>
5807 <div class="listingblock">
5808 <div class="content">
5809 <pre>
5810
5811 <code>StateIndicator.hh:
5812 ...
5813 /// details of the last error in this context.
5814 Error error;
5815
5816 /// the current-stack of this context.
5817 Prefix current_stack;
5818 ...</code>
5819
5820 </pre>
5821 </div></div>
5822 <div class="paragraph"><p><strong>StateIndicator::error</strong> contains the error information (if any) for the
5823 StateIndicator, e.g. for ⎕EM. There can be several StateIndicators on the
5824 stack that were (temporarily) suspended and can be continued later
5825 (by some →N back into the failed Executable and therefore information on
5826 several errors may be present in the different StateIndicator objects.</p></div>
5827 <div class="paragraph"><p><strong>StateIndicator::current_stack</strong> is far more interesting. It manages
5828 the inner loop that runs along the body of the Executable of the
5829 StateIndicator. See below.</p></div>
5830 </div>
5831 <div class="sect2">
5832 <h3 id="_class_prefix">Class Prefix</h3>
5833 <div class="paragraph"><p>Class <strong>Prefix</strong> is an iterator over the body of an Executable. Its most
5834 important member is a stack named <strong>content</strong>:</p></div>
5835 <div class="listingblock">
5836 <div class="content">
5837 <pre>
5838
5839 <code>Prefix.hh:
5840
5841 ...
5842 /// put pointer (for the next token at PC). Since content is a stack,
5843 /// its put position is also its size.
5844 int put;
5845
5846 /** the lookahead tokens (tokens that were shifted but not yet reduced).
5847 \b content is in body order, that is, content[0] is the oldest
5848 (= rightmost in APL order) token and content[put - 1] is the latest.
5849 **/
5850 Token_loc content[MAX_CONTENT];
5851 ...
5852 /// one phrase in the phrase table
5853 struct Phrase
5854 {
5855 const char * phrase_name; ///&lt; phrase name
5856 const char * reduce_name; ///&lt; reduce function name
5857 void (Prefix::*reduce_fun)(); ///&lt; reduce function
5858 unsigned int phrase_hash; ///&lt; phrase hash
5859 int prio; ///&lt; phrase priority
5860 int misc; ///&lt; 1 if MISC phrase
5861 int phrase_len; ///&lt; phrase length
5862 uint8_t sub_nodes[TC_MAX_PHRASE+1]; ///&lt; parent nodes
5863 };</code>
5864
5865 </pre>
5866 </div></div>
5867 <div class="paragraph"><p>Member <strong>Prefix::content</strong> is by and large processed as follows:</p></div>
5868 <div class="olist arabic"><ol class="arabic">
5869 <li>
5870 <p>
5871 The constructor <strong>Prefix::Prefix()</strong> starts with an empty stack.
5872 </p>
5873 </li>
5874 <li>
5875 <p>
5876 If the stack starts with a valid phrase, then:
5877 </p>
5878 <div class="olist loweralpha"><ol class="loweralpha">
5879 <li>
5880 <p>
5881 The function <strong>Phrase::reduce_fun</strong> in the phrase is called;
5882 </p>
5883 </li>
5884 <li>
5885 <p>
5886 The <strong>reduce_fun()</strong> is a non-static member of class <strong>Prefix</strong> and can
5887 therefore access the content in order to produce a result.
5888 The <strong>reduce_fun()</strong> has no arguments because accessing <strong>content</strong> directly
5889 is far more efficient than using (C++) function arguments.
5890 </p>
5891 </li>
5892 <li>
5893 <p>
5894 The <strong>reduce_fun()</strong> will then:
5895 </p>
5896 <div class="olist lowerroman"><ol class="lowerroman">
5897 <li>
5898 <p>
5899 extract the relevant data from the tokens <strong>Prefix::content</strong>,
5900 remove (pop) the tokens from the <strong>Prefix::content</strong>, and
5901 </p>
5902 </li>
5903 <li>
5904 <p>
5905 push the result (if any) onto the <strong>Prefix::content</strong>.
5906 </p>
5907 </li>
5908 </ol></div>
5909 </li>
5910 </ol></div>
5911 </li>
5912 <li>
5913 <p>
5914 Otherwise the stack does not start with a valid phrase. In this case
5915 the next <strong>Token_loc</strong> is fetched from the body of the <strong>Executable</strong>.
5916 </p>
5917 </li>
5918 </ol></div>
5919 <div class="paragraph"><p>The following picture may illustrate the above (the entire algorithm is
5920 somewhat more complicated):</p></div>
5921 <div class="listingblock">
5922 <div class="content">
5923 <pre>
5924
5925 <code> ╔══ StateIndicator::Executable-&gt;body ═════╗
5926 ║ ║
5927 ║ PC=0 ... PC=k-1 PC=k ... ║ PC is Prefix::PC
5928 ║┌────┐ ┌─────┐ ┌───┐ ┌───┐ ┌───┐ ║
5929 ║│ T0 │ ... │ Tₖ₋₁│ │ Tₖ│ │ T │ │ T │ ... ║ body
5930 ║└────┘ └─────┘ └─┬─┘ └───┘ └───┘ ║
5931 ╚═════════════════════│═══════════════════╝
5932 │
5933 ↓ Prefix::push_next_token()
5934 │
5935 ╔═══│══ Prefix::content ═════════╗
5936 ║ ┌─┴─┐┌─────┐ ┌─────┐ ┌───┐ ║
5937 ┌─── → ───│ Tₖ││ Tₖ₋₁│ │ Tₖ₋₂│ │ T │ ... ║
5938 │ ║ └─┬─┘└──┬──┘ └──┬──┘ └───┘ ║
5939 │ ╚═══│═════│═══════│══════════════╝
5940 │ │ │ │
5941 ↑ at0() at1() at2()
5942 │ │ │ │
5943 │ ┌─┴─────┴───────┴─┐
5944 │ │ reduce_XXX() │ i.e. (*Phrase::reduce_fun)()
5945 │ └───────┬─────────┘
5946 │ │
5947 └──── result ─────┘
5948 Prefix::pop_args_push_result(result)</code>
5949
5950 </pre>
5951 </div></div>
5952 <div class="paragraph"><p>Apart from <strong>Prefix::content</strong>, Prefix has the following members of interest
5953 (the others just helpers that make the code more readable):</p></div>
5954 <div class="ulist"><ul>
5955 <li>
5956 <p>
5957 The Functions <strong>Prefix::at0()</strong>, <strong>Prefix::at1()</strong>, <strong>Prefix::at2()</strong>, and
5958 <strong>Prefix::at3()</strong> are primarily used by the different
5959 <strong>Phrase::reduce_fun</strong>s in order to access the (up to) 4 leftmost items
5960 in <strong>Prefix::content</strong>. The phrases have different lengths (from 1 to 4)
5961 and the matching of a phrase guarantees that sufficiently many
5962 <strong>Token_loc</strong>s exist on the stack. It is important to note that, although
5963 the body of the <strong>Executable</strong> is in reversed order as explained earlier,
5964 at0(), at1(), at2(), at3() are in APL order (to make the code easier to
5965 understand). For example the items in phrase <strong>A + B</strong> are:
5966 </p>
5967 <div class="ulist"><ul>
5968 <li>
5969 <p>
5970 at0() ←→ <strong>A</strong>,
5971 </p>
5972 </li>
5973 <li>
5974 <p>
5975 at1() ←→ <strong>+</strong>, and
5976 </p>
5977 </li>
5978 <li>
5979 <p>
5980 at2() ←→ <strong>B</strong>.
5981 </p>
5982 </li>
5983 </ul></div>
5984 </li>
5985 <li>
5986 <p>
5987 Put differently, <strong>Prefix::content</strong>, has a "hot end" that is manipulated
5988 with <strong>Prefix::push_next_token()</strong> and the various <strong>reduce_XXX()</strong> functions and
5989 a "cold end" containing tokens that are put on ice for a while.
5990 </p>
5991 </li>
5992 <li>
5993 <p>
5994 <strong>Prefix::PC</strong> is the <strong>P</strong>rogram <strong>C</strong>ounter. The APL interpreter can be
5995 considered as a (lightweight) virtual machine whose opcodes are Tokens
5996 and whose program(s) are lists of Tokens (= <strong>Executable</strong>s). In this
5997 model <strong>PC</strong> is the program counter of the virtual machine.
5998 </p>
5999 <div class="ulist"><ul>
6000 <li>
6001 <p>
6002 The excution of an executable starts with PC=0 (= start of the body)
6003 in constructor <strong>Prefix::Prefix()</strong>.
6004 </p>
6005 </li>
6006 <li>
6007 <p>
6008 Whenever a new <strong>Token_loc</strong> is needed (in Step 3. above) then the new
6009 <strong>Token_loc</strong> is: <strong>Token_loc(body[PC], PC)</strong> and PC is incremented.
6010 </p>
6011 </li>
6012 <li>
6013 <p>
6014 The execution is complete when <strong>PC == body.size()</strong> (or after <strong>→0</strong> in defined
6015 functions).
6016 </p>
6017 </li>
6018 </ul></div>
6019 </li>
6020 <li>
6021 <p>
6022 <strong>saved_MISC</strong>: many APL functions are <em>nomadic</em>, which means that they
6023 can be called monadically or dyadically. This includes <em>most</em> APL primitives
6024 and <em>all</em> non-niladic defined functions.
6025 </p>
6026 <div class="ulist"><ul>
6027 <li>
6028 <p>
6029 There is a fixed set of <em>MISC</em> tokens (more precisely: <strong>TokenClass</strong>es)
6030 that cause a nomadic function to be evaluated monadically: ← → [ ◊ F C M
6031 and (.
6032 </p>
6033 </li>
6034 <li>
6035 <p>
6036 All phrases that start with a <strong>MISC</strong> token (and differ only by that token)
6037 use the same <strong>Phrase::reduce_fun</strong>. For example, the two APL statements
6038 <strong>X←⌊1.5</strong> and <strong>-⌊1.5</strong> will both first match phrase MISC_F_B. This is because
6039 </p>
6040 <div class="ulist"><ul>
6041 <li>
6042 <p>
6043 (aka. F) and ← are <strong>MISC</strong> tokens. Therefore both phrases will first
6044 compute <strong>⌊1.5</strong> in <strong>Prefix::reduce_MISC_F_B_()</strong> and result <strong>1</strong>.
6045 </p>
6046 </li>
6047 </ul></div>
6048 </li>
6049 <li>
6050 <p>
6051 In contrast, in APL statement <strong>2⌊1.5</strong> the leftmost value <strong>2</strong> is NOT a <strong>MISC</strong>
6052 token and therefore <strong>Prefix::reduce_A_F_B_()</strong> will be called.
6053 </p>
6054 </li>
6055 <li>
6056 <p>
6057 Unfortunately the decision about whether <strong>Prefix::reduce_MISC_F_B_()</strong> or
6058 <strong>Prefix::reduce_A_F_B_()</strong> matches can only be made <em>after</em> either <strong>MISC</strong>
6059 or <strong>A</strong> was fetched. IOW: this decision requires a lookahead of 1 token (and
6060 Prefix is therefore essentially a so-called LALR(1) parser). If the
6061 decision is in favor of <strong>Prefix::reduce_A_F_B_()</strong>, then everything is fine.
6062 However, for <strong>Prefix::reduce_MISC_F_B_()</strong> the <strong>MISC</strong> token is not used and
6063 must be considered somehow. The solution is to store the <strong>MISC</strong> token in
6064 C++ variable <strong>Prefix::saved_MISC</strong>. Note that one cannot simply decrement
6065 the PC in order to <em>undo</em> the fetching of the <strong>MISC</strong> token. Decrementing
6066 the PC looks much simpler at first glance, but would only work if the <strong>MISC</strong>
6067 token came directly from the body of the <strong>Executable</strong>.
6068 Sometimes, though, (defined function <strong>F</strong>, defined operator <strong>M</strong>,
6069 or complete index <strong>C</strong>) the <strong>MISC</strong> token is the result of a Symbol
6070 resolution (cases <strong>F</strong> and <strong>M</strong>) or even of a prior computation with
6071 side effects (case complete index <strong>C</strong>).
6072 </p>
6073 </li>
6074 </ul></div>
6075 </li>
6076 </ul></div>
6077 <div class="paragraph"><p>The following picture is a slightly more detailed version of the previous one:</p></div>
6078 <div class="listingblock">
6079 <div class="content">
6080 <pre>
6081
6082 <code> ╔══ StateIndicator::Executable-&gt;body ═════╗
6083 ║ ║
6084 ║ PC=0 ... PC=k-1 PC=k ... ║ PC is Prefix::PC
6085 ║┌────┐ ┌─────┐ ┌───┐ ┌───┐ ┌───┐ ║
6086 ║│ T0 │ ... │ Tₖ₋₁│ │ Tₖ│ │ T │ │ T │ ... ║ body
6087 ║└────┘ └─────┘ └─┬─┘ └───┘ └───┘ ║
6088 ╚═════════════════════│═══════════════════╝
6089 ↓
6090 Prefix::saved_MISC │
6091 ┌────┐ │
6092 ┌───┤ Tₖ ├─ → ─┤ saved_MISC ≠ TOK_VOID, otherwise body[PC++]
6093 │ └─┬──┘ ↓ in: Prefix::push_next_token()
6094 │ │ │
6095 │ ↑ ╔═══│══ Prefix::content ═════════╗
6096 │ └──MISC──┤ ║
6097 │ ║ ↓ ║
6098 ↑ ║ ┌─┴─┐┌─────┐ ┌─────┐ ┌───┐ ║
6099 │ ┌─── → ───│ Tₖ││ Tₖ₋₁│ │ Tₖ₋₂│ │ T │ ... ║
6100 │ │ ║ └─┬─┘└──┬──┘ └──┬──┘ └───┘ ║
6101 │ │ ╚═══│═════│═══════│══════════════╝
6102 │ │ │ │ │
6103 │ ↑ at0() at1() at2()
6104 │ │ │ │ │
6105 │ │ ┌─┴─────┴───────┴─┐
6106 │ │ │ reduce_XXX() │ i.e. (*Phrase::reduce_fun)()
6107 │ │ └──────┬──┬───────┘
6108 │ │ │ │
6109 │ └──── result ────┘ │ in: Prefix::reduce_XXX()
6110 │ │ └─── pop_args_push_result()
6111 │ │
6112 └──── clear_MISC() ────┘ in: Prefix::reduce_A_XXX()
6113 but not in: Prefix::reduce_MISC_XXX()</code>
6114
6115 </pre>
6116 </div></div>
6117 </div>
6118 <div class="sect2">
6119 <h3 id="_apl_functions">APL Functions</h3>
6120 <div class="paragraph"><p>APL implements two types of functions:</p></div>
6121 <div class="ulist"><ul>
6122 <li>
6123 <p>
6124 APL primitives (functions and operators) and system functions. They are
6125 defined by the interpreter itself and are implemented in C++, and
6126 </p>
6127 </li>
6128 <li>
6129 <p>
6130 defined functions, coded in APL by the programmer.
6131 </p>
6132 </li>
6133 </ul></div>
6134 <div class="paragraph"><p>Every function has a <strong>signature</strong> that defines the combination of arguments
6135 that the function can be called with. The total number of possible signatures
6136 is somewhat large, but a single function only implements a rather small subset
6137 of them and returns a <strong>VALENCE ERROR</strong> when the <strong>Prefix</strong> parser calls a
6138 function with a signature that the called function does not support. In GNU
6139 APL the signature is a bitmap of <strong>signature atoms</strong> where every atom
6140 corresponds to a possible position of an argument in a <strong>Prefix</strong> pattern.
6141 However, not all possible bitmaps are valid signatures (e.g. a function with
6142 a left argument but no right argument):</p></div>
6143 <div class="listingblock">
6144 <div class="content">
6145 <pre>
6146
6147 <code> APL_types.hh:
6148 ...
6149 /// signature of a defined function
6150 enum Fun_signature
6151 {
6152 // signature atoms
6153 //
6154 SIG_NONE = 0, ///&lt;
6155 SIG_Z = 0x01, ///&lt; function has a result
6156 SIG_A = 0x02, ///&lt; function has a left argument
6157 SIG_LO = 0x04, ///&lt; operator left operand
6158 SIG_FUN = 0x08, ///&lt; function (always set)
6159 SIG_RO = 0x10, ///&lt; operator right operand
6160 SIG_X = 0x20, ///&lt; RO has an axis
6161 SIG_B = 0x40, ///&lt; function has a right argument
6162
6163 // operator variants
6164 //
6165 SIG_FUN_X = SIG_FUN | SIG_X, ///&lt; function with axis
6166 SIG_OP1 = SIG_LO | SIG_FUN, ///&lt; monadic operator
6167 SIG_OP1_X = SIG_OP1 | SIG_X, ///&lt; monadic operator with axis
6168 SIG_OP2 = SIG_OP1 | SIG_RO, ///&lt; dyadic operator
6169 SIG_LORO = SIG_LO | SIG_RO, ///&lt; monadic or dyadic operator
6170
6171 // argument variants
6172 //
6173 SIG_NIL = SIG_NONE, ///&lt; niladic function
6174 SIG_MON = SIG_B, ///&lt; monadic function or operator
6175 SIG_DYA = SIG_A | SIG_B, ///&lt; dyadic function or operator
6176
6177 // allowed combinations of operator variants and argument variants...
6178
6179 // niladic
6180 //
6181 SIG_F0 = SIG_FUN | SIG_NIL, ///&lt; dito
6182
6183 SIG_Z_F0 = SIG_Z | SIG_F0, ///&lt; dito
6184
6185 // monadic
6186 //
6187 SIG_F1_B = SIG_MON | SIG_FUN, ///&lt; dito
6188 SIG_F1_X_B = SIG_MON | SIG_FUN_X, ///&lt; dito
6189 SIG_LO_OP1_B = SIG_MON | SIG_OP1, ///&lt; dito
6190 SIG_LO_OP1_X_B = SIG_MON | SIG_OP1_X, ///&lt; dito
6191 SIG_LO_OP2_RO_B = SIG_MON | SIG_OP2, ///&lt; dito
6192
6193 SIG_Z_F1_B = SIG_Z | SIG_F1_B, ///&lt; dito
6194 SIG_Z_F1_X_B = SIG_Z | SIG_F1_X_B, ///&lt; dito
6195 SIG_Z_LO_OP1_B = SIG_Z | SIG_LO_OP1_B, ///&lt; dito
6196 SIG_Z_LO_OP1_X_B = SIG_Z | SIG_LO_OP1_X_B, ///&lt; dito
6197 SIG_Z_LO_OP2_RO_B = SIG_Z | SIG_LO_OP2_RO_B, ///&lt; dito
6198
6199 // dyadic
6200 //
6201 SIG_A_F2_B = SIG_DYA | SIG_FUN, ///&lt; dito
6202 SIG_A_F2_X_B = SIG_DYA | SIG_FUN_X, ///&lt; dito
6203 SIG_A_LO_OP1_B = SIG_DYA | SIG_OP1, ///&lt; dito
6204 SIG_A_LO_OP1_X_B = SIG_DYA | SIG_OP1_X, ///&lt; dito
6205 SIG_A_LO_OP2_RO_B = SIG_DYA | SIG_OP2, ///&lt; dito
6206
6207 SIG_Z_A_F2_B = SIG_Z | SIG_A_F2_B, ///&lt; dito
6208 SIG_Z_A_F2_X_B = SIG_Z | SIG_A_F2_X_B, ///&lt; dito
6209 SIG_Z_A_LO_OP1_B = SIG_Z | SIG_A_LO_OP1_B, ///&lt; dito
6210 SIG_Z_A_LO_OP1_X_B = SIG_Z | SIG_A_LO_OP1_X_B, ///&lt; dito
6211 SIG_Z_A_LO_OP2_RO_B = SIG_Z | SIG_A_LO_OP2_RO_B, ///&lt; dito
6212 };
6213 ...</code>
6214
6215 </pre>
6216 </div></div>
6217 <div class="paragraph"><p>There are 0x80 = 128 possible signature bitmaps of which 22 are valid.</p></div>
6218 <div class="paragraph"><p>The base class <strong>Function</strong> of all functions has a non-virtual function
6219 <strong>get_signature()</strong> which is computed from some virtual functions related to
6220 signature atoms:</p></div>
6221 <div class="listingblock">
6222 <div class="content">
6223 <pre>
6224
6225 <code> Function.cc:
6226 ...
6227 un_signature
6228 Function::get_signature() const
6229 {
6230 int sig = SIG_FUN;
6231 if (has_result()) sig |= SIG_Z;
6232 if (has_axis()) sig |= SIG_X;
6233
6234 if (get_oper_valence() == 2) sig |= SIG_RO;
6235 if (get_oper_valence() &gt;= 1) sig |= SIG_LO;
6236
6237 if (get_fun_valence() == 2) sig |= SIG_A;
6238 if (get_fun_valence() &gt;= 1) sig |= SIG_B;
6239
6240 return Fun_signature(sig);
6241 }</code>
6242
6243 </pre>
6244 </div></div>
6245 <div class="paragraph"><p>System functions do not use signatures because their classes know the
6246 signatures that they support already at compile time. Defined functions
6247 are different because all are handled by the same class <strong>UserFunction</strong> even
6248 though the signatures of different functions are defined by the user in
6249 the function header and the signatures differ. The only class that cares
6250 about the signature (of its left function argument) is <strong>Bif_OPER1_EACH</strong>.</p></div>
6251 <div class="paragraph"><p>For every signature <strong>XXX</strong> that a function class supports it must overload
6252 the corresponding <strong>eval_XXX()</strong> function. In our example, primitive <strong>+</strong>
6253 supports monadic <strong>+B</strong> and dyadic <strong>A+B</strong>. It therefore overloads <strong>eval_B()</strong>
6254 and <strong>eval_AB()</strong>. Likewise the axis variant A[X] is overloaded with
6255 <strong>eval_AB()</strong> and then delegated to their companions in class <strong>ScalarFunction</strong>.</p></div>
6256 <div class="listingblock">
6257 <div class="content">
6258 <pre>
6259
6260 <code> ScalarFunction.hh:
6261 ...
6262 class Bif_F12_PLUS : public ScalarFunction
6263 {
6264 ...
6265 virtual Token eval_B(Value_P B) const
6266 { return eval_scalar_B(B, &amp;Cell::bif_conjugate); }
6267
6268 /// overloaded Function::eval_AB().
6269 virtual Token eval_AB(Value_P A, Value_P B) const
6270 { return eval_scalar_AB(A, B,
6271 inverse ? &amp;Cell::bif_add_inverse : &amp;Cell::bif_add); }
6272 ...
6273 }</code>
6274
6275 </pre>
6276 </div></div>
6277 <div class="admonitionblock">
6278 <table><tr>
6279 <td class="icon">
6280 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
6281 </td>
6282 <td class="content">You may wonder what is the matter with <strong>bif_add_inverse()</strong>. Some
6283 primitives have an inverse function which, if present, can be used by the
6284 dyadic operator <strong>⍣</strong> (power operator, not to be confused with the power
6285 function <strong>⋆</strong>) for negative power arguments. The inverse of <strong>A+B</strong> is
6286 <strong>A-B</strong> because A = (A+B)-B. Most of the circular functions "A○B" have
6287 inverses but most others do not. The power operator is, for good reasons,
6288 neither defined in the ISO APL standard, nor in the IBM APL2 language
6289 reference, but implemented in GNU APL. Avoid it where possible.</td>
6290 </tr></table>
6291 </div>
6292 <div class="paragraph"><p>To summarize the execution of APL functions:</p></div>
6293 <div class="olist arabic"><ol class="arabic">
6294 <li>
6295 <p>
6296 The Prefix parser matches a pattern, for example <strong>A F B</strong>.
6297 </p>
6298 </li>
6299 <li>
6300 <p>
6301 The reduce function for the pattern A F B is <strong>reduce_A_F_B()</strong>.
6302 </p>
6303 </li>
6304 <li>
6305 <p>
6306 The <strong>Function *</strong> in token <strong>F</strong> is e.g. <strong>Bif_F12_PLUS::fun</strong>.
6307 </p>
6308 </li>
6309 <li>
6310 <p>
6311 The pattern also implies that the eval_AB() function of <strong>F</strong>, i.e.
6312 <strong>Bif_F12_PLUS::fun&#8594;eval_AB(A, B)</strong> shall be called in order to produce the
6313 reduction result.
6314 </p>
6315 </li>
6316 <li>
6317 <p>
6318 <strong>Bif_F12_PLUS::fun&#8594;eval_AB()</strong> is in-lined and calls
6319 <strong>ScalarFunction::eval_AB()</strong> with argument <strong>Cell::bif_add</strong>. It returns
6320 some result Z.
6321 </p>
6322 </li>
6323 <li>
6324 <p>
6325 The Prefix parser replaces <strong>A F B</strong> with <strong>Z</strong> which finalizes the execution of
6326 <strong>A F B</strong>.
6327 </p>
6328 </li>
6329 </ol></div>
6330 <div class="paragraph"><p>All other derivates of class <strong>Function</strong> are executed in the same fashion.</p></div>
6331 <div class="sect3">
6332 <h4 id="_important_differences">Important differences</h4>
6333 <div class="paragraph"><p>An important difference between <strong>ExecuteList</strong>, <strong>StatementList</strong>, and
6334 <strong>UserFunction</strong> concerns the last token in their body. This token is not
6335 produced by the tokenizer (since there is no APL code for it), but by the
6336 corrsponding <strong>fix()</strong> function of the derived class. The last token
6337 determines if and how a value is returned to the caller of the executable:</p></div>
6338 <div class="tableblock">
6339 <table rules="all"
6340 frame="border"
6341 cellspacing="0" cellpadding="4">
6342 <col />
6343 <col />
6344 <col />
6345 <thead>
6346 <tr>
6347 <th align="left" valign="top"> Executable Type </th>
6348 <th align="center" valign="top"> Final Token </th>
6349 <th align="center" valign="top"> Result</th>
6350 </tr>
6351 </thead>
6352 <tbody>
6353 <tr>
6354 <td align="left" valign="top"><p class="table">ExecuteList</p></td>
6355 <td align="center" valign="top"><p class="table">TOK_RETURN_EXEC</p></td>
6356 <td align="center" valign="top"><p class="table">APL Value</p></td>
6357 </tr>
6358 <tr>
6359 <td align="left" valign="top"><p class="table">StatementList</p></td>
6360 <td align="center" valign="top"><p class="table">TOK_RETURN_STATS</p></td>
6361 <td align="center" valign="top"><p class="table">None</p></td>
6362 </tr>
6363 <tr>
6364 <td align="left" valign="top"><p class="table">UserFunction w/o result</p></td>
6365 <td align="center" valign="top"><p class="table">TOK_RETURN_VOID</p></td>
6366 <td align="center" valign="top"><p class="table">None</p></td>
6367 </tr>
6368 <tr>
6369 <td align="left" valign="top"><p class="table">UserFunction with result</p></td>
6370 <td align="center" valign="top"><p class="table">TOK_RETURN_SYMBOL</p></td>
6371 <td align="center" valign="top"><p class="table">APL Value (if assigned)</p></td>
6372 </tr>
6373 </tbody>
6374 </table>
6375 </div>
6376 </div>
6377 </div>
6378 <div class="sect2">
6379 <h3 id="_class_nable">Class Nable</h3>
6380 <div class="paragraph"><p>Class <strong>Nable</strong> implements the interactive function editor (for lack of a
6381 better name, sometimes referred to as the ∇-editor). The constructor
6382 <strong>Nable::Nabla(const UCS_string &amp; cmd)</strong> starts the editor with the first
6383 editor command (which either creates a new function or else modifies an
6384 existing one). Every serious APL programmer knows the ∇-editor and its
6385 implementation is straightforward.</p></div>
6386 <div class="paragraph"><p>IBM APL2 defines the ∇-editor to be recursive (so one can start the editing
6387 of some other defined function while editing another defined function),
6388 this feature was <em>not</em> implemented in the GNU APL ∇-editor.</p></div>
6389 </div>
6390 <div class="sect2">
6391 <h3 id="_class_workspace">Class Workspace</h3>
6392 <div class="paragraph"><p>Class <strong>Workspace</strong> is a container that includes all objects that were
6393 created or modified by the user (through the execution of APL programs,
6394 APL commands, and/or APL expressions). These objects are:</p></div>
6395 <div class="ulist"><ul>
6396 <li>
6397 <p>
6398 Defined functions,
6399 </p>
6400 </li>
6401 <li>
6402 <p>
6403 User variables,
6404 </p>
6405 </li>
6406 <li>
6407 <p>
6408 (The values of) System variables,
6409 </p>
6410 </li>
6411 <li>
6412 <p>
6413 the symbol table, and
6414 </p>
6415 </li>
6416 <li>
6417 <p>
6418 The state indicator, aka. <em>the SI stack</em>.
6419 </p>
6420 </li>
6421 </ul></div>
6422 <div class="paragraph"><p>APL primitives are constant in nature therefore need not be stored in a
6423 workspace.</p></div>
6424 <div class="paragraph"><p>The APL interpreter has (only) one static <strong>Workspace</strong> instance named
6425 <strong>Workspace::the_workspace</strong>.</p></div>
6426 <div class="paragraph"><p>The <strong>)SAVE</strong> command serializes the binary <strong>Workspace::the_workspace</strong> into a
6427 human readable .xml file. The <strong>)LOAD</strong> or <strong>)COPY</strong> commands may later
6428 de-serialized a .xml workspace file back into the <strong>Workspace::the_workspace</strong>
6429 of another APL session. Many of the APL commands modify the state of
6430 <strong>Workspace::the_workspace</strong>. According to <strong>Workspace.hh</strong> the data relevant
6431 members of class <strong>Workspace</strong> are:</p></div>
6432 <div class="listingblock">
6433 <div class="content">
6434 <pre>
6435
6436 <code> Workspace
6437 ╔═══════════════════════════════════╗
6438 ║ ┌─────────┐ ┌────────────┐ ║
6439 ║ │ WS_name │ │ APL prompt │ ║
6440 ║ └─────────┘ └────────────┘ ║
6441 ║ ║
6442 ║ ┌─────────────────┐ ║
6443 ║ ┌─┴───────────────┐ │ ║
6444 ║ │ System Variable ├─┘ ∘∘∘ ║
6445 ║ └─────────────────┘ ║
6446 ║ ║
6447 ║ ┌────────────────┐ ║
6448 ║ ┌─┴──────────────┐ │ ║
6449 ║ │ StateIndicator ├─┘ ∘∘∘ ║
6450 ║ └────────────────┘ ║
6451 ╚═══════════════════════════════════╝</code>
6452
6453 </pre>
6454 </div></div>
6455 <div class="paragraph"><p>As a matter of fact, class <strong>Workspace</strong> is derived from a simple container
6456 <strong>Workspace_0</strong> which contains 2 more items <strong>symbol_table</strong> and
6457 <strong>distinguished_names</strong>,</p></div>
6458 <div class="listingblock">
6459 <div class="content">
6460 <pre>
6461
6462 <code>Workspace.hh:
6463
6464 class Workspace_0
6465 {
6466 protected:
6467 /// the symbol table for user-defined names of this workspace.
6468 SymbolTable symbol_table;
6469
6470 /// the symbol table for system names (aka. distinguished names) of
6471 /// this workspace.
6472 SystemSymTab distinguished_names;
6473 };</code>
6474
6475 </pre>
6476 </div></div>
6477 <div class="paragraph"><p>The idea between this distinction between base class <strong>Workspace_0</strong> and
6478 derived class <strong>Workspace</strong> is twofold:</p></div>
6479 <div class="olist arabic"><ol class="arabic">
6480 <li>
6481 <p>
6482 It ensures that the two symbol tables <strong>symbol_table</strong> (for user-defined
6483 names) and <strong>distinguished_names</strong> for system functions and system variables
6484 are properly initialized (to empty tables) so that the constructor of the
6485 derived class <strong>Workspace</strong> can simply add system functions and variables
6486 without needing to drain (clear) the tables first.
6487 </p>
6488 </li>
6489 <li>
6490 <p>
6491 It separates the interpreter state produced by the user (and which is
6492 <strong>)SAVE</strong>d into a .xml file) from the interpreter state produced by the
6493 intialization of the interpreter itself. Since <em>system functions</em> are
6494 constant in nature, they have no state related to them. Therefore the
6495 initial state of the interpreter is primarily the initial state of all
6496 system variables, for example after a <strong>)CLEAR</strong> command.
6497 </p>
6498 </li>
6499 </ol></div>
6500 </div>
6501 <div class="sect2">
6502 <h3 id="_class_xml_archive">Class XML_Archive</h3>
6503 <div class="paragraph"><p>The <strong>)SAVE</strong>ing or <strong>)LOAD</strong>ing of workspaces into or from resp. .xml files
6504 is a somewhat lengthy process. It therefore has its own implementation files
6505 <strong>Archive.hh</strong> and <strong>Archive.cc</strong>. Class <strong>Workspace</strong> is the only user of these
6506 classes. There are actually 3 classes:</p></div>
6507 <div class="ulist"><ul>
6508 <li>
6509 <p>
6510 Class <strong>XML_Archive</strong> is the base class. It merely contains an
6511 <strong>enum ArchiveSyntax</strong> for verifying that the archive software versions of
6512 a <strong>)LOAD</strong>ed .xml file is compatible with the software version that
6513 <strong>)SAVE</strong>d the archive (and warns the user if not).
6514 </p>
6515 </li>
6516 <li>
6517 <p>
6518 Derived class <strong>XML_Saving_Archive</strong> contains the functions for serializing
6519 a <strong>Workspace</strong> object into a file. Serializing means that the internal
6520 (binary) representation of a workspace is written into a (human readable)
6521 format from which it can later be de-serialized. IOW: class derived
6522 <strong>XML_Saving_Archive</strong> implements the <strong>)SAVE</strong> command.
6523 </p>
6524 </li>
6525 <li>
6526 <p>
6527 Class <strong>XML_Loading_Archive</strong> contains the functions for de-serializing a
6528 <strong>Workspace</strong> object from the (human readable) format back to its internal
6529 (binary) representation. IOW: class <strong>XML_Loading_Archive</strong> implements the
6530 various <strong>)LOAD</strong> and <strong>)COPY</strong> commands for .xml files.
6531 </p>
6532 </li>
6533 </ul></div>
6534 <div class="sect3">
6535 <h4 id="_serialization_apl_to_xml">Serialization (APL to XML)</h4>
6536 <div class="paragraph"><p>The serialization is split into a small number of categories or <em>loops</em>.
6537 Each such loop iterates over the same kind of data structures in the
6538 workspace. To simplify the later de-serialization of a .xml file, the order
6539 of the different loops matters, while the order of items inside a loop is
6540 arbitrary. The loops are:</p></div>
6541 <div class="olist arabic"><ol class="arabic">
6542 <li>
6543 <p>
6544 loop over all defined functions, then
6545 </p>
6546 </li>
6547 <li>
6548 <p>
6549 loop over all APL values, then
6550 </p>
6551 </li>
6552 <li>
6553 <p>
6554 loop over all symbols, then
6555 </p>
6556 </li>
6557 <li>
6558 <p>
6559 loop over user defined commands (a non-standard GNU APL feature), then
6560 </p>
6561 </li>
6562 <li>
6563 <p>
6564 loop over )SI stack entries.
6565 </p>
6566 </li>
6567 </ol></div>
6568 <div class="paragraph"><p>The de-serializer expects the XML tags to appear in the same order. The
6569 following table shows the mapping between C++ types and XML tags (some
6570 trivial C++ types were omitted):</p></div>
6571 <div class="tableblock">
6572 <table rules="all"
6573 frame="border"
6574 cellspacing="0" cellpadding="4">
6575 <col />
6576 <col />
6577 <thead>
6578 <tr>
6579 <th align="left" valign="top"> C++ type</th>
6580 <th align="left" valign="top"> XML Tag(s) and attributes</th>
6581 </tr>
6582 </thead>
6583 <tbody>
6584 <tr>
6585 <td align="left" valign="top"><p class="table">Cell []</p></td>
6586 <td align="left" valign="top"><p class="table">&lt;Ravel vid="&#8230;" bytes="&#8230;"/&gt;</p></td>
6587 </tr>
6588 <tr>
6589 <td align="left" valign="top"><p class="table">Prefix</p></td>
6590 <td align="left" valign="top"><p class="table">&lt;Parser size="&#8230;" assign-pending="&#8230;"
6591 action="&#8230;" lookahead-high="&#8230;"&gt; &#8230; &lt;/Parser&gt;</p></td>
6592 </tr>
6593 <tr>
6594 <td align="left" valign="top"><p class="table">StateIndicator</p></td>
6595 <td align="left" valign="top"><p class="table">&lt;SI-entry level="&#8230;" pc="&#8230;" line="&#8230;"&gt; Body &lt;/SI-entry&gt;</p></td>
6596 </tr>
6597 <tr>
6598 <td align="left" valign="top"><p class="table">Symbol</p></td>
6599 <td align="left" valign="top"><p class="table">&lt;Symbol name="&#8230;" stack-size="&#8230;"&gt; Value Stack&#8230; &lt;/Symbol&gt;</p></td>
6600 </tr>
6601 <tr>
6602 <td align="left" valign="top"><p class="table">SymbolTable []</p></td>
6603 <td align="left" valign="top"><p class="table">&lt;SymbolTable size="&#8230;"&gt; Symbols&#8230; &lt;/SymbolTable&gt;</p></td>
6604 </tr>
6605 <tr>
6606 <td align="left" valign="top"><p class="table">Token</p></td>
6607 <td align="left" valign="top"><p class="table">&lt;Token pc="&#8230;" tag="&#8230;" val="&#8230;" /&gt;</p></td>
6608 </tr>
6609 <tr>
6610 <td align="left" valign="top"><p class="table">UserFunction</p></td>
6611 <td align="left" valign="top"><p class="table">&lt;Function vid="&#8230;"/&gt; Body &lt;/Function&gt;</p></td>
6612 </tr>
6613 <tr>
6614 <td align="left" valign="top"><p class="table">UserCommand []</p></td>
6615 <td align="left" valign="top"><p class="table">&lt;Commands size="&#8230;"&gt; Command&#8230; &lt;/Commands&gt;</p></td>
6616 </tr>
6617 <tr>
6618 <td align="left" valign="top"><p class="table">UserCommand</p></td>
6619 <td align="left" valign="top"><p class="table">&lt;Command name="&#8230;" mode="&#8230;" fun="&#8230;"/&gt;
6620 rk="&#8230;" sh_0="&#8230;"&#8230;/&gt;</p></td>
6621 </tr>
6622 <tr>
6623 <td align="left" valign="top"><p class="table">UCS_string</p></td>
6624 <td align="left" valign="top"><p class="table">&lt;UCS uni="&#8230;"/&gt;</p></td>
6625 </tr>
6626 <tr>
6627 <td align="left" valign="top"><p class="table">Value</p></td>
6628 <td align="left" valign="top"><p class="table">&lt;Value flg="&#8230;" vid="&#8230;" parent="&#8230;"</p></td>
6629 </tr>
6630 </tbody>
6631 </table>
6632 </div>
6633 <div class="paragraph"><p>Some remarks:</p></div>
6634 <div class="ulist"><ul>
6635 <li>
6636 <p>
6637 The XML tag corresponds to the C++ type,
6638 </p>
6639 </li>
6640 <li>
6641 <p>
6642 the tag attributes correspond to the data members of the C++ type.
6643 To the extent possible, all data members are stored in a single tag (i.e.
6644 &lt;TAG &#8230; /&gt; in XML) as opposed to start and end tags (i.e. &lt;TAG &#8230;&gt; &#8230;
6645 &lt;/TAG&gt; in XML. The exceptions are C++ classes of variable size (arrays,
6646 vectors, linked lists, etc.), whose items are serialized between the
6647 corresponding start and end tags,
6648 </p>
6649 </li>
6650 <li>
6651 <p>
6652 empty vectors, etc., may be omitted.
6653 </p>
6654 </li>
6655 </ul></div>
6656 <div class="paragraph"><p>Most C++ types are mapped to XML in a straightforward fashion. The exceptions
6657 are described in more detail in the following.</p></div>
6658 </div>
6659 <div class="sect3">
6660 <h4 id="_serialization_of_apl_values">Serialization of APL Values</h4>
6661 <div class="paragraph"><p>The serialization of APL Values is implemented with two different loops: one
6662 for the (typically large) ravel of the value, and one for the rest (i.e.
6663 shape, flags, etc.</p></div>
6664 <div class="paragraph"><p>In C++, objects refer to each other by their addresses in memory (aka.
6665 <em>pointers</em> in C, and/or <em>references</em> in C++). Even though one could, in
6666 theory, emulate pointers with file offsets, this would lead to a rather
6667 complicated implementation. Instead, GNU APL uses IDs that define relations
6668 between different objects in the serialization; the IDs thus replace pointers
6669 and/or references in C++.</p></div>
6670 <div class="paragraph"><p>The IDs are different <strong>enum</strong> types (so that the C++ compiler can detect
6671 the use of incompatible pointers). The <strong>enum Vid { &#8230; }</strong> is the ID that
6672 relates XML &lt;Value &#8230;/&gt; tags to the corresponding XML &lt;Ravel &#8230;/&gt; tag.</p></div>
6673 <div class="paragraph"><p>For example, Let:</p></div>
6674 <div class="listingblock">
6675 <div class="content">
6676 <pre>
6677
6678 <code> )CLEAR
6679 )WSID TEST
6680
6681 Z←1 2 3
6682
6683 )SAVE</code>
6684
6685 </pre>
6686 </div></div>
6687 <div class="paragraph"><p>The )SAVE command serializes workspace TEST (into file TEST.xml) like this:</p></div>
6688 <div class="listingblock">
6689 <div class="content">
6690 <pre>
6691
6692 <code>TEST.xml:
6693 ...
6694 &lt;Ravel vid="15" cells="³1³2³3"/&gt;
6695 ...
6696 &lt;Value flg="0x400" vid="15" parent="-1" rk="1" sh-0="3"/&gt;
6697 ...
6698 &lt;SymbolTable size="1"&gt;
6699 &lt;Symbol name="Z" stack-size="1"&gt;
6700 &lt;Variable vid="15"/&gt;
6701 &lt;/Symbol&gt;
6702 &lt;/SymbolTable&gt;</code>
6703
6704 </pre>
6705 </div></div>
6706 <div class="paragraph"><p>The serialization of the &lt;Ravel&gt; deserves some more explanation. As already
6707 explained, the <strong>vid="15"</strong> attribute of the &lt;Ravel&gt; tag links the ravel to
6708 the &lt;Value with (the same) <strong>vid="15"</strong>. This information alone suffices to
6709 de-serialize the right side <strong>1 2 3</strong> of the assignment <strong>Z←1 2 3</strong>. However,
6710 the assignment to APL variable (aka. Symbol) <strong>Z</strong> has created a second tag
6711 <strong>&lt;Variable vid="15"/&gt;</strong> which links the (current) APL value <strong>1 2 3</strong> to symbol
6712 <strong>Z</strong>. In the above example <strong>Z</strong> is a global variable, therefore there is only
6713 one <strong>&lt;Variable &#8230;&gt;</strong> tag between the <strong>&lt;Symbol&gt;</strong> &#8230; <strong>&lt;/Symbol&gt;</strong> tags.
6714 However, if the symbol <strong>Z</strong> were localized, then every localization would
6715 have added a new XML item between the <strong>&lt;Symbol&gt;</strong> &#8230; <strong>&lt;/Symbol&gt;</strong> tags.</p></div>
6716 <div class="paragraph"><p>Now, a "proper" XML would have serialized the ravel <strong>1 2 3</strong> as follows:</p></div>
6717 <div class="paragraph"><p>&lt;Ravel vid="15"&gt;
6718 &lt;Cell type="integer" value="1"/&gt;
6719 &lt;Cell type="integer" value="2"/&gt;
6720 &lt;Cell type="integer" value="3"/&gt;
6721 &lt;/Ravel&gt;</p></div>
6722 <div class="paragraph"><p>Since ravels tend to be rather long (so that Cells are frequent), the "proper"
6723 &lt;Cell /&gt; tags were optimized into a far more compact format which may
6724 be called <strong>micro tags</strong> (or <strong>μ-Tags</strong> ). A micro tag is a single Unicode
6725 character that defines the following properties and purposes that are
6726 related to (and later needed to de-serialize) a Cell:</p></div>
6727 <div class="ulist"><ul>
6728 <li>
6729 <p>
6730 the C++ Cell Type (CharCell, IntCell, FloatCell, ComplexCell, PointerCell,
6731 or LvalCell)
6732 </p>
6733 </li>
6734 <li>
6735 <p>
6736 the Encoding used for the Cell value (only relevant for CharCell), and
6737 </p>
6738 </li>
6739 <li>
6740 <p>
6741 to serve as a delimiter between adjacent Cells of the ravel.
6742 </p>
6743 </li>
6744 </ul></div>
6745 <div class="paragraph"><p>The different encodings of CharCells are needed because XML has restrictions
6746 on the characters that are permitted in XML attributes and to abbreviate
6747 the most likely characters (i.e ASCII).</p></div>
6748 <div class="paragraph"><p>The following table lists the micro tags used:</p></div>
6749 <div class="tableblock">
6750 <table rules="all"
6751 frame="border"
6752 cellspacing="0" cellpadding="4">
6753 <col />
6754 <col />
6755 <col />
6756 <col />
6757 <col />
6758 <thead>
6759 <tr>
6760 <th align="center" valign="top">μ-Tag</th>
6761 <th align="left" valign="top">C++ Cell Type</th>
6762 <th align="left" valign="top">Encoding </th>
6763 <th align="left" valign="top">Example </th>
6764 <th align="right" valign="top">Cells</th>
6765 </tr>
6766 </thead>
6767 <tbody>
6768 <tr>
6769 <td align="center" valign="top"><p class="table">⁰</p></td>
6770 <td align="left" valign="top"><p class="table">CharCell</p></td>
6771 <td align="left" valign="top"><p class="table">end of Unicode character sequence</p></td>
6772 <td align="left" valign="top"><p class="table">²Hello⁰</p></td>
6773 <td align="right" valign="top"><p class="table">≥ 1</p></td>
6774 </tr>
6775 <tr>
6776 <td align="center" valign="top"><p class="table">¹</p></td>
6777 <td align="left" valign="top"><p class="table">CharCell</p></td>
6778 <td align="left" valign="top"><p class="table">sprintf("%X", ASCII-value)</p></td>
6779 <td align="left" valign="top"><p class="table">¹A (aka. LF)</p></td>
6780 <td align="right" valign="top"><p class="table">1</p></td>
6781 </tr>
6782 <tr>
6783 <td align="center" valign="top"><p class="table">²</p></td>
6784 <td align="left" valign="top"><p class="table">CharCell</p></td>
6785 <td align="left" valign="top"><p class="table">start of Unicode character sequence</p></td>
6786 <td align="left" valign="top"><p class="table">²World⁰</p></td>
6787 <td align="right" valign="top"><p class="table">≥ 1</p></td>
6788 </tr>
6789 <tr>
6790 <td align="center" valign="top"><p class="table">³</p></td>
6791 <td align="left" valign="top"><p class="table">IntCell</p></td>
6792 <td align="left" valign="top"><p class="table">integer (sequence of digits 0-9)</p></td>
6793 <td align="left" valign="top"><p class="table">³42</p></td>
6794 <td align="right" valign="top"><p class="table">1</p></td>
6795 </tr>
6796 <tr>
6797 <td align="center" valign="top"><p class="table">⁴</p></td>
6798 <td align="left" valign="top"><p class="table">FloatCell</p></td>
6799 <td align="left" valign="top"><p class="table">sprintf("%.17g", value)</p></td>
6800 <td align="left" valign="top"><p class="table">⁴42.56</p></td>
6801 <td align="right" valign="top"><p class="table">1</p></td>
6802 </tr>
6803 <tr>
6804 <td align="center" valign="top"><p class="table">⁵</p></td>
6805 <td align="left" valign="top"><p class="table">ComplexCell</p></td>
6806 <td align="left" valign="top"><p class="table">sprintf("%17gJ%17g", real, imag)</p></td>
6807 <td align="left" valign="top"><p class="table">⁵1.2J3.4</p></td>
6808 <td align="right" valign="top"><p class="table">1</p></td>
6809 </tr>
6810 <tr>
6811 <td align="center" valign="top"><p class="table">⁶</p></td>
6812 <td align="left" valign="top"><p class="table">PointerCell</p></td>
6813 <td align="left" valign="top"><p class="table">integer (vid)</p></td>
6814 <td align="left" valign="top"><p class="table">⁶42</p></td>
6815 <td align="right" valign="top"><p class="table">1</p></td>
6816 </tr>
6817 <tr>
6818 <td align="center" valign="top"><p class="table">⁷</p></td>
6819 <td align="left" valign="top"><p class="table">LvalCell</p></td>
6820 <td align="left" valign="top"><p class="table">sprintf("%d[%lld]", vid, offset)</p></td>
6821 <td align="left" valign="top"><p class="table">⁷42[2]</p></td>
6822 <td align="right" valign="top"><p class="table">1</p></td>
6823 </tr>
6824 <tr>
6825 <td align="center" valign="top"><p class="table">⁸</p></td>
6826 <td align="left" valign="top"><p class="table">FloatCell</p></td>
6827 <td align="left" valign="top"><p class="table">integer ÷ integer</p></td>
6828 <td align="left" valign="top"><p class="table">⁸2÷3</p></td>
6829 <td align="right" valign="top"><p class="table">1</p></td>
6830 </tr>
6831 </tbody>
6832 </table>
6833 </div>
6834 <div class="paragraph"><p>All Cell types except CharCell use one tag per Cell and the Cell value extends
6835 from the first character after the μ-Tag to the character before the next
6836 μ-Tag. Since the number of ASCII characters in the Cell value is somewhat
6837 larger, the overhead for the μ-Tag is relatively small.</p></div>
6838 <div class="paragraph"><p>In contrast, CharCells contain one character and the μ-Tag tags for a string
6839 like "Hello" would require 5 bytes for the ASCII "Hello", plus 10 to 15 bytes
6840 for the 5 μ-Tags (since the Unicodes of the μ-Tags are UTF8-encoded, and most
6841 have 2 or 3 bytes per Unicode).</p></div>
6842 <div class="paragraph"><p>Therefore, and since APL characters more often occur in groups than as
6843 individual characters, the ²&#8230;⁰ values can span entire strings with only
6844 one start μ-Tag ² and one end μ-Tag ⁰. This is similar to APL where
6845 <em>Hello</em> is the same as <em>H</em> <em>e</em> <em>l</em> <em>l</em> <em>o</em>.</p></div>
6846 </div>
6847 <div class="sect3">
6848 <h4 id="_serialization_of_ucs_strings">Serialization of UCS_strings</h4>
6849 <div class="paragraph"><p>UCS_strings do not only appear as groups of characters in the ravels of APL
6850 values (as described above) but also in other contexts (e.g. the APL text
6851 that defines a defined function). For these cases, a common XML tag
6852 <strong>&lt;UCS uni="&#8230;"/&gt;</strong> is used; the value of the tag attribute <strong>uni=</strong> is encoded
6853 with μ-Tags ⁰, ¹, and ² as described above. For example. the APL function:</p></div>
6854 <div class="listingblock">
6855 <div class="content">
6856 <pre>
6857
6858 <code>∇Z←A DIV B
6859 Z←A÷B
6860 ∇</code>
6861
6862 </pre>
6863 </div></div>
6864 <div class="paragraph"><p>will be serialized as:</p></div>
6865 <div class="listingblock">
6866 <div class="content">
6867 <pre>
6868
6869 <code> &lt;Symbol name="DIV" stack-size="1"&gt;
6870 &lt;Function fid="0x55BCF671DC80" creation-time="1687101368729013"
6871 exec-properties="0,0,0,0"&gt;
6872 &lt;UCS uni="²Z←A DIV B⁰¹A
6873 ² Z←A÷B⁰¹A
6874 "/&gt;
6875 &lt;/Function&gt;
6876 &lt;/Symbol&gt;</code>
6877
6878 </pre>
6879 </div></div>
6880 <div class="paragraph"><p>Note the <strong>¹A</strong> above, which is the ASCII line feed character <strong>0x0A</strong> (which
6881 is not allowed in a tag attribute value in XML).</p></div>
6882 </div>
6883 <div class="sect3">
6884 <h4 id="_de_serialization_xml_to_apl">De-Serialization (XML to APL)</h4>
6885 <div class="paragraph"><p>All the complexity above was motivated by the following design goals:</p></div>
6886 <div class="ulist"><ul>
6887 <li>
6888 <p>
6889 <strong>)SAVE</strong>d workspaces should be resonably compact,
6890 </p>
6891 </li>
6892 <li>
6893 <p>
6894 it should be possible to fix a broken workspace with a simple ASCII text
6895 editor such as vi or emacs, with only some basic knowledge of the XML
6896 syntax, and
6897 </p>
6898 </li>
6899 <li>
6900 <p>
6901 the <strong>)LOAD</strong>ing and <strong>)COPY</strong>ing of workspaces (i.e. the decoding of XML
6902 files should be simple and, for portability and maintenance reasons, not
6903 depend on external libraries.
6904 </p>
6905 </li>
6906 </ul></div>
6907 <div class="paragraph"><p>Almost every serialization function, say <strong>save_XXX()</strong>, in class <strong>SavingArchive</strong>
6908 has a corresponding de-serialization function <strong>read_XXX()</strong> in class
6909 <strong>LoadingArchive</strong>:</p></div>
6910 <div class="tableblock">
6911 <table rules="all"
6912 frame="border"
6913 cellspacing="0" cellpadding="4">
6914 <col />
6915 <col />
6916 <col />
6917 <thead>
6918 <tr>
6919 <th align="left" valign="top"> SavingArchive </th>
6920 <th align="left" valign="top"> LoadingArchive </th>
6921 <th align="left" valign="top"> C++ class (de-)serialized</th>
6922 </tr>
6923 </thead>
6924 <tbody>
6925 <tr>
6926 <td align="left" valign="top"><p class="table">save()</p></td>
6927 <td align="left" valign="top"><p class="table">read_Workspace()</p></td>
6928 <td align="left" valign="top"><p class="table">entire Workspace</p></td>
6929 </tr>
6930 <tr>
6931 <td align="left" valign="top"><p class="table">save_Derived()</p></td>
6932 <td align="left" valign="top"><p class="table">read_Derived()</p></td>
6933 <td align="left" valign="top"><p class="table">DerivedFunction</p></td>
6934 </tr>
6935 <tr>
6936 <td align="left" valign="top"><p class="table">save_functions()</p></td>
6937 <td align="left" valign="top"><p class="table">-</p></td>
6938 <td align="left" valign="top"><p class="table">UserFunction (all)</p></td>
6939 </tr>
6940 <tr>
6941 <td align="left" valign="top"><p class="table">save_Function()</p></td>
6942 <td align="left" valign="top"><p class="table">read_Function()</p></td>
6943 <td align="left" valign="top"><p class="table">UserFunction (one)</p></td>
6944 </tr>
6945 <tr>
6946 <td align="left" valign="top"><p class="table">save_Function_name()</p></td>
6947 <td align="left" valign="top"><p class="table">read_Function_name()</p></td>
6948 <td align="left" valign="top"><p class="table">(helper for UserFunction)</p></td>
6949 </tr>
6950 <tr>
6951 <td align="left" valign="top"><p class="table">save_Parser()</p></td>
6952 <td align="left" valign="top"><p class="table">read_Parser()</p></td>
6953 <td align="left" valign="top"><p class="table">Prefix</p></td>
6954 </tr>
6955 <tr>
6956 <td align="left" valign="top"><p class="table">save_Ravel()</p></td>
6957 <td align="left" valign="top"><p class="table">read_Ravel()</p></td>
6958 <td align="left" valign="top"><p class="table">Cell (all in a ravel)</p></td>
6959 </tr>
6960 <tr>
6961 <td align="left" valign="top"><p class="table">save_shape()</p></td>
6962 <td align="left" valign="top"><p class="table">-</p></td>
6963 <td align="left" valign="top"><p class="table">Shape</p></td>
6964 </tr>
6965 <tr>
6966 <td align="left" valign="top"><p class="table">save_SI_entry()</p></td>
6967 <td align="left" valign="top"><p class="table">read_SI_entry()</p></td>
6968 <td align="left" valign="top"><p class="table">StateIndicator</p></td>
6969 </tr>
6970 <tr>
6971 <td align="left" valign="top"><p class="table">save_Symbol()</p></td>
6972 <td align="left" valign="top"><p class="table">read_Symbol()</p></td>
6973 <td align="left" valign="top"><p class="table">Symbol</p></td>
6974 </tr>
6975 <tr>
6976 <td align="left" valign="top"><p class="table">save_symtab()</p></td>
6977 <td align="left" valign="top"><p class="table">read_SymbolTable()</p></td>
6978 <td align="left" valign="top"><p class="table">SymbolTable</p></td>
6979 </tr>
6980 <tr>
6981 <td align="left" valign="top"><p class="table">save_token_loc()</p></td>
6982 <td align="left" valign="top"><p class="table">read_Token()</p></td>
6983 <td align="left" valign="top"><p class="table">Token</p></td>
6984 </tr>
6985 <tr>
6986 <td align="left" valign="top"><p class="table">save_UCS()</p></td>
6987 <td align="left" valign="top"><p class="table">read_UCS()</p></td>
6988 <td align="left" valign="top"><p class="table">UCS_string</p></td>
6989 </tr>
6990 <tr>
6991 <td align="left" valign="top"><p class="table">save_user_commands()</p></td>
6992 <td align="left" valign="top"><p class="table">read_Commands()</p></td>
6993 <td align="left" valign="top"><p class="table">Command::user_command []</p></td>
6994 </tr>
6995 <tr>
6996 <td align="left" valign="top"><p class="table">save_vstack_item()</p></td>
6997 <td align="left" valign="top"><p class="table">-</p></td>
6998 <td align="left" valign="top"><p class="table">(helper for Symbol)</p></td>
6999 </tr>
7000 </tbody>
7001 </table>
7002 </div>
7003 <div class="paragraph"><p>De-serialization (aka, decoding) is inherently more complex than
7004 serialization (aka, encoding). Therefore some more <strong>read_XXX()</strong> functions
7005 have no corresponding <strong>save_XXX()</strong> function.</p></div>
7006 </div>
7007 </div>
7008 </div>
7009 </div>
7010 <div class="sect1">
7011 <h2 id="_parallel_multi_core_execution">Parallel (Multi-Core) Execution</h2>
7012 <div class="sectionbody">
7013 <div class="paragraph"><p>Parallel execution of APL has a longer history.</p></div>
7014 <div class="sect2">
7015 <h3 id="_background">Background</h3>
7016 <div class="paragraph"><p>In the early 1980s the author learned APL (after ALGOL, Assembler, and BASIC,
7017 and before C. C++ was in its early days and not (yet) commercially available.
7018 The platform was an IBM 5110 desktop, a precursor to the IBM PC which came
7019 later. C compilers and APL interpreters were expensive and on the IBM 5110 was
7020 slow, particularly in comparison with Assembler or compiled languages like C
7021 on comparable platforms.</p></div>
7022 <div class="paragraph"><p>Shortly afterwards, the owner of the IBM 5110 replaced it with a faster 16-bit
7023 machine (a Motorola 68000 CPU with an APL 68000 interpreter [today named APL-X]
7024 with multi-user capabilities). APL performance was still an issue.</p></div>
7025 <div class="paragraph"><p>To address the performance issue, the author undertook the construction of a
7026 parallel APL computer. The first draft architecture was based on
7027 <strong>Transputer</strong>s, a brand new CPU technology at that time (which died out
7028 shortly after). The advantage of Transputers was that every CPU had 4 fast
7029 communication channels which could be connected to neighboring Transputers.
7030 The resulting topology is then a torus (a mesh with the leftmost Transputers
7031 connected to the corresponding rightmost Transputers and the top Transputers
7032 connected to the bottom Transputers):</p></div>
7033 <div class="listingblock">
7034 <div class="content">
7035 <pre>
7036
7037 <code> 4×4 Transputer Torus
7038
7039 ┌─────┐ ┌───┐ ┌───┐ ┌───┐
7040 │ ┌───────────────────────────────┐
7041 │ │ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ │
7042 │ └─╢ T ╟───╢ T ╟───╢ T ╟───╢ T ╟─┘
7043 │ ╚═╤═╝ ╚═╤═╝ ╚═╤═╝ ╚═╤═╝
7044 │ ┌─ │ │ │ │ ─┐
7045 │ │ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ │
7046 │ └─╢ T ╟───╢ T ╟───╢ T ╟───╢ T ╟─┘
7047 │ ╚═╤═╝ ╚═╤═╝ ╚═╤═╝ ╚═╤═╝
7048 │ ┌─ │ │ │ │ ─┐
7049 │ │ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ │
7050 │ └─╢ T ╟───╢ T ╟───╢ T ╟───╢ T ╟─┘
7051 │ ╚═╤═╝ ╚═╤═╝ ╚═╤═╝ ╚═╤═╝
7052 │ ┌─ │ │ │ │ ─┐
7053 │ │ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ ╔═╧═╗ │
7054 │ └─╢ T ╟───╢ T ╟───╢ T ╟───╢ T ╟─┘
7055 │ ╚═╤═╝ │ ╚═╤═╝ │ ╚═╤═╝ │ ╚═╤═╝
7056 └─────┘ └───┘ └───┘ └───┘</code>
7057
7058 </pre>
7059 </div></div>
7060 <div class="paragraph"><p>Another architecture that was initially considered was the Intel Hypercube.
7061 However, neither the Transputer torus nor the Intel Hypercube seemed to suit
7062 APL very well. We therefore decided to develop our own architecture named
7063 DATIS-P (derived from the first names of the students that developed the
7064 hardware and software developed for the DATIS-P and probably the last name
7065 of the author. The developers were (in DATIS-P order):
7066 <strong>D</strong>ieter Scheerer <a href="#Sch">[Sch]</a>,
7067 <strong>A</strong>ndreas Gergen <a href="#Ger">[Ger]</a>,
7068 <strong>T</strong>homas Mörsdorf <a href="#Mör">[Mör]</a>, and last but not least
7069 <strong>I</strong>na Gläs <a href="#Glä">[Glä]</a>.
7070 The DATIS-P was prepared for 256 CPUs, but for budgetary reasons our
7071 prototype had only 32 CPUs. The power consumption was about 50 Amperes
7072 at 5 Volts per CPU (which blew the fuses in our lab when we turned it on for
7073 the first time). The size was three 19-inch cabinets (roughly the size of
7074 a mainframe at that time).</p></div>
7075 <div class="paragraph"><p>The most challenging part of the DATIS-P was its communication network. Its
7076 structure was a 3-stage permutation network as invented by <strong>Waksman</strong>. Since
7077 no suitable technology existed at that time we developed an ASIC (a 1-bit wide
7078 8×8 crossbar; the network for 32 CPUs was 8-bit wide and was comprized of
7079 8-bit × 3 stages × 4 width = 96 such ASICs. The 3 stages were pipelined,
7080 a reconfiguration of the network to an arbitrary permutation of the inputs
7081 was done in parallel by all CPUs and took only 3 CPU instructions. The
7082 communication bandwith was 2 CPU instructions per byte (one MOVE.B instruction
7083 to write into the network and one MOVE.B to read the byte (written by some
7084 other CPU) back. The total bandwidth of the network of our 32-CPU machine was
7085 therefore (with a 16 MHz clock) ÷ (3 clocks per MOVE.B) ÷ (2 MOVE.B per
7086 transfer) = 2.6 MBytes/s or 21 MBits/s per CPU = 670 MBits/s for the entire
7087 network (a lot at that time). The bandwidth of the DATIS-P scales linearly
7088 with the number of CPUs, therefore a 256 CPU machine would have had a total
7089 communication bandwidth of 5.3 Gigabits/s.</p></div>
7090 <div class="paragraph"><p>The architecture of the DATIS-P is:</p></div>
7091 <div class="listingblock">
7092 <div class="content">
7093 <pre>
7094
7095 <code> Entire DATIS-P One CPU
7096 ────────────── ───────
7097
7098 ┌──────────────────────────┐ network ┌───┐
7099 │ Permutation Network │ │ ┌─┤CP1│
7100 └──┬────────┬───────────┬──┘ ╔══════╧══════╗ │ └───┘
7101 │ │ │ ║ MC68020 CPU ╟─┤ ┌───┐
7102 ╔══╧══╗ ╔══╧══╗ ╔══╧══╗ ║ 16 MHz ║ ├─┤CP2│
7103 ║ CPU ║ ║ CPU ║ ... ║ CPU ║ ╚══════╤══════╝ │ └───┘
7104 ╚══╤══╝ ╚══╤══╝ ╚══╤══╝ │ │ ...
7105 │ │ │ ┌──────┴──────┐ │ ┌───┐
7106 └────────┴─┬─────────┘ │ static RAM │ └─┤CP8│
7107 │ │ 1 MByte │ └───┘
7108 ╔══╧══╗ └──────┬──────┘
7109 ║ CPU ║ │
7110 ╚═════╝ Master</code>
7111
7112 </pre>
7113 </div></div>
7114 <div class="paragraph"><p>An entire DATIS-P consists of:</p></div>
7115 <div class="ulist"><ul>
7116 <li>
7117 <p>
7118 The permutation network,
7119 </p>
7120 </li>
7121 <li>
7122 <p>
7123 32 worker CPUs, and
7124 </p>
7125 </li>
7126 <li>
7127 <p>
7128 one master CPU. This master CPU does the APL interpretation and delegates
7129 all ravel computations to the worker CPUs. To support scalar extensions
7130 like <strong>1 + 2 3 4 5</strong> &#8230; the master was able to broadcast the scalar 1 into
7131 the dual port memories of the worker with a single instruction.
7132 </p>
7133 </li>
7134 </ul></div>
7135 <div class="paragraph"><p>Every CPU of the DATIS-P:</p></div>
7136 <div class="ulist"><ul>
7137 <li>
7138 <p>
7139 was a Mototola 68020 processor (the cutting edge when the development
7140 of DATIS-P started),
7141 </p>
7142 </li>
7143 <li>
7144 <p>
7145 had up to 8 numerical co-processors MC 68881 to boost the floating point
7146 performance
7147 </p>
7148 </li>
7149 <li>
7150 <p>
7151 A dual-port 1 MByte static memory; one port connected to the local CPU the
7152 other to the Master CPU.
7153 </p>
7154 </li>
7155 </ul></div>
7156 <div class="paragraph"><p>The development of the hardware (PCB designs, ASIC design) and the software
7157 took from 1985 to 1989; the author was managing the hardware development
7158 and finished a Ph.D thesis about the parallel algorithms for the DATIS-P in
7159 2.00 <a href="#Sau">[Sau]</a>.</p></div>
7160 <div class="paragraph"><p>The primary conclusion of the thesis was:</p></div>
7161 <div class="paragraph"><p>───────────────────────────────────────────────────────────────────────────────</p></div>
7162 <div class="paragraph"><p>On the DATIS-P architecture with P processors:</p></div>
7163 <div class="ulist"><ul>
7164 <li>
7165 <p>
7166 All scalar APL functions can be computed in parallel with a speed-up
7167 of <strong>O(P)</strong>. That is, the performance of every scalar function scales linearly.
7168 </p>
7169 </li>
7170 <li>
7171 <p>
7172 Most of the non-scalar APL functions can be computed in parallel with
7173 speed-up <strong>O(P÷log P)</strong>. That is, the other functions scale almost linearly.
7174 </p>
7175 </li>
7176 <li>
7177 <p>
7178 The remaining non-scalar functions are those that require sorting.
7179 Their speed-up is <strong>O(P÷log²P)</strong> (using Batcher&#8217;s Bitonic Sort).
7180 </p>
7181 </li>
7182 <li>
7183 <p>
7184 There is one exception which may happen for particular index vectors <strong>X</strong> in
7185 the indexed assignment <strong>A[X]←B</strong>, in the indexed reference <strong>A←B[X]</strong>, and
7186 functions derived from them, e.g. <strong>X/B</strong>. If the index vector <strong>X</strong> is such
7187 that all ravel items of A are concentrated on the same worker CPU, then
7188 the speed-up can degrade down to <strong>O(1)</strong> because that worker CPU has to do
7189 all of the work while the others are idle. Usually, though, the speed-up
7190 is <strong>O(P÷log²P)</strong> i.e. like the sorting functions ⍋ and ⍒. These days one
7191 can use P-RAM machines that do not suffer from this problem, but in 2.00
7192 P-RAMs were not available.
7193 </p>
7194 </li>
7195 </ul></div>
7196 <div class="paragraph"><p>───────────────────────────────────────────────────────────────────────────────</p></div>
7197 <div class="paragraph"><p>The benchmarks perfomed on the DATIS-P essentially confirmed the predictions
7198 of the Ph.D. thesis. Since we had no APL interpreter at that time, we only
7199 implemented the most important parallel algorithms performed by the workers.
7200 The implementation languages were C on the master and Assembler on the workers.</p></div>
7201 <div class="paragraph"><p>These results were also submitted to the 2.00 APL conference in Copenhagen,
7202 Denmark. Without any feedback. The author therefore pursued a career in
7203 the telecommunications industry.</p></div>
7204 <div class="paragraph"><p>Around 2000, the author came across a PC demo version of IBM APL2 and
7205 played a little with it. However, the license cost for a full version
7206 was entirely out of range for a private user. At about the same time
7207 he worked with Unicode, UTF8-encodings and the like and discovered that
7208 all APL characters were available in Unicode. Probably at that time the
7209 idea of writing my own APL interpreter began to mature.</p></div>
7210 <div class="paragraph"><p>Around 2008, the first version of the interpreter was ready. It was not
7211 yet a GNU project and not very stable either.</p></div>
7212 <div class="paragraph"><p>In 2013 the interpreter was accepted by the GNU project and became GNU APL.
7213 Version 1.0 was sort of stable (i.e. it did not crash immediately), but still
7214 rather buggy (primarily suffering from memory leaks).</p></div>
7215 <div class="paragraph"><p>In 2014, multi-core support (parallel APL) was added. However, only the
7216 APL workhorses (i.e. scalar functions and their outer products) were
7217 parallelized and benchmarked. The results were somewhat disappointing
7218 and suggested that non-scalar functions will not gain much by parallization.
7219 They are memory bound (as opposed to the more compute bound scalar functions)
7220 The main reason seems to be that the interface between the CPU cores and
7221 the memory does not scale with the number of cores so that the cores are
7222 competing for memory access. In contrast, the DATIS-P had only local
7223 memories whose total bandwidth grows linearly with the number of CPUs.</p></div>
7224 </div>
7225 <div class="sect2">
7226 <h3 id="_implementation">Implementation</h3>
7227 <div class="admonitionblock">
7228 <table><tr>
7229 <td class="icon">
7230 <img src="/usr/share/asciidoc/icons/warning.png" alt="Warning" />
7231 </td>
7232 <td class="content">Parallel APL is a purely experimental feature that should not be used
7233 for production code. Expect crashes from time to time.</td>
7234 </tr></table>
7235 </div>
7236 <div class="paragraph"><p>The details are described in a separate document <strong>Parallel-APL.html</strong>, which
7237 is shipped with GNU APL in the same directory as this document.</p></div>
7238 </div>
7239 </div>
7240 </div>
7241 <div class="sect1">
7242 <h2 id="_exercise_1">Exercise 1</h2>
7243 <div class="sectionbody">
7244 <div class="paragraph"><p>We conclude our journey through the GNU APL source code with a little exercise
7245 for the alert reader:</p></div>
7246 <div class="paragraph"><p><em>Add a new monadic system function named <strong>⎕FOO</strong> to GNU APL. <strong>⎕FOO</strong> takes a
7247 1-item integer argument <strong>B</strong> and returns the APL value (string) "FOO-B".</em></p></div>
7248 <div class="paragraph"><p>You may look backwards in this document and consult the GNU APL source code,
7249 but not peek into the solution sketch below, If you stumble over something
7250 that is not properly described above then please report that on
7251 mailto:[email protected].</p></div>
7252 <div class="sect2">
7253 <h3 id="_solution_sketch_with_comments">Solution (sketch with comments)</h3>
7254 <div class="olist arabic"><ol class="arabic">
7255 <li>
7256 <p>
7257 Add a symbol <strong>FOO</strong> to <strong>src/Id.def:</strong>
7258 </p>
7259 <div class="listingblock">
7260 <div class="content">
7261 <pre>
7262
7263 <code>qf( FOO , "⎕FOO" , )</code>
7264
7265 </pre>
7266 </div></div>
7267 <div class="paragraph"><p>The proper position of <strong>FOO</strong> in <strong>Id.def</strong> is important (after the last
7268 ⎕-symbol group that starts with <strong>F</strong>, i.e. after the <strong>⎕FFT</strong> line.</p></div>
7269 </li>
7270 <li>
7271 <p>
7272 Add a header file <strong>src/Quad_FOO.hh</strong> like this:
7273 </p>
7274 <div class="listingblock">
7275 <div class="content">
7276 <pre>
7277
7278 <code>#ifndef __Quad_FOO_HH_DEFINED__
7279 #define __Quad_FOO_HH_DEFINED__
7280
7281 #include "QuadFunction.hh"
7282 ...
7283
7284 class Quad_FOO : public QuadFunction
7285 {
7286 public:
7287 /// Constructor.
7288 Quad_FOO()
7289 : QuadFunction(TOK_Quad_FOO),
7290 system_wisdom_loaded(false)
7291 {}
7292
7293 static Quad_FOO fun; ///&lt; Built-in function.
7294 static Quad_FOO * _fun; ///&lt; Built-in function.
7295
7296 protected:
7297 /// overloaded Function::eval_B()
7298 Token eval_B(Value_P B) const;
7299
7300 ...
7301 };
7302
7303 #endif // __Quad_FOO_HH_DEFINED__</code>
7304
7305 </pre>
7306 </div></div>
7307 <div class="paragraph"><p>Since <strong>⎕FOO</strong> is monadic, we have one handler eval_B(Value_P B), which
7308 overloads <strong>virtual Function::eval_B()</strong> in <strong>Function.hh</strong> and causes our
7309 <strong>Quad_FOO::eval_B</strong> to be called instead of <strong>eval_B()</strong> of the base class. If
7310 <strong>⎕FOO</strong> should support more signatures then it is better to add them at a later
7311 point in time.</p></div>
7312 </li>
7313 <li>
7314 <p>
7315 Add a C++ source file <strong>src/Quad_FOO.cc</strong> like this:
7316 </p>
7317 <div class="listingblock">
7318 <div class="content">
7319 <pre>
7320
7321 <code>#include "Quad_FOO.hh"
7322
7323 Quad_FOO Quad_FOO::fun;
7324 Quad_FOO * Quad_FOO::_fun = &amp;Quad_FOO::fun;
7325
7326 //-----------------------------------------------------------------------------
7327 Token
7328 Quad_FOO::eval_B(Value_P B) const
7329 {
7330 Value_P Z(LOC); // makes Z an APL scalar,
7331 Z-&gt;next_ravel_int(42); // which is the integer 42
7332 Z-&gt;check_value(LOC); // check Z and set its Z-&gt;VF_complete flag
7333 return Token(TOK_APL_VALUE1, Z); // return the result.
7334 }
7335 //-----------------------------------------------------------------------------</code>
7336
7337 </pre>
7338 </div></div>
7339 <div class="paragraph"><p>We have not (yet) implemented the requested functionality in
7340 <strong>Quad_FOO::eval_B()</strong>. It makes life easier to first integrate <strong>⎕FOO</strong> into the
7341 GNU APL framework (and test the integration) then to add the functionality
7342 later on.</p></div>
7343 </li>
7344 <li>
7345 <p>
7346 When using SVN then add the new files to the repository:
7347 </p>
7348 <div class="listingblock">
7349 <div class="content">
7350 <pre>
7351
7352 <code>$ svn add Quad_FOO.cc Quad_FOO.hh
7353 $ svn commit -m "new file"</code>
7354
7355 </pre>
7356 </div></div>
7357 </li>
7358 <li>
7359 <p>
7360 Add the following line to several source files:
7361 </p>
7362 <div class="listingblock">
7363 <div class="content">
7364 <pre>
7365
7366 <code>#include "Quad_FOO.hh"</code>
7367
7368 </pre>
7369 </div></div>
7370 <div class="paragraph"><p>This makes the new declarations available in the <strong>.cc</strong> files that need it.
7371 The files concerned are:</p></div>
7372 <div class="olist loweralpha"><ol class="loweralpha">
7373 <li>
7374 <p>
7375 src/Archive.cc
7376 </p>
7377 </li>
7378 <li>
7379 <p>
7380 src/Id.cc
7381 </p>
7382 </li>
7383 <li>
7384 <p>
7385 src/QuadFunction.cc
7386 </p>
7387 </li>
7388 <li>
7389 <p>
7390 src/Workspace.cc
7391 </p>
7392 </li>
7393 </ol></div>
7394 </li>
7395 <li>
7396 <p>
7397 Update <strong>Makefile.am</strong>.
7398 </p>
7399 <div class="olist loweralpha"><ol class="loweralpha">
7400 <li>
7401 <p>
7402 Add the new files <strong>Quad_FOO.cc</strong> and <strong>Quad_FOO.hh</strong> to the make variable
7403 <strong>common_SOURCES</strong> in <strong>src/Makefile.am</strong>:
7404 </p>
7405 <div class="listingblock">
7406 <div class="content">
7407 <pre>
7408
7409 <code> Quad_FOO.cc Quad_FOO.hh</code>
7410
7411 </pre>
7412 </div></div>
7413 <div class="paragraph"><p>Keep <strong>src/Makefile.am</strong> sorted alphabetically and indented in the same way as
7414 the other files.</p></div>
7415 </li>
7416 <li>
7417 <p>
7418 (re-)run <strong>autoreconf</strong> (in the GNU APL top-level directory)
7419 </p>
7420 <div class="listingblock">
7421 <div class="content">
7422 <pre>
7423
7424 <code> $ autoreconf</code>
7425
7426 </pre>
7427 </div></div>
7428 <div class="paragraph"><p>This creates a new <strong>src/Makefile.in</strong> from <strong>src/Makefile.am</strong>, and</p></div>
7429 </li>
7430 <li>
7431 <p>
7432 (re-)run <strong>./configure</strong> (also in the GNU APL top-level directory).
7433 </p>
7434 <div class="listingblock">
7435 <div class="content">
7436 <pre>
7437
7438 <code> $ ./configure</code>
7439
7440 </pre>
7441 </div></div>
7442 <div class="paragraph"><p>This includes the new files in <strong>src/Makefile</strong> so that they are properly
7443 compiled and linked.</p></div>
7444 </li>
7445 </ol></div>
7446 </li>
7447 <li>
7448 <p>
7449 Add a <strong>TD()</strong> macro for <strong>⎕FOO</strong> in <strong>src/Token.def</strong>:
7450 </p>
7451 <div class="listingblock">
7452 <div class="content">
7453 <pre>
7454
7455 <code>TD(TOK_Quad_FOO , TC_FUN1 , TV_FUN , ID::Quad_FOO )</code>
7456
7457 </pre>
7458 </div></div>
7459 <div class="paragraph"><p>This defines a new Token with token class <strong>TC_FUN1</strong>, value type <strong>TV_FUN</strong> i.e.
7460 <strong>Function</strong> (-pointer), and ID <strong>ID::Quad_FOO</strong>. Note that:</p></div>
7461 <div class="olist loweralpha"><ol class="loweralpha">
7462 <li>
7463 <p>
7464 <strong>TC_FUN1</strong> is an alias for <strong>TC_FUN12</strong> (and so is <strong>TC_FUN2</strong>. These aliases
7465 are used to make explicit that ⎕FOO is monadic. If you should add a dyadic
7466 signature later on then change the token class to <strong>TC_FUN12</strong>.
7467 </p>
7468 </li>
7469 <li>
7470 <p>
7471 Since <strong>src/Token.def</strong> is a <strong>.def</strong> file, it will be <strong>#included</strong> by several
7472 <strong>.cc</strong> files and magically insert the new token in several source files.
7473 </p>
7474 </li>
7475 </ol></div>
7476 </li>
7477 <li>
7478 <p>
7479 Add a <strong>sf_def()</strong> macro to <strong>src/SystemVariable.def</strong>:
7480 </p>
7481 <div class="listingblock">
7482 <div class="content">
7483 <pre>
7484
7485 <code> sf_def(Quad_FOO, "FOO", "Definitely not ⎕BAR" )</code>
7486
7487 </pre>
7488 </div></div>
7489 <div class="paragraph"><p>This takes care of a number of things:</p></div>
7490 <div class="olist loweralpha"><ol class="loweralpha">
7491 <li>
7492 <p>
7493 <strong>)SAVE</strong> of functions in <strong>.xml</strong> workspace snapshots,
7494 </p>
7495 </li>
7496 <li>
7497 <p>
7498 Tab expansion of ⎕-function names,
7499 </p>
7500 </li>
7501 <li>
7502 <p>
7503 update of the <strong>)HELP</strong> command (short help text), and
7504 </p>
7505 </li>
7506 <li>
7507 <p>
7508 Instantiation in workspaces.
7509 </p>
7510 </li>
7511 </ol></div>
7512 </li>
7513 <li>
7514 <p>
7515 And finally: increment <strong>ASX_MINOR</strong> minor in <strong>enum ArchiveSyntax</strong> in file
7516 <strong>src/Archive.hh</strong>. Adding ⎕-functions to GNU is a backward compatible change
7517 of the <strong>.xml</strong> file format. The new interpreter can <strong>)LOAD</strong> files that were
7518 <strong>)SAVE</strong>d with older versions, but not vice versa. Incrementing <strong>ASX_MINOR</strong>
7519 will then issue a warning in older (but not too old) interpreters.
7520 </p>
7521 </li>
7522 <li>
7523 <p>
7524 After these changes you can build and test that the new intepreter compiles
7525 and works (if not then the author has missed something above).
7526 </p>
7527 <div class="listingblock">
7528 <div class="content">
7529 <pre>
7530
7531 <code>$ make
7532 $ src/apl
7533
7534 ⎕FOO 5
7535 42</code>
7536
7537 </pre>
7538 </div></div>
7539 </li>
7540 <li>
7541 <p>
7542 If so, then we can add some more beef into <strong>Quad_FOO::eval_B()</strong>:
7543 </p>
7544 <div class="listingblock">
7545 <div class="content">
7546 <pre>
7547
7548 <code>#include "Quad_FOO.hh"
7549
7550 Quad_FOO Quad_FOO::fun;
7551 Quad_FOO * Quad_FOO::_fun = &amp;Quad_FOO::fun;
7552
7553 //-----------------------------------------------------------------------------
7554 Token
7555 Quad_FOO::eval_B(Value_P B) const
7556 {
7557 // check that B is a 1-item integer
7558 //
7559 if (B-&gt;element_count() != 1) // rank or length error
7560 {
7561 if (B-&gt;get_ranke() != 1) RANK_ERROR;
7562 else LENGTH_ERROR;
7563 }
7564
7565 const Cell &amp; B0 = B-&gt;get_cfirst(); // first item in B
7566 const APL_Integer b = B0.get_int_value(); // DOMAIN_ERROR for non integers
7567 UCS_string z("FOO-"); // z is string "FOO-"
7568 z.append_number(b); // now z is e.g. "FOO-42"
7569
7570 Value_P Z(z); // constructor: APL value from string
7571 return Token(TOK_APL_VALUE1, Z); // return the result.
7572 }
7573 //-----------------------------------------------------------------------------</code>
7574
7575 </pre>
7576 </div></div>
7577 </li>
7578 </ol></div>
7579 </div>
7580 </div>
7581 </div>
7582 <div class="sect1">
7583 <h2 id="_exercise_2">Exercise 2</h2>
7584 <div class="sectionbody">
7585 <div class="paragraph"><p>This exercise is similar to <a href="#Exercise 1">[Exercise 1]</a> above, but this time we want to
7586 create a system variable instead of a system function:</p></div>
7587 <div class="paragraph"><p><em>Add a new system variable named <strong>⎕FOO</strong> to GNU APL so that:</em></p></div>
7588 <div class="ulist"><ul>
7589 <li>
7590 <p>
7591 <em>*⎕FOO←B* with 1-item integer B shall remember B, while</em>
7592 </p>
7593 </li>
7594 <li>
7595 <p>
7596 <em>*Z←⎕FOO* shall be the APL value (string) "FOO-B".</em>
7597 </p>
7598 </li>
7599 </ul></div>
7600 <div class="paragraph"><p>System variables are inherently more complex than system functions because
7601 most system functions are stateless, while system variables typically have
7602 state that needs to be <strong>)SAVE</strong>d in workspaces.</p></div>
7603 <div class="sect2">
7604 <h3 id="_solution_sketch_with_comments_2">Solution (sketch with comments)</h3>
7605 <div class="paragraph"><p>The solution is for the most part, similar to the previous example. We
7606 therefore only discuss the differences.</p></div>
7607 <div class="ulist"><ul>
7608 <li>
7609 <p>
7610 System variables are (directly or indirectly) derived from class
7611 <strong>SystemVariable</strong>, while system functions are derived from class <strong>Function</strong>.
7612 System functions overload one or more (virtual) <strong>eval_XXX()</strong>, while system
7613 variables overload the following (virtual) functions:
7614 </p>
7615 <div class="ulist"><ul>
7616 <li>
7617 <p>
7618 <strong>Value_P get_apl_value() const</strong>, called when the variable is referenced,
7619 </p>
7620 </li>
7621 <li>
7622 <p>
7623 <strong>assign(Value_P B, &#8230;)</strong>, called when a values is assigned to the variable,
7624 </p>
7625 </li>
7626 <li>
7627 <p>
7628 possibly <strong>assign_indexed()</strong>,
7629 called for indexed assignments (if the variable shall support it), and
7630 </p>
7631 </li>
7632 <li>
7633 <p>
7634 possibly <strong>push()</strong> and <strong>pop()</strong> functions if the variable can be localized.
7635 </p>
7636 </li>
7637 <li>
7638 <p>
7639 Therefore our <strong>Quad_FOO.hh</strong> reads:
7640 </p>
7641 </li>
7642 </ul></div>
7643 </li>
7644 </ul></div>
7645 <div class="listingblock">
7646 <div class="content">
7647 <pre>
7648
7649 <code>//----------------------------------------------------------------------------
7650 #ifndef __Quad_FOO_HH_DEFINED__
7651 #define __Quad_FOO_HH_DEFINED__
7652
7653 /**
7654 System variable Quad-FOO.
7655 */
7656 /// The class implementing ⎕FOO
7657 class Quad_FOO : public SystemVariable
7658 {
7659 public:
7660 /// Constructor.
7661 Quad_IO()
7662 : SystemVariable(ID_Quad_IO)
7663 {
7664 Symbol::assign(IntScalar(0, LOC), false, LOC);
7665 }
7666
7667 protected:
7668 /// overloaded Symbol::assign().
7669 virtual void assign(Value_P B, bool clone, const char * loc);
7670
7671 // overloaded Symbol::push()
7672 virtual void push()
7673 {
7674 Symbol::push();
7675 Symbol::assign(IntScalar(0, LOC), false, LOC);
7676 }
7677 };
7678
7679 #endif // __Quad_FOO_HH_DEFINED__</code>
7680
7681 </pre>
7682 </div></div>
7683 <div class="ulist"><ul>
7684 <li>
7685 <p>
7686 and the <strong>TD()</strong> macro now becomes:
7687 </p>
7688 <div class="listingblock">
7689 <div class="content">
7690 <pre>
7691
7692 <code>TD(TOK_Quad_FOO , TC_SYMBOL , TV_SYM , ID_Quad_FOO )</code>
7693
7694 </pre>
7695 </div></div>
7696 </li>
7697 <li>
7698 <p>
7699 The <strong>sf_def()</strong> macro (<strong>sf</strong> stands for <strong>s</strong>ystem <strong>f</strong>unction) above in
7700 <strong>SystemVariable.def</strong> now becomes:
7701 </p>
7702 <div class="listingblock">
7703 <div class="content">
7704 <pre>
7705
7706 <code> rw_sv_def(Quad_FOO, "FOO", "not BAR " )</code>
7707
7708 </pre>
7709 </div></div>
7710 <div class="paragraph"><p>where <strong>rw_sv_def</strong> stands for <strong>r</strong>ead/<strong>w</strong>rite <strong>s</strong>ystem <strong>v</strong>ariable. If
7711 <strong>⎕FOO</strong> were read-only (which implies that localizing it has no effect), then
7712 macro <strong>ro_sv_def</strong> would have been used instead.</p></div>
7713 </li>
7714 </ul></div>
7715 <div class="paragraph"><p>The name <strong>SystemVariable.def</strong> is a little misleading because it contains the
7716 macros for both variables and functions.</p></div>
7717 </div>
7718 </div>
7719 </div>
7720 <div class="sect1">
7721 <h2 id="_references">References</h2>
7722 <div class="sectionbody">
7723 <div class="ulist"><ul>
7724 <li>
7725 <p>
7726 <a id="ISO"></a>[ISO] ISO/IEC Standard 13751.
7727 <em>Programming Language APL, Extended</em>
7728 </p>
7729 <div class="paragraph"><p><strong>www.math.uwaterloo.ca/~ljdickey/apl-rep/docs/is13751.pdf</strong></p></div>
7730 <div class="admonitionblock">
7731 <table><tr>
7732 <td class="icon">
7733 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
7734 </td>
7735 <td class="content">This file is, despite its <strong>.pdf</strong> extension, a <strong>gzip</strong> compressed
7736 file which will only expand into a proper <strong>.pdf</strong> file after uncompressing
7737 it (with <strong>gunzip</strong>).
7738 &#160;<br />
7739 &#160;<br /></td>
7740 </tr></table>
7741 </div>
7742 </li>
7743 <li>
7744 <p>
7745 <a id="IBM"></a>[IBM] IBM Corp.
7746 <em>APL2: Language Reference</em>
7747 </p>
7748 <div class="paragraph"><p><strong>www.ibm.com/downloads/cas/ZOKMYKOY</strong>.</p></div>
7749 <div class="admonitionblock">
7750 <table><tr>
7751 <td class="icon">
7752 <img src="/usr/share/asciidoc/icons/note.png" alt="Note" />
7753 </td>
7754 <td class="content">The exact download location seems to change every now and then.
7755 &#160;<br />
7756 &#160;<br /></td>
7757 </tr></table>
7758 </div>
7759 </li>
7760 <li>
7761 <p>
7762 <a id="Sau"></a>[Sau] Dr. Jürgen Sauermann
7763 <em>Ein Paralleler APL Rechner (A Parallel APL Computer)</em>.
7764 </p>
7765 <div class="paragraph"><p>Ph.D Thesis 2.00, Universität des Saarlandes.
7766 &#160;<br />
7767 &#160;<br /></p></div>
7768 </li>
7769 <li>
7770 <p>
7771 <a id="Sch"></a>[Sch] Dipl. inf. Dieter Scheerer
7772 <em>Entwurf und Realisierung eines Verbindungsnetzwerks als Teil des
7773 Multiprozerssorsystems DATIS-P-256.</em>
7774 </p>
7775 <div class="paragraph"><p>Masters Thesis 1989, Universität des Saarlandes.
7776 &#160;<br />
7777 &#160;<br /></p></div>
7778 </li>
7779 <li>
7780 <p>
7781 <a id="Ger"></a>[Ger] Dipl. inf. Andreas Gergen
7782 <em>Entwurf und Realisierung einer Master-Slave Kommunikation im
7783 Multiprozerssorsystem DATIS-P-256</em>
7784 </p>
7785 <div class="paragraph"><p>Masters Thesis 1989, Universität des Saarlandes.
7786 &#160;<br />
7787 &#160;<br /></p></div>
7788 </li>
7789 <li>
7790 <p>
7791 <a id="Mör"></a>[Mör] Dipl. inf. Thomas Mörsdorf
7792 <em>Entwurf, Bau, und Test einer 32-bit CPU als Teil des
7793 Multiprozerssorsystems DATIS-P-256.</em>
7794 </p>
7795 <div class="paragraph"><p>Masters Thesis 1989, Universität des Saarlandes.
7796 &#160;<br />
7797 &#160;<br /></p></div>
7798 </li>
7799 <li>
7800 <p>
7801 <a id="Glä"></a>[Glä]] Dipl. inf. Ina Gläs
7802 <em>Die Lösung spezieller Probleme bei der Realisierung eines
7803 Permutationsnetzwerkes als Teil des Multiprozerssorsystems DATIS-P-256.</em>
7804 </p>
7805 <div class="paragraph"><p>Masters Thesis 1989, Universität des Saarlandes.
7806 &#160;<br />
7807 &#160;<br /></p></div>
7808 </li>
7809 </ul></div>
7810 </div>
7811 </div>
7812 </div>
7813 <div id="footnotes"><hr /></div>
7814 <div id="footer">
7815 <div id="footer-text">
7816 Last updated
7817 2026-03-15 16:04:04 CET
7818 </div>
7819 </div>
7820 </body>
7821 </html>