Pascal MT+ Language Manual Feb83
Pascal MT+ Language Manual Feb83
Language
Reference Manual
Copyright © 1983
Digital Research
P.O. Box 579
801 Lighthouse Avenue
Pacific Grove, CA 93950
(408) 649-3896
TWX 910 360 5001
DISCLAIMER
TRADEMARKS
***********************************
* First Edition: February 1983 *
***********************************
Table of Contents
1 Pasca1/MT+ Programs
1.1 Program Stiucture 1-1
v
Table- of Contents
( continued)
5 Statements
5.1 The Assignment Statement 5-1
5.2 The CASE Statement •• 5-2
5.3 The Empty Statement
·..···· ··· 5-3
5.4 The FOR Statement
· ··· 5-3
5.5 The GOTO Statement
· 5-5
IF Statement .
5.6 The
···· 5-6
5.7 The REPEAT Statement
· · ··· 5-7
5.8 The WHILE Statement
···· · 5-8
WITH Statement . . .
5.9 The
· ···· ·· 5-8
vi
Table of Contents
(continued)
vii
Table of Contents
(continued)
viii
Foreword
This manual assumes you are already familiar with the Pascal
language in general. If you are not familiar with Pascal, refer to
Appendix C for a bibliography of textbookf~.
This manual uses Backus-Naur Form (BNF) notation to formally
describe the syntax of Pascal statements. If you are not familiar
with BNF notation, see Appendix B.
iii
Appendixes
o Bibliography 0-1
ix
Figures,Tables and Listings
Figures
1-1 Block Structure in Pascal/MT+ 1-1
7-1 Lines in a TEXT File • • •• 7-9
7-2 Records in a File . • • • • • • • . • • • 7-14
Tables
3-1 Predefined Data Types • 3-2
4-1 Summary of Pascal/MT+ Operators • . • • • 4-1
4-2 Boolean Operations • • • • • • · . • • 4-4
4-3 Logical Ope~ators • . • • • • • • • • 4-5
Listings
1-1 Simple Pascal/MT+ Program • • • · 1-2
1-2 Declarations and Definitions • • • . · 1-4
'1:-3 Example of Scope Rules • 1-6
1-4 Example Program with Comments 1-7
3-1 Program Using Sets 3-10
4-1 Set Expressions . • • • 4-7
6-1 FORWARD Declarations • • • • • • . • . 6-3
6-2a Parameter Passing • •• • • • . • 6-4
6-2b Output from VALVAR Program • 6-4
6-3 Procedural Parameters • • • · . • • 6-5
6-4 Conformant Array Example · .6-7
7-1 File Input ~nd Output • • . . . . . • . . • 7-4
7-2 Red irec ted I/O • • • • . . . • • • 7-8
7-3 TEXT File Processing 7-11
7-4 Writing to a Printer and Number Formatting 7-12
7-5 Random File I/O • . . • • • _. • • • • . • • 7-15
x
Section 1
Pascal/MT+ Programs
PROGRAM
VAR _
PROCEDURE _
BEGIN .
[
END
PROCEDURE _ .
VAR_
BEGIN
[
END
BEGIN
BEG'"
[
B~~
[
END:
END:
END.
1-1
Pascal/MT+ Reference Manual 1.1 Program Structure
CONST
LIMIT = lO~
MESSAGE = 'TESTING PASCAL/MT+'~
VAR
NAME : STRING~
VAR
I INTEGER~
BEGIN
FOR I :~ 1 TO LIMIT DO
BEGIN
WRITELN (ST) ~
ST : = CONCAT (' I, ST) (* SHIFTS NAME TO RIGHT *)
END
END~
BEGIN
WRITELN (MESSAGE)~
WRITELN (' WHAT IS YOUR NAME? I ) ;
READLN (NAME)~
RESPOND ( NAME) ;
WRITELN' ('FINISHED I , MESSAGE)
END •.
1) LABEL declarations
2) CONSTANT declarations
3) TYPE definitions
4) VAR declarations
5) PROCEDURE and FUNCTION definitions
1-3
Pascal/MT+ Reference Manual 1.1 Program Structure
LABEL
34, 356, 755, 1000;
CONST
TOP = lOa:
BOTTOM = -TOP;
LIMIT = 1.OE-16:
MESSAGE = 'THANK YOU FOR NOT SMOKING":
TYPE
COLOR = (RED, YELLOW, BLUE, GREEN, ORANGE) ;
INDEX = BOTTOM •• TOP;
PERPt = "PERSON;
PERSON RECORD
NAME,
ADDRESS STRING;
PHONE S~RING [8]
END;
VAR
COLR COLOR:
I, J INTEGER;
LIST ARRAY [ INDEX] OF PERPT;
1 •. 1.4 Modules
1-4
Pascal/MT+ Reference Manual 1.1 Program Structure
MODULE SIMPLE~
Notice that the word MODULE replaces the word PROGRAM and that the
word MODEND replaces the main statement body.
Refer to the Pascal/MT+ Language Programmer's Guide for your
implementation for more information about modules and modular
programs.
1.2 Scope
Every identifier in a Pascal/MT+ program has a scope. The
scope of an identifier is the set of all blocks where you can make a
valid reference to the identif ier. The normal scope of an
identifier is anywhere inside its defining block, starting from its
actual definition.
However, when a nested block redefines. the same identifier, the
outer variable is inaccessible from the inner block. When the same
identifier has multiple definitions, the innermost definition is the
one that applies.
This manual uses the terms global and local. The declarations
at the outermost level in the program are the global declarations.
Declarations in a block are local to that block. A variable is
local to a block if its declaration is in that same block. Inside a
nested block, a variable declared in a containing block 'is usable,
but it is not local to that nested .block. Wi thin a contained block,
a reference to a variable in a containing block is called an "up-
level reference".
Listing 1-3 Shows a program containing nested blocks with
multiple definitions for the same identifiers. The comments in the
program
. explain which definitions apply at various points.
.
1-5
Pascal/MT+ Reference Manual 1.2 Scope
PROGRAM SHOWSCOPE;
VAR
X, Y, Z : INTEGER; (* X,Y,Z ARE GLOBAL *)
PROCEDURE PROC1;
VAR
BEGIN
X := Y / Z (* Y & Z FROM MAIN BLOCK *)
END;
PROCEDURE PROC2;
VAR
W : INTEGER; (* W LOCAL TO PROC2 *)
Y : STRING; (* Y LOCAL TO· PROC2 *)
BEGIN
y := 'ABCDEFG' ;
W := X; (* X FROM MAIN BLOCK *)
Z := X DIV 3 (* X FROM MAIN BLOCK *)
END;
BEGIN
Y : = 35; (* X, Y, & Z ARE ALL INTEGERS *)
Z : = 12; (* IN THIS BLOCK *)
PROC1; (* CHANGES X *)
PROC2; (* CHANGES Z *)
WRITELN (X, Y, Z)
END.
1.3 Comaents
You can put a comment anywhere in a program that you can put a
blank space; the compiler ignores comments. There are two ways to
write a comment in a Pascal/MT+ program:
• Surround the comment with the characters { and } .
• Surround the couent with the character pairs. (* and *).
The compiler differentiates between the two sets of comment
delim.iters, so you can nest comments •. You can use one set of
delimiters for regular comments in your program, and use the other
set of delimiters to comment out sections of code for debugging or
development, as shown in the following program fragment.
1-6
Pascal/MT+'Reference Manual 1.3 Comments
End of Section 1
1-7
Section 2
Identifiers and Constants
2.1 Identifiers
A Pascal/MT+ identifier can represent a variable,· a type, a
constant, a procedure or function, or an entire program. The same
rules apply to all Pascal/MT+ identifiers, regardless of what kind
of objects they represent.
A Pascal/MT+ identifier can be any length, as long as it fits
on one line. However, the compiler uses only the first eight
characters to distinguish one identifier from another. Only the
first seven characters are significant in external identifiers.
Identifiers can contain,any combination of letters, digits, and
underscores. They must begin with a letter, and they cannot contail'l
any blank spaces. The compiler ignores underscores and typecase.
For example,
is the same as
abc
You can also use an @ as the first character in an identifier#
as long as you do not use the @ compiler option. You cannot use the
@ inside an identifier. The .compiler allows the @character, so you
can access the run-time routines whos~ name begin~ with @.
However, if you use the @ compiler option, then the compiler
interprets the @character as the standard pointer character, A, and
does not allow the @ as part of an identifier.
The followin9 are examples of valid Pascal/MT+ identifiers:
X
@CPMRD
file name
LA225prefix
Thisfile
Thisfile_for~91803_zip-only
2-1
Pascal/MT+ Reference Manual 2.1 Identifiers
2.2 Constants
You can express a constant as a literal value, or you can give
the constant a name and then use the name anywhere you need that
value. Pascal/MT+constants can be str ings, integers, real numbers,
or scalar types.
2-2
Pascal/MT+ Reference Manual 2.2 Constants
2-3
Pascal/MT+ Reference Manual 2.2 Constants
End of Section 2
~-4
Section 3
Variables and Data Types
TYPE
NUMBERS = ARRAY [1 •• 10} OF INTEGER~
STRPT ='" STR ING ;
LETTER I AI •• I ZI ;
VAR
X, Y, Z INTEGER;
NAMES LIST;
NUMl Oeo200;
NUM2 0.e200;
Notice in the example above how you can group more than one
name w.ith a particular type definition, and that you can use an
explicit type definition instead of just a type name.
If the compiler is using strong type checking, you must
declare variables with the same type name if you want the variables
to be compatible. Strong type checking requires that compatible
3-1
Pascal/MT+ Reference Manual 3.2 Var iable Dec la ration
var iables have exactly the same type, not just the same internal
structure. In the above example, NUMl and NUM2 are not compatible
under strong type checking. To make them compatible, you could use
the declaration,
NUMl,NUM2 : O•• 200;
See the programmer's guide for more information about how the
compiler performs type checking.
3-2
Pascal/MT+ Reference Manual 3.3 Simple Types
3.3.1 BOOLEAN
The BOOLEAN type has two values: TRUE and FALSE. The ordinal
value of FALSE is 0, and the ordinal value of TRUE is 1.
A BOOLEAN variable uses one byte, even in a packed structure
(see Section 3.4). Within the byte, only the least-significant bit
matters in determining the value. If the bit is set, the value of
the variable is TRUE, if not, the value is FALSE. However, logical
operations use the whole byte.
3.3.2 CHAR
Variables of type CHAR use one byte. The internal
representation of a character is the ASCII value of the character.
The range for CHAR variables is CH~(O) to CHR(255).
To express a CHAR value in a program, enclose the character in
single apostrophes if it is a print'able character, or use the CHR
pseudo-function. Use two single apostrophes to represent the single
apostrophe character.
The following example program demonstrates the CHR and ORO
pseudo-functions.
PROGRAM CHR_ORO;
VAR
I, J INTEGER;
C, 0 CHARi
BELL CHAR;
BEGIN
I : = 7;
C := '8';
o := CHR(I + ORD('O'»; (* ASCII VALUE OF '0' IS 48*)
J : = ORO (C) - oRo ( , 0 ' );
BELL := CHR(7)
END.
3-3
Pascal/MT+ Reference Manual 3.3 Simple Types
3.3.4 REAL
3-4
Pascal/MT+ Reference Manual 3.3 Simple Types
3-5
Pascal/MT+ Reference Manual 3.3 Simple Types
3.3.7 Pointers
A po inter is a var iable whose value is the address of a
dynamically allocated variable of some specific type. To define a
pointer type, use the pointer character, A, followed by a type name,
as in the following examples.
TYPE
INTPT "INTEGER~
LINK "TREE NODE;
NAMEPTR "STRING;
You can assign the value NIL to any type pointer to represent a
null pointer.
To reference the object whose address a pointer contains,
follow the pointer's name with the" character, as in the following
examples.
NEWREC := NEXT";
NAME" := 'ALPHA FIVE';
EMPLOYEE".AGE := 32;
If the compiler is using strong type checking, two pointers
must be of the .same type to be compatible. When the compiler is
using weak type checking, all pointer types are compatible, allowing
you to treat the same ·object as more than one data type.
3-6
Pascal/MT+ Reference Manual 3.4 Structured Types
3.4.1 Arrays
An array is a collection of a fixed number of elements of the
same type. Arrays can have any type element, including other
structured types. An array type definition has the general format:
ARRAY [<index type> {,<index type>}] OF <element type>
The <index type> can be arq subrange type except LONGINT. You
can either use the name for a subrange type, or specify the bounds
explicitly_ For the <element type>, you can either use a type name,
or define the type right in the array definition. The following are
examples of array type definitions.
TYPE
LIST = ARRAY [FIRST •• LAST] OF STRING;
GRIDl = ARRAY [1 •• 20] OF ARRAY [1 •• 20] OF INTEGER;
GRID2 = ARRAY [1 •• 20, 1 •• 20] OF INTEGER;
TABLE PACKED ARRAY. [INDEX] OF PERPT;
Note that the definitions for GRIDl and GRID2 are functionally
identical.
You can use the reserved word PACKED in an array definition of
the form:
PACKED ARRAY [1 •• n] OF CHAR:
In this context, the word PACKED causes the compiler to treat
the array as a static string_
When accessing an array, the array's name by itself represents
the entire array: the name followed by an index references an
individual element in the array, as in the following.example.
3-7
Pascal/MT+ Reference Manual 3.4 Structured Types
PROCEDURE WORTHLESS:
CONST
FIRST =
LAST
TYPE
LIST = ARRAY [1 •• 20] OF STRING;
VAR
I : INTEGER;
NAMESA :'--LIST;
NAMESB : LIST:
BEGIN
FOR I := FIRST TO LAST DO
NAMEsA [ I] : = ' ';
NAMESB : = NAMESA
END;
3.4.2 Strings
The predefined type STRING is like a packed array of characters
in which byte 0 contains the dynamic length of the string and bytes
I through n contain the characters. When you declare a string, the
compiler allocates a predetermined number "of bytes for the string.
The default'length is 80, but you can specify from I to 255 bytes.
The dynamic length is the length of the string actually in use, not
the total available space. To specify the maximum length of a
string, put the length in square brackets, as in the following
example: .
VAR
TITLE· STRING [16]
LINE STRING;
LONGLINE STRING [255] ;
You can assign a string of any length to a string variable.
You can also assign a CHAR value to a str ing. The length byte o'f
the string variable reflects the new dynamic length, and the extra
bytes are undefined. However, if the assigned str ing is longer than
the maximum length of the string variable, errors can occur.
Assigning individual characters to a string does not change the
declared ).ength.
To access individual characters in a string, you index the
string like an array.
The predefined function LENGTH returns the dynamic length of a
string. Section 6 descr'ibes several other predefined string
rou.tines.
Pascal/MT+ Reference Manual 3.4 Structured Types
3.4.3 Sets
A set is a structured type that contains elements of the same
base type. Unlike arrays or records, in which each element has a
value, the elemen ts of a se t are only sign ifican t in their presence
or absence from the set. Each element in a set has a corresponding
bit. If an' element is in a set, its bit is set, if the element is
not in the set,' its bit is O. .
Set opera tions are the standa rd rna thema tical opera t:Lons lik e
union, intersection, and difference. Section 4 describeG the set
opefators and expressions.
A set type definition has the general form:
SET OF <base type>
In Pascal/MT+, the <base type> can be any ord inal type. The
ordinal value of the upper and lower bounds of the base tyt:e must be
in the range a to 255. A se t-type var iable always takes up 32
bytes.
Listing 3-1 is an example program that uses sets e
3-9
Pascal/MT+ Reference Manual 3.4 Structured Types
PROGRAM USE_SETS;
VAR
LOWER, UPPER : SET OF CHAR;
DIGIT, DELIMIT : SET OF CHAR;
I, NUMLETS, NUMDIGS : INTEGER;
LINE STRING;
BEGIN
LOWER : = [' a ' • • ' z ' ] i
UPPER : = [' A' •• ' Z ' ] ;
DIGIT : = [' 0 ' •• ' 9' ] ; , , , , 'i', ':', '1',
DEL IMIT : = [" , •' , '?',];
NUMLETS := OJ
NUMDIGS :=-0;
READLN (LINE) j
FOR I := 1 TO LENGTH(LINE) DO
IF LINE[I] IN (LOWER + UPPER) THEN
BEGIN
NUMLETS := NUMLETS +.1;
IF LINE[I] IN LOWER THEN (* MAKE UPPERCASE *)
LINE[I] := CHR(ORD(LINE[I]) - 32)
END
ELSE
IF LINE[I] IN DIGIT THEN
NUMDIGS := NUMDIGS +1 .
ELSE
IF LINE[I] IN PELIMIT THEN
LINE [ I ] : = '*'
END.
3.4.4 Records
A record is a collection of distinct elements called fields,
each of which" can be of any type. Records are useful for describing
logically related data items that are of different types.
Pascal/MT+ records can either be variant, or nonvariant. Any
two nonvariant records of a particular type always have the same
internal structure whereas variant records can vary in internal
structure.
The type definition for a nonvariant record has the general
form:
RECORD
<field list> <field type> {i
<field list> <field type> }
END 1
3-10
Pascal/MT+ Reference Manual 3.4 Structured Types
field has a variant part, it must be the last field in the list. To
indicate that a variant has no fields, use an empty parentheses
pair.
The <case selector> is either a <tag field> or simply a type
name. In either case, the type must be some previously defined
simple (scalar") type. The case labels are constants of the type of
the selector. If there are more than one, separate them with
commas.
If the <case selector> is a <tag field>, it has the form:
<field name> : <type name>
and is one of the regular fields in the record. The field list, or
variant with the correct case label, is selected depending on the
value of the <tag field>.
The following example shows a variant record definition:
RECORD
NAME RECORD
FIRST STRING [15] ;
MID CHAR;
LAST ". STRING [15]
END;
AGE, BIRTH : INTEGER;
SEX : CHAR;
CASE EMPLOYED : BOOLEAN OF (* START OF VARIANT PART *)
FALSE ();
TRUE (SALARY: REAL;
CASE EMP BY : EMP TYPE OF
SELF :-(YEARS :-INT~GER);
GOV, BUSI : {TITLE: STRING"[12];
NUMYRS : INTEGER
END;
Both the main variant and the nested variant in the preceding
example have a field that controls which variant applies. It is
also possible to use a type name "to control the variant, as in the
following example. This kind of variant is called a free variant.
RECORD
CASE INTEGER OF
1 (A, B, C, 0 CHAR) ;
2 (X, Y INTEGER);
3 : (Z LONGINT)
END;
3-12
Pascal/MT+ Reference Manual 3.4 Str uc tured Types
End of Section 3
3-13
Pascal/MT+ Reference Manual 4 Operators and Precedence
4-2
Section 4
Operators and Expressions
4-1
Pascal/MT+ Reference Manual 4 Operators and Precedence
= equality scalar,string
set, pointer
record boolean lowest
<> inequality scalar,string
set, pointer
record boolean lowest
< less than scalar or
> greater than string boolean lowest
<= less or equal scalar or
,string boolean lowest
or
set inclusion set boolean lowest
>= greater or scalar or
equal string boolean lowest
or
set inclusion (see 4.4) boolean lowest
-
IN set membership (see 4.4) boolean lowest
Boolean
NOT negation boolean boolean highest
OR disju~ction boolean boolean . 3rd highest
AND conjunction 'boolean boolean 2nd highest
Logical
- ? one's comple- integers and same as
or \ ment of operand' pointers operand highest
1 or logical OR integers and same as
I pointers operand 3rd highest
..
& logical AND integers and same as
pointers operand 2nd highest
Set
+ union set set 3rd highest
- set -difference set set 3rd highest
4-2
Pascal/MT+ Reference Manual 4.1 Arithmetic Expressions
4-3
Pascal/MT+ Reference Manual 4.2 Boolean Expressions
as
x < (3 OR X) > 15
The Boolean operators AND, OR, and NOT have the same effect as
in standard Boolean algebra. Table 4-2 shows the results from
Boolean operations. T and F stand for TRUE and FALSE.
4-4
Pascal/MT+ Reference Manual 4.3 Logical Expressions
(or I) logical OR
- ( or ? or \) one's comp lemen t NOT
[<member list>]
spec ifies the values of a set. The <member list> can be any
combination of individual elements and closed intervalS, separated
by commas. The following examples demonstrate the set construe tor:
4-S
Pascal/MT+ Reference Manual 4.4 Set Expressions
4-6
Pascal/MT+ Reference Manual 4.4 Set Expressions
VAR
CH : CHAR;
I : INTEGER;
ALLOWED, FOUND : SET OF CHARi
BEGIN
ALLOWED : = [I A I •• ' Z " '0' •• ' 9', ' . ' , ' 'J;
FOUND := [];
FOR I := 1 TO LENGTH(ST) DO
FOUND := FOUND + [ST[I]];
IF FOUND = ALLOWED THEN
WRITELN ('ALL USED, NO EXTRAS')
ELSE
IF FOUND <= ALLOWED THEN
BEGIN
WRITELN ('NO EXTRA CHARACTERS IN STRING, BUT');
WRITELN ('THE FOLLOWING CHARACTERS ARE MISSING:');
FOR CH := CHR(32} TO CHR(126) DO
IF (CH IN ALLOWED) AND NOT (CH IN FOUND) THEN
WRITELN (CH)
END
ELSE
IF FOUND >= ALLOWED THEN
BEGIN
WRITELN ('ALL CHARACTERS USED, BUT SOME EXTRA:');
FOR CH := CHR(32) TO CHR(126} DO
IF (CH IN FOUND) AND NOT (CH IN ALLOWED) THEN
WRITELN (CH)
END
ELSE
WRITELN ('NOT EVEN .IN THE BALLPARK!')
END;
End of Section 4
4-7
Section 5
Statements
appear s, you can use one of the sta temen ts descr ibed in th is
section, or you can use a procedure call or compound statemen t. A
compound statement is zero or more statements enclosed by a BEGIN
and an END.
<variable> := <expression>
• You can assign different set types if all members of the right
set can be members of the left set.
5-1
Pascal/MT+ Reference Manual 5.1 Ass ignmen t Sta t~men t
Examples:
COUNT := COUNT + 1;
CASE <expression> OF
{ <constant> {, <constant>} <statement> ;}
END
or
CASE <expression> OF
{ <constant> {, <constant>} <statement> ;}
ELSE
<sta temen t>
END
5-2
Pascal/MT+ Reference Manual 5.2 The CASE Statement
Examples:
CASE CH OF
I a I, I A' : WRITELN (' A' ) ;
'q', '0' : WRITELN ('0'); (* SEMICOLON OPTIONAL *)
ELSE
WRITELN ('NOT A OR 0')
END
5-3
Pascal/MT+ Reference Manual 5.4 The FOR Statement
5-4
Pascal/MT+ Reference Manual 5.5 The GOTO Sta temen t
GOTO <label>
The labeled sta temen t must be in the same block as the GOTO
statement or at a higher nesting level. The Pascal/MT+'run-time
system can transfer control out of ··routines and structures,
including deeply nested recursive routines, to any higher level that
meets the scope requirements for the label. However, transferring
control into procedures, functions, .or structured statements
produces unpredictable results.
Examples:
PROGRAM USE_GOTO;
LABEL
9999;
CONST
MAGIC WORD 'QU·IT'i
VAR
INP : STRING;
ELSE
GOTO 9999
END;
BEGIN
WHILE TRUE DO (* INFINITE LOOP *)
BEGIN
WRITELN (' WHAT IS .THE MAGIC WORD? I ) ;
READLN (INP) ;
BAILOUT (INP)
END;
9999
END.
5-5
Pascal/MT+ Reference Manual 5.6 The IF Statement
or
IF <Boolean expression> THEN
<statemen t>
ELSE
<sta temen t>
IF <exp> THEN
IF <exp> THEN
<s ta temen t>
ELSE
<sta temen t>
the compiler assOciates the ELSE part with the closest IF.
5-6
Pascal/MT+ Reference Manual 5.6 The IF Statement
Examples:
REPEAT
<sta temen t> {;
<statement> }
UNTIL <Boolean expres~ion.>
Examples:
REPEAT
READLN (INP) ; .
WRITELN (F, INP);
LINECNT : = LINECNT + 1
UNTIL INP =' I
5-7
Pascal/MT+ Reference Manual 5.8 The WHILE Statement
WITH EMPLOYEE DO
BEGIN
NAME := 'John Doe';
AGE := 47;
TITLE := 'Programmer IV'
END
5-8
Pascal/MT+ Reference Manual 5.9 The WITH Statement
TYPE
FULLNAME RECORD
FIRST, LAST STR~NG[15];
MIDDLE CHAR
END;
MEMBER RECORD
NAME FULLNAME;
JOINED STRING[8]i
ID INTEGER
END;
VAR
NEWMEM MEMBER;
BEGIN
WITH NEWMEM, NAME DO
BEGIN
FIRST := 'JOHN';
MIDDLE:= 'Q';
LAST := 'PUBLIC';
JOINED := '02/27/53';
ID := a
END
END.
End of Section 5
5-9
Section 6
Procedures and Functions
6-1
Pascal/MT+ Reference Manual 6.1 Procedure Definitions
6-2
Pascal/MT+ Reference Manual 6.1 Procedure Definitions
PROGRAM RECURSE;
VAR
I : INTEGER;
FUNCTION G (X INTEGER) INTEGER; FORWARD;
FUNCTION F (X INTEGER) INTEGER;
BEGIN
IF X < 2 THEN
F := 1
ELSE
F := F(X-l) + G(X-2)
END;
FUNCTION G; (* NO PARAMETER LIST OR FUNCTION TYPE *)
BEGIN
IF X < 2 THEN
G := 1
ELSE
G := (X*X) + G(F(X-l) MOD X)
END;
BEGIN (* MAIN PROGRAM *)
FOR I := 1 TO 10 DO
WRITELN ('F(', I:2, ') = F (I) )
END.
6.2 Parameters
The parameters in the. procedure heading are called formal
parameters. The parameters in the procedure call are called actual
paraineters. There are two types of formal parameters in Pascal/MT+:
value and va~iable parameters. The difference between the two is
the way that the parameters are passed at run-time.
A value para~eter is like a local variable in the procedure~
During a proced~recall, th~ value of the actual parameter passes
into the procedure.. If you change the value of the formal parameter
ins ide the procedure body, it does not effect the value of the
actual parameter. In the procedure call, the: actual parameter can
be any expression whose type is compatible with the formal
parameter.
Changing a variable parameter inside a procedure body changes
the actual parameter. During a procedure call, the address of the
formal parameter, instead of its value, passes into the procedure.
The actual parameter in the procedure call must be a variable whose
type is compatible with the formal parameter. . A variable parameter
cannot be acon~tant or an element of a packed structure. A file
. par ametermust be a. variable parameter. .
6-3
Pascal/MT+ Reference Manual 6.2 Parameters
PROGRAM VALVAR~
VAR
XVAL, XVAR : INTEGER;
PROCEDURE MUDDLE (MVAL INTEGER~ VAR MVAR INTEGER) ;
BEGIN (* MUDDLE *)
MVAL := ll~
MVAR := 33;
WRITELN('IN MUDDLE AT END MVAL, ' " MVAR)
END~
6-4
Pascal/MT+ Reference Manual . 6.2 Parameters
PROGRAM PASSPROC;
TYPE
REC RECORD
NAME, PHONE : STRING
END;
PTR "'REC;
.LST ARRAY [1 •• 10] OF PTR;
VAR
LIST : LST;
J : INTEGER;
6-5
Pascal/MT+ Reference Manual 6.3 Conformant Arrays
6-6
Pascal/MT+ Reference Manual 6.3 Conformant Arrays
PROGRAM DEMOCOM:
VAR
Al : ARRAY [1 •• 10] OF INTEGER:
A2 : ARRAY [2 •• 20] OF INTEGER;
PROCEDURE DISPLAYIT
(VAR ARl : ARRAY [LOW •• HI : INTEGER] OF INTEGER);
BEGIN (* DISPLAYIT *)
FOR I := LOW TO HI DO
WRITELN('INPUT ARRAY[', I, '] =', AR1[I])
END;
6-7
pascal/MT+ Reference Manual 6.4 Functions and Procedures
6-8
Pascal/MT+ Reference Manual 6.4 Functions and' Procedures
Input/Output routines
Function IParameter List
I "Returns
PROCEDURE ASSIGN (FILE, NAME)
PROCEDURE BLOCKREAD (FILE, BUF, lOR, NUMBYTES, RELBLK)
PROCEDURE BLOCKWRITE (FILE,BUF,IOR,NUMBYTES,RELBLN)
PROCEDURE CLOSE (FILE, RESULT)
PROCEDURE CLOSEDEL (FILE, RESULT)
FUNCTION EOF (FILE) BOOLEAN
FUNCTION EOLN (FILE) BOOLEAN
PROCEDURE GET (FILE)
FUNCTION GNB (FILE) CHAR
FUNCTION IORESULT INTEGER
PROCEDURE OPEN (FILE, TITLE, RESULT)
PROCEDURE OPENX (FILE, TITLE, RESULT, EXTENT)
PROCEDURE PAGE (FILE)
PROCEDURE PURGE (FILE)
PROCEDURE PUT (FILE)
PROCEDURE
PROCEDURE
READ
READHEX
" (FILE, VARIABLE, VARIABLE,
(FILE, VAR, SIZE) i
) · ..
PROCEDURE
PROCEDURE
READLN
RESET
(FILE, VARIABLE, VARIABLE,
(FILE)
) ·..
PROCEDURE REWRITE (FILE)
PROCEDURE SEEKREAD (FILE i RECORD NUMBER)
PROCEDURE SEEKWRITE (FILE, RECORD:NUMBER)
FUNCTION" WNB (FILE, CHAR) BOOLEAN
PROCEDURE WRITE (FILE, VARIABLE, VARIABLE, ) ....
PROCEDURE WRITEHEX (FILE, EXPRESSION, SIZE)
PROCEDURE
PROCEDURE
WRITELN "
LWRITEHEX.
(FILE, VAlUABLE, VARIABLE,
(FILE, EXPRESS ION, SI ZE ") *
) ·...
* does not apply to the 8080 implem~ntation
6-9
pascal/MT+ Reference Manual 6.4 Functions and Procedures
Transfer Functions
Function I Parameter List I Returns
FUNCTION CHR (INTEGER) CHAR
FUNCTION ODD (ORDINAL) BOOLEAN
FUNCTION ORD (ORDINAL) INTEGER
FUNCTION ROUND (NUM) INTEGER
FUNCTION TRUNC (NUM) INTEGER
Miscellaneous routines
Function I Parameter List I Returns
FUNCTION @BDOS (INTEGER, WORD) ** INTEGER
FUNCTION @BDOS86 (INTEGER, POINTER) * INTEGER
FUNCTION @CMD PTR_TO_STRING
PROCEDURE @ERR (INTEGER)
FUNCTION @HERR
PROCEDURE @HLT
FUNCTION @MRK INTEGER
FUNCTION. @RLS (INTEGER)
FUNCTION ADDR (VARIABLE REFERENCE) INTEGER
PROCEDURE CHAIN
PROCEDURE EXIT·
PROCEDURE INLINE (see Programmer's Guide)
FUNCTION MAXAVAIL. INTEGER
FUNCTION MEMAVAIL INTEGER
FUNCTION PRED (X) same type as X
FUNCTION RIM8S ** BYTE
FUNCTION SIZEOF (VARIABLE OR TYPE NAME) INTEGER
PROCEDURE SIM85 (VAL : BYTE) **
FUNCTION SUCC (X) same type as X
PROCEDURE WAIT (PORTNUM, MASK, POLARITY) **
ASS Function
Syntax:
FUNCTION ABS(X)i
Explanation:
ABS returns the absolute value of X. X must be a real or
integer expression. The result has the same type as X.
Examples:
ABS(-5.789) 5.789
ABS(56) 56
6-11
Pascal/MT+ Reference Manual ADDR Function
AQDR Punction
Syntax:
FUNCTION ADDR(VARIABLE OR ROUTINE) POINTER:
Explanation:
ADDR returns the address of a var iable, function, or procedure.
Variable references can include subscripted variables and record
fields. ADDR does not work with constants, user-defined ordinal
types, or any item that does not take- code or data space:
You can reference externals, including those in over lays.
However, you must keep in mind the scope of th~ referenced item.
For example, you cannot use ADDR in the main program to find the
address of a variable you declare in a nested procedure.
Example:
PROCEDURE ADDR_DEMO(PARAM INTEGER);
VAR
REC RECORD
J : INTEGER;
BOOL : BOOLEAN;
END;
ADDRESS : INTEGER;
R : REAL;
Sl : ARRAY[l .• lO] OF CHAR;
P : . . INTEGER;
BEGIN
P := ADDR(ADDR DEMO);
P := ADDR(PARAM);
P := ADDR (REC) ;
P := ADDR(REC.J)i
END;
6-12
Pascal/MT+ Reference Manual ARCTAN Function
ARC'.rAR Function
Syntax:
FUNCTION ARCTAN(X);'
Explanation:
~ARCTAN returns the angle, expressed in radians, whose tangent
is X. X must be a real or integer expression. The result is real
number.
Example:
ARCTAN (1) o. (* THE ANGLE IS PI / 4 *)
6-13
Pascal/MT+ Reference Manual ASSIGN Function
MSIGl! Punction
Syntax:
PROCEDURE ASSIGN( FILE, NAME ),
Explanation:
ASSIGN attaches an external filename to a file variable before
using a RESET or REWRITE procedure. FILE is a filename7 NAME is a
literal or a variable string containing the name of the file to
create. FILE can be of any type, but must be of type TEXT to use~
the special device names listed in Table 6-2.
Pascal/MT+ implements the Pascal local file facility using
temporary filenames in the form
PASTMPxx.$$$
where xx is sequentially assigned, starting at zero, ~rom the
beginning of each program.
If an ASSIGN does not precede an external file REWRITE, a
. temporary filename attaches before creation. Locally declared files
cannot be used as temporary files unless you initialize the file
with ASSIGN«file>,").
The following table defines the device names supported in the
CP/M ~ run-time environment.
6-14
pascal/MT+ Reference Manual ASSIGN Function
Name I Definition
Note that using CON: and KBD: together can create problems
because of the way they are implemen ted. To implemen t CTRL-S, CP/M
checks for typed characters when performing ·BDOS Function 2, wr iting
to CON:. If you type a character other than CTRL-S, CP/M stores it
internally, anticipating a subsequent call using Function 1.
EX~!!l£le!.!.
ASSIGN(CONIN,'CON: I);
ASSIGN(KEYBOARD,'KBD: ' );
ASSIGN(CRT,'TRM: ' );
ASSIGN(PRINTFILE,'LST: ' );
6-15
Pascal/MT+ Reference Manual BLOCKREAD, BLOCKWRITE Function
Syntax:
BLOCK READ (F:FILEVAR1 BUF:ANYJ VAR IOR:INTEGER1 SZ,RB:INTEGER)7
BLOCKWRITE(F:FILEVAR7 BUF:ANY7 VAR IOR:INTEGER7 SZ,RB:INTEGER):
Explanation:
These procedures enable direct disk access. FILEVAR is an
untyped file (FILE:). BUF is any array variable large enough to
hold the data. It can be indexed. lOR is an integer that receives
the returned value from the operating system. SZ is the number of
bytes to transfer. SZ is related to the size of BUFi it must be a
multiple of 128.
If BUF is 128 bytes, SZ must be 128. If BUF is 4096 bytes, SZ
can be as large as 4096. RB is the relative block number, which can
be in the range -1 to 32767. When RB is -1, the run-time routines
assume sequential block transfer. When RB is greater than -1, the
routine calculates the correct file location and opens new extents
as needed.
The data transfers either to or from your HUF variable for the
specified number of bytes.
6-16
Pascal/MT+ Reference Manual CHAIN Function
ClAIR Pupction
Syntax:
PROCEDURE CHAIN (FILE) 1
Explanation:
CHAIN allows you to chain from one program to another.
See Section 3.3 in the Pascal/MT+ Language Programmer's Guide
for more information.
6-17
Pascal/MT+ Reference Manual CHR Function
CDR PuQCtion
Syntax:
FUNCTION caR (X) CHAR:
Explanation:
caR returns.the character whose ASCII value is the integer X.
Examples:
WRITELN(CHR(7»: (* BEEP THE TERMINAL *)
IF C IN [·a· •• ·z'] THEN
C := CHR(ORD(C) - 32); (* CONVERT TO UPPERCASE *)
·6-18
Pascal/MT+ Reference Manual CLOSE Function
CLOSE Punction
Syntax:
PROCEDURE CLOSE FILE, RESULT ) ~
PROCEDURE CLOSEDEL FILE, RESULT )~
Explanation:
The CLOSE porocedure closes files. You must use it to guarantee
that data written to a file is purged from the buffer to th~ disk.
CLOSEDEL closes and deletes temporary files after use. FILE is
any filetype variable. RESULT is a VAR INTEGER parameter that has
the same value as IORESULT upon return from CLOSE •.
Files are implicitly closed when. an open file is RESET. The
number of files that can be open at a time is CPU-dependent. For
CP/M systems, this number is limited only by the amount of memory
available for File Control Blocks (FCBs).
6-19
Pascal/MT+ Reference Manual CONCAT Function
COICAZ luostioo
Syntax:
FUNCTION CONCAT( SOURCEl, SOURCE2, •••• , SOURCEn) STRING~
Explanation:
CONCAT returns a string in which all strings in the parameter
list are concatenated. The strings can be string variables, string
literals, or characters. You can concatenate a string of zero
length. The total length of all strings truncates at 256 bytes.
See the COpy function for restrictions when using both CONCAT and
COpy.
Example:
PROCEDURE CONCAT_DEMO;
VAR
Sl,S2 STRING;
BEGIN
Sl := 'left link, right linkf;
S2 := 'root roct root';
WRITELN(Sl,'/',S2) ;
Sl:= CONCAT(Sl,' ',S2,'!!!!!!');
WRITELN(Sl);
end;
Output:
left link, right link/root root root.
left link, right link root root root!!!!!!
6-20
Pascal/MT+ Reference Manual COpy Function
COPY Function
Syntax:
FUNCTION COPY( SOURCE, LOCATION, 'NUM_BYTES) STRING 1
Explanation:
COpy returns a string with the number of characters specified
in NUM BYTES from SOURCE, beginning at the index specified in
LOCATION. SOURCE must be a str ing. LOCATION and NUM_BYTES are
integer expressions. .
The COpy routine does not check whether LOCATION is out of
bounds or negative. Truncation occurs if NUM BYTES is negative or
NUM_BYTES plus LOCATION exceeds the length of the SOURCE.
Example:
PROCEDURE COPY_DEMO;
BEGIN
LONG STR := 'Hi from Cardiff-by-the-sea';
WRITELN(COPY(LONG STR,9,LENGTH(LONG STR)-9+1»;
END; - -
Output:
Cardiff-by-the-sea
. Hote:· COpy and CONCAT are str ing returning pseudo-functions and
have only one statically allocated buffer for the return value.
Therefore, if you use these functions more than once within the same
expression, the value of each occurrence becomes the value of the
last occurrence. For example,
CONCAT(A,STRING1) = CONCAT(A,STRING2)
is always true, because the concatenation of A and STRING2 ieplaces
that of A and STRINGI. As a further example,
WRITELN ( COpy (STRINGI, 1,4), COpy (S.TRINGI, 5,4) )
writes the second set of four characters in STRINGl twice.
6-21
Pascal/MT+ Reference Manual COS Function
cos Function
Syntax:
FUNCTION COS (X) REAL 1
Explanation:
COS returns the cosine of X. X, the angle in radians, must be
real or integer. The result is real.
Example:
IF COS(ANG) = SIN(ANG) THEN
WRITELN('4S DEGREES'):
6-22
Pascal/MT+ Reference Manual DELETE Function
DELETE Function
Syntax:
PROCEoqRE DELETE( TARGET, INDEX, SIZE) ~
Explanation:
DELETE removes SIZE characters from TARGET beginning at the
byte named in INDEX. TARGET is a string. INDEX ~nd SIZE are
integer expressions. No action occurs if SIZE is zero.
Example:
PROCEDURE DELETE_DEMO;
VAR
LONGSTR : STRING;
BEGIN -
LONG_STR := , get rid of the leading blanks';
WRITELN(LONG STR);
DELETE(LONG STR,l,POS('g' ,LONG STR)-l);
WRITELN(LONG STR); -
END; -
Output:
get rid of the leading blanks
get rid of the leading blanks
6-23
Pascal/MT+ Reference Manual DISPOSE Function
DISPOSB Function
Syntax:
PROCEDURE DISPOSE(VAR P POINTER) ;
PROCEDURE DISPOSE(VAR P POINTER, VARIANTS);
Explanation:
DISPOSE deallocates space that NEW allocates. When DISPOSE
returns, the value of the pointer var iable is uhdefined. If you are
using the FULLHEAP memory manager, the space is available for reuse.
Otherwise, the space is not available for reallocation.
See NEW for an example of using DISPOSE and more information
about deallocating variant records.
6-24
Pascal/MT+ Reference Manual EOLN, EOF Function
Syntax:
FUNCTION EOLN : BOOLEAN;
FUNCTION EOIN (VAR F : TEXT) : BOOLEAN;
FUNCTION EOF : BOOLEAN;
FUNCTION EOF(VAR F : FILE) : BOOLEAN;
EOF does not become TRUE at the end of the valid data in non-
TEXT files if the data does riot fill up the entire last sector of
the file.
The following example illustrates these concepts. Suppose the
input stream for a TEXT file consists of
6-25
Pascal/MT+ Reference Manual EOLN, EOF Function
If you repeatedly read characters from this stream, EOLN and EOF
return the values summarized in Table 6-3.
Table 6-4 shows the values of EOF when you repeatedly. read integers
from the input stream.
6682 T
(Note that 6682 is the end of the sector)
6-26
Pascal/MT+ Reference Manual EXIT Function
EXI'!' Function
~yntax:
PROCEDURE EXIT~
Explanation:
EXIT leaves the current procedure or function, or the main
program. If used in an INTERRUPT procedure, EXIT also loads the
regist.rs and reenables interrupts before exiting. EXIT is the
equivalent of the RETURN statement in FORTRAN or BASIC. You usually
execute it as a statement following a test.
Example:
PROCEDURE EXITTEST;
EXIT THE CURRENT FUNCTION OR MAIN ~ROGRAM.
BEGIN
IF BOOL THEN
BEGIN
WRITELN('EXITING EXITPROC');
EXIT;
END;
WRITELN('STILL IN EXITPROC, ABOUT TO LEAVE NORMALLY');
END;
BEGIN
WRITELN('EXITTEST •...••• ') ;
EXITPROC'(TRUE) ;
WRITELN('IN EXITTEST AFTER 1ST CALL TO EXITPROC')i
EXITPROC (FALSE) ;
WRITELN('IN EXITTEST AFTER 2ND CALL "TO EXITPROC')i
EXIT;
WRITELN('THIS LINE WILL NEVER BE PRINTED');
END;
Output:
EXITTEST •••••..
EXITING EXITPROC
IN EXITTEST AFTER 1ST CALL TO EXITPROC
STILL IN EXITPROC, ABOUT TO LEAVE NORMALLY
IN EXITTESTAFTER 2ND CALL TO EXITPROC
6-27
Pascal/MT+ Reference Manual EXP Function
UP Function
Syntax:
FUNCTION EXP(X)
Explanation:
EXP returns the exponential of X. X must be real or integer.
The'result is real. The function returns a value that is the
natural logarithm (base e), raised to the power of X. Use this
function with the natural logarithm function, LN.
Examples:
IF (EXP(LN(X) + LN(Y» - (X • Y) <= TOLERANCE THEN
WRITELN('LOGARITHM FUNCTIONS PASS TEST');
WRITELN(X, ' •• ', Y, '=', EXP(Y • LN(X»);
6-28
Pascal/MT+ Reference Manual FILLCHAR Function
FILLCBAR Function
Syntax:
PROCEDURE FILLCHAR( DESTINATION, LENGTH, CHARACTER);
Explanation:
FILLCHAR is a fast way to fill in large data structures with
the same data. For example, FILLCHAR can blank out a buffer.
DESTINATION is a variable reference, but need not be a packed
ar ray of characters as in UCSD Pascal. It can be subscr ipted.
LENGTH is an integer expression.
Hote: if LENGTH is negative or greater than the length of
DESTINATION, it overwrites adjacent code or data. CHARACTER is a
literal or variable of type CHAR. Fill the DESTINATION with the
number of characters specified by LENGTH.
Example:
PROCEDURE FILL_DEMO;
VAR:
BUFFER PACKED ARRAY[l .. 256] OF CHAR;
BEGIN
FILLCHAR(BUFFER,256,' '); {BLANK THE BUFFER}
END;
6-29
Pascal/MT+ Reference Manual GET Function
GET Punction
Syntax:
PROCEDURE GET(VAR F, FILE VARIABLE);
Explanation:
GET advances the window variable by one element and moves the
contents of the indicated file into the window var iable. EOF must'.,.
be FALSE before GET executes. When there is no next element, EOF
becomes'TRUE and the value of the window var iable becomes undef ined
0
6-30
Pascal/MT+ Reference Manual HI,LO, SWAP Function
Syntax:
FUNCTION HI (BASIC VAR) INTEGER~
FUNCTION LO(BASIC-VAR) INTEGER;
FUNCTION SWAP(BASIC=VAR) INTEGER;
. Explanation:
HI returns the upper 8 bi ts of BASIC_VAR ,(an 8- or l6-bi t
variable) in the lower 8 bits of the result.
LO returns the lower 8 bits, with the upper 8 bits forced to zero.
SWAP returns the upper 8 bits of BASIC_VAR in the lower 8 bits of
the resu~t and the lower 8 bits of BASIC VAR in the upper 8 bits of
the result. -
Passing an 8-bit variable to HI results in o. Passing 8, bits
to LO does nothing.
The following example shows the results of these functions.
Example:
PROCEDURE HI_LO_SWAP;
VAR
HL : INTEGER;
BEGIN
WRITELN ( 'HI LO SWAP ........ ');
HL := $104;- -
WRITELN ( 'HL=' ,HL) ;
IF HI (HL) = 1 THEN
WRITELN('HI(HL)=',HI(HL»;
IF LO(HL) = 4 THEN
WRITELN (' LO (HL) =' ,LO (HL) ) ;
IF SWAP (HL) = $0401 THEN
WRITELN ( • SWAP (HL)=' , SWAP (HL» ;
END;
Output:
HI LO SWAP •••••••
HL;;'260
HI(HL)=l
LO(HL)=4
SWAP (HL) =1025
6-31
Pascal/MT+ Reference Manual INLINE Function
IlILID Function
Syntax:
PROCEDURE INLINE(arg/arg/ ••• );
Explanation:
INLINE is a built-in feature that allows you to insert data in
the middle of a Pascal/MT+ procedure or function. You can insert
small machine-code sequences and constant tables into a Pascal/MT+
program without using externally-assembled routines. .
Section 4.3.2 of the Pascal/MT+Language Programmer's Gu ide has
examples of using INLINE.
6-32
Pascal/MT+ Reference Manual INSERT Function
IRSERr' Punction
Syntax:
Explanation:
Example:
PROCEDURE INSERT_DEMO:
VAR
LONG STR : STRING;
S1 :-STRING[lO];
BEGIN
LONG STR := 'Remember Luke';
S1 :; 'the Force,';
INSERT(Sl,LONG STR,10);
WRITELN(LONG STR);
INSERT('to use ',LONG STR,lO);
WRITELN(LONG STR); -
end; -
Output:
6-33
Pascal/MT+ Refe~ence Manual IORESULT Function
IORBSOLT Function
Syntax:
FUNCTION IORESULT :.INTEGER:
Explanation:
After each I/O operation, the run-time library routines set the
value returned by the IORESULT function. In general, the value of
IORESULT is system-dependent. Never attempt to WRITE. the IORESULT
because it resets to 0 before any I/O operation.
Refer to the Pascal/MT+ Language Programmer's Guide for more
information about IORESULT.
Example:
ASSIGN(F,'C:HELLO');
RESET(F);
IF IORESULT = 255 THEN
WRITELN('C:HELLO IS NOT PRESENT');
6-34
Pascal/MT+ Reference Manual LENGTH Function
LENGTH Function
Syntax:
Explanation:
Example:
PROCEDURE LENGTH_DEMO~
VAR
Sl : STRING[40];
BEGIN
Sl := 'This string is 33 characters long';
WRITELN('LENGTH OF ',Sl,'=' ,LENGTH(Sl»;
WRITELN('LENGTH OF EMPTY STRING = ',LENGTH("»;
END;
Output:
6-35
Pascal/MT+ Reference Manual LN Function
LII Function
Syntax:
FUNCTION LN(X) REAL:
Explanation:
LN returns the natural logarithm of X. X must be real or
integer. The result is real.
6-36
Pascal/MT+ Reference Manual MAXAVAIL, MEMAVAIL Function
Syntax:
FUNCTION MAXAVAIL .' INTEGER:
FUNCTION MEMAVAIL INTEGER;
Explanation:
The functions MAXAVAIL and MEMAVAIL work with NEW and DISPOSE
to manage the heap memory area in Pascal/MT+.
MEMAVAIL returns the available memory at any given time,
regardless of fragmentation. MAXAVAIL reports the largest block
available.
/,If the result of these functions displays as a negative number,
the~amount of memory remaining is too large to express as a positive
integer. You can display the return value with WRITEHEX.
See your Pascal/MT+ Language Programmer's Guide for more
information on the use of dynamic memory.
6-37
Pascal/MT+ Reference Manual MOVE, MOVERIGHT, MOVELEFT Function
Syntax:
PROCEDURE MOVE (SOURCE, DESTINATION, NUM BYTES)
PROCEDURE MOVELEFT (SOURCE, DESTINATION, NUM-BYTES)
PROCEDURE MOVERIGHT(SOURCE, DESTINATION, NUM:BYTES)
Explanation:
These procedures move the number of bytes contained in
NOM BYTES from the SOURCE location to the DESTINATION location~
~and MOVELEFT are synonyms. They move from the left end of the
source to the left end of the destination. MOVERIGHT moves from the
right end of the source to the right end of the destination. The
parameters passed to MOVERIGHT specify the left end of the source
and destination.
The source and destination can be variables of any type, and
they need not be of the same type. They can be pointer s to
variables, but not named or literal constants. The number of bytes
is an integer expression between 0 and 64K.
MOVELEfT and MOVERIGHT transfer bytes from one data structure
to another or move data withfn a data stracture. These procedures
move on a byte level, ignoring the data structure type. MOVERIGHT
transfers bytes from the low end of an array to the high end ~
Without this procedure, you would need a FOR loop to pick up each
character and put it down at a higher address. MOVERIGHT is much
faster. You can use MOVERIGHT in an insert character routine to
make room for characters in a buffer.
MOVELEFT can transfer bytes from one array to another, delete
characters from a buffer, or move the values in one· data structure
to another.
When you use these. procedures keep .in mind the following:
Example:
PROCEDURE MOVE DEMO:
CONST -
STRINGSZ =
80:
VAR
BUFFER: STRING (STRINGSZ] :
LINE : STRING:
BEGIN
WRITELN ( 'MOVE DEMO ...... ') ;
BUFFER := 'Judy J. Smith/ 335 Drive/ Lovely, Ca. 95666';
WRITELN(BUFFER) :
LINE := 'Roland 'i
INSRT(BUFFER, POSt'5',BUFFER)+2,LINE);
WRITELN(BUFFER) ;
END;
Output:
MOVE DEMO •.....
Judy-J. Smith/ 355 Drive/ Lovely, Ca. 95666
Judy J. Smith/ 355 Roland Drivel Lovely, Ca. 95666
6-39
Pascal/MT+ Reference Manual NEW Function
_ Function
Syntax:
PROCEDURE NEW (VAR P POINTER) 1
PROCEDURE NEW (VAR P POINTER1 VARIANTS);
Explanation:
NEW dynamically allocates space for a record of the pointerts
type, and sets the value of the pointer to the new record. For
variant records, the procedure allocates enough space to hold the
largest variant, unless you specify which variant you want.
Specify the variant by its tag value. If the record has nested
variants, specify the variants in the order of nesting. When you
deallocate a record with DISPOSE, use the same parameter list.
Example:
PROGRAM NEWDEMO;
TYPE
COL = (RED, YELLOW, BLUE, GREEN, ORANGE, PURPLE):'
PTR = "REC;
REC RECORD
A : INTEGER;
CASE LIGHT COL OF
RED : ();
YELLOW: (R : REAL);
BLUE : (.
CASE TINT : COL OF
GREEN (W, X, Y, Z INTEGER);"
PURPLE: (H, I, J, K REAL)
)
END;
VAR
GENERAL, SMALL, BIG : PTR;
BEGIN
WRITELN(tTHIS PROGRAM DOES NOTHING BUT TWEAK THE HEAPt);
NEW(GENERAL) ; (* FOR ANY VARIANT *)
NEW (SMALL, RED); (* FOR SMALLEST VARIANT *)
NEW(BIG, BLUE, PURPLE); (* FOR LARGER VARIANT *)
DISPOSE(GENERAL);
DISPOSE (SMALL, RED);
DISPOSE (BIG, BLUE, PURPLE)
END.
6-40
Pascal/MT+ Reference Manual ODD Function
000 Function
Syntax:
FUNCTION ODD (INTEGER) BOOLEAN 1
Explanation:
ODD returns TRUE if the expression is odd and FALSE if it is
not.
Example:
IF OOO(LENGTH(ANSWER» THEN
WRITELN ( 'THAT' 's ODD!')
ELSE
WRITELN('EVEN I BELIEVE THAT')
6-41
Pascal/MT+ Reference Manual OPEN Function.
OPEN Function
Syntax:
PROCEDURE OPEN ( FILE, FILENAME, RESULT )~
Explanation:
The OPEN procedure opens an existing file for input. FILE is
any file variable. Filename is a string that contains the CP/M
filename. RESULT is an integer variable, which on return from OPEN,
has the same value as IORESULT. .
The OPEN procedure is the same as the sequence:
ASSIGN (FILE, FILENAME):
RESET(FILE):
RESULT := IORESULT:
Example:
OPEN ( INFILE, 'A:FNAME.DAT', RESULT );
6-42
Pascal/MT+ Reference Manual ORO Function
. ORO PUDction
Syntax:
FUNCTION ORO (SCALAR) INTEGER;
Explanation:
ORO returns the ordinal value of a scalar or enumerated type
expression. The result is an integer. For an enumerated type, the
ordinal value is the same as the order of declaration, startin9 with
O.
Example:
FUNCTION OIG20EC (C : CHAR) : INTEGER;
BEGIN
OIG20EC := ORO(C) - ORD('O');
END;
6-43
Pascal/MT+ Reference Manual PACK, UNPACK Function
Syntax:
PROCEDURE PACK(A : ARRAY[M ••• N] OF T: Z ARRAY[U ••• V] OF T:
PROCEDURE UNPACK(A : ARRAY[M ••• N] OF T: Z : ARRAY[U ••• V] OF T:
Explanation:
The Pascal/MT+ compiler accepts PACK and UNPACK but does not
execute them. Because Pascal/MT+ i's byte-or iented, these procedures
are unnecessary.
6-44
Pascal/MT+ Reference Manual PAGE Function
PAGE Function
Syntax:
PROCEDURE PAGE(FILE VARIABLE);
Explanation:
PAGE sk ips to the top of a· new page when a TEXT file is
printing by inserting a begin-page character in the outp~ file. If
you do not specify the output file, it defaults to standard output.
6-45
Pascal/MT+ Reference Manual POS Function
POS Function
Syntax:
FUNCTION POS ( PATTE,RN, SOURCE ) INTEGER;
Explanation:
POS returns the integer value of the position of the first
occurrence of PATTERN in SOURCE. If PATTERN is not in the string,
the function returns O. SOURCE is a string. PATTERN is a string,
charact'er, or li teral.
Example:
PROCEDURE POS_DEMO;
VAR
STR,PATTERN : STRING;
CH : CHAR;
BEGIN
STR := 'Ada Lovelace';
PATTERN := 'Love';
CH : = 'v';
WRITELN('position of ',PATTERN,' in I,STR,' is " POS(PATTERN,STR»;
WRITELN ('position of ',CH,' in ',STR,' is ',POS (CH ,STR» i
WRITELN('pos of "z" in ',STR,' ~s ',POS('·z',STR»;
END;
Output:
position of Love in Ada Lovelace is 5
position of v in Ada Lovelace is 7
position of 'z' in Ada Lovelace is 0
6-46
Pascal/MT+ Reference Manual PRED Function
POD Put:lction
~yntax:
Explanation:
PRED returns the value of the predecessor of a scalar
express ion. The ord inal value of the predecessor is 1 less than the
ordi~al value of the expression.
Example:
TYPE
WEEKDAY = (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY):
PRED(FRIDAY) THURSDAY
PRED(2 * 2) 3
PRED (' 0') 'c'
6-47
Pascal/MT+ Reference Manual PURGE Function
PURGE Punction
Syntax:
PROCEDURE PURGE( FILE )1
Explanation:
PURGE deletes the E"ile associated with th.! file variable. The
file is deleted from the disk directory.
Example:
ASSIGN(F,'BADFILE.BAD');
PURGE(F); (* DELETE BADFILE.BAD *)
6-48
Pascal/MT+ Reference Manual PUT Function
PUT 'Function
Syntax:
PROCEDURE PUT(FILE VARIABLE);
Explanation:
PUT transfers the contents of the window variable associated
with F to the next available record in the file. You must assign to
the window variable before executing a PUT. You can -use this
procedure only if EOF is TRUE. After execution, EOF remains TRUE
and the window variable becomes undefined.
6-49
Pascal/MT+ Reference Manual READ, READLN Function
Syntax:
PROCEDURE READ (FILE VARIABLE, variable, variable, ••• ):
PROCEDUREREADLN(FILE VARIABLE, variable, variable, •• );
Explanation:
These procedures read from the file associated with the file
variable into the variables listed. If you do not specify a file,
the procedures default to the standard input.
READ~N works with TEXT files only, but both routines, when
reading from TEXT files, convert Booleans, reals, and integers from
their ASCII representations. All numbers convert on input, but the
formatting is lost. Therefore, you should separate numbers from
each other and from other data types by a blank or a carriage
return/line-feed.
READLN reads the data and then sets the file pointer at the
beginning -of the next lineo READ does not skip over data. When
reading strings, both procedures read from the current position to
the end of the line. Use READLN to read strings.
When reading from non-console files, the sequence of operations
for each data item is equivalent to:
<variable> := FA~
GET(F);
When reading from the console, the sequence is
GET (F) ;
<variable> := FA;
For non-TEXT files, the var iables in the parameter list must be
the same type as the data read from the file. The compiler does not
typecheck, however. You must construct a parameter list compatible
with your file's format.
6-50
Pascal/MT+ Reference Manual READHEX, WRITEHEX, Function
Syntax:
PROCEDURE READ HEX VAR F TEXT VAR W : ANYTYPE; SIZE: 1 •• 4);
PROCEDURE WRITEHEX VAR F TEXT EXPRESSION ANYTYPE; SIZE: 1 ..
PROCEDURE LWRITEHEX VAR F TEXT EXPRESSION: LONGINT; SIZE: 1 ••
Explanation:
These routines read and write text in hex-adecimal
representation. SIZE specifies the number of bytes to read or
write.
READHEX reads two characters for each byte, then it skips to
the next carriage return/line feed. You cannot read more than one
hexadecimal number from a single line.
WRITEHEX writes two characters for each byte. It does not
output any leading or trailing blanks or a carriage return/line
feed.
LWRITEHEX is like WRITEHEX, except that it only works with long
integers, and it can handle up to four by~es. '
The 8~bit version of Pascal/MT+ does not have LWRITEHEX, and
its maximum data size for READHEX is 2 bytes.
6-5'1
Pascal/MT+ Reference Manual RESET Function
RBSB'l' Function
Syntax:
PROCEDURE RESET(FILE VARIABLE):
Explanation:
RESET moves the window pointer to the begir\ning of a file so
that you can read it. The window variable is set to the first
element of F. If you try to reset a file that does not exist,
IORESULT returns a value of 255. Any other val'ue means success.
RESET calls CLOSE if the file is already open.
The file is open to reading and' writing for random access.
With nonconsole typed files, the procedure RESET does an initial
GET. This process moves the first element of the file into the
window variable.
The initial GET does not perform on console ~r untyped files
because GET waits for a character, and you would have to type a
character before your program could execute.
6-52
pascal/MT+ Reference Manual REWRITE Function
RBWRXTE Function"
Syntax:
PROCEDURE REWRITE(FILE VARIABLE)~
Explana tion:
REWRITE creates a file on disk using the name associated with
the file variable, deleting any existing file by that name. If the
variable has no associated filename, specified with ASSIGN, REWRITE
creates a temporary file. -
Tenporary files are useful for scratch pad memory and data that
you no longer need after executing the program. The last two dig its
in the name make every temporary file unique, so you can have up to
100 temporary files.
The EOF and EOLN functions return TRUE because the file is an
output file. The file is open for sequential writing only and is
ready to receive data into its first element.
6-53
Pascal/MT+ Reference Manual RIM8s, SIM8s Function
Syntax:
FUNCTION RIM8s :". BYTE~
PROCEDURE SIM8s(VAL : BYTE)~
Explanation:
These routines use, the special 8085 instructions RIM and SIM.
They call the procedure that contains the instruction. Under CP/M,
the heap grows from the end of the data area, and the stack frame
(for recursion) grows from the top of memory down. CP/M preloads
the hardware stack register with the contents of absolute location
0006, unless the $Z option overrides it. The stack frame grows
starting at 512 bytes pelow the initialized hardware value.
Hote: these routines are only supported in the a-bit version of
Pascal/MT+.
6-54
Pascal/MT+ Reference Manual ROUND Function
ROORD Function
Syntax:
FUNCTION ROUND(REAL) INTEGER;
Explanation:
ROUND converts a real to an integer by rounding it up or down
to the nearest integer value.
Examples:
ROUND(2.67) = 3
ROUND(45.49) = 45
6-55
Pascal/MT+ Reference Manual SEEKREAD, SEEKWRITE Function
Syntax:
Explana tion:
6-56
Pascal/MT+ Reference Manual SHL, SHR Function
Syntax:
FUNCTION SHL(BASIC VAR, NUM) INTEGER:
FUNCTION SHR(BASIC:VAR, NUM) INTEGER:
Explanation:
SHR shifts BASIC VAR by NUM bits to the right, inserting 0
bits. SHL shifts the BASI~ VAR by NUM bits to the left, inserting 0
bits. BASIC_VAR is an a=or l6-bit variable. NUM is an integer
expression.
Suppose you obtain a lO-bi t value from two separ.ate input
ports. Use SHL to read them in:
X := SHL(INP(a] & $lF, 3) ! (INP[9] & $lF);
The example reads from port number a, masks out the three high
bits returned from the INP array, and shifts the result left. Next,
this result logically OR's with the input from port number 9, which
has also been masked. .
Example:
PROCEDURE SHIFT DEMO;
VAR I : INTEGERT
BEGIN
WRITELN('SHIFT DEMO •••••••• ');
.I : = 4; -
,I);
WRITELN('I~'
WRITELN('SHR(I,2)=' ,SHR(I,2»;
WRITELN('SHL(I,4)=',SHL(I,4»;
ENO;
Output:
SHIFT DEMO ••••••••
1=4 -
SHR(I,2)=l
SHL(I,4)=64
6-57
Pascal/MT+ Reference Manual SIN Function
SIR Punction
Syntax:
FUNCTION SIN (ANGLE) REAL~
Explanation:
SIN returns the sine of the angle. Express the angle in
radians, as an integer or real expression.
6-58
Pascal/MT+ Reference ~anual SIZEOF Function
SIZBOP Function
Syntax:
FUNCTION SIZEOF (VARIABLE OR TYPE NAME) INTEGER~
Explanation:
SIZEOF is a compile-time function that returns the size of the
parameter in bytes. Use it in MOVE statements for the number of
bytes to be moved. With SIZEOF you do not need to keep changing
constants as the program evolves. The parameter can be any var iable
or user-defined ordinal type •.
SIZEOF is a compile-time function. Only the size of items that
do not generate code to calculate their address can be a parameter
to SIZEOF. The compiler must know the size of the item.
Example:
PROCEDURE SIZE_DEMO;
CONST
NAMELN 10;
ADDRLN 30;
VAR
A RECORD
NAME STRING [NAMELN] ;
ADDR STRING [ADDRLN]
END;
B RECORD
NAME STRING [NAMELN] ;
ADDR STRING [ADDRLN] ;
HIRE DATE INTEGER;
EMP NUM INTEGER
END; -
BEGIN
READLN(A.NAME);
READLN(A.ADDR);
B.HIRE DATE := 0;
B.EMP_NUM := 0;
6-59
Pascal/MT+ Reference Manual SIZEOF Function
6-60
Pascal!MT+ Reference Manual SQR Function
SQR Function
Syntax:
FUNCTION SQR(X) REAL or INTEGER
Explanation:
SQR returns the square of X. X must be real or integer. The
result has the same type as X.
Example:
SQR(S) = 2S
SQR ( 4 • 0 ) = 16 • 0
6-61
Pascal/MT+ Reference Manual SQRT Function
SQRT Punction
Syntax:
FUNCTION SQRT(X) REALr
Explanation:
SQRr returns the square root of X. X must be real or integere
The result is real.
6-62
Pascal/MT+ Reference Manual SUCC Function
SOCC Function
Syntax:
FUNCTION SUCC(X) SCALAR~
Explanation:
X is a scalar or subrange expression. SUCC returns the value
of XiS successor.
Examples:
SUCC ( I AI ) = I B I
SUCC(FALSE) = TRUE
SUCC(23) = 24
6-63
Pascal/MT+ Reference Manual TRUNC Function
TRDHC Punction
Syntax:
FUNCTION TRUNC(REAL) INTEGER1
Explanation:
TRUNC converts a real number to an integer by dropping the
digits to the right of the decimal point.
Examples:
TRUNC(4.99) =4
TRUNC(36.2 + 1.11) 37
6-64
Pascal/MT+ Reference Manual TSTBIT, SETBIT, CLRBIT Function
Syntax:
FUNCTION TSTBIT( BASIC VAR, BIT NUM} BOOLEAN;
PROCEDURE SETBIT(VAR BAS IC-VAR, BIT-NUM};
PROCEDURE CLRBIT(VAR BASIC:VAR, BIT:NUM};
Explanation:
TSTBIT returns TRUE if the designated bit is on, and returns
FALSE if the bit is off.
SETBIT sets the designated bit in the parameter.
CLRBIT clears the designated bit in the parameter.
BASIC VAR is any a-or l6-bi t var iable. BIT_NUM is' o•• 15 wi th
bit 0 on the right.
If BITNUM is out of range, results are unpredictable but the
program continues. For example, trying to set or clear bit 10 of an
a-bit variable causes unpredictable results, but no error message.
Example:
PROCEDURE TST SET CLR_BITS;
VAR
I : INTEGER;
BEGIN
WRITELN('TST SET CLR BITS ....•.• ');
I := 0; -
SETBIT(I,5};
IF I = 32 THEN
IF TSTBIT(I,5) THEN
WRITELN ( , 1=' , I) ;
CLRBIT(I,5);
IF I = 0 THEN
IF NOT (TSTBIT(I,5»- THEN
WRITELN('I=' ,I);
END;
Output:
TST SETCLR BITS ••••.••
1=32 - -
1=0
6-65
Pascal/MT+ Reference Manual WAIT Function
WAIT Function
Syntax:
PROCEDURE WAIT(PORTNUM , MASK, POLARITY)~
Explanation:
The WAIT procedure is only available in the a-bit version of
Pascal/MT+. PORTNUM and MASK are literal or named constants.
POLARITY is a Boolean constant. WAIT generates a tight ~tatus wait
loop:
IN portnum
ANI mask
J?? $-4
The WAIT procedure does not generate in-line code for the
status loop. A status loop is constructed in the DATA area and
called by the WAIT run-time subroutine. Thus, the loop is fast, but
the call and return from the loop add a small amount of execution
time. Use INLINE if time is critical.
Example:
PROCEDURE WAIT_DEMO;
CONST
CONSPORT = $F7; (* for EX?O NOBUS-Z COMPUTER *)
CONSMASK = $01;
BEGIN
WRITELN('WAIT DEMO ••.•••• ');
WRITELN('WAITING FOR A CHARACTER');
WAIT(CONSPORT,CONSMAXK,TRUE);
WRITELN ( I THANKS I ) ;
END;
6-66
Pascal/MT+ Reference Manual WNB, GNB Function
Syntax:
FUNCTION GNB(FILEVAR: FILE OF PAOC):CHAR;
FUNCTION WNB(FILEVAR: FILE OF CHAR; CH:CHAR) BOOLEAN;
Explanation:
These functions give you byte-level, high-speed access to a
file. PAOC is any type that is a Packed Array Of Char. The optimum
size of the packed array is in the range 128 •• 4095.
GNB lets you read a file one byte at a time. GNB returns a
value of type CHAR. The EOF function is valid when the physical
end-of-file is reached but not based upon any data in the file.
Attempts to read past the end of the file return $FF.
WNB lets you write a file one byte at a time. WNB requires a
file and a char acter to wr i te. The function returns a Boolean value
that is TRUE if there was an error while writing that byte to the
file. Written bytes are not interpreted.
GNB and WNB are faster than using F"", GET/PUT combinations,
because of their larger buffer.
6-67
Pascal/MT+ Reference Manual WRITE, WRITELN Function
Syntax:
PROCEDURE WRITE (FILE VARIABLE, EXPR, EXPR, ••• );
PROCEDURE WRITELN(FILE VARIABLE, EXPR, EXPR, ••• );
Explanation:
These procedures write data to the file associated witt. F. If
the file is a TEXT file, they convert numbers to ASCII and write the
Boolean values as the strings TRUE and FALSE.
WRITE (F, DATA);
is equivalent to
F'" := DATA;
PUT(F);
WRITELN works only with TEXT files, ending an old line and
starting a new one. The procedure is like WRITE, except it puts a
carriage return and line feed after the data. A WRITELN with no
expressions outputs only a carriage retu;n/line-feed.
Data 'can be li teral and named constants, integers, reals,
subranges, enumerated, Booleans, strings, and packed arrays of
characters, but cannot be structured types, such as records.
If you do,not specify a file, the procedures default to the
standard output file.
WRITE and WRITELN treat strings as arrays of characters. They
do not write the length byte to the file.
You can specify the field format for any data type~ The field
format 1's
<real or non-real variable> : <field width>
or
<real variable> : <field width> : <fraction length>
The minimum <field width>, which is optional, is a natural
number that specifies the smallest number of characters to writeo
The optional <fraction length> specifies the number of digits to
follow the decimal point in a real 'number. For non-real numbers,
specify only the field width. The da,ta is right-justified in the
field. A number is always expressed in exponential notation if a
number is larger or smaller than the significant digits can
represent.
6-68
Pascal/MT+ Reference Manual WRITE, WRITELN Function
Example:
PROGRAM DO_WRITE;
CONST
STR 'COLORLESS GREEN IDEAS':
BUL TRUE:
INT 9876:
REL 2345.678:
VAR
F TEXT;
I INTEGER;
BEGIN
ASSIGN(F, 'SAMPLE.TXT'):
REWRITE(F) ;
WRITE(F, '*', 1, 2, 3) i
WRITE (F ,4, 5, 6) i
WRITELN(F, '*'):
WRITELN(F, '2: *' STR, '*');
WRITELN (F, '3: *' STR: 40, '*');
WRITELN (F, '4: *', BUL, '*', INT, '*', REL, '*');
WRITELN(F, '5: *',BUL:10,'*',INT:10,'*' ,REL:10,'*');
WRITELN(F, '6: *', REL:10:3, '*', REL:8:1, '*');
CLOSE (F, I)
END.
Output:
*123456*
2: *COLORLESS GREEN IDEAS*
3: * COLORLESS GREEN IDEAS*
4 *TRUE*9876* 2.34567E+03*
5 * TRUE* 9876* 2.3456E+03*
6 * 2345.678* 2345.7*
6"-69
Pascal/MT+ Reference Manual @BDOS Function
@BDOS Function
Syntax:
FUNCTION @BDOS~
Explanation:
@BDOS enables direct access to the CP/M operating system.
See the Pascal/MT+ Language Programmer I s Guide _ for more-
information.
6-70
Pascal/MT+ Reference Manual @BDOS86 Function
@BDOS86 Function
Syntax:
FUNCTION @BDOS86;
Explanation:
6-71
'Pascal/MT+ Reference Manual @CMD Function
@OID Function
Syntax:
FUNCTION @CMD "'STRING:
Explanation:
@CMD lets you access the command tail of a'command line. The
function retrieves the information from the command tail, moves it
to a string, and returns a pointer to this string. The command tail
starts with a blank. You can call @CMD only once, at the beginning
of the program before you open any files.
Example:
PROGRAM @CMD_DEMO:
TYPE
PSTRG = "STRING;
VAR S : STRING[16]:
PTR : PSTRG:
F : FILE OF INTEGER;
BEGIN
PTR := @CMD;
S := PTR"'i
ASSIGN(F,S);
RESET (F)
END.
6-72
Pascal/MT+ Reference Manual @ERR Function
@BRR- Function
Syntax:
PROCEDURE @ERR ~
Explanation:
@ERR is the default error handling routine in PASLIB. You can
replace @ERR with your own error handling routines. See Section
4.6.3 of the Pascal/MT+ Language Programmer's Guide for more
information.
6-73
Pascal/MT+ Reference Manual @HLT Function
@BL~ Function
Syntax:
PROCEDURE @HLT ~
Explanation:
6-74
Pascal/MT+ Reference Manual @HERR Function
@BBRR Punction
Syntax:
FUNCTION @HERRi
Explanation:
@HERR is a predefined BOOLEAN variable that the NEW procedure
uses to return the result of an allocation request. @HERR returns
FALSE if space is available, or TRUE when there is no space.
You should always use @HERR in conjunction with NEW, because
the heap management system in PASLIB does not signal an error if
there is no space available when you make an allocation request.
6-75
Pascal/MT+ Reference Manual @MRK Function
@MRK Function
Syntax:
FUNCTION @MRK INTEGER:
Explanation:
@MRK returns the address of the top of the heap. You must save
the address if you want to use @RLS to restore the heap· to its
previous state.
You can use @MRK to mark more than one address, and then use
@RLS to return to any of them.
See Section 4.3.5 of the Pascal/MT+ Languaqe Programmer • s Guide
for more information.
6-76'
Pascal/MT+ Reference Manual @RLS Function
@RLS Function
Syntax:
FUNCTION @RLS (INTEGER);
Explanation:
@RLS resets the top of the heap to the address returned by
@MRK. ."
See Section 4.3.5 of the Pascal/MT+ Language Programmer I s Guide
for more information.
End of Section 6
6-77
Section 7
Input and Output
You declare a f i'le var iable like any other var iable, as in the
following example:
TYPE
INTFILE = FILE OF INTEGER;
REC RECORD
x, Y, Z REAL;
I, J, K INTEGER
END;
VAR
Fl, F2 INTFILE;
F3 FILE OF REC;
F4 FILE OF ARRAY[l •• lO] OF CHAR;
FS FILE; (* UNTYPED FILE FOR BLOCK I/O *)
7-1
pascal/MT+ Reference Manual 7.1 Fundamentals of Pascal/MT+ I/O
When you declare a file var iable, the I/O system does not
associate a physical disk file with that variable. You have to use
the ASSIGN or OPEN procedure to associate an actual filename with
the variable. After that, all input and output to the file is
through the file variable.
In general, you use the file var iable I s name to refer to the
file. If you want to reference the buffer, follow the name with the
pointer character. For example,
ASSIGN (F3, ITEST.OAT I );
associates the name TEST.OAT with the file variable F3, and
F2" := 45;
puts the integer value 45 in the buffer of the file variable F2.
Each file must have an explicit end-of-file indicator. Most
operating systems use a control character to indicate the end-of-
file. When the I/O system encounters this character, the predefined
function EOF returns TRUE.
Under some cond it ions, however, the valid da ta end s before the
opera ting system signals an end-of-file condition. This can happen,
for example, when the data does not fill the last sector in the
file. In this case, EOF does not detect tl1e actual end of the Data
file. There fore, you must use a dummy record as the la st record, or
save the number of records in a separate file.
To write data to a file using PUT, you have to assign the data
to the buffe r and then call PUT a,s in the following sequence:
F ... : = ITEM;
PUT (F) ;
To read data with GET, you take the data from the buffer and
then call GET, as in the following sequence:
ITEM := F . . . ;
GET (F) ;
The reason for this sequence is not intuitive. Note however, that
when you call RESET to open the file for reading, the first element
in the file is au tomatically placed in the buffer e --carling GET
places the ~ item in the buffer.
7-2
Pascal/MT+ Reference Manual 7.2 Regular I/O
If you are reading from the console, you have to call GET
before you access the buffer, because initially there is nothing in
the buffer, and the program would wait indefinitely for the first
character.
The program shown in Listing 7-1 demonstrates the GET and PUT
routines. The program creates a file, writes some data to it, and
then reads the data back from the file. Notice that you have to
explicitly move data in and out of the buffer.
You usually do not have to use GET and PUT. The procedures
READ and WRITE allow you to read and write data without worrying
about the buffer. Both routines can handle any filetype. You do
not have to treat the console and other devices differently when you
use READ and WRITE.
7-3
Pascal/MT+ Reference Manual 7.2 Regular I/O
7-4
Pascal/MT+ Reference Manual 7.3 INP and OUT Arrays
Examples:
OUT[(PORTNUM + I)] := $88~
OUT [ 0 ] : = $ 8 8 1
J := INP[(PORTNUM)];
When you use this mechanism, keep in mind the following points:
7-5
Pascal/MT+ Reference Manual 7.4 Redirected I/O
7-6
Pascal/MT+ Reference Manual 7.4 Redirected I/O
7-7
Pascal/MT+ Reference Manual 7.5 Sequential I/O
This )jis ~ a Iii IineC) 6 This Iii is'1ii the ~ next li'line G6 This Ii' is Ii' the li'last Iii' line 8 68
7-8
Pascal/MT+ Reference Manual 7.5 Sequential I/O
The program in Listing 7-3 writes data to a TEXT file and reads
it back for display on the output device. The procedure WRITE DATA
writes to the TEXT file and READ DATA retrieves the information
stored in the file. -
The field format can be specified for any data type. For non-
real numbers only the field width is specified, not the number of
places after the decimal point. The data is right-justified in the
field. The output is always expressed in exponential notation if a
number is larger than the significant digits can represent. It is
also written in exponential notation if the field width is too small
to express the number.
The body of the WRITE_DATA procedure can be written in the
following manner with the same results:
WRITELN(F,S) ;
WRITELN(F,I:4, 45.6789 : 9 : 4);
Referring to Listing 7-3, note that if a READLN were used on
line 31, the integer value 35 would be read properly because the
first blank terminates the number. However, the window variable
would advance past the real number to the end of the file. Then, if
you try to read the real number, you would only get the EOF.
STRINGS must always be read wi th a READLN because they are
terminated with end-of-line char'acters. If the data in the. file was
This t6 is t6 a t6 string 35 e6
the value returned for S would be the entire line, including the
ASCII 35.
Within the READ_DATA procedure, lines 20 and 21 write the data
to the console in the same format as in the file.
The main program stops after processing the call to READ DATA
on line 43. A CLOSE is not necessary because the data in TEXT'.. TST
is not altered from the last CLOSE ori that file;
Pascal/MT+ Reference Manual 7.5 Sequential I/O
7-10
Pascal/MT+ Reference Manual 7.5 Sequential I/O
7-11
Pascal/MT+'Reference Manual 7 •.6 Random Access I/O
7-12
Pascal/MT+ Reference Manual 7.6 Random Access I/O
7-13
Pascal/MT+ Reference Manual 7.6 Random Access I/O
1 0
2 0 PROGRAM RANDOM_DEMO:
3 0
4 0 TYPE
5 1 PERSON. REX:ORD
6 1 NAME : STRING:
7 1 ADDRESS : STRING:
8 1 END:
9 1
10 1 VAR
11 1 BF : FILE OF PERSON:
12 1 S : STRING;
13 1 I : INTEGER;
14 1 ERROR: BOOLEAN:
15 1 CH : CHAR:
16 1
17 1 EXTERNAL PROCEDURE @HLT:
18 1
19 1 PROCEDURE HALT:
20 1 BEGIN
21 2 CLOSE (BF ,I) :
22 2 @HLT
23 2 END:
24 1
25 1 PROCEDURE ERRCHK:
7.6 1 BEGIN
27 2 ERROR : = TRUE: (*DEFAULT*)
28 2 CASE IORESULT OF
29 2 o : BEGIN •
30 4 WRITELN (' SUCCESSFUL');
31 4 ERROR : = FALSE;
32 4 END;
33 3 1 WRITELN (' READ ING UNWRI'rl'EN DATA'):
34 3 2 WRITEIN ('CP/M ERROR');
35 3 3 WRITELN('SEEKING TO UNWRI'rl'EN EXTENT');
36 3 4 WRITELN ('CP/M ERROR'):
37 3 5 WRITELN('SEEK PAST PHYSICAL END OF DISK')
38 3 ELSE
39 3 WRITELN (' UNREX:OGNIZABLE ERROR CODE : ' ,IORESULT)
40 3 END;
41 2 END:
42 1 .
7-14
Pascal/MT+ Reference Manual 7.6 Random Access I/O
43 1 PROCEDURE READRECS;
44 1 BEGIN
45 2 WRITE('RECORD NUMBER? I);
46 2 READLN(I) :
47 2 SEEKREAD(BF,I):
48 2 ERRCHK;
49 2 IF ERROR THEN
50 2 EXIT:
51 2 WRITELN(BFA.NAME,'/',BFA.ADDRESS):
52 2 END:
53 1
54 1 PROCEDURE WRITERECS;
55 1 BEGIN
56 2 WRITE ( 'NAME? ' ) ;
57 2 READLN(S):
58 2 BFA.NAME := S;
59 2 WRITE('ADDRESS?t) ;
60 2 READLN(S) i
61 2 BFA.ADDRESS := S:
62 2 WRITE('RECORD NUMBER?'):
63 2 READLN (I) ;
64 2 SEEKWRITE(BF,I)~
65 2 ERRCHK:
66 2 END;
67 1
68 1 BEGIN
69 1 WRITE('CREATE FILE?') ;
70 1 READLN (S) ;
71 1 IF S ( 1] IN (' Y' , 'y '] THEN
72 1 BEGIN
73 2 ASSIGN(BF,'BlG.FIL') ;
74 2 REWRITE (BF) ;
75 2 CLOSE(BF,I) ;
76 2 END;
77 1 ASSIGN(BF,'BIG.FIL') ;
78 1 RESET (BF) ;
79 1 REPEAT
80 2 WRITE('R)EAD,WrRITE OR Q)UIT? I);
81 2 READ (CH) ;
82 2 WRITELN;
83 2 CASE CH OF
84 2 'R' " r' READRECS:
85 3 'WI, 'Wi WRITERECSi
86 3 'Q', 'q' HALT
87 3 ELSE
88 3 WRITELN('ENTER R, W OR Q ONLY')
89 3 END
90 2 UNTIL FALSE:
91 1 END.
End of Section 7
7-15
. Appendix A
Reserved Words and Predefined Indentifiers
End of Appendix A
A-l
Appendix B
Pascal/ MT+ Syntax
For example,
End of Appendix B
B-1
Appendix C
Differences From ISO Standard
End of Appendix C
0-1
Appendix D
Bibliography
End of Appendix 0
0-1
Index
Index-l
LONGINT, 3-4 6-53, 7-1, 7-2
ordinal, 3-5, 5-2, 5-4 fixed-point format, 2-3
pointer, 3-6 floating-point,
record, 3-10 format,
scalar, 3-1 real numbers, 3-5
sets, 3-9 FOR DOWNTO statement, 5-4
short, 3-4 FOR statement, 5-3
simple, 3-1, '5-4 formal parameters, 6-3
structured, 3-1, 3-7 FORWARD declaration, 6-2
subrange, 3-2 fragmentation, 6-37
WORD, 3-5 free variant, 3-12
decimal integer, 2-2 function, 1-1, 1-4, 6-1, 6-2,
declaration section, 1-1, 6-2 6-27
default length of a string, 3-8 FUNCTION,
definition section, 1-1 @BDOS, 6-69
device names, 6-14 @BDOS86, 6-67
DIV operator, 4-3 @CMD, 6-71
DO, @HERR, 6-74
reserved word, 5-3 @MRK, 6-75
dot operator, 3-11 @RLS, 6-76
dynamic allocation, 6-9, 6-24, ABS, 6-11
6-37,6-40 ADDR,6-12, 7-6
dynamic strings, 3-8, 3-9 ARCTAN, 6-13
CHR, 6-18
E CONCAT, 6-20
COPY, 6-21
element of a structure, 5-4 COS, 6-22
empty statement, 5-3 EOF, 6-25, 6-49, 6-53, 6-66,
end-of-file-indicator, 7-2 7-2, 7-6
end-of-line indicator, 7-9 EOLN, 6-25, 6-53, 7-6
environment, EXP, 6-28
CP/M, 6-54, 7-13 GNB, 6-66
exponentiation, 2-3, 4-3, 6-28, HI, 6-31
6-67, 7-10 IORESULT, ,6-34, 6-42, 6-52,
expres.ions, 4-1, 5-1, 5-4 7-13 .
external, LENGTH, 6-35
devices, 7-1 LN, 6-36
filename, 6-14 LO, 6-31
EXTERNAL FUNCTION INPORT_W, MAXAVAIL, 6-37
7-5 MEMAVAIL, 6-37
identifiers, 2":2 ODD, 6-41
EXTERNAL PROCEDURE OUTPRT W, ORO, 6-43
7-5 - POS, 6-46
PRED, 6-47
p RIM85, 6-54
ROUND, 6-55
FALSE, SHL, 6-57
BOOLEAN value, 3-3 SHR, 6-57
fields, SIN, 6-58
elements ofa record, 3-10 SIZEOF, 6-59
names ina record, 3-11 SQR, 6-60
fil~s, 3-1, 4-3, 5-1, 6-19 SQRT, 6-61
buffer, 7-1, 7-2, 7-3, 7-12 SUCC, 6~62
Information Block, 7-1, 7-9 SWAP, 6-31
variable, 6-14, 6-48, 6-50, TRUNC, 6-63
Index-2
TSTBIT, 6-64 members of a set, 4-6, 5-1
WNB, 6-66 miscellaneous functions, 6-10
G MOD operator, 4-3
MODEND,
garbage collection, 6-37 reserved word, 1-5
global, module, 1-4
declaration, 1-5 MODULE,
scope, 2-2 reserved word, 1-5
GOTO statement, 5-2, 5-5 mutually recursive procedures,
6-1
N
hardware ports, 7-5, 7-6
heap, 6-37, 6-54 named constant, 2-3, 3-6, 6-38,
hexadecimal integer, 2-2 6-65 -
named constants, 6-67
I native machine word, 3-3
natural logarithm, 6-28, 6-36
identifier, 1-3, 1-5, 2-1, 3-11 nested block, 1-1, 1-5,
IF statement, 5-6 procedure, 6-12
indexes for arrays, 3-4 variants, 3-11, 6-40
inner block, 1-5 nested WITH statements, 5-9
INP, nesting comments, 1.-6
predeclared array, 7-5 NIL,
input/output routines, 6-9 pointer value, 3-6
INTEGER, nonvariant record, 3-10
data type NOT,
literal, 2-2, 3-4 Boolean operator, 4-4
internal data representation, null pointer, 3-6
3-4, 3-5 numeric literal, 2-2
L o
label, ODD,
on a GOTO stat~ment, 5-5 pseu~o-function, 3-3
on CASE statements, -5-2 one's complement NOT, 4-5
least-significant bit, 3-3 operator, 4-1
l~ngth of identifiers, 2-1 arithmetic, 4-1
LENGTH, . assignment, 5-1
'predefined function, 3-8 Boolean, 4.;..2
local declaration, 1~5 logical, 4-2
logical expressions, 4-5 precedence, 4-1
logical operators, 4-2 relational, 4-2
AND, OR, one's complement NOT, set, 4-3
4-5 OR,
long· integer, 2-2 Boolean operator, 4-4
long integer. literal, 2-3 ORO,
LONGINT, pseudo-function, 3-3
data type, 3-4 ordinal,
literal data types, 3-2, 3-5
type. 3-2, 3-6, 3-9, 5-2, 5-4,
M v~iue, 3-6, 6-43, 6-47
ordinal value of FALSE, 3-3
mai.n program block, 1-1, 1-4 ordinal value of TRUE, 3-3
ma~n v~riant,3~12 OUT,
Index-3
predeclared array, 7-5 @HLT, 6-73
outer block, 1-5 ASSIGN, 6-14, 6-53, 7-2, 7-12
outermost block, 1-1 CHAIN, 6-17
over flow, 4-3 CLOSE, 6-19, 6- 52, 6- 56, 7-10,
over lays, 6-12 7-12, 7-13
CLOSEDEL, 6-19
p CLRBIT, 6-64
DELETE, 6- 23
packed structure, 3-3 DISPOSE, 6-24, 6-4:0
~ PACKED, EXIT, 6- 27
reserved word, 3-7 FILLCHAR, 6-29
parameters, GET, 6- 30, 6- 52, 6- 66, 7- 2,
variable, 6-4 7-3, 7-9
Pascal statements, 5-1 INLINE, 6-32
passing arrays to procedures, INSERT, 6-33
6-6 LWRITEHEX, 6-51
passing procedures and MOVE, 6-38, 6- 59
functions, 6-4 MOVELEFT, 6-38
pointer character, MOVERIGHT, 6-38
.... , 2-1, 3-6, 7-2 NEW, 6-40
pOinter type compatibility, 3-6 . OPEN, 6-42, 7-2
poin ter, PACK, 6-44
data type, 3-6 PAGE, 6-45
null, 3-6 PURGE, 6-48
precedence of operators, 4-1, 4-4 PUT, 6-49, 6-66, 7-2, 7-3, 7-9
predecessor of a scalar, 6-47 READ, 6-50, 7-3, 7-9
predeclared arrays, READ HEX , 6- 51
INP, OUT, 7~5 READLN, 6-50, 7-6; 7-9, 7-10
predefined data type, RESET, 6-14, 6-52, 7-2, 7-13
STRING, 3-8 REWRITE, 6-14, 6-53, 7-12
predefined function, RIM85, 6-54
LENGTH, 3-8 SEEKREAD, 6-56, 7-13
predefined functions and SEEKWRITE, 6-56, 7-13
procedures, SETBIT, 6-64
ar ithmetic, 6-8 UNPACK, 6-44
bit and byte man ipu1ation WAIT, 6-65
routines, 6-8 WRITE, 6-67, 7-3, 7-9
character array man ipulation WRITEHEX, 6- 51
routines, 6-8 WRITELN, 6-67, 7-7, 7-9
dynamic allocation routines, 6-8 pr09ram,
input/output routines, 6-8 heading, 1-2
string handling routines, 6-8 parame ter s, 1-2
transfer functions, 6-8 pseudo-function, 3-2
miscellaneous routines, 6-8 CHR, 3-3
predefined identifier, 1-3, 2-2 ODD, 3-3
predefined simple data types, ORO, 3-3·.
3-2 WORD, 3-3
pr in table charac ter, 2-3, 3-3 pseudo-functions, 6-21
procedure, 1-1, 1-4, 6-1, 6-27
procedure definition, 6-2 Q
procedure parameters,
ac tual, formal, 6- 3 quotien t, 4- 3.
procedure-oriented language, 6-1
PROCEDURE,
@ERR, 6-72
Index-4
R CASE, 5-2
compound, 5-1
random access I/O, 6-56, 7-1, 7-13 empty, 5-3
random record number, 7-13 FOR, 5-3
real number, 2-2, 3-5 FOR DOWNTO, 5-4
real-number literal, 2-3 GOTO, 5-2, 5- 5
record, IF, 5-6
da ta type, 3-10 Pascal, 5-1
recursive procedures, 6-1 REPEAT, 5-7
redirected I/O, 7-5, 7-6 WHILE, 5-8
relational operators, 4-2, 4-3 WITH, 5-8
relational operators on sets str ing,
IN, =, < >, < =, >= , 4- 6 handling routines," 6-10
r-emainder, 4-3 indexing, 3-8
REPEAT statement, 5-7 literal, 2-3, 3-9, 6-20
reserved word PACKED, 3-7 s ta tic, 2- 9 , 3- 9
reserved word, zero-leng th ,6- 20
DO, 5-3 STRING,
----- PACKED, 3-7 predefined data type, 3-8
reserved words, 2-2 strong type check, 3-2, 3-6
run-time entry points, 2-2" structured data types, 3-1
arrays, records, sets, files,
s 3-7
structured type, 6-67, 7-1
scalar data type, 3-1, 5-4 subrange, 3-5, 3-6
scalar type, 6-43, 6-62 subrapge data types, 3-2, 3-9,
scientific tlotation, 2-3 5-1, 6-62, 6-67
scope, 1-5, 2-2, 5-5, 6-12 successor of a scalar, 6-62
global, 1-5 syntax, 5-1
local, 1-5
of a CASE sta temen t, 5- 2
of a control variable, 5-4
semicolon TEXT file, 6-14, 6-50, 6-67, 7-9
as a valid sta temen t, as a transfer functions, 6-10
statement'separator, 5-3 TRUE,
statement separator, 1-4 BOOLEAN value, 3-3
sequential I/O, 7-1, 7-9 FALSE, Boolean values, 5-6, 5=7,
set constructor, 4-5 5-8
set expressions, 4-3, 4-5 type check ing, 3- 2, 6- 38, 6- 50
set opera tions, type conversion, 3-2
un ion, in te·r sect ion, type conversion functions,
difference, 3-9 FUNCTION SHORT, 3- 4
set operators, 3-9, 4-3 FUNCTION LONG, 3-4
. +, *, - . pp, 4- 6 FUNCTION XLONG, 3-4
set type d~finition, 3-9 type convers ion opera tor, 3- 2
sets, type definition, 3-1
data type, 3-9 nonvar ian t record, 3-11
short data type, 3-4 ·variant record, 3-12
!) imp le da ta type, 3-1, 5- 4
simple type, 4-3, 7-1
sine of an angle, 6-58
square root of a number, 6-61
statements
assignment, 5-1
statements, 1-4
Index-5
u
up-level reference, 1-5
user-defined ordinal type, 3-5,
6-59
user-defined ordinal types,
6-12
v
value parameters, 6-3
variable,
address, 6-12
allocation of space, 3-1
declaration, 3-1
pa r a me te r , 6- 3
variant record, 3-10, 6-40
w
weak type check ing, 3-6
WHILE statement, 5-8
window variable, 6-25, 6-30,
6- 49 , 6- 5 2 , 6- 56, 7 -1, 7 - 9
WITH sta temen to' 5-8
WORD,
da ta type, 3- 5
pseudo-funct:on, 3-3
Index-6
Reader Comment Form
We welcome your comments and suggestions. They help us provide you with better
product documentation. .
2. What suggestions do you have for improving this manual? What information
is missing or incomplete? Where are examples needed?
3. Did you find errors in this manual? (Specify section and page number.)
NO POSTAGE
NECESSARY
IF MAILED IN THE
UNITED STATES