• Aucun résultat trouvé

Data Type Overview

Dans le document Domain Pascal Language Reference (Page 56-61)

Data Types

3.1 Data Type Overview

Domain Pascal supports data types that can be sorted into three groups-the simple, struc-tured, and pointer data types. Furthermore, Domain Pascal provides extensions to the standard in each category of data type. In this section, we list all the Domain Pascal data types according to category.

The following list shows the simple data types:

• Integers-Domain Pascal supports the three predeclared integer data types integer, integer16, and integer32.

• Real numbers-Domain Pascal supports the three predeclared real number data types real, single, and double.

• Boolean-Domain Pascal supports the predeclared data type boolean.

• Character-Domain Pascal supports the predeclared char data type.

• Enumerated-Domain Pascal supports enumerated data types.

• Subrange-Domain Pascal supports a subrange of scalar data types. The scalar data types are integer, Boolean, character (char), and enumerated.

Data Types 3-1

You can use the simple data types to build the following structured data types:

• Sets-Domain Pascal permits you to create a set of elements of a scalar data type.

• Records-Domain Pascal supports the record, aligned record, unaligned record, and packed record data types.

• Array-Domain Pascal supports the array and packed array data types. It also supports a predeclared character array type called string, and variable-length ar-ray type declared with the varying keyword.

• Files-Domain Pascal supports the file and text data types.

You can declare any of three kinds of pointer data types:

• Type-specific pointer-points to any previously defined data type.

• Universal pointer-Domain Pascal supports univ_ptr, a predeclared pointer data type that is compatible with any pointer type.

• Procedure and function pointers-Domain Pascal supports a special data type that points to procedures and functions.

The program shown in Figure 3-1 contains sample declarations of the above data types.

This program is available online and is named sample_types.

3-2 Data Types

PROGRAM sample_types;

writers; {good_writers is an enumerated variable.}

SET OF writers; {tw is a set variable.}

hamlets_soliloquy: TEXT; {hamlets_soliloquy is a text file variable.}

periodic_table FILE OF element;

pp

Figure 3-1. Program Declaring All Available Data Types

3.2 Integers

This section explains how to declare variables as integers, how to initialize integer variables, and how to define integer constants. It also explains how Domain Pascal represents integers internally.

Data Types 3-3

3.2.1 Declaring Integer Variables

Domain Pascal supports the following predeclared integer data types:

• Integer-Use it to declare a signed 16-bit integer. A signed 16-bit integer variable can have any value from -32768 to +32767.

• Integer16-Use it to declare a signed 16-bit integer. (Integer and integer16 have identical meanings.)

• Integer32-Use it to declare a signed 32-bit integer. A signed 32-bit integer vari-able can be any value from -2147483648 to +2147483647.

For example, consider the following integer declarations:

VAR

x, y, z : INTEGER;

quarts : INTEGER16;

social_security_number : INTEGER32;

If you want to define unsigned integers, you must use a subrange declaration. (Refer to the

"Subrange" section later in this chapter.)

3.2.2 Initializing Integer Variables-Extension

Domain Pascal permits you to initialize the values of integers within the variable declaration in most cases. You initialize a variable by placing a colon and equal sign (:=) immediately after the data type. For example, the following excerpt initializes X and Y to 0, and Z to 7000000:

VAR

X,Y INTEGER16.- 0;

Z INTEGER32 .- 7000000;

If the variable declaration occurs within a procedure or function, you cannot initialize the variable at the declaration unless it has been declared static. This is because storage within routines is dynamic and so variables in them do not necessarily retain their values between executions.

For example, the following is incorrect:

FUNCTION do_nothing(IN OUT x : INTEGER) : BOOLEAN;

VAR

init_value : INTEGER := 0; {wrong!}

This is the correct way to initialize the variable at its declaration in a routine:

init value: STATIC INTEGER := 0; {Right!}

3-4 Data Types

See the" Accessing a Variable Stored in Another Pascal Module" section of Chapter 7 for more information on the static attribute.

3.2.3 Denning Integer Constants

When you declare an integer constant, Domain Pascal internally represents the value as a 32-bit integer. For example, in the following declarations, Domain Pascal represents both poco and grande as 32-bit integers.

CONST

poco 6;

grande

=

6000000;

You can specify an integer constant anywhere in the range -2147483648 to +2147483647.

It is also possible to compose constant integers as a mathematical expression. (Refer to the

"Const Declaration Part" section in Chapter 2 for details.) The predeclared integer constant maxint has the value +32767.

3.2.4 Internal Representation of Integers

Domain Pascal represents a 16-bit integer (types integer and integer16) as two contiguous bytes, as shown in Figure 3-2. Bit 15 contains the most significant bit (MSB), and bit 0 contains the least significant bit (LSB). If the integer is signed, bit 15 contains the sign bit.

15 (MSB) 0 (LSB)

1---B~-e--O---B-~-e--1---1

Figure 3-2. 16-Bit Integer Format

Domain Pascal represents a 32-bit integer (type integer32) in four contiguous bytes as il-lustrated in Figure 3-3. The most significant bit in the integer is bit 31; the least significant bit is bit O. If the integer is signed, bit 31 contains the sign bit.

31 (MSB) 16

B~e 0 B~e 1

B~e 2 B~e 3

15

o

(LSB)

Figure 3-3. 32-Bit Integer Format

By default, Domain Pascal aligns freestanding 16-bit integers on word boundaries and 32-bit integers on longword boundaries. (See the "Internal Representation of Records"

section of this chapter for details about alignment for integers that are part of records.)

Data Types 3-5

Dans le document Domain Pascal Language Reference (Page 56-61)