Research V6 development
[unix-history] / usr / doc / bc / bca
CommitLineData
1064b2f2
LC
1.bp
2.ft B
3.DS C
4Appendix
5.DE
6.ft
7.NH
8Notation
9.PP
10In the following pages syntactic categories are in \fIitalics\fP;
11literals are in \fBbold\fP; material in brackets [\|] is optional.
12.NH
13Tokens
14.PP
15Tokens consist of keywords, identifiers, constants, operators,
16and separators.
17Token separators may be blanks, tabs or comments.
18Newline characters or semicolons separate statements.
19.NH 2
20Comments
21.PP
22Comments are introduced by the characters /* and terminated by
23*/.
24.NH 2
25Identifiers
26.PP
27There are three kinds of identifiers \- ordinary identifiers, array identifiers
28and function identifiers.
29All three types consist of single lower-case letters.
30Array identifiers are followed by square brackets, possibly
31enclosing an expression describing a subscript.
32Arrays are singly dimensioned and may contain up to 2048
33elements.
34Indexing begins at zero so an array may be indexed from 0 to 2047.
35Subscripts are truncated to integers.
36Function identifiers are followed by parentheses, possibly enclosing arguments.
37The three types of identifiers do not conflict;
38a program can have a variable named \fBx\fP,
39an array named \fBx\fP and a function named \fBx\fP, all of which are separate and
40distinct.
41.NH 2
42Keywords
43.PP
44The following are reserved keywords:
45.ft B
46.ta .5i 1.0i
47.nf
48 ibase if
49 obase break
50 scale define
51 sqrt auto
52 length return
53 while quit
54 for
55.fi
56.ft
57.NH 2
58Constants
59.PP
60Constants consist of arbitrarily long numbers
61with an optional decimal point.
62The hexadecimal digits \fBA\fP\-\fBF\fP are also recognized as digits with
63values 10\-15, respectively.
64.NH 1
65Expressions
66.PP
67The value of an expression is printed unless the main
68operator is an assignment.
69Precedence is the same as the order
70of presentation here, with highest appearing first.
71Left or right associativity, where applicable, is
72discussed with each operator.
73.bp
74.NH 2
75Primitive expressions
76.NH 3
77Named expressions
78.PP
79Named expressions are
80places where values are stored.
81Simply stated,
82named expressions are legal on the left
83side of an assignment.
84The value of a named expression is the value stored in the place named.
85.NH 4
86\fIidentifiers\fR
87.PP
88Simple identifiers are named expressions.
89They have an initial value of zero.
90.NH 4
91\fIarray-name\fP\|[\|\fIexpression\fP\|]
92.PP
93Array elements are named expressions.
94They have an initial value of zero.
95.NH 4
96\fBscale\fR, \fBibase\fR and \fBobase\fR
97.PP
98The internal registers
99\fBscale\fP, \fBibase\fP and \fBobase\fP are all named expressions.
100\fBscale\fP is the number of digits after the decimal point to be
101retained in arithmetic operations.
102\fBscale\fR has an initial value of zero.
103\fBibase\fP and \fBobase\fP are the input and output number
104radix respectively.
105Both \fBibase\fR and \fBobase\fR have initial values of 10.
106.NH 3
107Function calls
108.NH 4
109\fIfunction-name\fB\|(\fR[\fIexpression\fR\|[\fB,\|\fIexpression\|\fR.\|.\|.\|]\|]\fB)
110.PP
111A function call consists of a function name followed by parentheses
112containing a comma-separated list of
113expressions, which are the function arguments.
114A whole array passed as an argument is specified by the
115array name followed by empty square brackets.
116All function arguments are passed by
117value.
118As a result, changes made to the formal parameters have
119no effect on the actual arguments.
120If the function terminates by executing a return
121statement, the value of the function is
122the value of the expression in the parentheses of the return
123statement or is zero if no expression is provided
124or if there is no return statement.
125.NH 4
126sqrt\|(\|\fIexpression\fP\|)
127.PP
128The result is the square root of the expression.
129The result is truncated in the least significant decimal place.
130The scale of the result is
131the scale of the expression or the
132value of
133.ft B
134scale,
135.ft
136whichever is larger.
137.NH 4
138length\|(\|\fIexpression\fP\|)
139.PP
140The result is the total number of significant decimal digits in the expression.
141The scale of the result is zero.
142.NH 4
143scale\|(\|\fIexpression\fP\|)
144.PP
145The result is the scale of the expression.
146The scale of the result is zero.
147.NH 3
148Constants
149.PP
150Constants are primitive expressions.
151.NH 3
152Parentheses
153.PP
154An expression surrounded by parentheses is
155a primitive expression.
156The parentheses are used to alter the
157normal precedence.
158.NH 2
159Unary operators
160.PP
161The unary operators
162bind right to left.
163.NH 3
164\-\|\fIexpression\fP
165.PP
166The result is the negative of the expression.
167.NH 3
168++\|\fInamed-expression\fP
169.PP
170The named expression is
171incremented by one.
172The result is the value of the named expression after
173incrementing.
174.NH 3
175\-\-\|\fInamed-expression\fP
176.PP
177The named expression is
178decremented by one.
179The result is the value of the named expression after
180decrementing.
181.NH 3
182\fInamed-expression\fP\|++
183.PP
184The named expression is
185incremented by one.
186The result is the value of the named expression before
187incrementing.
188.NH 3
189\fInamed-expression\fP\|\-\-
190.PP
191The named expression is
192decremented by one.
193The result is the value of the named expression before
194decrementing.
195.NH 2
196Exponentiation operator
197.PP
198The exponentiation operator binds right to left.
199.NH 3
200\fIexpression\fP ^ \fIexpression\fP
201.PP
202The result is the first
203expression raised to the power of the
204second expression.
205The second expression must be an integer.
206If \fIa\fP
207is the scale of the left expression
208and \fIb\fP is the absolute value
209of the right expression,
210then the scale of the result is:
211.PP
212min\|(\|\fIa\(mub\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP\|)\|)
213.NH 2
214Multiplicative operators
215.PP
216The operators *, /, % bind left to right.
217.NH 3
218\fIexpression\fP * \fIexpression\fP
219.PP
220The result is the product
221of the two expressions.
222If \fIa\fP and \fIb\fP are the
223scales of the two expressions,
224then the scale of the result is:
225.PP
226min\|(\|\fIa+b\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP,\|\fIb\fP\|)\|)
227.NH 3
228\fIexpression\fP / \fIexpression\fP
229.PP
230The result is the quotient of the two expressions.
231The scale of the result is the value of \fBscale\fR.
232.NH 3
233\fIexpression\fP % \fIexpression\fP
234.PP
235The % operator produces the remainder of the division
236of the two expressions.
237More precisely,
238\fIa\fP%\fIb\fP is \fIa\fP\-\fIa\fP/\fIb\fP*\fIb\fP.
239.PP
240The scale of the result is the sum of the scale of
241the divisor and the value of
242.ft B
243scale
244.ft
245.NH 2
246Additive operators
247.PP
248The additive operators bind left to right.
249.NH 3
250\fIexpression\fP + \fIexpression\fP
251.PP
252The result is the sum of the two expressions.
253The scale of the result is
254the maximun of the scales of the expressions.
255.NH 3
256\fIexpression\fP \- \fIexpression\fP
257.PP
258The result is the difference of the two expressions.
259The scale of the result is the
260maximum of the scales of the expressions.
261.NH 2
262assignment operators
263.PP
264The assignment operators bind right to left.
265.NH 3
266\fInamed-expression\fP = \fIexpression\fP
267.PP
268This expression results in assigning the value of the expression
269on the right
270to the named expression on the left.
271.NH 3
272\fInamed-expression\fP =+ \fIexpression\fP
273.NH 3
274\fInamed-expression\fP =\- \fIexpression\fP
275.NH 3
276\fInamed-expression\fP =* \fIexpression\fP
277.NH 3
278\fInamed-expression\fP =/ \fIexpression\fP
279.NH 3
280\fInamed-expression\fP =% \fIexpression\fP
281.NH 3
282\fInamed-expression\fP =^ \fIexpression\fP
283.PP
284The result of the above expressions is equivalent
285to ``named expression = named expression OP expression'',
286where OP is the operator after the = sign.
287.NH 1
288Relations
289.PP
290Unlike all other operators, the relational operators
291are only valid as the object of an \fBif\fP, \fBwhile\fP,
292or inside a \fBfor\fP statement.
293.NH 2
294\fIexpression\fP < \fIexpression\fP
295.NH 2
296\fIexpression\fP > \fIexpression\fP
297.NH 2
298\fIexpression\fP <= \fIexpression\fP
299.NH 2
300\fIexpression\fP >= \fIexpression\fP
301.NH 2
302\fIexpression\fP == \fIexpression\fP
303.NH 2
304\fIexpression\fP != \fIexpression\fP
305.NH 1
306Storage classes
307.PP
308There are only two storage classes in BC, global and automatic
309(local).
310Only identifiers that are to be local to a function need be
311declared with the \fBauto\fP command.
312The arguments to a function
313are local to the function.
314All other identifiers are assumed to be global
315and available to all functions.
316All identifiers, global and local, have initial values
317of zero.
318Identifiers declared as \fBauto\fP are allocated on entry to the function
319and released on returning from the function.
320They therefore do not retain values between function calls.
321\fBauto\fP arrays are specified by the array name followed by empty square brackets.
322.PP
323Automatic variables in BC do not work in exactly the same way
324as in either C or PL/I. On entry to a function, the old values of
325the names that appear as parameters and as automatic
326variables are pushed onto a stack.
327Until return is made from the function, reference to these
328names refers only to the new values.
329.NH 1
330Statements
331.PP
332Statements must be separated by semicolon or newline.
333Except where altered by control statements, execution
334is sequential.
335.NH 2
336Expression statements
337.PP
338When a statement is an expression, unless
339the main operator is an assignment, the value
340of the expression is printed, followed by a newline character.
341.NH 2
342Compound statements
343.PP
344Statements may be grouped together and used when one statement is expected
345by surrounding them with { }.
346.NH 2
347Quoted string statements
348.PP
349"any string"
350.sp .5
351This statement prints the string inside the quotes.
352.NH 2
353If statements
354.sp .5
355\fBif\|(\|\fIrelation\fB\|)\|\fIstatement\fR
356.PP
357The substatement is executed if the relation is true.
358.NH 2
359While statements
360.sp .5
361\fBwhile\|(\|\fIrelation\fB\|)\|\fIstatement\fR
362.PP
363The statement is executed while the relation
364is true.
365The test occurs before each execution of the statement.
366.NH 2
367For statements
368.sp .5
369\fBfor\|(\|\fIexpression\fB; \fIrelation\fB; \fIexpression\fB\|)\|\fIstatement\fR
370.PP
371The for statement is the same as
372.nf
373.ft I
374 first-expression
375 \fBwhile\|(\fPrelation\|\fB) {\fP
376 statement
377 last-expression
378 }
379.ft R
380.fi
381.PP
382All three expressions must be present.
383.NH 2
384Break statements
385.sp .5
386\fBbreak\fP
387.PP
388\fBbreak\fP causes termination of a \fBfor\fP or \fBwhile\fP statement.
389.NH 2
390Auto statements
391.sp .5
392\fBauto \fIidentifier\fR\|[\|\fB,\fIidentifier\fR\|]
393.PP
394The auto statement causes the values of the identifiers to be pushed down.
395The identifiers can be ordinary identifiers or array identifiers.
396Array identifiers are specified by following the array name by empty square
397brackets.
398The auto statement must be the first statement
399in a function definition.
400.NH 2
401Define statements
402.sp .5
403.nf
404\fBdefine(\|\fR[\fIparameter\|\fR[\fB\|,\|\fIparameter\|.\|.\|.\|\fR]\|]\|\fB)\|{\fI
405 statements\|\fB}\fR
406.fi
407.PP
408The define statement defines a function.
409The parameters may
410be ordinary identifiers or array names.
411Array names must be followed by empty square brackets.
412.NH 2
413Return statements
414.sp .5
415\fBreturn\fP
416.sp .5
417\fBreturn(\fI\|expression\|\fB)\fR
418.PP
419The return statement causes termination of a function,
420popping of its auto variables, and
421specifies the result of the function.
422The first form is equivalent to \fBreturn(0)\fR.
423The result of the function is the result of the expression
424in parentheses.
425.NH 2
426Quit
427.PP
428The quit statement stops execution of a BC program and returns
429control to UNIX when it is first encountered.
430Because it is not treated as an executable statement,
431it cannot be used
432in a function definition or in an
433.ft B
434if, for,
435.ft
436or
437.ft B
438while
439.ft
440statement.