HP 3000 Manuals

Constant Definition [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation


HP Pascal/iX Reference Manual

Constant Definition 

A constant definition establishes an identifier as a synonym for a
constant value.  The identifier may then be used in place of the value.
The value of a symbolic constant may not be changed by a subsequent
constant definition in the same scope or by an assignment.

The reserved word CONST precedes one or more constant definitions.  A
constant definition consists of an identifier, the equal sign, (=) and a
constant value.  For more information about CONST, refer to the section
"CONST"  in this chapter.

The reserved word NIL is a pointer value representing a NIL value for all
pointer types.  Predeclared constants include the standard constants
maxint and minint, as well as the standard Boolean constants true and
false.  These constants are discussed in detail in the following pages of
this chapter.

Constant expressions are a restricted class of HP Pascal expressions.
Consequently, operands in constant expressions must be integers, reals,
or ordinal declared constants.  Operators must be +, -, *, /, DIV, or
MOD. Note that all other operators are excluded.  Furthermore, only calls
to the standard functions abs, binary, chr, hex, octal, odd, ord, pred,
strlen, and succ are legal.

One exception to the restrictions on constant expressions is permitted;
the sign of a real or longreal declared constant may be changed using the
negative real unary operator (-).  The positive operator (+) is legal,
but has no effect.

In HP Pascal, constant definitions must follow label declarations and
precede function or procedure declarations.  CONST, TYPE, VAR, MODULE,
and IMPORT sections may be intermixed.

Example 

     CONST
        fingers   = 10;              { Unsigned integer.          }
        pi        = 3.1415;          { Unsigned real.             }
        message   = 'Use a fork!';   { String literal.            }
        nothing   = NIL;
        delicious = true;            { Standard constant.         }
        neg_pi    = -pi;             { Real unary operator.       }
        hands     = fingers DIV 5;   { Constant expression.       }
        numforks  = pred(hands);     { Constant expression with   }
                                     { call to standard function. }

CONST 

This reserved word indicates the beginning of one or more constant 
definitions that introduces an identifier as a synonym for a constant
value.  The identifier may then be used in place of that value.

Constant definitions appear after the program header or any LABEL
declarations, and before any procedure or function definitions.  In HP
Pascal, CONST, TYPE, VAR, MODULE, and IMPORT definitions may be
intermixed.

Syntax 

     Const_decl:

[]
Example PROGRAM show_CONST; LABEL 1; TYPE type1 = integer; type2 = Boolean; str1 = string[5]; CONST const1 = 3.1415; { constant } const2 = true; strconst = str1['abcde']; { string_constructor } VAR var1 : type1; BEGIN END. For examples of structured constants, see the appropriate sections. false This predefined Boolean constant is equal to the Boolean value false. The ordinal value of false is 0. Example PROGRAM show_false(output); VAR what, lie : Boolean; BEGIN IF false THEN writeln('Always false, never printed.'); what := false; lie := NOT true; IF what = lie THEN writeln('Would I lie?'); END. Output: Would I lie? true This predefined Boolean constant is equal to the Boolean value true. The ordinal value of true is 1. Example PROGRAM show_true(output); VAR what, truth : boolean; BEGIN IF true THEN writeln('Always true, always printed.'); what := true; truth := NOT false; IF what = truth THEN writeln('Everything I say is a lie.'); END. Output: Always true, always printed. Everything I say is a lie. maxint This standard constant returns the upper bound of the integer type. The value is implementation defined, however, it must allow for at least nine decimal digits. For more information, see the HP Pascal/iX Programmer's Guide or the HP Pascal/HP-UX Programmer's Guide, depending on your implementation. Example PROGRAM show_maxint(input,output); VAR i,j : integer; r : real; BEGIN readln(i,j); r := i + j; IF r > maxint THEN writeln('Sum too large for integers.'); END. minint This standard constant returns the lower bound of the integer type. The value is implementation defined, however, it must allow at least nine decimal digits. In general, the range of signed integers allows the absolute value of minint to be greater than maxint. For more information, see the HP Pascal/iX Programmer's Guide or the HP Pascal/HP-UX Programmer's Guide, depending on your implementation. Example PROGRAM show_minint(input,output); VAR i,j : integer; r : real; BEGIN readln(i,j); r := i - j; IF r < minint THEN writeln('Difference too large for integers.'); END. NIL This predefined constant is the value of a pointer that designates that the pointer does not point at anything. NIL is compatible with any pointer type. A NIL pointer or pointer that has been assigned to NIL does not point to any variable at all. It is an error to dereference a NIL valued pointer. NIL pointers are useful in linked list applications where the link pointer points to the next element of the list. The last element's pointer can be assigned to NIL to indicate that there are no further elements in the list. Array Constants and Array Constructors An array constant is a declared constant defined with an array constructor that specifies values for the components of an array type. The values for all elements of the structured type must be specified and must have a type identical to the type of the corresponding elements. An array constructor consists of a previously defined array type identifier and a list of values in square brackets. Array constructors are only legal in a CONST section of a declaration part. They cannot appear in other sections or in executable statements. Each component of the array type must receive a value that is assignment compatible with the component type. There is a shorthand allowed for PAC and string constants where a string literal may be used to assign values to multiple components. An array constant may not contain files. An array constant may be used to initialize a variable in the executable part of a block. Individual components of an array constant may also be accessed in the body of a block, but not in the definition of other constants. Within the square brackets, the reserved word OF indicates that a value occurs repeatedly. For example, 3 OF 5 assigns the integer value 5 to three successive array components. The symbols (. and .) may replace the left and right square brackets, respectively. Syntax Array_constructor
[]
Example TYPE Boolean_table = ARRAY [1..5] OF Boolean; table = ARRAY [1..100] OF integer; row = ARRAY [1..5] OF integer; matrix = ARRAY [1..5] OF row; color = (red, yellow, blue); color_string = PACKED ARRAY [1..6] OF char; color_array = ARRAY [color] OF color_string; CONST true_values = Boolean_table [5 OF true]; init_values1 = table [100 OF 0]; init_values2 = table [60 OF 0, 40 OF 1]; identity = matrix [row [1, 0, 0, 0, 0], row [0, 1, 0, 0, 0], row [0, 0, 1, 0, 0], row [0, 0, 0, 1, 0], row [0, 0, 0, 0, 1]]; colors = color_array [color_string ['RED', 3 OF ' '], color_string ['YELLOW'], color_string ['BLUE', 2 OF ' ']]; The name of the previously declared constant may be specified within a structured constant. The previous example can also be written as indicated below. Note that for the special case of PAC that if all of the components are not specified, as in the example below, the remaining components are filled with blanks as assignment compatibility indicates. CONST red = 'RED'; yellow = 'YELLOW'; blue = 'BLUE'; colors = color_array [ color_string[red]; color_string[yellow]; color_string[blue] ]; Record Constructor A record constant is a declared constant defined with a record constructor that specifies values for the fields of a record type. A record constant may be used to initialize a variable in the body of a block. Individual fields of a record constant in the body of a block may be selected, but not when defining other constants. A record constructor consists of a previously declared record type identifier and a list in square brackets of fields and values. All fields of the record type must appear, but not necessarily in the order of their declaration. Values in the construct or must be assignment compatible with the fields. Note that a record constructor is only legal in the CONST section of a declaration part. It cannot appear in other sections or in an executable statement. For records with variants, the constructor must specify the tag field before any variant fields. Then only the variant fields associated with the value of the tag may appear. For records with tagless variants, the initial variant field selects the variant. The values may be constant values or constructors. To use a constructor as a value, the field in the record type must be defined with a type identifier. A record constant may not contain a file. Syntax Record_constructor:
[]
Example TYPE securtype = (light, medium, heavy); counter = RECORD pages: integer; lines: integer; characters: integer; END; report = RECORD revision: char; price: real; info: counter; CASE securtag: securtype OF light: (); medium: (mcode: integer); heavy: (hcode: integer; password: string[10]); END; CONST no_count = counter [pages: 0, characters: 0, lines: 0]; big_report = report [revision: 'B', price: 19.00, info: counter [pages: 19, lines: 25, characters: 900], securtag: heavy, hcode: 999, password: 'unity']; no_report = report [revision : ' '; price : 0.00; info : no_count; securtag : light]; Restricted Set Constructor A set constant is a declared constant defined with a restricted set constructor that specifies set values. A restricted set constructor consists of an optional previously declared set type identifier and a list of constant values in square brackets. Subranges may appear in this list. Restricted set constructors may appear in a CONST section of a declaration part, or in executable statements and can be used to initialize a set variable in the body block. The constant must be an ordinal constant value or an ordinal subrange. A constant expression is legal as a value. The symbols (. and .) may replace the left and right square brackets, respectively. Syntax Restricted_set_constructor:
[]
Example TYPE digits = SET OF 0..9; charset = SET OF char; CONST all_digits = digits [0..9]; { Subrange. } odd_digits = digits [1, 1+2, 5, 7, 9]; letters = charset ['a'..'z', 'A'..'Z']; no_chars = charset [ ]; no_iden = [2, 4, 6, 8] { No set identifier. } String Constructor A string constant is a declared constant defined with a string constructor that specifies values for a string type. The length of the string constant may not exceed the maximum length of the string type used in its definition. The number of characters in the definition determines the current length of the string constant. A string constructor consists of a previously defined string type identifier and a list of values in square brackets. Note that string constructors are only legal in a CONST section of a declaration part. They cannot appear in other sections or in executable statements. Within the square brackets, the reserved word OF indicates that a value occurs repeatedly. For example, 3 OF 'a' assigns the character a to three successive string components. The symbols (. and .) may replace the left and right square brackets, respectively. String literals of more than one character may appear as values. A string constant may be used to initialize a variable in the statement part of a block. Individual components of a string constant in the body of the block may be accessed, but not in the definition of other declared constants. Syntax String_constructor:
[]
Example TYPE s = string[80]; CONST blank = ' '; greeting = s['Hello!']; farewell = s['G',2 OF 'o','d','bye']; blank_string = s[10 OF blank];


MPE/iX 5.0 Documentation