• Aucun résultat trouvé

Data Types

Dans le document Language Pascal (Page 36-44)

A type is used in deClaring variables; it determines the set of values whIch those varIables can assume, and the operations that can be performed upon them. A type-declaration associates an identifier wIth a type.

trpe-declaralim ..

I

identifier

"",t .&....-_~-... simple-type t - - -...

painter-type t - - - - " " ' - - . . .

The occurrence of an identifier on the left-hand side of a type-declaration declares 1t as a type-identifier for the blOCK 1n Wh1ch the type-aeClaratlon occurs. The scope of a type-identifier does not include its own deClaration, except for pointer-types (see Sectlons 2.2.3 and 3.3~

To help clarify the syntax description with some semantic hints, the following terms are used to d1stinguish identifiers according to what they denote.

Syntactically, all of them mean simply an identifier:

slmple-type-identifier structured-t ype-identi fier pointer-type-identifier ordinal-type-identlf1er real-type-ldentlfler strlng-type-ldentlfier

In other words, a simple-type-identifier is any identifier that is declared to denote a simple type, a structured-type-identifier is any identifier that is declared to denote a structured type, and so forth. A simple-type-identifier can be the predeclared identifier of a standard type such as integer, boolea'l, etc.

3-1

Pascal Refe.rence Mantlal

3.1 Si"1l1e-Types (Cfij Drdinal-Types)

All the simple-types define ordered sets of values .

.real-type ..

I

real-type-ldentifier

I ...

subrange-type

enumerated-type 1 - - - _ _ . . . 1

The standard real-type-identifier is real.

String-types are discussed in Section 3.1.1.6 below.

Data TYpes

t1n1InaJ-types are a subset of the simple-types, with the following speCial characteristics:

• Within a given ordinal-type, the possible values are an ordered set and each possible value Is associated with an ordinelily, which is an integer value.

The first value of the ordinal-type has ordlnallty 0, the next has ordinal1ty 1, etc. Each possible value except the first has a predecessor based on this ordering, and each possible value except the last has a sllCcessor based on this ordering.

• The standard function oro (see Section 11.5.1) can be applied to any value of ordinal-type, and returns the ordinal1ty of the value.

• The standard function pred (see Section 11.5.4) can be appl1ed to any value of ordinal-type, and returns the predecessor of the value. (For the first value in the Ordinal-type, the result is unspecified.)

• The standard function succ (see Section 11.5.3) can be applied to any value of ordinal-type, and returns the successor of the value. (For the first value in the ordinal-type, the result is unspecified.)

3-2

Pascal Reference M8niJ8l Data 7ypes

3.1.1 Stcmard Sif'T1)le-Types and StrIng-Types

A standard type is denoted by a predefined type-IdentIfier. The simple-types Integer, longlnt, real, char, and boolea1 are standard. The string-types are These are 16-bit, 2's-complement integers.

3.1.1.2 The Longint Type similar to the FCRTRAN single/double precision floating-pOint arithmetic rules:

• All "integer" constants 1n the range of type integer are considered to be of

Pascal Reference HanI./al Data 7)lpes and mUltipllcatlon; for division and multiplication, the penalty is much worse than a factor of 2.

The largest absolute numeric real value Is approximately 3.402823466E38 in Pascal notation.

The smallest absolute numeric non-zero real value is approxImately 1.401298464E -45 in Pascal notation.

The real zero value has a sign, like other numbers. However, the sIgn of a operations using these values.

• NaNs (the word "NaN" stands for "Not a Number"~ These are values of

Pescal ReFerence M8I1U8l Data rypes 3.1.1.6 Strlng-Types

A string value is a sequence of characters that has a dynamic length attri-bute. The length Is the actual number of characters in the sequence at any time during program execution.

A string type has a static size attribute. The size Is the maximum Umlt on the length of any value of this type. The current value of the length attribute Is returned by the standard function length (see Section 11.6); the size attribute of a string type is determined when the strtng type Is defined.

size-attribute

s.='J.=rze::;...--=a:.:..ttI:.=1.='bu=te~~."'1 unSigned-integer t - I - - -...

where the size attribute is an unsigned-integer.

If'-PLEM:NTA TI{)\I I'IlTE

In the current Implementation~ the size-attribute must be In the range from 1 to 255.

The orderIng relationshIp between any two string values is determined by lexical comparIson based on the ordering relationship between character values In corresponding positions In the two strtngs. (When the two strings are of unequal lengths~ each character in the longer string that does not correspond to a character In the shorter one compares "higher"; thus the string 'attribute' is ordered higher than 'at'.)

Do not confuse the sIze with the length.

3-5

Pascal Reference Hanl/al Data TypeS

The size attribute of a string constant is equal to the length of the string constant value .. namely the number of characters actually In the string.

Although string-types are simple-types by definition .. they have some characteristics of structured-types. As explaIned in Section 4.3.1, individual characters In a string can be accessed as if they were components of an array. AlSo, all string-types are implicitly packed types and all restrictions on paCked types apply to strings (see sections 7.3.2, 5.1.6.1, and 11.7).

Do not make any assumptions about the internal storage format of strings .. as this format may not be the same In all implementations.

~rators appliCable to strings are specifIed in Section 5.1.5. Standard

procedures and functions for manIpulating strings are described In Section 11.6.

3.12 Erunerated-Types

M enumerated-type defines an ordered set of values by listing the identifiers that denote these values. The orderIng of these values is determlnea by tne sequence in which the identifiers are listed.

entlmerated-trpe

"C:D---i

identifier-list ~

identifier-jist

~

The occurrence of an identifier within the identifier-Ust of an enumerated-type declares it as a constant for the block in which the

enumerated-type Is declared. The type of this constant Is the enumerated-type beIng declared.

Examples of enumerated-types:

color = (reo, yellOW, green, blue) suit = (clltl, dianDld, heart, spade)

maritalstatus

=

(married, divorced, widowed, sirY;Jle)

Given these declarations, yellow is a constant of type color, dicmond is a constant of type suit, and so forth.

When the ord function (see Section 11.5.1) is appl1ed to a value of an

enumerated-type, it returns an integer representing the orderIng of the value

3-6

Pascal Reference MantJal Data TYpes wlth respect to the other values of the enumerated-type. For example, glven the declarations above,on(red) returns 0, on(yellow) returns 1, and on(blue) returns 3.

3.1.3 Sl&lrange-Types

A suorange-type provides for range-checking of values within some ordinal-type. The syntax for a subrange-type is

slJb.ranqe-type ., constant ~ constant ~

Both constants must be of ordinal-type. Both constants must either be of the same ordinal-type, or one must be of type integer and the other of type long1nt If both are of the same ordinal-type, this type Is called the host-type If one is of type Integer and the other of type Ia"glnt, the host-type is la1Qlnt Note that no range-CheCking is done if the host-type Is longlnt

Examples of slIorange-types:

1..100 -10 ... 10

red .. green

A variable of subrange-type possesses all the properties of variables of the host type, with the restriction that its run-time value must be In the speclf1ed closed interval.

II"'PlEI'w'ENT A TICI'I NOTE

Range-CheckIng is enabled and dlsaDled oy the complIer commandS :IR+

and $R- (see Chapter 12). The default is $R+ (range-checKing enabled).

3.2 StIooturea-Types

A structured-type is characterized by its structuring method and by the type(s) of its components. If the component type is itself structured, the reSUlting structured-type eXhibits more than one level of structuring. There is no specIfied llmlt on the number Of levelS to Which data-types can oe structured.

structured-type-identi fier

3-7

Pascal Reference Manual Data ljpes

The use of the word pcd<ed in the declaration of a structured-type indicates to the compller that data storage should be economized, even If this causes an access to a component of a variable of this type to be less efficient

The word packed only affects the representation of one level of the structured-type In whIch it occurs. If a component is itself structured, the component's representation Is paCked only If the word packed also occurs In the declaration of Its type.

For restrIctIons on the use of components of packed variables, see Sections 7.3.2, 5.1.6.1, and 11.7.

The implementation of paCkIng Is complex, and details of the allocation of storage to components of a packed variable are tl/7specilJet.t

IfVPL.ErvENT A TICN f\IJTE

In the current Implementation, the word packed has no effect on types other than array and record.

3.2.1 Array-Types

All array-type consists of a fixed number of components that are all of one type, called the component-type. The number of elements Is determined by one or more indeX-types, one for each dimension of the array. There is no specified limit on the number of dimensions. In each dimension, the array can be indexed by every poSSible value of the corresponding Index-type, so the number of elements is the prOduct of the cardinallties of all the Index-types.

a08Y-type

index-tyPe ..,,1 ordinal-type ~

The type following the word Of Is the component-type of the array.

IrvPLErvENT A TI(]\I I'IJTE

In the current implementation, the Index-type should not be longlnt or a subrange of longlnt, and arrays should not contaIn more than 32767 oytes.

3-8

Pascal Reference Manl/al

Examples of array-types:

array[1. .100] of real array[bOOlea'l] Of color

Data Types

If the component-type of an array-type is also an array-type, the result can be regarded as a single multi-dimensional array_ The declaration of such an array

is equivalent to the declaration of a multi-dimensional array, as illustrated by the following examples:

array[bOOlea'l] of array[1. .10] of array[slze] of real is equIValent to:

array [bOOlea'l,1 .. 10, size] of real likewIse,

packed array[l. .10] Of packed array[l. .8] Of bOOlea'l Is equIvalent to:

packed array[1. .10, 1. .8] Of boolecll

"Equivalent" means that the compHer does the same thing with the two constructions.

A component of an array can be accessed by referencing the array and applying one or more indexes (see Section 4.3.1~

3.22 Record-Types

A record-type consists of a fixed number of components called fields, possibly of different types. For each component, the record-type declaration specifies the type of the field and an identIfier that denotes It .

.recon:i-type

.( rermti) 1. ~

.@--'-t.I

field-list

t--'

fjeld-list

\.1

fixed-part ,

~;

Dans le document Language Pascal (Page 36-44)

Documents relatifs