mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
348 lines
12 KiB
Plaintext
348 lines
12 KiB
Plaintext
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
|
|
|
|
|
|
## Copyright (C) 1999-2007 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.
|
|
##
|
|
## @(#) $Revision: 30.1 $
|
|
## @(#) $Id: statement,v 30.1 2007/03/16 11:10:42 chongo Exp $
|
|
## @(#) $Source: /usr/local/src/bin/calc/help/RCS/statement,v $
|
|
##
|
|
## 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/
|