• Aucun résultat trouvé

Program Heading

Dans le document Domain Pascal Language Reference (Page 43-50)

2.1. 7 Spreading Source Code Across Multiple Lines

2.2.1 Program Heading

Your program must contain a program heading. The program heading has the following format:

In Domain Pascal, as in standard Pascal, you must supply a name for the program. Name must be an identifier. This identifier has no meaning within the program, but is used by the binder, the librarian, and the loader. (See the Domain/OS Programming Environment Reference manual for details on these utilities for Aegis. For the UNIX utilities Id and ar, refer to the SysV Command Reference and the BSD Command Reference.)

In standard Pascal you can supply an optional file _list to the program heading. The file_list specifies the external files (including standard input and output) that you are going to access from the program. However, unlike standard Pascal, the file_list in a Domain Pascal program has no effect on program execution; the compiler ignores it. (For details

on 110, see Chapter 8.)

Code_section_name and data_section_name are optional elements of the program heading.

Use them to specify the names of the sections in which you want the compiler to store your code and data. A section is a named contiguous area of memory in which all entities share the same attributes. (See the Domain/OS Programming Environment Reference for details on sections and attributes.) By default, Domain Pascal assigns all the code in your program to the . text section and all the data in your program to the . data section. To as-sign your code and data to nondefault sections, specify a code_section_name and a data _section _name.

NOTE: In addition to nondefault code and data section names for the entire program, you can also specify a nondefault section name for a procedure, a function, or a group of variables. See the" Sec-tion" section of Chapter 5 for an explanation of how to assign section names to procedures and functions, and see the" Putting Variables into Sections" section of Chapter 3 to learn how to assign section names to groups of variables.

To specify the default .text section together with an alternate .data section, use the follow-ing syntax:

program name [(file_list)], , data_section_name;

2-10 Blueprint of a Program

Let's now consider some sample program headings. Despite the options available, most Do-main Pascal program headings can look as simple as the following:

Program trapezoids;

Those of you desiring to write standard Pascal programs will also probably want to supply a file _list as in the next example:

Program trapezoids (input, output, datafile);

Finally, those of you wanting to capitalize on certain run-time features may wish to define your own section names. For example, if you want the compiler to store the code into sec-tion mycode and the data into secsec-tion mydata, you would issue the following program heading:

Program trapezoids, mycode, mydata;

2.2.2 Declarations

The declarations part of a program is optional. It can consist of zero or more label decla-ration parts, const decladecla-ration parts, type decladecla-ration parts, var decladecla-ration parts, define declaration parts, and attribute declaration parts. Domain Pascal allows you to specify these parts in any order.

2.2.2.1 Label Declaration Part

You define labels in the label declaration part. A label has only one purpose-to act as a target for a goto statement. (See Chapter 4 for a description of the goto statement.) In other words, the statement

GOTO 40;

works only if you have defined 40 as a label.

The format for a label declaration part is label

labell [, ... labelN

l

A label is either an identifier or an unsigned integer. If there are multiple labels, you must separate them with commas. Remember, though, to put a semicolon after the final label.

For example, the following is a sample label declaration:

LABEL

40, reprompt, finish, 9999;

Blueprint of a Program 2-11

2.2.2.2 Const Declaration Part

You define constants in the const declaration part. A constant is a synonym for a value that will not (and cannot) change during the execution of the program.

The const declaration part takes the following syntax:

const

identifier} = value};

[

identifierN = valueN;]

An identifier is any valid Domain Pascal identifier. A value must be a real, integer, string, char, or set constant expression. Value can also be the pointer expression nil.

For example, here is a sample const declaration part:

CONST

pi = 3.14;

cup

=

8;

key = 'Y';

blank

= ' ';

words

=

'To be or not to be';

v owe 1 s = [ ' a', ' e', ' i', ' 0 ' , ' u '] ; ptr1

=

nil;

{A real number}

{An integer}

{A character}

{A character}

{A string}

{A set}

{A pointer}

The preceding sample involves simple expressions; however, you can also specify a more complex expression for value. Such an expression can contain the following types of terms:

• A real number, an integer, a character, a string, a set, a Boolean, or nil

• A constant that has already been defined in the const declaration part (note that you cannot use a variable here)

• Any predefined Domain Pascal function (for example, chr, sqr, Ishft, sizeof, but only if the argument to the function is a constant)

• A type transfer function

You can optionally separate these terms with any of the operators shown in Table 2-1.

2-12 Blueprint of a Program

Table 2-1. Domain Pascal Mathematical Operators

Operator Data Type of Operand

+, -, * Integer, real, or set

/ Real

mod, div, I, &, - Integer

** Exponentiation

Chapter 4 describes these operators.

For example, the following const declaration part defines eight constants:

CONST x 10;

y 100;

z x + y;

current_year leap_offset bell

pathname pathname_len

1994;

(current_year mod 4);

chr (7) ;

'jjetjgo_home';

sizeof(pathname);

2.2.2.3 Type Declaration Part

Chapter 3 details the many predeclared data types Domain Pascal supports. In addition to these Pascal-defined data types, you can create your own data types in the type declara-tion part. After creating your own data type, you can then declare variables (in the var declaration part) that have these data types. The format for a type part is as follows:

type

identifier 1 = typename 1 :

[

identifierN = typenameN:]

An identifier is any valid Domain Pascal identifier. A typename is any predeclared Domain Pascal data type (like integer or real), any data type that you create, or the identifier of a data type that you created earlier in the type declaration part.

Blueprint of a Program 2-13

For example, here is a sample type declaration part:

type

long = integer32; {A predeclared Domain Pascal data type}

student_name

=

array[l .. 20] of long; {A user-defined data type}

colors = (magenta, beige, mauve); {A user-defined data type}

hue

=

set of colors; {A user-defined data type}

table

=

array [magenta .. mauve] of real; {A user-defined data type}

2.2.2.4 Var Declaration Part

Declare variables in the var declaration part. A variable has two components - a name and a data type. The format for the var declaration part is:

var

identifier _list 1 : typename 1 ;

[

identifier _listN : typenameN;]

An identifier _list consists of one or more identifiers separated by commas. Each identifier in the identifier _list is assigned the data type of typename. Typename must be one of these:

• A predeclared Domain Pascal data type

• A data type you declared in the type declaration part

• An anonymous data type (that is, a data type you define for the variables in this identifier _list only)

2-14 Blueprint of a Program

For example, consider the following type declaration part and var declaration part: {An anonymous enumerated data

type.}

2.2.2.5 Define Declaration Part-Extension

In addition to the const, type, var, and label declaration parts of standard Pascal, Do-main Pascal also supports an optional define declaration part, which is described in the first three sections of Chapter 7.

2.2.2.6 Attribute Declaration Part-Extension

Domain Pascal supports an optional attribute declaration part, which is described in the

"Attribute Declaration Part" section of Chapter 3.

~.2.3

Routines

A program can contain zero or more routines. There are two types of routines in Domain Pascal: procedures and functions. A routine consists of three parts: a routine heading, an optional declaration part, and an action part.

Blueprint of a Program 2-15

2.2.3.1 Routine Heading

Routine headings take the following format:

[ attribute_list] procedure name [(parameter _list) ] ; [routine_options; ] or

[ attribute_list] function name [(Parameter _list)] : typename; [routine_options;]

where:

attribute _list is optional. Inside the attribute _list, you can specify nondefault sec-tion names for the routine's code and data. For a descripsec-tion, see Chapter 5.

name is an identifier. You call the routine by this name.

parameter _list is optional. It is here that you deClare the names and data types of all the parameters that the routine expects from the caller. See Chapter 5 for de-tails on the parameter _list.

typename is the data type of the value that the function returns. The difference between a procedure and a function is that the name of a procedure is simply a name, but the name of a function is itself a variable with its own typename. You must assign a value to this variable at some point within the action part of the function. (It is an error if you don't.) You cannot assign a value to the name of a procedure. (It is an error if you do.)

routine_options is an optional element of the routine heading. You can specify characteristics of the routine such as whether or not it can be called from another routine. Chapter 5 describes the routine_options.

2.2.3.2 Declaration Part of a Routine

The optional declaration part of a routine follows the same rules (with one exception) as the optional declaration part under the program heading. The constants, data types, vari-ables, and labels are local to the routine declaring them and to any routines nested within them. (See the "Global and Local Variables" and "Nested Routines" sections at the end of this chapter for details.)

One difference between the declaration part of a routine and the declaration part of the main program is that the declaration part of a routine cannot contain a define declaration part. Another difference is that you cannot initialize non-static variables in a routine, though you can initialize them in the main program.

2-16 Blueprint of a Program

2.2.3.3 Nested Routines

You can optionally nest one or more routines within a routine. See the "Nested Routines"

section at the end of this chapter for details.

2.2.3.4 Action Part of a Routine

The action part of a routine starts with the keyword begin and finishes with the keyword end. Between begin and end you supply one or more Domain Pascal statements. (See Chapter 4 for a description of Domain Pascal statements.) You must place a semicolon after the final end in a routine.

For example, consider the following sample action part of a routine:

BEGIN

x := x

*

100;

writeln(x);

END;

2.2.4 Action Part of the Main Program

The action part of the main program is almost identical to the action part of a routine.

Both start with begin, both finish with end, and both contain Domain Pascal statements in between. The only difference is that you must place a period (rather than a semicolon) after the final end in the main program. For example, consider the following sample action part of the main program:

BEGIN

x := x

*

100;

writeln(x);

END.

Dans le document Domain Pascal Language Reference (Page 43-50)