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