Arithmetic Expression Source Parts

When a string literal is to be interpreted as an arithmetic expression, it must be enclosed in parentheses. Without the parentheses, the compiler interprets it as a string literal and generates code or issues syntax errors accordingly. For example the following is an invalid statement and results in a syntax error:

REPLACE POINTER(A) BY "A" FOR 3 DIGITS

On the other hand, the following statement is valid:

REPLACE POINTER(A) BY ("A") FOR 3 DIGITS

<arithmetic expression>

A source part of this form is processed as follows:

  1. The arithmetic expression is evaluated and assigned to a one-word stack-source-operand.

  2. The stack-source-operand is copied once to the destination specified by the stack-destination-pointer.

The character size of the stack-destination-pointer is not used to determine the default value to be assigned to the stack-integer-counter. Since the <unit count> is not specified, the stack-integer-counter is assigned the value 6.

Examples of Arithmetic Expressions

In the following examples, the first column shows a REPLACE statement, and the second column shows, in hexadecimal format, the resulting string.

Statement

Result

REPLACE P BY 7.5

267800000000

REPLACE P BY 3

000000000003

REPLACE P BY 1.68@@2

248540000000

REPLACE P BY ("A")

0000000000C1

REPLACE POINTER(W,4) BY "ABCDEF"

C1C2C3xxxxxx

REPLACE POINTER(W) BY "ABCDEF"

C1C2C3C4C5C6

An x in the result indicates that the hexadecimal character at that position was unchanged by the given statement.

<arithmetic expression> FOR <arithmetic expression>

A source part of this form is processed as follows:

  1. The first arithmetic expression is evaluated and assigned to a one-word stack-source-operand. Unpredictable results can occur if the absolute value of the stack-source-operand exceeds the maximum possible single-precision integer value. An INTEGEROVERFLOW execution-time fault occurs if the absolute value of the stack-source-operand exceeds the maximum possible double integer value. Refer to Compiler Number Conversion for information on the maximum single and double integer values.

  2. If evaluation of the second arithmetic expression yields a positive value, this value is rounded to an integer, if necessary, and assigned to the stack-integer-counter; otherwise, zero is assigned to the stack-integer-counter.

  3. The number of characters specified by the stack-integer-counter are copied from the stack-source-operand to the destination specified by the stack-destination-pointer. If the stack-source-operand contains fewer than the specified number of characters, it is reused as often as necessary. The character size is specified by the stack-destination-pointer. If the destination is specified by a noncharacter array row or array element, the character size is eight bits.

Examples of the <arithmetic expression> FOR <arithmetic expression> Form

The following example copies the character 48"00" to P. The stack-source-operand is 4"000000000003", and the leftmost character of this operand is copied to P.

REPLACE P BY 3 FOR 1

The following example copies the character 48"03" to P:

REPLACE P BY (3).[7:48] FOR 1

The following example copies the EBCDIC character A to P:

REPLACE P BY ("A").[7:48] FOR 1

<arithmetic expression> FOR <arithmetic expression> WORDS

A source part of this form is processed as follows:

  1. If the evaluation of the first arithmetic expression yields a double-precision value, this double-precision value is assigned to a two-word stack-source-operand. Otherwise, the value of the first arithmetic expression is assigned to a one-word stack-source-operand.

  2. If evaluation of the second arithmetic expression yields a positive value, this value is rounded to an integer, if necessary, and assigned to the stack-integer-counter; otherwise, zero is assigned to the stack-integer-counter.

  3. The stack-destination-pointer is moved forward, if necessary, to the nearest word boundary.

  4. The number of words specified by the stack-integer-counter is copied from the stack-source-operand to the destination specified by the stack-destination-pointer. If the stack-source-operand is double-precision, it is copied to the destination one word at a time (first word first, second word second) with TAGs. If the stack-integer-counter specifies more than one word (when the stack-source-operand is single-precision) or more than two words (when the stack-source-operand is double-precision), then the stack-source-operand is reused until the number of words specified by the stack-integer-counter has been copied.

Example of the <arithmetic expression> FOR <arithmetic expression> WORDS Form

The following example copies a single-precision zero into every element of array A.

REPLACE POINTER(A) BY 0 FOR SIZE(A) WORDS

<arithmetic expression> FOR <arithmetic expression> DIGITS

A source part of this form is processed as follows:

  1. The absolute value of the first arithmetic expression is rounded to an integer value, if necessary, and assigned to the stack-source-operand. Unpredictable results might occur if the absolute value of the stack-source-operand exceeds the maximum possible single-precision integer value. An INTEGER OVERFLOW execution-time fault occurs if the absolute value of the stack-source-operand exceeds the maximum possible double integer value.

  2. For information about the maximum integer and double integer values, refer to Compiler Number Conversion.

  3. If evaluation of the second arithmetic expression yields a positive value, this value is rounded to an integer, if necessary, and assigned to the stack-integer-counter; otherwise, zero is assigned to the stack-integer-counter.

  4. A string of hexadecimal characters that represents the decimal value of the stack-source-operand is generated. If the value of stack-source-operand can be expressed in fewer digits than indicated by the stack-integer-counter, the string is filled on the left with zeros.

  5. The N rightmost hexadecimal characters, where N is the number specified by the stack-integer-counter, are copied from this hexadecimal string to the destination. If the character size of the stack-destination-pointer is four bits, the characters are copied without change; if it is six or eight bits, the appropriate zone field is supplied.

If the value of the stack-integer-counter is greater than 24, the program is discontinued with a fault.

Examples of the <arithmetic expression> FOR <arithmetic expression> DIGITS Form

In the following examples, the first column shows the source part, the second column shows the resulting string when the destination is an 8-bit pointer, and the third column shows the resulting string when the destination is a 4-bit pointer.

Source Part

8-Bit Destination

4-Bit Destination

1234 FOR 6 DIGITS

8"001234"

4"001234"

7.5 FOR 3 DIGITS

8"008"

4"008"

–10 FOR 3 DIGITS

8"010"

4"010"

1234 FOR 3 DIGITS

8"234"

4"234"

<arithmetic expression> FOR * DIGITS

This source part functions similarly to a source part of the following form except that the stack-integer-counter is assigned a value equal to the minimum number of characters required to express accurately the value of the stack-source-operand.

<arithmetic expression> FOR <arithmetic expression> DIGITS

Examples of the <arithmetic expression> FOR * DIGITS Form

In the following examples, the first column shows the source part, the second column shows the resulting string when the destination is an 8-bit pointer, and the third column shows the resulting string when the destination is a 4-bit pointer.

Source Part

8-Bit Destination

4-Bit Destination

1234 FOR * DIGITS

8"1234"

4"1234"

7.5 FOR * DIGITS

8"8"

4"8"

–10 FOR * DIGITS

8"10"

4"10"

<arithmetic expression> FOR <arithmetic expression> SDIGITS

This source part functions similarly to a source part of the following form except that the sign of the first arithmetic expression is also recorded.

<arithmetic expression> FOR <arithmetic expression> SDIGITS

If the $TARGET option or the COMPILERTARGET ODT option requests object code generation for unsupported machines, explicitly or by omission, and the first arithmetic expression evaluates to (–0), the generated object code can produce a negative sign and a value of zero (0) in the destination. This does not occur when object code generation tailored for any supported machine is requested. For further information on the object code generation, see the TARGET option in Compiling Programs.

If the character size of the stack-destination-pointer is four bits, then a 4"D" (1"1101") character, indicating a negative value, or a 4"C" (1"1100") character, indicating a positive value, is copied before the first digit. If the character size is eight bits, the zone field of the rightmost digit is changed to 1"1101" for negative values or 1"1100" for positive values.

When the character size of the stack-destination-pointer is four bits, the 4"C" or 4"D" character, indicating the sign of the value, is not counted as a digit.

For example, the statement REPLACE POINTER(A,4) BY -123 FOR 3 SDIGITS yields D123. Four, not three, characters are copied to the destination.

Strings produced by this form of source part can later be converted to an integer value with the correct sign using the INTEGER function. For example, the statement in the above example could be followed by the following statement, after which integer I would contain the value -123:

I := INTEGER(POINTER(A,4),3)

Examples of the <arithmetic expression> FOR <arithmetic expression> SDIGITS

In the following examples, the first column shows the source part, the second column shows the resulting string when the destination is an 8-bit pointer, and the third column shows the resulting string when the destination is a 4-bit pointer.

Source Part

8-Bit Destination

4-Bit Destination

1234 FOR 6 SDIGITS

4"F0F0F1F2F3C4"

4"C001234"

-1234 FOR 6 SDIGITS

4"F0F0F1F2F3D4"

4"D001234"

<arithmetic expression> FOR * SDIGITS

This source part functions similarly to a source part of the following form, except that the stack-integer-counter is assigned a value equal to the minimum number of characters required to express accurately the value of the stack-source-operand:

<arithmetic expression> FOR <arithmetic expression> SDIGITS

Examples of the <arithmetic expression> FOR * SDIGITS Form

In the following examples, the first column shows the source part, the second column shows the resulting string when the destination is an 8-bit pointer, and the third column shows the resulting string when the destination is a 4-bit pointer.

Source Part

8-Bit Destination

4-Bit Destination

1234 FOR * SDIGITS

4"F1F2F3C4"

4"C1234"

-1234 FOR * SDIGITS

4"F1F2F3D4"

4"D1234"

<arithmetic expression> FOR <count part> NUMERIC

A source part of this form is processed as follows:

  1. If the arithmetic expression in the count part yields a positive value, this value is rounded to an integer, if necessary, and assigned to the stack-integer-counter; otherwise, zero is assigned to the stack-integer-counter.

  2. The first arithmetic expression is evaluated, and an internal procedure is called. This procedure generates an EBCDIC character string representing the decimal value of the arithmetic expression as precisely and concisely as possible given the field width specified by the stack-integer-counter.

  3. If the character size of the stack-destination-pointer is eight bits, the string is copied to the destination without translation. If the character size is four bits, the program is discontinued with a fault.

If a residual count does not appear in the count part, the string is copied to the destination, right-justified with blank fill, in a field with a width equal to the value of the stack-integer-counter. If a residual count does appear in the count part, the string is copied to the destination, left-justified, and the simple variable is assigned the difference between the initial value of the stack-integer-counter and the number of characters copied.

The form of the decimal representation is determined by the operand type (single or double-precision), whether or not the operand value is an integer, the magnitude of the operand, the number of significant digits in its decimal representation, and the field width. The basic rule is that the number is represented as compactly as possible using integer, simple decimal, or exponential notation, as appropriate.

For example, the following source parts generate the decimal representations shown:

Source Part

Decimal Representation

12345678 FOR 8 NUMERIC

12345678

12345678 FOR 6 NUMERIC

1.23+7

123/100 FOR N:6 NUMERIC

1.23 (N := 2)

<arithmetic expression> FOR * NUMERIC

This source part functions similarly to a source part of the following form except that no maximum field width is specified:

<arithmetic expression> FOR <count part> NUMERIC

Thus, the internal procedure that generates the string is allowed to use as many as 36 characters to represent the decimal value of the arithmetic expression.

For example, the following source parts generate the decimal representations shown:

Source Part

Decimal Representation

123 FOR * NUMERIC

123

1/3 FOR * NUMERIC

0.3333333333333333333333