BSD 3 development
[unix-history] / usr / src / cmd / apl / documents / apl.how
CommitLineData
d9b46301
KT
1.nr PO .5i
2.de B
3.ft B
4..
5.RP
6.TL
7The UNIX APL Interpreter -- UCSF Version
8.AU
9H. Ross Harvey
10.AI
11UCSF Computer Graphics Laboratory
12.AB
13The UNIX APL interpreter is an incremental `compiler' which
14employs a multi-pass translator to produce an intermediate
15code. This technique is much faster
16than the normal interpretive method and requires less memory.
17The action of the translator is visible
18in only one way: poor run-time error reporting.
19The UCSF APL version features a 50K byte workspace, a simple
20file access procedure, character comparisons and an extended
21set of I-beams. The interpreter itself has been made much more
22reliable and the error messages have been improved.
23.AE
24.ND
25.PP
26UNIX APL is a very well written package containing all the
27APL 360 operators plus execute, scan, and relational character
28operators. UNIX APL does not have a trace feature or a state
29indicator.
30.NH 1
31Major Data Structures
32.NH 2
33Overview
34.PP
35Functions are compiled when they are first referenced;
36this compiled code is then stored in memory. Data always
37resides in memory, although there is a facility for
38reading and write files. The source
39code to the functions is maintained in the disk-saved workspaces,
40in the APL temporary file, and possibly as single user files.
41The generated code usually consists of single-byte values
42which are indices into the array \fBexop\fR.
43This array contains the addresses of functions implementing the
44APL operators. A reference to a variable will be specified by the
45\fBNAME\fR
46operator and the two-byte address of an \fBitem\fR or \fBnlist\fR
47structure. A reference to a constant will be specified by the
48\fBELID\fR or \fBCONST\fR operator and an eight-byte double constant.
49.NH 2
50The Item and Nlist Structures
51.PP
52The item and nlist structures are:
53.DS
54.B
55struct item { struct nlist {
56 char rank; char use;
57 char type; char type;
58 int size; int *itemp;
59 int index; char *namep;
60 data *datap; int label;
61 int dim[0]; };
62};
63.R
64.DE
65The \fBrank\fR element is precisly the APL rank of the variable.
66The \fBtype\fR element may contain one status byte.
67The \fBuse\fR element contains the same information as the type
68element of the item structure. \fBItemp\fR usually points to
69the item structure describing a variable or function whose name
70is addressed by the \fBnamep\fR element.
71If the nlist structure describes a function, then itemp will
72be zero until the function is referenced and compiled; if
73the nlist structure describes some other type of variable, itemp
74will be zero until the variable has been set.
75\fBSize\fR is the total number
76of elements in the vector and index is an index into the
77currently selected member. \fBDatap\fR points to the actual data
78and dim[] (actually dim[rank]) naturally contains the various
79dimensions of the object.
80Since the \fBtype\fR field
81indicates important information such as whether the data
82can be destroyed, it is important to understand the
83meaning of the different types.
84.IP DA 10
85This indicates numeric data \fInot\fR associated with
86some variable. This attribute is given to data which
87exists only on the stack in some way as an intermediate
88value. Objects of type \fBDA\fR may be overwritten if necessary
89and \fBwill\fR be deallocated if they are found on the
90stack after a line is executed.
91.IP LV
92\fBLV\fR indicates an \fBlvalue\fR or assignable quantity. This descriptor
93declares that the element is the actual data of some variable. It
94can not be overwritten or de-allocated. It also specifies that
95the pointed-to structure is not an item structure at all
96but is in fact the nlist structure. In this case, the
97\fBuse\fR member will specify the actual data type
98and the \fBitemp\fR member will point to an \fBitem\fR structure.
99.IP CH
100This indicates character data.
101.IP QD
102This is the \fBquad\fR variable. A reference to a quantity of this
103type will cause the appropriate processing (an expression is read
104and evaluated) and result in an element of type \fBDA\fR being placed
105on the stack.
106.IP QQ
107\fBQQ\fR refers to a quote-quad variable. A line of text is read from the
108terminal and placed (as type \fBCH\fR) on the stack.
109.IP IN
110This is integer data. This type is not fully supported so it
111works only when the next operator is expecting an integer.
112.IP EL
113This data type is used for data entered literally in function
114calls. \fBEL\fR data is deallocated in the UCSF version.
115.IP "NF MF DF"
116These types specify functions which are to be called with
1170, 1 or 2 arguments.
118.NH 2
119The Env Structure
120.PP
121The \fBenv\fR structure contains the index origin, printing
122precision, terminal width, and fuzz factor associated with the
123current workspace. When a saved workspace is loaded, these
124parameters are restored to their state at the time the workspace
125was saved.
126.NH 2
127The Nlist Array
128.PP
129The \fBnlist\fR structure described above is found in an
130array of structures which is also called \fBnlist\fR.
131This array is maintained by ???????.
132.NH 2
133The Labldefs Structure
134.PP
135The \fBlabldefs\fR structure is the start of a linked list
136containing label-name/line-number pairs. This list is allocated
137dynamically and is used only when a function is being compiled.
138.NH 2
139The Idx Structure
140.PP
141The \fBidx\fR structure is used ????????.
142.NH 2
143The Stack
144.PP
145Most operators take their source operands from the top
146of the internal stack. Most operators place their
147results on the top of the stack. In addition, local
148variables require space on the stack at each entrance
149to the function with which they are bound.
150The principle objects
151involved here are:
152.DS
153.I
154struct item **sp, **stack, **staktop;
155.R
156.DE
157The address of a memory block of size \fIsizeof(sp)*STKS\fR is
158initially assigned to \fBstack\fR. The variable \fBsp\fR
159points the the top of the stack.
160A call to the machine-coded function \fBpush(\fIaddress\fB)\fR
161will place \fIaddress\fR on top of the stack, incrementing
162\fBsp\fR appropriatly. \fBStaktop\fR marks the top of the
163current stack; if \fBpush(...)\fR finds that \fBsp\fR
164has passed \fBstaktop\fR, a call to \fBnewstak()\fR will
165allocate a new (contiguous) stack that is \fBSTKS\fR words
166larger. The information on the old stack is copied to the
167new one; \fBstack\fR is assigned the address of the new
168stack, and the memory occupied by the old stack is freed.
169.PP
170The function \fBReset\fR is called when APL returns
171to the top command level or when any error is detected.
172\fBReset\fR frees the current stack and allocates a
173new stack of size \fBSTKS\fR. This is done to ensure
174the integrity of the stack in the face of errors such
175as \fIWS EXCEEDED\fR or \fBINTERRUPT\fR. In addition,
176recursive or deeply nested function calls
177will cause a large amount of memory to be allocated to
178the stack. It is considered desireable to reset the stack
179to a small default value when possible. This prevents
180intentional or accidental recursion from impairing
181the operation of the interpreter by permanently allocating
182a large block of memory.