• Aucun résultat trouvé

HP Pascal distinguishes two classes of variables: static and dynamic.

A static variable is explicitly declared in the declaration part of a block and can then be referred to by name in the body. The compiler allocates storage for this variable on the stack. The system does not deallocate this space until the process closes the scope of the variable.

On the other hand, a dynamic variable is not declared and cannot refer to by name.

Instead, a declared pointer references this variable. The system allocates and deallocates storage for a dynamic variable during program execution as a result of calls to the standard procedures new and dispose. The area of memory reserved for dynamic variables is called the "heap".

HP Pascal also supports the standard procedures mark and release. Mark records the state of the heap. A subsequent call to release returns the heap to the state recorded by mark. Effectively, this disposes any variables allocated since the call to mark.

Dynamic variables permit the creation of temporary buffer areas in memory. Further-more, since a pointer can be a component of a structured dynamic variable, it is possible to write programs with dynamic data structures such as linked lists or trees.

Depending on implementation, mark and release mayor may not perform any action.

Heap Procedures 85

hex

This function converts a hexadecimal string expression or PAC into an integer.

Item string expression or PAC variable

Input leading and trailing blanks in the argument. All other characters must be legal digits in the indicated base.

Since binary, hex, and octal return an integer value, all bits must be specified if a negative result is desired. Alternatively, you can negate the positive representation.

1 This form can be used on systems that support 32-bit 2's-complement notation.

86 hex

Identifiers

An HP Pascal identifier consists of a letter preceding an optional character sequence of letters, digits, or the underscore character (_).

Examples

Identifier Description

GOOD_TIME_9 These identifiers

good_time_9 are

gOOd_Tlme_9 equivalent.

x2_GO

a_long_identifier

boolean Standard identifier.

Semantics

Identifiers denote declared constants, types, variables, procedures, functions, and pro-grams.

A letter can be any of the letters in the sub ranges A .. Z or a .. z. The compiler makes no distinction between upper and lower case in identifiers. A digit can be any of the digits

o

through 9. The underscore (_) is an HP Standard Pascal extension of ANSI Standard Pascal.

An identifier can be up to a source line in length with all characters significant.

Identifiers 87

In general, you must define an identifier before using it. Two exceptions are identifiers that define pointer types and are themselves defined later in the same declaration part, and identifiers that appear as program parameters and are declared subsequently as variables. Also, you need not define an identifier that is a program, procedure, or function name, or one of the identifiers defining an enumerated type. Its initial appearance in a function, procedure, or program header is the "defining occurrence". Finally, HP Pascal has a number of standard identifiers that can be redeclared. These standard identifiers include names of standard procedures and functions, standard file variables, standard types, and procedure or function directives.

Reserved words are system defined symbols whose meaning can never change; that is, you cannot declare an identifier that has the same spelling as a reserved word.

88 Identifiers

IF

An IF statement specifies a statement the system will execute provided that a particular condition is true. If the condition is false, then the system doesn't execute the statement, or, optionally, it executes another statement.

The IF statement consists of the reserved word IF, a boolean factor, the reserved word THEN, a statement, and, optionally, the reserved word ELSE and another statement.

When an IF statement is executed, the boolean factor is evaluated to either true or false, and one of the three actions is performed.

1. If the value is true, the statement following THEN is executed

2. If the value is false and ELSE is specified, the statement following the ELSE is executed.

3. If the value is false and no ELSE is specified, execution continues with the statement following the IF statement.

The statements after THEN or ELSE can be any HP Pascal statements, including other IF statements or compound statements. No semicolon separates the first statement and the reserved word ELSE.

The following IF statements are equivalent:

IF a = b THEN associated with the nearest IF statement.

IF 89

A common use of the IF statement is to select an action from several choices. This often appears in the following form:

IF e1 THEN ELSE IF e2 THEN ELSE IF e3 THEN ELSE

This form is particularly useful to test for conditions involving real numbers or string literals of more than one character, since these types are not legal in CASE statements.

It is possible to direct the compiler to perform partial evaluation of the boolean expressions used in an IF statement. See the compiler directives for your particular implementation.

90 IF

Example Code

PROGRAM show_if (input, output);

VAR

writeln ('Still looking');

IF i

=

j THEN

{Select among different}

{boolean expressions. }

{This IF statement } {cannot be rewritten as}

{a CASE statement }

IF 91

IMPLEMENT

This reserved word indicates the beginning of the internal part of a MODULE. The implement section can be empty or it may contain declarations of the types, imports, constants, variables, procedures, and functions that are only used within the module.

See MODULE.

IMPORT

This reserved word indicates what modules will be needed to compile a program or module.

See MODULE.

92 IMPORT 98615-90053, rev: 10/87

IN

This operator returns true if the specified element is in the specified set.

--..j

element ~ set ~

identifier IN identifier

Item Description Range

element identifier expression of an ordinal type see semantics set identifier expression of type SET see semantics

Example

IF item IN set_of_items THEN process;

Semantics

Both the element being tested and the elements in the setmust be of the same type.

The result is false if the object is not a member of the set.

input

The standard textfiles input and output often appear as program parameters. When they do, there are several important consequences:

1. You must not declare input and output in the source code.

2. The system automatically resets input and rewrites output.

3. The system automatically associates input and output with the implementation-dependent physical files.

4. If certain file operations omit the logical file name parameter, input or output is the default file. For example, the call read(x), where x is some variable, reads a value from input into x. Or consider:

PROGRAM mute (input);

VAR answer : string [255] ; BEGIN

readln(answer);

END.

The program waits for something to be typed. No prompt can be written without adding output to the program heading.

94 input

integer

This type is a subrange whose lower bound is the standard constant minint and whose upper bound is the standard constant maxint.

----c

INTEGER

r

Examples

VAR

wholenum:

i,j,k,l

Semantics

integer;

integer;

Integer is a standard simple ordinal type whose range is implementation defined.

Permissible Operators

Operation Operator

assignment

relational <, <=, =, <>, >, >=, IN, arithmetic +, -, *, /,DIV, MOD Standard Functions

parameter Function

integer argument abs, arctan, chr, cos, exp, In, odd. ord,pred, sin, sqr, sqrt, succ

integer return abs, binary, hex, linepos, lastpos, maxpos, oc-tal, ord, position, pred, round, strlen, strmax, strpos, sqr, trunc

integer 95

LABEL

A label declaration specifies integer labels that mark executable statements in the body of the block. The GOTO statement transfers control to a labeled statement.

Label Declaration

Labelled Statement

Semantics

unlabelled statement

The reserved word LABEL precedes one or more integers separated by commas.

Integers must be in the range 0 to 9999. Leading zeros are not significant. For example, the labels 9 and 00009 are identical.

Label declarations must come first in the declaration part of a block.

You cannot use a label to mark a statement in a procedure or function nested within the procedure, function, or outer block where the label is declared. This means a GOTO statement can jump out of but not into a procedure.

The Label declaration must occur in the declaration part of the block that contains the label.

Example

LABEL 9, 19, 40;

96 LABEL

lastpos

This function returns the integer index of the last component written on a file.

Item Description

file identifier a file type variable

Example

lastpos(file_var) Semantics

Range

file must be opened in the read-write state

The function lastpos(f) returns the integer index of the last component of f that the program can access. An error occurs if f is not opened as a direct access file.

lastpos 97

linepos

This function returns the number of characters read from or written to a textfile since the last end-of-line marker.

Item Description

textfile identifier a textfile

Example

linepos(text_file) Semantics

Range textfile must be opened

The function linepos(f) returns the integer number of characters read from or written to the textfile f since the last end-of-line marker. This does not include the character in the buffer variable

r.

The result is zero after reading a line marker, or immediately after a call to readln or writeln.

The standard files input or output must be specified by name.

98 linepos

In

This function returns the natural logarithm (base e) of the argument.

Item Description Range

argument numeric expression must be greater than 0

Examples

Input Result

In (num_exp)

In(43) 3.761200E+OO

In(2.121) 7.518874E-Ol

In(O) error

Semantics

The function In(x) computes the natural logarithm of x. If x is 0 or less than 0, a run-time error occurs.

In 99

Documents relatifs