File: statement

package info (click to toggle)
calc 2.15.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,848 kB
  • sloc: ansic: 62,147; makefile: 7,664; sh: 503; awk: 74; sed: 7
file content (348 lines) | stat: -rw-r--r-- 13,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
Statements

    Statements are very much like C statements.  Most statements act
    identically to those in C, but there are minor differences and
    some additions.  The following is a list of the statement types,
    with explanation of the non-C statements.

    Statements are generally terminated with semicolons or { ... }.

    C-like statements
    -----------------
    { statement }
    { statement; ... statement }


    C-like flow control
    -------------------
    if (expr) statement
    if (expr) statement else statement
    for (optionalexpr ; optionalexpr ; optionalexpr) statement
    while (expr) statement
    do statement while (expr)

            These all work like in normal C.

            IMPORTANT NOTE: When statement is of the form { ... },
            the leading { must be on the same line as the if, for,
            while or do keyword.

            This works as expected:

                if (expr) {
                    ...
                }

            However this WILL NOT WORK AS EXPECTED:

                if (expr)
                {
                    ...
                }

            because calc will parse the if being terminated by
            an empty statement followed by a

                if (expr) ;
                {
                    ...
                }

            In the same way, use these forms:

                for (optionalexpr ; optionalexpr ; optionalexpr) {
                        ...
                }

                while (expr) {
                        ...
                }

                do {
                        ...
                while (expr);

            where the initial { is on the SAME LINE as the if, while,
            for or do.

            See 'help expression' for details on expressions.
            See 'help builtin' for details on calc builtin functions.
            See 'help unexpanded' for things C programmers do not expect.
            See also 'help todo' and 'help bugs'.


    C-like flow breaks
    ------------------
    continue
    break
    goto label
            These all work like in normal C.

            See 'help expression' for details on expressions.
            See 'help builtin' for details on calc builtin functions.


    return
    ------
    return
    return expr
    return ( expr )
            This returns a value from a function.  Functions always
            have a return value, even if this statement is not used.
            If no return statement is executed, or if no expression
            is specified in the return statement, then the return
            value from the function is the null type.


    switch
    ------
    switch (expr) { caseclauses }
            Switch statements work similarly to C, except for the
            following.  A switch can be done on any type of value,
            and the case statements can be of any type of values.
            The case statements can also be expressions calculated
            at runtime.  The calculator compares the switch value
            with each case statement in the order specified, and
            selects the first case which matches.  The default case
            is the exception, and only matches once all other cases
            have been tested.


    matrix
    ------
    mat variable [dimension] [dimension] ...
    mat variable [dimension, dimension, ...]
    mat variable [] = { value, ... }
            This creates a matrix variable with the specified dimensions.
            Matrices can have from 1 to 4 dimensions.  When specifying
            multiple dimensions, you can use either the standard C syntax,
            or else you can use commas for separating the dimensions.
            For example, the following two statements are equivalent,
            and so will create the same two dimensional matrix:

                    mat foo[3][6];
                    mat foo[3,6];

            By default, each dimension is indexed starting at zero,
            as in normal C, and contains the specified number of
            elements.  However, this can be changed if a colon is
            used to separate two values.  If this is done, then the
            two values become the lower and upper bounds for indexing.
            This is convenient, for example, to create matrices whose
            first row and column begin at 1.  Examples of matrix
            definitions are:

                    mat x[3]    one dimension, bounds are 0-2
                    mat foo[4][5]       two dimensions, bounds are 0-3 and 0-4
                    mat a[-7:7] one dimension, bounds are (-7)-7
                    mat s[1:9,1:9]      two dimensions, bounds are 1-9 and 1-9

            Note that the MAT statement is not a declaration, but is
            executed at runtime.  Within a function, the specified
            variable must already be defined, and is just converted to
            a matrix of the specified size, and all elements are set
            to the value of zero.  For convenience, at the top level
            command level, the MAT command automatically defines a
            global variable of the specified name if necessary.

            Since the MAT statement is executed, the bounds on the
            matrix can be full expressions, and so matrices can be
            dynamically allocated.  For example:

                    size = 20;
                    mat data[size*2];

            allocates a matrix which can be indexed from 0 to 39.

            Initial values for the elements of a matrix can be specified
            by following the bounds information with an equals sign and
            then a list of values enclosed in a pair of braces.  Even if
            the matrix has more than one dimension, the elements must be
            specified as a linear list.  If too few values are specified,
            the remaining values are set to zero.  If too many values are
            specified, a runtime error will result.  Examples of some
            initializations are:

                    mat table1[5] = {77, 44, 22};
                    mat table2[2,2] = {1, 2, 3, 4};

            When an initialization is done, the bounds of the matrix
            can optionally be left out of the square brackets, and the
            correct bounds (zero based) will be set.  This can only be
            done for one-dimensional matrices.  An example of this is:

                    mat fred[] = {99, 98, 97};

            The MAT statement can also be used in declarations to set
            variables as being matrices from the beginning.  For example:

                    local mat temp[5];
                    static mat strtable[] = {"hi", "there", "folks");


    object
    ------
    obj type { elementnames } optionalvariables
    obj type variable
            These create a new object type, or create one or more
            variables of the specified type.  For this calculator,
            an object is just a structure which is implicitly acted
            on by user defined routines.  The user defined routines
            implement common operations for the object, such as plus
            and minus, multiply and divide, comparison and printing.
            The calculator will automatically call these routines in
            order to perform many operations.

            To create an object type, the data elements used in
            implementing the object are specified within a pair
            of braces, separated with commas.  For example, to
            define an object will will represent points in 3-space,
            whose elements are the three coordinate values, the
            following could be used:

                    obj point {x, y, z};

            This defines an object type called point, whose elements
            have the names x, y, and z.  The elements are accessed
            similarly to structure element accesses, by using a period.
            For example, given a variable 'v' which is a point object,
            the three coordinates of the point can be referenced by:

                    v.x
                    v.y
                    v.z

            A particular object type can only be defined once, and
            is global throughout all functions.  However, different
            object types can be used at the same time.

            In order to create variables of an object type, they
            can either be named after the right brace of the object
            creation statement, or else can be defined later with
            another obj statement.  To create two points using the
            second (and most common) method, the following is used:

                    obj point p1, p2;

            This statement is executed, and is not a declaration.
            Thus within a function, the variables p1 and p2 must have
            been previously defined, and are just changed to be the
            new object type.  For convenience, at the top level command
            level, object variables are automatically defined as being
            global when necessary.

            Initial values for an object can be specified by following
            the variable name by an equals sign and a list of values
            enclosed in a pair of braces.  For example:

                    obj point pt = {5, 6};

            The OBJ statement can also be used in declarations to set
            variables as being objects from the beginning.  If multiple
            variables are specified, then each one is defined as the
            specified object type.  Examples of declarations are:

                    local obj point temp1;
                    static obj point temp2 = {4, 3};
                    global obj point p1, p2, p3;


    print expressions
    -----------------
    print expr
    print expr, ... expr
    print expr: ... expr
            For interactive expression evaluation, the values of all
            typed-in expressions are automatically displayed to the
            user.  However, within a function or loop, the printing of
            results must be done explicitly.  This can be done using
            the 'printf' or 'fprintf' functions, as in standard C, or
            else by using the built-in 'print' statement.  The advantage
            of the print statement is that a format string is not needed.
            Instead, the given values are simply printed with zero or one
            spaces between each value.

            Print accepts a list of expressions, separated either by
            commas or colons.  Each expression is evaluated in order
            and printed, with no other output, except for the following
            special cases.  The comma which separates expressions prints
            a single space, and a newline is printed after the last
            expression unless the statement ends with a colon.  As
            examples:

                    print 3, 4;                 prints "3 4" and newline.
                    print 5:;                   prints "5" with no newline.
                    print 'a' : 'b' , 'c';      prints "ab c" and newline.
                    print;                      prints a newline.

            For numeric values, the format of the number depends on the
            current "mode" configuration parameter.  The initial mode
            is to print real numbers, but it can be changed to other
            modes such as exponential, decimal fractions, or hex.

            If a matrix or list is printed, then the elements contained
            within the matrix or list will also be printed, up to the
            maximum number specified by the "maxprint" configuration
            parameter.  If an element is also a matrix or a list, then
            their values are not recursively printed.  Objects are printed
            using their user-defined routine.  Printing a file value
            prints the name of the file that was opened.


    Also see the help topic:

            help command        top level commands
            help expression     calc expression syntax
            help builtin        calc builtin functions
            help usage          how to invoke the calc command and calc -options

    You may obtain help on individual builtin functions.  For example:

            help asinh
            help round

    See:
            help builtin

    for a list of builtin functions.

    Some calc operators have their own help pages:

            help ->
            help *
            help .
            help %
            help //
            help #

    See also:

            help help

    The man command is an alias for the help command.  For example:

            man jacobi

    Only calc help files may be displayed by the help and man commands.

## Copyright (C) 1999-2007,2017,2021  Landon Curt Noll
##
## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License
## as published by the Free Software Foundation.
##
## Calc is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
## Public License for more details.
##
## A copy of version 2.1 of the GNU Lesser General Public License is
## distributed with calc under the filename COPYING-LGPL.  You should have
## received a copy with calc; if not, write to Free Software Foundation, Inc.
## 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##
## Under source code control:   1991/07/21 04:37:23
## File existed as early as:    1991
##
## chongo <was here> /\oo/\     http://www.isthe.com/chongo/
## Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/