Structured Types [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation
HP Pascal/iX Reference Manual
Structured Types
Structured data types are the array, file, record, set, and string types.
These data types can be preceded by a packing modifier. The effect of
the packing modifier is implementation-defined. Refer to the HP
Pascal/iX Programmer's Guide or the HP Pascal/HP-UX Programmer's Guide,
depending on your implementation, for more information.
Syntax
Structured_type:
Unp_Struc_type:
ARRAY
An array is a structured type consisting of a fixed number of components
that are all of the same type. The maximum number of components is
implementation dependent. Depending on your implementation, refer to the
HP Pascal/iX Programmer's Guide or the HP Pascal/HP-UX Programmer's Guide
for more information.
Syntax
Array_type:
Array Declarations
An array type definition consists of the reserved word ARRAY, an index
type in square brackets, the reserved word OF, and the component type.
The reserved word PACKED may precede ARRAY. It instructs the compiler to
optimize storage space for the array components, possibly at the expense
of execution time.
An index that must be an ordinal type specifies the number of component
of an array. The component type may be any simple, structured, or
pointer type, including a file type. The symbols (. and .) may replace
the left and right square brackets, respectively. The component of an
array may be accessed using the index of the component in a selector.
In the ANSI/IEEE770X3.97 - 1983 Standard Pascal, the term string
designates a packed array of char with a starting index of 1 and an
ending index >1. HP Pascal uses the term PAC to designate a packed array
of char with a starting index of 1. HP Pascal also defines a standard
type string that is similar to a packed array with a declared maximum
length, whose actual length may vary at run time.
Permissible Operators
assignment :=
relational (PAC <, <=, =, <>, >=, >
only)
Standard Functions
strlen**
hex**
octal**
binary**
Standard Procedures
array parameters - pack strread**
prompt** strwrite**
read* unpack
readdir* write*
readln** writedir*
strmove** writeln**
NOTE One asterisk (*) after a routine name indicates that this routine
can be used on all arrays, whereas two asterisks (**) indicates
that this routine should be used with PAC arrays only.
Example
TYPE
name = PACKED ARRAY [1..30] OF char; { PAC type }
list = ARRAY [1..100] OF integer;
strange = ARRAY [Boolean] OF char;
flag = ARRAY [(red, white, blue)] OF 1..50;
files = ARRAY [1..10] OF text;
Multi-Dimensioned Arrays
If an array definition specifies more than one index type or if the
components of an array are themselves arrays, then the array is said to
be multi-dimensioned. The maximum number of array dimensions is
implementation dependent.
Example
TYPE
{ equivalent definitions of truth }
truth = ARRAY [1..20] OF
ARRAY [1..5] OF
ARRAY [1..10] OF Boolean;
truth = ARRAY [1..20] OF
ARRAY [1..5, 1..10] OF Boolean;
truth = ARRAY [1..20, 1..5] OF
ARRAY [1..10] OF Boolean;
truth = ARRAY [1..20, 1..5, 1..10] OF Boolean;
FILE
This reserved word designates a declared data structure that consists of
a sequence of components all of the same type. Files are usually
associated with peripheral storage devices, and their length is not
specified in the program. A file_type consists of the reserved words
FILE OF and a component type that may be predefined or user-defined. The
type text is a special type of FILE OF CHAR that has additional
attributes. For further information about textfiles, refer to the
section "Standard Textfiles" in this chapter.
A logical file is a file variable declared in an HP Pascal program. A
physical file is a file that exists in the environment outside the
program and is controlled by the operating system. During program
execution, logical files are associated with physical files, allowing any
operation performed on the logical file to be performed on the physical
file. Thus, a program is allowed to manipulate data in the external
environment.
A logical file may be any type except a file type or a structured type
with a file type component. The number of components is not fixed by the
file type definition. File components may be accessed sequentially or
directly using a variety of HP Pascal standard procedures and functions.
It is legal to declare a packed file. The effect on the storage of the
file is implementation dependent.
Syntax
File_type:
Example
TYPE
person = RECORD
name: PACKED ARRAY [1..30] OF char;
age: 1..100;
END;
person_file = FILE OF person;
bit_vector = PACKED ARRAY [1..100] OF Boolean;
vector_file = FILE OF bit_vector;
data_file = FILE OF integer;
doc_file = text;
Standard Textfiles
text
Text type variables are called textfiles. The standard file type text
permits ordinary input and output that is oriented to characters and
lines. Text type files have two important features:
* The components are type char.
* The file is subdivided into lines by special end-of-line markers.
Textfiles cannot be opened for direct access with the procedure open.
Textfiles can be sequentially accessed, however, with the procedures
reset, rewrite, or append. All standard procedures that are legal for
sequentially-accessed files are also legal for textfiles.
Certain standard procedures and functions, on the other hand, are only
legal for textfiles. These procedures are:
* eoln
* linepos
* overprint
* page
* prompt
* readln
* writeln
Textfiles permit conversion from the internal form of certain types to an
ASCII character representation and vice versa.
Example
VAR
myfile: text;
i: integer;
r: real;
BEGIN
rewrite(myfile);
writeln(myfile,'integer',i);
writeln(myfile,'real',r);
END.
input
When the standard textfile input appears as a program parameter, there
are several important consequences:
* Input may not be declared in the global declaration of the source
code.
* The system automatically associates input with an
implementation-dependent physical file.
* The system automatically resets input.
* If certain file operations omit the logical file name parameter,
input is the default file. For example, the call read(x) where x
is some variable, reads a value from input into x. Consider:
PROGRAM mute (input);
VAR answer : string[255];
BEGIN
readln(answer);
END.
The program waits for input. Output need not appear.
If an imported module uses input, it must appear as a program parameter
for the importing program, and the module must import the predefined
module stdinput.
output
When the standard textfile output appears as a program parameter, there
are several important consequences:
* Output may not be declared in the global declaration part of the
source code.
* The system automatically associates output with an implementation
dependent, physical file. Depending on your implementation, refer
to the HP Pascal/iX Programmer's Guide or the HP Pascal/HP-UX
Programmer's Guide for more information.
* The system automatically rewrites output.
* If certain file operations omit the logical file name parameter,
output is the default file. For example, the call write(x), where
x is some variable, writes the value of x onto output. Consider:
PROGRAM sample (output);
BEGIN
writeln('I like Pascal!');
END.
The program displays the string literal on the default output device.
output must appear as a program parameter; input need not appear if the
program does not use it.
If an imported module uses output, it must appear as a program parameter
for the importing program, and the module must import the predefined
module stdoutput.
Record
A record is a structured type consisting of a collection of components
that are not necessarily of the same type. Each component is termed a
field of the record and has its own identifier. A field of a record is
accessed by using the appropriate field selector.
A record type consists of the reserved word RECORD, a field list, and the
reserved word END. The reserved word PACKED may precede the reserved word
RECORD. If PACKED is used, it instructs the compiler to optimize storage
of the record fields.
Syntax
Record_type:
Field List
The field list has an optional fixed part and an optional variant part.
The field list may have any number of fields, and each field is given a
unique name called a field identifier.
Syntax
Field_list:
Fixed Part
In the fixed part of the field list, a field definition consists of an
identifier, a colon (:), and a type. Any simple, structured, or pointer
type is legal. Several fields of the same type may be defined by listing
the identifiers separated by commas.
Syntax
Fixed_part:
Variant Part
In the variant part, the reserved word CASE introduces an optional tag
field identifier and a required ordinal type identifier. The reserved
word OF precedes a list of case constants and alternative field lists.
Case constants must be compatible with the tag. See "Type Compatibility"
in this chapter for more information. Several case constants may be
associated with a single field list. The various constants appear
separated by commas. Subranges are also legal case constants in HP
Pascal. The empty field list may be used to indicate that a variant
doesn't exist. This is illustrated in the example in this section. HP
Pascal does not require that all possible tag values be specified.
The OTHERWISE construction may not be used in the variant part of the
field list. OTHERWISE is only legal in CASE statements.
Variant parts allow variables of the same record type to exhibit
structures that differ in the number and type of their component parts.
If a record has multiple variants, when a value is assigned to the tag
field, any fields associated with a previous variant cease to exist, and
the new variant's fields become active with undefined values. If there
is no tag field when a value is assigned to a field of any particular
variant, any fields associated with another variant cease to exist, and
the new variant fields become active with undefined values. An error
results when a reference is made to a field of a variant other than the
current variant. A field of a record is accessed using the appropriate
field selector.
Syntax
Variant_part:
Permissible Operators
assignment (entire :=
record)
field selection .
Standard Procedures
read
readdir
write
writedir
Example
TYPE
word_type = (int, ch);
word = RECORD { variant part only with tag }
CASE word_tag: word_type OF
int: (number: integer);
ch : (chars : PACKED ARRAY [1..2] of char);
END;
polys = (circle, square, rectangle, triangle);
polygon = RECORD { fixed part and tagless variant part }
poly_color: (red, yellow, blue);
CASE polys OF
circle: (radius: integer);
square: (side: integer);
rectangle: (length, width: integer);
triangle: (base, height: integer);
END;
date_info = PACKED RECORD { fixed part only }
mo: (jan, feb, mar, apr, may, jun,
jul, aug, sep, oct, nov, dec);
da: 1..31;
yr: 1900..2001;
END;
marital_status = (married, separated, divorced, single);
name_string = PACKED ARRAY [1..30] of CHAR;
person_info = RECORD { nested variant parts }
name: name_string;
born: date_info;
CASE status: marital_status of
married..divorced:
(when: date_info;
CASE has_kids: Boolean OF
true: (how_many: 1..50);
false: (); { Empty variant }
)
single: ();
END;
Set
A set is a user-defined, structured type that is the power set consisting
of the set of all subsets of a base type. A set type consists of the
reserved words SET OF and a base type. The base type may be any ordinal
type. The maximum number of elements is implementation defined, but must
be at least 256 elements. It is legal to declare a packed set. However,
whether this affects the storage is implementation dependent. HP Pascal
defines "SET OF integer" (or any other integral-type) as "SET OF 0..255".
Syntax
Set_type:
Permissible Operators
assignment :=
union +
intersection *
difference -
subset <=
superset >=
equality =, <>
inclusion IN
Example
TYPE
charset = SET OF char;
fruit = (apple, banana, cherry, peach, pear, pineapple);
somefruit = SET OF apple..cherry;
poets = SET OF (Blake, Frost, Brecht);
some_set = SET OF 1..200;
PACKED
This reserved word indicates that the compiler should minimize data
storage even if the access time may be increased. The reserved word
PACKED may appear with an ARRAY, RECORD, SET, or FILE. By declaring a
PACKED structured data type, the amount of memory needed to store an item
is generally reduced. The decision to pack a particular data type
depends on many factors including available memory size, processor speed,
required response time, and volume of data. Therefore, a choice that is
valid for one environment may be quite inappropriate for another. It is
illegal to pass a component of a packed structure by reference.
Syntax
Packing:
Example
CONST
wordsize = 20;
VAR
buffer: ARRAY [1..wordsize] OF char;
word: PACKED ARRAY [1..wordsize] OF char;
String
In HP Pascal a string type consists of the standard identifier string and
an integer constant expression in square brackets that specifies the
maximum length. Integer constant expressions are constant expressions
that return an integer value, an unsigned integer being the simple case.
The limit for the maximum length is implementation defined, but must be
at least 255. The symbols (. and .) may replace the left and right
brackets, respectively.
Characters enclosed in single quotes are string literals. The compiler
interprets a string literal as type PAC, string, or char, depending on
the context.
When a formal reference parameter is type string, the maximum length need
not be specified. This allows actual string parameters to have various
maximum lengths.
A single component of a string can be accessed by using an integer
expression in square brackets as a selector. The numbering of the
characters in the string begins at one. Strings are initialized by
performing an operation that sets the current length, making an
assignment to the entire string or by calling setstrlen.
Syntax
String_type:
A string expression may consist of any of the following:
* A string literal.
* A string variable.
* A string constant.
* A function result that is a string.
* An expression formed with the concatenation operator.
NOTE Variables of type string, as well as other Pascal variables, are
not initialized. The current string length contains meaningless
information until the string is initialized.
Permissible Operators
assignment :=
concatenation +
relational =, <>, <=, >=, >, <
Standard Functions
string argument - str strpos
strlen strrpt
strltrim strrtrim
strmax
string return - str
strltrim
strrpt
strrtrim
Standard Procedures
string parameter prompt strinsert
-
read strmove
readdir strread
readln strwrite
setstrlen write
strappend writedir
strdelete writeln
Example
CONST
maxlength = 100;
TYPE
name = string[30];
remark = string[maxlength * 2];
PROCEDURE proc1 (VAR s: string); EXTERNAL; { Maximum length }
{ not required. }
MPE/iX 5.0 Documentation