\def\center{}
\def\newpage{}
\def\newline{\hfill\break}
\def\ul{}
\def\lt{$<$}
\def\gt{$>$}
\def\bslash{/}
\def\amp{\&}
\def\neq{\#}
{\center\rm
THE IMP-77 LANGUAGE
As implemented by
PETER S. ROBERTSON
DEPARTMENT OF COMPUTER SCIENCE
UNIVERSITY OF EDINBURGH
A REFERENCE MANUAL
first edition: DECEMBER 1977
this edition reproduced: FEBRUARY 2003}
\newpage
{\rm {\bf INTRODUCTION}}
{\rm IMP is an 'ALGOL-like' high-level language.   Relative to ALGOL 60, the language adds program
structuring, data structuring, event signalling, and string handling facilities, but removes (or retains in a
modified form) intrinsically inefficient features such as the ALGOL 60 name (substitution) parameter.
The language, based on Atlas Autocode, was originally designed as the implementation language for the
Edinburgh Multi-Access System - hence its name - but has since been used successfully for implementing
systems, teaching programming and as a general-purpose programming language on many different machines.
Two of the major design aims were:

1. The language should compile to efficient machine code.

2. The syntax of the language should be verbose rather than obscure.

The main disadvantage of IMP is that it is not currently in widespread use.
Most IMP systems provide comprehensive compile-time and run-time diagnostics, together with an option to
suppress generation of run-time checks when compiling tested programs.
Input/output facilities are provided through the external procedure mechanism and are therefore open-ended
and can be defined as required, though a standard set of procedures is supported. }
\newpage
{\rm {\bf INDEX }}
{\rm {\bf PROGRAM LAYOUT CONVENTIONS \hrule 1}}
\newline
{\rm NEWLINE \hrule 1}
\newline
{\rm SPACES \hrule 1}
\newline
{\rm LOWER CASE LETTERS \hrule 1}
\newline
{\rm QUOTES \hrule 1}
\newline
{\rm IDENTIFIERS \hrule 1}
\newline
{\rm STATEMENTS \hrule 2}
\newline
{\rm TERMINATION \hrule 2}
\newline
{\rm NULL STATEMENTS \hrule 2}
\newline
{\rm INSTRUCTIONS \hrule 2}
\newline
{\rm CONTINUATION \hrule 2}
\newline
{\rm LISTING CONTROL \hrule 3}
\newline
{\rm INCLUDE \hrule 3}
\newline
{\rm {\bf CONSTANTS \hrule 4}}
\newline
{\rm INTEGER CONSTANTS (FIXED POINT) \hrule 4}
\newline
{\rm REAL CONSTANTS (FLOATING POINT) \hrule 4}
\newline
{\rm STRING CONSTANTS \hrule 4}
\newline
{\rm {\bf EXPRESSIONS \hrule 6}}
\newline
{\rm ARITHMETIC EXPRESSIONS \hrule 6}
\newline
{\rm BIT-VECTOR EXPRESSIONS \hrule 6}
\newline
{\rm STRING EXPRESSIONS \hrule 7}
\newline
{\rm PRECEDENCE OF OPERATORS \hrule 7}
\newline
{\rm {\bf DECLARATIONS \hrule 8}}
\newline
{\rm {\bf RECORD ELEMENT SELECTION \hrule  11}}
\newline
{\rm {\bf OWN VARIABLES \hrule  12}}
\newline
{\rm {\bf INITIALISATION \hrule  12}}
\newline
{\rm {\bf CONSTANT IDENTIFIERS \hrule  12}}
\newline
{\rm {\bf ALIAS\hrule  14}}
\newline
{\rm {\bf ASSIGNMENT \hrule  15}}
\newline
{\rm {\bf RECORD ASSIGNMENT \hrule  15}}
\newline
{\rm {\bf STRING RESOLUTION \hrule  16}}
\newline
{\rm {\bf CONDITIONS \hrule  17}}
\newline
{\rm EVALUATION OF CONDITIONS \hrule  18}
\newline
{\rm {\bf CONDITIONAL STATEMENTS \hrule  19}}
\newline
{\rm {\bf ALTERNATIVE FORMS \hrule  19}}
\newline
{\rm {\bf REPETITION (LOOPS OR CYCLES) \hrule  21}}
\newline
{\rm {\bf SIMPLE FORMS OF LOOP \hrule  21}}
\newline
{\rm {\bf CYCLE CONTROL INSTRUCTIONS \hrule  22}}
\newline
\newpage
{\rm {\bf JOINING INSTRUCTIONS USING `AND' \hrule  22}}
\newline
{\rm {\bf BLOCK STRUCTURE \hrule  23}}
\newline
{\rm {\bf BEGIN BLOCKS \hrule  23}}
\newline
{\rm {\bf LOCAL AND GLOBAL VARIABLES\hrule  24}}
\newline
{\rm {\bf PROCEDURES \hrule  25}}
\newline
{\rm {\bf PARAMETERS \hrule  27}}
\newline
{\rm {\bf PROCEDURE PARAMETERS \hrule  28}}
\newline
{\rm {\bf GENERAL TYPE PARAMETERS\hrule  28}}
\newline
{\rm {\bf PROCEDURE SPECIFICATION \hrule  30}}
\newline
{\rm {\bf CONTROL TRANSFER INSTRUCTION S\hrule  31}}
\newline
{\rm LABELS AND JUMPS \hrule  31}
\newline
{\rm {\bf OTHER CONTROL TRANSFER INSTRUCTIONS \hrule  32}}
\newline
{\rm {\bf EXTERNAL LINKAGE \hrule  33}}
\newline
{\rm {\bf PREDEFINED PROCEDURES \hrule  34}}
\newline
{\rm {\bf EVENTS \hrule  35}}
\newline
{\rm {\bf APPENDIX 1 \hrule  38}}
\newline
{\rm {\bf COMPILER ERROR MESSAGES \hrule  38}}
\newline
{\rm {\bf APPENDIX 2 \hrule  39}}
\newline
{\rm {\bf SAMPLE PROGRAM L ISTING \hrule  39}}
\newline
{\rm {\bf APPENDIX 3 \hrule  40}}
\newline
{\rm {\bf DATA PRECISION SPECIFICATION \hrule  40}}
\newline
{\rm {\bf APPENDIX 4 \hrule  41}}
\newline
{\rm {\bf IMP KEYWORDS \hrule  41}}
\newline
{\rm {\bf APPENDIX 5 \hrule  42}}
\newline
{\rm {\bf COMPARISON WITH EMAS IMP \hrule  42}}
\newline
\newpage
{\rm 1}{\rm {\bf PROGRAM LAYOUT CONVENTIONS}}
{\rm An IMP program is a sequence of statements constructed using the ASCII character set extended with an
underlined alphabet.  Underlined letters, which are used to form keywords, are generated using the shift
character percent (%), which is defined as underlining all subsequent letters, the underlining being terminated
by any non-alphabetic character.
Hence the following statements are equivalent: }
{\tt %STRING (7)%ARRAY %NAME P
%STRING (7)%ARRAYNAME P }
{\rm and both represent: }
{\tt {\ul string}(7){\ul array} {\ul name} P }
{\rm In this manual, keywords are in lower case and underlined. }
{\rm {\bf {\it Newline}}}
{\rm The NEWLINE (or LINE BREAK) character is ASCII character 10 (LF). }
{\rm {\bf {\it Spaces}}}
{\rm Except when used to terminate keywords or when between quotes (see Quotes) spaces are ignored by the
compiler and may be used to improve the legibility of the program. }
{\rm {\bf {\it Lower Case Letters}}
\newline
{\rm Except when enclosed in quotes (see Quotes) lower case letters are equivalent to upper case letters. }
{\rm {\bf {\it Quotes  }}}
{\rm Several language constructions call for one or more characters to be enclosed in quotes; between quotes all
characters are significant and stand for themselves.
N.B.   Space, newline, and percent characters may appear between quotes and stand for space, newline, and
percent.
Two quote characters are used:

' -  symbol quote e.g. 'A'

' -  string quote e.g. 'FRED'

If it is required to include the delimiting quote within the text it must be represented by two consecutive
quotes: e.g. }
{\tt ''''}
{\rm - the symbol quote\newline
'A ''big'' dog' - a string of eleven characters
However, note: ''' and 'it's mine' }
{\rm {\bf {\it Identifiers  }}}
{\rm An identifier is a sequence of any number of letters and digits starting with a letter, e.g. }
{\tt MAX, X, CASE 1, CASE 2, CASE 2B }
{\rm All letters and digits are significant.
Except in the case of simple labels (see Control Transfer Instructions) all identifiers must be declared before
they may be used (see Declarations). }
\newpage
{\rm 2}{\rm {\bf {\it Statements  }}}
{\rm A STATEMENT is a sequence of atomic elements (keywords, constants, identifiers, etc.) arranged according
to the syntactic rules of IMP. }
{\rm {\bf {\it Termination  }}}
{\rm Every statement must be terminated by a newline or a semicolon (however, see Comments). }
{\rm {\bf {\it Null Statements  }}}
{\rm There are two types of null statement, both of which are ignored by the compiler.  They may be used to
improve the legibility of the program.
1. Redundant terminators, e.g. blank lines
2. Comments
A comment  is  any sequence of characters (the 'comment text') preceded by a comment mark and ending
with a newline character (note: a semicolon does not terminate a comment; it is included as part of the
comment text).
The comment marks are either the keyword comment or an exclamation mark (!); e.g. }
{\tt comment main loop starts here
! return here on error }
{\rm As a semicolon does not terminate a comment it is a simple matter to `comment out' sections of code. }
{\tt COMPUTE CASES
! if CASES {\lt} 0 start
!     DISPLAY DATA; NEWLINE; TIDY UP
!     stop
!finish
HANDLE CASES }
{\rm (Also see note on continuation) }
{\rm {\bf {\it Instructions  }}}
{\rm An instruction is any imperative statement which may be made conditional, and is either an assignment, a
Routine call, or a control transfer. }
{\rm {\bf {\it Continuation  }}}
{\rm A statement may extend over several physical lines provided that each line newline occurs after a comma, or is
preceded by the keyword c.  E.g. }
{\tt if X = Y then P = 1 c
 else P = 0 }
{\rm is exactly equivalent to: }
{\tt if X = Y then P = 1 else P = 0 }
{\rm Notes

1. A statement may have an unlimited number of continuations.

2. The line newline following c causes underlining to be terminated.

3. c is only permitted between complete atoms of the language; that is, it may not split keywords, constants,
etc.

4. %C between quotes stands for the two characters percent and C.

5. The effect of a comment mark is limited to one physical line (see Null statements). }
\newpage
{\rm   3}
{\rm {\bf {\it Listing Control  }}}
{\rm During the compilation of a program a line-numbered listing  is produced.  The statements list and endoflist
may be used respectively to enable or disable the listing for selected parts of a program.   The default is for
listing to be enabled. }
{\rm {\bf {\it Include  }}}
{\rm A file of statements (terminated by 'end of file') may be compiled into a program by giving the statement }
{\tt 'include' {file specification} }
{\rm where {file specification} is a string constant representing a (system dependent) file name.  E.g. }
{\tt include 'ECSC17.LISTVARS' }
{\rm Refer to the relevant appendix for details of system-dependent limitations on the use of include. }
\newpage
{\rm   4}
{\rm {\bf CONSTANTS  }
{\bf {\it Integer Constants (Fixed Point)}}
{\bf {\it  }}}
{\rm a) DECIMAL constants

A decimal constant is a sequence of decimal digits. For example:
7, 43, 2195, 0, 8, 100 000 000

b) NON-DECIMAL constants

The prefix {decimal constant}'\_' may be used to specify the base of the following constant.  The letters A,
B, ..., Z are used to represent the `digits' 10, 11, ..., 35
E.g.

2\_1010 - binary ten

8\_12  - octal ten

16\_a  - hexadecimal ten

c) CHARACTER constants

The ASCII code value of any character may be obtained by enclosing the character in single quotes. When
the required character is a single quote it must be represented by two consecutive single quotes.

Examples: 'A', 'a', '+', '0', ''', '''', ' ', '
'

{\bf  }
Note the last three examples, which represent the code values for single quote, space, and newline.
The predefined named constant NL may be used in place of the rather cumbersome form of a newline
character enclosed in quotes.

d) MULTI-CHARACTER constants
The previous form may be extended to pack together the codes for several characters to form a single
integer constant. }
{\tt 'over', 'Max', 'l+2', `*@@£' }
{\rm The exact nature of the packing and the maximum number of characters which may be packed are both
machine dependent.
An integer expression with operands which are constants may be used wherever an integer constant is required
(see Expressions). }
{\rm {\bf {\it Real Constants (Floating Point)}}
{\bf {\it  }}}
{\rm A  real  constant  is  a  sequence  of decimal digits optionally including one decimal point.
The constant may also be followed by a scaling factor of the form '@'{signed integer constant} meaning
'times ten to the power {signed integer constant}'. For example, the following real constants all have the same
value: }
{\tt 120.0, 120, l.2@2, l2@l, l200@-l }
{\rm Note that a decimal integer constant is a special case of a real constant. }
{\rm {\bf {\it String Constants}}
{\bf {\it  }}}
{\rm A string constant is a sequence of not more than 255 character enclosed in double quote characters  - a double
quote being represented inside a string constant by two consecutive double quotes.
E.g. "STARTING TIME", "x = y*4+z", "a ""red"" hood" }
\newpage
{\rm   5
a) 'A' is a string constant of one character.  'A' is a character (integer) constant.
b) The null string, a string of no characters, is permitted and is represented by two consecutive double quotes
(""). }
\newpage
{\rm   6}
{\rm {\bf EXPRESSIONS  }
{\bf {\it Arithmetic Expressions}}
{\bf {\it  }}}
{\rm An arithmetic expression is a sequence of operators and integer or real operands obeying the usual rules of
algebra.   An operand is either a constant, a variable, a function call, a map call, or a numerical expression
enclosed in parentheses (see Declarations and Procedures).

a) Integer Expressions

All the operands and operators in an integer expression must yield an integer value.
The operators available are:

+ addition

- subtraction or unary minus

* multiplication

// integer division (the remainder of the division, which is of the same sign as the dividend, is ignored).

{\bslash}{\bslash} integer exponentiation.   The second operand (the exponent) must be a non-negative integer.

b) Real Expressions

All  the  operands and operators in a real expression must yield real (or integer) results.  Where an operator
will take either real or integer operands (E.g. *) and the types of the given operands differ the integer
operand will be converted to a real value, otherwise the result of the operation will be of the same type as
the original operands.  The pre-defined real function FLOAT may be used to force the conversion of an
integer expression into a real expression.

The operators available are:

+ addition

- subtraction or unary minus

* multiplication

/ division

{\bf {\bslash}{\bslash}} real exponentiation
The modulus  or  absolute value  of  an  expression (integer  or  real) may be obtained by enclosing that
expression between vertical bars.
E.g. }
{\tt |X-Y| }
{\rm Notes

1. Unary minus is treated as '0 - ...'

2. Unary plus (+) is not accepted.

3. An expression may not contain two adjacent operators - they must be separated by parentheses.
E.g. 23*(-14)

4. Integer values will be converted to real where necessary, but real values will never be converted to
integer unless this is explicitly specified using the pre-defined functions INT or INTPT. }

{\rm {\bf {\it Bit-Vector Expressions}}
\newline
{\rm All operands must yield bit-vector (integer) values.   The operations are performed on a bit-by-bit basis using
the operators:

 {\amp} AND }
\newpage
{\rm   7

! INCLUSIVE OR

!! EXCLUSIVE OR

«  LEFT SHIFT (logical)

» RIGHT SHIFT (logical)

\ COMPLEMENT (unary not)

It is possible to mix integer and bit-vector expressions but the full implications of this may be machine
dependent. }
{\rm {\bf {\it String Expressions}}
{\bf {\it  }}}
{\rm All operands of a string expression must yield values of type string.   The only operator available is  '.' for
concatenation (joining together).  No sub-expressions in parentheses are permitted.
E.g. 'MR '.SURNAME }

{\rm {\bf {\it Precedence of operators}}
{\bf {\it  }}}

{\rm Highest:  1.  \ (unary not)

2.  {\bslash}, {\bslash}{\bslash}, {\lt}{\lt}, {\gt}{\gt}

3.  *, /, //, {\amp}

Lowest:

4.  +, - (unary and binary), !, !!

In general, sub-expressions with operators of equal precedence are evaluated from left to right.
The precedence rules may be over-ridden by means of parentheses.
Note: }

{\tt -1{\bslash}{\bslash}2 = -1

(-1){\bslash}{\bslash}2 = 1

2{\bslash}{\bslash}2{\bslash}{\bslash}3 = 4{\bslash}{\bslash}3 = 64 }

\newpage
{\rm   8}{\rm {\bf DECLARATIONS  }}

{\rm All identifiers (except simple labels) must be declared at the start of a block before they are used.   The scope
of an identifier is the rest of the block in which it is declared, including any blocks subsequently defined
therein (see Block Structure and note 3 on Labels and Jumps).
In the following discussion the phrase {type} has four variants: }

{\tt 1. 'integer'

2. 'real'

3. 'string' '(' {max} ')'

4. 'record' '(' {format} ')' }

{\rm and {max} is an integer constant in the range 1 {\lt}= max {\lt}= 255 defining the maximum number  of
characters which may be held in the string.
{fmt} defines  the  structure of  the record (see Records). }

{\rm Variables }

{\rm a) Simple Variables

{type} {idlist} }

{\tt integer J,K,COUNT

real PRESSURE

string (30) COUNTRY, TOWN

record (CARFM) MINI, ROVER }

{\rm Each variable is allocated an appropriate (machine dependent) amount of storage to hold a value of the
appropriate type.

b) Pointer Variables

{type} 'name' {idlist} }

{\tt integer name P

real name DATUM

string (15) name WHO,WHERE

record (CARFM) name CAR }

{\rm Each variable is allocated enough storage to hold a pointer to (i.e. the address of) a simple variable of the
specified type.

c) Array Pointer Variables

{type}'array' 'name' {idlist}

or

{type} 'name' 'array' 'name' {idlist} }

{\tt integer array name AN

real array name VALUES

string (20) array name NAMES, ADDRESSES

record (CARFM) array name MAKE

real name array name ANSWERS }

{\rm Each variable is allocated enough storage to hold a pointer to (i.e. the address of) an one-dimensional array
of the specified type.
... 'array' '(' {dim} ')' 'name' {idlist}
is provided for declaring pointers to multi-dimensional arrays.  E.g. }

{\tt real array (4) name SPACE TIME
integer name array (2) name LISTS }
\newpage
{\rm   9}
{\rm Arrays }
{\rm {type} 'array' {adefn} (',' {adefn})*
or
{type} 'name' 'array' {adefn} (',' {adefn})*
{adefn} ::= {idlist}'(' {pair} (',' {pair})* ')'
{pair} ::= {integer exprn} ':' {integer exprn} }
{\tt integer array A(1:l0) ,B,C(-4:LIMIT)
real array Q(l:J+K, l:J-K)
string (12) array CLASS(-7:16)
record (CARFM) array TABLE(LOWER:UPPER)
integer name array pointers(1:12) }
{\rm The bound pairs, {pair}, are evaluated and the required amount of storage is allocated to each
identifier.
Note
1. In each bound pair the value of the first expression (lower bound) must be less than or equal to the value of
the second expression (upper bound).
2. The  number  of bound pairs (the dimension of the array) usually may not exceed six,  but  this  is
implementation dependent. }
{\rm Records }
{\rm A record is a named collection of variables, arrays and records.   The components (elements) of a record may
be any of the forms discussed in (1) and (2) above, with the following limitations:
a) Arrays must be one-dimensional and have constant bounds.
b) A record may not contain a simple record (or a record arrays) of its own format.   However it may contain
record pointer variables of its own format.
There are three ways to specify formats:
1.  Explicit definition
record '(' {declaration list} ') ... }
{\tt record (integer X, Y, Z) R
record (real P, real name Q) name S, T
record (real array A(1:5), real V) array X(1:4) }
{\rm 2.  Using a format identifier
record format {id} '(' {declaration list} ')'
record '(' {id} ')' ... }
{\tt record format F (integer X, record(F)name LINK)
record (F) HEAD
record (F) array CELL(1:15) }
{\rm 3.   Using a previously declared record as a format definition. }
{\tt record (integer ONE, TWO, THREE) R
record (like R) S, T }
{\rm Note
1. Within a format each identifier must be unique but will not clash with any identifiers outwith that format
(see Block Structure for a discussion of local and global identifiers). }
\newpage
{\rm   10
2. When space is allocated to a record variable the elements are laid out in the order in which they were
declared.  However see the relevant implementation notes for machine-dependent alignment
considerations. }
\newpage
{\rm   11}
{\rm {\bf RECORD ELEMENT SELECTION  }}
{\rm Selection of a specific element from a record is achieved by following the record identifier by:
'\_'{element id}
E.g. given the declarations: }
{\tt record format F(integer x, record(F) name LINK)
record (F) R }
{\rm some valid references to variables are:
 R   - a record of format F
 R\_X   - an integer
 R\_LINK   - a pointer to a record of format F
 R\_LINK\_X  - an integer
 R\_LINK\_LINK  - a pointer to a record of format F
R\_LINK\_LINK\_ X - an integer }
\newpage
{\rm   12}
{\rm {\bf OWN VARIABLES  }}
{\rm Each variable declared in a block is allocated storage when that block is entered, the storage being returned
(released) when the block is left.  This means that variables (and the values in them) are lost between traverses
of the block.
If, however, the prefix own is applied to a declaration the variables are allocated statically and so retain their
values when the block is not being executed  (see Procedures).  The scope of the identifier is unchanged.
Own arrays must be one-dimensional and have constant bounds. }
{\rm {\bf INITIALISATION }}
{\rm Own variables may be given initial values (effectively before the program starts execution); if no initial value is
specified the content of an own variable is undefined. }
{\tt own integer A, B=4, C=-1
! the initial value in A is undefined
own real R=1.234@-5
own string(7) WHO='Anon' }
{\rm In the case of own name and own array name variables the initial value (if present) represents the absolute
address of respectively the initial variable to be pointed at or the (possibly hypothetical) `zeroth element' of an
array. }
{\tt own integer name CLOCK==72
own integer array name SAVE AREA == 16 }
{\rm This is highly machine dependent.
If an own or constant array is to be initia1ised, every element in the array must be given a value.   In order to
simplify this, each initial value may be followed by a repetition count in parentheses, and a star,  (*), may be
used to represent the number of remaining elements in the array.   For convenience a repetition count of zero is
permitted and means that the initialising constant is to be ignored.   For example the following declarations are
all equivalent: }
{\tt own integer array A(2:5) = 7,7,7,7
own integer array A(2:5) = 7(4)
own integer array A(2:5) - 7(*) }
{\rm The list of constants may extend over several physical lines without the need for a continuation mark if each
line ends with a comma; a line newline is also allowed after the equals sign. }
{\tt own string(3)array MONTH(1:12) =
'JAN', 'FEB', 'MAR',
'APR', 'MAY', 'JUN',
'JUL', 'AUG', 'SEP',
'OCT', 'NOV', 'DEC' }
{\rm Any number of null statements may be placed between the lines of constants. }
{\tt own integer array VALUE(1:50) =;  ! TAG VALUES
    1, 2, 3, 0(7),
! ------type 1------
   11, 22, 33, 4(3), 55(4),
!------type 2------------
   111, 222, 3, -1(5),
! ------types 3 {\amp} 4-----------
    -2(*);            ! ALL THE REST }
{\rm {\bf CONSTANT IDENTIFIERS  }}
{\rm The prefix constant may replace own to indicate that the initial value can never change.  A constant integer
may be used wherever an integer constant is required. }
\newpage
{\rm   13}
{\tt constant integer MAX = 17
constant real PI = 3.14159
constant string (7) VERSION = 'vsn:1.6'
constant integer array VAL(1:MAX) = 1,6,9,-1(*) }
{\rm Constant pointer variables may be declared but are highly machine and system dependent. }
{\tt constant integer name STATUS REG == 160
constant integer array name WORD == 0 }
{\rm Note: constant pointers are effectively simple variables of the appropriate type located at the specified
(absolute) address.
The keyword constant may be abbreviated to const. }
\newpage
{\rm   14}
{\rm {\bf ALIAS }}
{\rm In many programs it is convenient to be able to interpret a variable in several different ways.  This may be
achieved by declaring a new variable to be an alias of an existing variable.  Any identifier in the identifier list
of a declaration may be followed by 'alias' {var} where {var} is a variable of any type.  The effect is that the
new identifier is allocated storage at the same address in memory as the variable of which it is an alias. }
{\tt real VALUE
integer REAL BIT PATTERN alias VALUE
record (F1) name N1
record (F2) name N2 alias N1
integer X, Y, Z
integer array A alias X (1:3) }
{\rm Note: }
{\tt integer name N
integer X alias N
integer Y alias INTEGER(ADDR(N))
integer J
J = X;  ! same as J = ADDR(N)
J = Y;  ! same as J = N }
{\rm An array can be mapped onto an absolutely addressed area of store by means of the built-in map INTEGER
(see Predefined Procedures) }
{\tt integer array SEGMENT alias INTEGER(SEGAD) (1:LIMIT) }
{\rm Note that this feature is highly machine dependent.
[ABD note:  as far as I understand it, much of the preceding is simply not true for real IMP-77 compilers.  The
real use of alias is in renaming external routines] }
\newpage
{\rm   15}
{\rm {\bf ASSIGNMENT  }}
{\rm There are three forms of assignment:
1. {variable} '=' {expression} }
{\tt X = Y
A(P) = A(P)+l
Y = BIT{\lt}{\lt}12
PERSON = INITIALS.SURNAME }
{\rm The expression is evaluated and the resulting value is stored in the given variable.  The expression may be of
type integer, real, or string, and the variable must be of the corresponding type; in the case of a real variable an
integer expression will have its result converted to real before the assignment.
Valid types of assignment are:
{integer variable} '=' {integer expression}
{real variable}      '=' {real expression}
{real variable}      '=' {integer expression}
{string variable}   '=' {string expression}
2. {pointer variable} '==' {reference to a variable}
The pointer variable is dynamically made equivalent to the given variable; the types of both sides of the
assignment must be identical - this includes the formats of records.
The assignment may be thought of as the assignment of the address of the variable to the pointer.
Once equivalenced the pointer variable may be used as an alternative to the variable. }
{\tt integer name N
integer J
integer array A(l:6)
integer name array PT(2:12)
J = 1
N == A(J);  ! N IS NOW EQUIVALENT TO A(1)
J = 2;   ! N HAS NOT CHANGED
N = 0;   ! SAME AS A(1) = 0
PT(J) == A(4) }
{\rm N.B. Extreme care should be taken if variables declared in different blocks are to be equivalenced as it is
possible to leave a pointer referencing a variable which no longer exists (see Block Structure).
3.  {variable} '{\lt}-' {expression}
This is similar to 1. above except that the value of the expression will be truncated if necessary (see Data
Precision Specification).
E.g. }
{\tt string(4) S
S =  '12345'; ! fails CAPACITY EXCEEDED at run-time
S {\lt}- '12345'; ! will assign '1234' to S. }
{\rm {\bf RECORD ASSIGNMENT  }}
{\rm Two extra assignments exist for records:
1. {record variable} '=' {record variable}
The right-hand record is copied bit by bit into the left-hand record.  The formats of the two records must be the
same.
2. {record variable} '=' 0
Each bit of the record is set to zero. }
\newpage
{\rm   16}
{\rm {\bf STRING RESOLUTION  }}
{\rm The contents of a string variable may be searched for a sub-string and decomposed accordingly.
The format of a resolution is:
{string var} '-{\gt}' {string var}'.('{string exp}').'{string var}
where either the second string variable, the third, or both may be omitted. }
{\tt S  -{\gt} T.(',').U
TITLE(J)-{\gt} ('Sir').REST
WHO -{\gt} WHO. (LETTERS.'B.Sc.')
S  -{\gt} ('HELLO'.T) }
{\rm The string expression is evaluated and the first variable is searched from left to right to find that string of
characters.  The string to the left of the sub-string so found is assigned to the second variable and the string to
the right is assigned to the third.
The resolution is deemed to have failed if the required sub-string is not found or either of the second or third
variables has been omitted and would have been assigned a non-null string.
For example, the following resolutions all fail if the string variable S contains the string 'ABCDEFG'. }
{\tt S -{\gt} T.('H').U
S -{\gt} ('CD').U
S -{\gt} T.('EF')
S -{\gt} ('ABCDEF') }
{\rm and the following all succeed: }
{\tt S -{\gt} T.('CDE').U
S -{\gt} ('ABC').U
S -{\gt} T.('G')
S -{\gt} ('ABCDEFG') }
{\rm A resolution may occur in two contexts:
1.  as an instruction, in which case failure of the resolution causes an event to be signalled (see Events) }
{\tt S -{\gt} A.(WANTED).B;  S = A.B }
{\rm 2.  as a simple condition (see Conditions), in which case failure deems the simple condition false and success
deems it true; in the latter case the resolution is performed and the necessary assignments are made. }
{\tt if WHO -{\gt} ('SIR ').WHO then KNIGHT = 1 }
\newpage
{\rm   17}
{\rm {\bf CONDITIONS  }}
{\rm Conditional statements are specified using the phrase {condition}, which is defined as:
{condition} ::= {simple cond} ('and' {simple cond})*, {simple cond} ('or' {simple cond})*
where {simple cond} has seven forms:-
1. {expression} {comp} {expression}
{comp} ::=
'='  - is equal to
'{\neq}', '\='  - is not equal to
'{\lt}'  - is less than
'{\lt}='  - is less than or equal to
'{\gt}'  - is greater than
'{\gt}='  - is greater than or equal to
The given expressions are evaluated and compared.   The simple condition is true or false depending on the
validity of the relation specified by the comparator.  Both expressions must yield values of the same type.
2. {expression} {comp} {expression} {comp} {expression}
This form of simple condition may be thought of as a contraction of the form:
({x1}{comp1}{x2} 'and' {x2}{comp2}{x3})
except that the middle expression {x2} is only evaluated once.   Note that the third expression is not evaluated
unless the condition specified by the first two expressions is true.
Such a simple condition is frequently used to check for a range of values, E.g. }
{\tt 0 {\lt}= VALUE {\lt}=100 }
{\rm 3. {reference to a variable} '==' {reference to a variable}
The two variables, which must be of identical  type, are compared for equivalence, that is their addresses are
compared.
Note that the address of a pointer variable is the address of the variable to which it is equivalent.
4. {predicate call} - see Procedures
The given predicate is called and the simple condition is true or false depending on whether the exit from the
predicate was performed using true or false respectively.
5. {resolution} - see String Resolution
The resolution is attempted.   If it fails the simple condition is deemed false, otherwise the resolution is
performed and the condition is deemed true.
Note that this form of simple condition has a side effect if the simple condition is true!
6. '(' [condition] ')'
This form of simple condition is provided to enable the use of both and and or in a condition.  The connectives
and and or may  not  appear  in the  same  condition unless separated by levels of parentheses. E.g. }
{\tt A=0 or (B=l and C=2) or D=3 }
{\rm 7. 'not' {simple cond}
The given simple condition is evaluated and its truth is negated.  E.g. the following simple conditions are
exactly equivalent: }
{\tt A {\neq} 0
not A = 0 }
\newpage
{\rm   18}
{\rm {\bf {\it Evaluation of conditions}}
{\bf {\it  }}}
{\rm The evaluation of a condition proceeds from left to right, simple condition by simple condition, terminating as
soon as the inevitable result of the condition is known.
For example, considering the condition: }
{\tt A {\neq} 0 and B//A {\neq} C }
{\rm If the variable A has the value zero the condition will be deemed false without attempting the evaluation of
'B//A {\neq} C'. }
\newpage
{\rm   19}
{\rm {\bf CONDITIONAL STATEMENTS  }}
{\rm The general form of a conditional statement is: }
{\tt if {condition} start
 ! STATEMENTS TO BE EXECUTED IF
 ! {condition} IS TRUE
finish else start
 ! STATEMENTS TO BE EXECUTED IF
 ! {condition} IS FALSE
finish }
{\rm If start-finish brackets enclose one instruction only, that part may be reduced to: }
{\tt if {condition} then {instruction} else start }
{\rm or }
{\tt finish else {instruction} }
{\rm or in the simplest case: }
{\tt if {condition} then {instruction} else {instruction} }
{\rm If nothing is to be done specifically when the condition is false the else part may be omitted. }
{\tt if {condition} start
 ! STATEMENTS TO BE EXECUTED IF
 ! {condition} IS TRUE
finish }
{\rm or }
{\tt if {condition} then {instruction} }
{\rm start-finish groups may be nested to any depth.
 }
{\rm {\bf ALTERNATIVE FORMS  }}
{\rm 1. A conditional statement of the form: }
{\tt if {condition} then {instruction} }
{\rm has the same effect if rewritten in the more natural form: }
{\tt {instruction} if {condition}
X = ERROR if X {\gt} LIMIT }
{\rm 2. The keyword if may always be replaced by unless with the effect of negating the whole of the condition.
For example, the following two statements are equivalent: }
{\tt if X = 0 then Y = 1 else Y = -1
unless X = 0 then Y = -1 else Y = 1 }
{\rm 3. The statement 'finish else start' may be abbreviated to 'else'. }
{\tt if X = 0 start
   FLAG = 1; COUNT = 0
else
   FLAG = 2; COUNT = -1
finish }
{\rm 4. The else part of any conditional group may be replaced by another complete conditional group, treated as
though it were a single instruction.
For example: }
\newpage
{\rm   20}
{\tt if A = 0 start
   P = 1; Q = 2
finish else start
   if A {\lt} 0 start
      P = -1; Q = 2
   finish else start
      P = 1; Q = -2
   finish
finish }
{\rm may be rewritten: }
{\tt if A = 0 start
   P = 1; Q = 2
finish else if A {\lt} 0 start
   P = -1; Q = 2
finish else start
   P = 1; Q = -2
finish }
\newline
\newpage
{\rm   21}
{\rm {\bf REPETITION (LOOPS OR CYCLES)  }}
{\rm a.   Indefinite Repetition
A group of statements may be repeated indefinitely by enclosing them between the statements 'cycle' and
'repeat'. }
{\tt cycle
   GET DATA
   PROCESS DATA
repeat }
{\rm Subsequently the group of statements between cycle and repeat will be referred to as the cycle body.
b.   Conditional Repetition
1.   while {condition} cycle
Before each execution of the cycle body the specified condition is tested.  If the condition is true the cycle
body is executed; otherwise control is passed to the statement following the matching repeat.
2.   for {control} '=' {init} ',' {inc} ',' {final} cycle
where
{control} ::= {integer variable} - CONTROL VARIABLE
{init}  ::= {integer expression} - INITIAL VALUE
{inc}  ::= {integer expression} - INCREMENT
{final}  ::= {integer expression} - FINAL VALUE
On each entry to the cycle the address of the control variable and the value of the three expression are
evaluated and saved; thus the cycle body cannot change them.  The control variable is assigned the value
'{init}-{inc}'.
The value in the control variable is compared with the value of {final}.  If they are equal control is passed
to the statement following the matching repeat, otherwise the value {inc} is added to the control variable
and the cycle body is executed.
On normal exit from the cycle the control variable will contain the value {final}, however see exit.
Note: the effects of altering the control variable within the cycle body are undefined.
3.   The final form of conditional cycle is: }
{\tt cycle
! CYCLE BODY
repeat until {condition} }
{\rm In this construction, the cycle body is always executed at least once.  The loop may also be qualified by a
while or for as defined above.  For example: }
{\tt while {condition} cycle
! CYCLE BODY
repeat until {condition} }
{\rm cycle-repeat groups may be nested to any depth }
{\rm {\bf SIMPLE FORMS OF LOOP  }}
{\rm If the cycle body comprises only one instruction the loop may be rewritten in the form: }
{\tt {instruction} {loop clause} }
{\rm i.e. }
{\tt {instruction} 'while' {condition}
{instruction} 'for' {control} '=' {init}','{inc}','{final}
{instruction} 'until' {condition} }
\newpage
{\rm   22
For example }
{\tt A(J) = 0 for J = 1, 1, 20
READSYMBOL(S) until S = NL
SKIPSYMBOL while NEXTSYMBOL = ' ' }
\newline
{\rm {\bf  CYCLE CONTROL INSTRUCTIONS  }}
{\rm Two instructions are provided to control the execution of a cycle from within the cycle body.
1. exit - causes the cycle to be terminated and control to be passed to the statement following the matching
repeat.  In the case of a for loop the control variable will retain the value it contained immediately prior to
the exit.
2. continue - causes control to be passed to the repeat (and any associated until condition) of the current loop. }
{\rm {\bf JOINING INSTRUCTIONS USING `AND'  }}
{\rm Several simple instructions may be joined together using and to form a more complex instruction.  The
execution of such an instruction is achieved by executing each of the component simple instructions in the
order given.  This construction is used to simplify small start-finish or cycle-repeat groups.
E.g. }
{\tt if X = 0 start
 P = 1; Q = 1
finish }
{\rm may be rewritten }
{\tt P = 1 and Q = 1 if X = 0 }
{\rm or }
{\tt if X = 0 then P = 1 and Q = 1 }
{\rm
 }
\newpage
{\rm   23}
{\rm {\bf BLOCK STRUCTURE  }}
{\rm An IMP program is constructed using one or more blocks.  Blocks may be nested one within another. The
depth to which this nesting may be performed is implementation dependent. }
{\rm {\bf BEGIN BLOCKS  }}
{\rm The simplest type of block is enclosed between the statements 'begin' and 'end' and is referred to as a begin
block. If the block is the outermost block of a complete program it must be terminated by the statement 'end of
program'.
For example, a complete program might take the form: }
{\tt begin
 integer COUNT, LIMIT
 .
 begin
  real SUM
  .
 end
 .
end of program  }
{\rm A begin block is entered by executing the begin and is left by passing through the end to the following
statement. The main uses of begin blocks are to declare arrays with bounds calculated at run-tine, and to
enable the re-use of space taken up by large arrays which are only needed for part of the program.  }
{\tt begin
 integer UPPER
 UPPER = . . . . ! CALCULATE VALUE FOR UPPER BOUND
 begin
  integer array CASES(1:UPPER)
  .
 end
end of program
begin
.
 begin
  integer array TEMP(1:10000)
  .
 end
 begin
  real array WORK AREA(1:11000)
  .
 end
end of program }
\newpage
{\rm   24}
{\rm {\bf LOCAL AND GLOBAL VAR IABLES  }}
{\rm An identifier is described as being local to a block if it was declared at the head of that block. Any identifiers
which are in scope but which were not declared in the block in question are referred to as being global to the
block.
Clearly identifiers may be local to only one block but may be global to many. }
{\tt begin;   !  START OF OUTER BLOCK
 integer X;  !  X IS LOCAL TO THIS BLOCK
 begin;  !  START OF INNER BLOCK
  integer Y; !  Y IS LOCAL TO THIS BLOCK
  X = 0; !  X IS GLOBAL TO THIS BLOCK
 end;   !  END OF INNER BLOCK
end;   !  END OF OUTER BLOCK }
{\rm Identifiers may always be redeclared in any block to which they are global ­ the local incarnation taking
precedence over the global one. }
{\tt begin
 integer X
 begin
  integer X
  X = 0; ! USES THE X OF THE PREVIOUS LINE
 end
end }
{\rm An attempt to redeclare a local variable will be faulted by the compiler.
On entry to a block, space from the stack is allocated to any local variables, and when the block is left the
space is returned to the stack (but see Own Variables).  }
\newpage
{\rm   25}
{\rm {\bf PROCEDURES  }}
{\rm A procedure is a block which has an associated identifier; a complete procedure block may be considered as the
declaration of the procedure identifier.
Unlike begin blocks, procedures are not entered simply by reaching their first statement (this results in control
being transferred to the statement following the matching end). Instead procedures are activated when they are
called by giving the procedure identifier in a context determined by the type of procedure.
The effect of a call is to suspend t h e current flow of control and to pass control to the procedure. When the
procedure terminates, the previous flow of control is resumed.
There are four forms of procedure, the exact form required being specified by the first statement of the block.
The phrase {param def}? stands for the optional parameter definition and will be described later (see
Parameters).
1. routine {id}{param def}?
When a routine is called its statements are executed until either the end is reached or the instruction return is
executed. This causes the routine to terminate and the previous flow of control to be resumed. }
{\tt integer X, Y
routine CONVERT
 if X {\lt} Y start
  X = X+Y
 finish else start
  X = X-Y
 finish
end
. .
CONVERT
. .
CONVERT unless X = 0  }
{\rm 2.  {type} function {id} {param def}?
A function is a procedure which calculates a value of the specified type (integer, real, string, or record) and
may be used wherever an operand of the specified type is required.
When a function is called its statements are executed until an instruction of the form:  }
{\tt result '=' {expression} }
{\rm is executed. This causes the function to terminate, returning the value of the expression. }
{\tt integer X,Y,Z
integer function SUM
 result = X+Y
end
Z = SUM;  ! SAME EFFECT AS 'Z=X+Y'  }
{\rm The keyword function may be abbreviated to fn.
3.  {type} map {id}{param def}?
A map is a procedure which calculates a reference to a variable of the specified type (integer, real, string or
record), and may be used wherever a variable of the specified type is required.
When a map is called its statements are executed until an instruction of the form:  }
{\tt result '==' {variable reference} }
{\rm is executed. This causes the map to terminate, returning a reference to (i.e. the address of) the given variable.
E.g. }
\newpage
{\rm   26}
{\tt integer X,Y
integer map MIN
 if x {\lt} Y then result == X else result == Y
end
MIN = 0
! THE ABOVE STATEMENT IS EXACTLY EQUIVALENT TO:
! if X {\lt} Y then X = 0 else Y =0 }
{\rm 4.  predicate {id} {param def}?
A predicate is a procedure which tests the validity of an hypothesis and then returns, being either true or false.
Predicates may be used wherever a simple condition is required.
When a predicate is called its statements are executed until either of the instructions 'true' or 'false' is
executed. This causes the predicate to terminate accordingly.
Note that a predicate does not return any value.
E.g.  }
{\tt integer N
predicate SINGLE DIGIT
true if 0 {\lt}= N {\lt}= 9
false
end
N = N//10 unless SINGLE DIGIT  }
\newline
{\rm Notes }
{\rm a. A routine may terminate by reaching end; all other types of procedure must not be able to reach their end,
otherwise the compiler will report a fault.
b. Procedures may be nested within any form of block.
c. Procedures may be recursive, that is, they may call themselves. }
\newpage
{\rm   27}
{\rm {\bf PARAMETERS  }}
{\rm In the previous discussion about procedures the phrase {param def}? was used. This stands for an optional
parameter list definition.  }
{\tt {param def} ::= '(' {dec list} ')'  }
{\rm where {dec list} is a list of declarations defining the FORMAL PARAMETERS. The declarations may be of
any data type except array - arrays may only be passed to a procedure as array name parameters.
E.g. }
{\tt routine SWOP(integer name P, Q)
integer function MAX(integer array name A, integer F, T)
predicate EQUIV(record(FM)name LEFT, RIGHT) }
{\rm Parameters are identical to any local variables declared inside the procedure, except that the parameters are
initialised each time the procedure is called.
When a procedure is called a list of ACTUAL PARAMETERS must be supplied which must match the formal
parameters exactly in number, order, and type. Parameters are effectively assigned using '==' for those passed
by name. (E.g. integer name, real array name) and using '=' for those passed by value (E.g. string(10),
integer).
For example assuming the declarations:  }
{\tt integer L, M, N
real R
integer array V(-7:7)
record (FM) ONE, TWO }
{\rm valid calls on the procedures mentioned in the previous example are :  }
{\tt SWOP(L, M)
SWOP(V(L), V(M))
N = MAX(V, -1, 0)
M = MAX(V, L, 7)
N = M if EQUIV(ONE, TWO) }
{\rm N.B. IMP name type parameters are called by reference and not by substitution (c.f. ALGOL 60).  }
\newpage
{\rm   28}
{\rm {\bf PROCEDURE PARAMETERS  }}
{\rm In addition to being able to pass variables to procedures it is possible to pass procedures as parameters. This is
achieved by using the procedure heading as the 'declaration' of the formal parameter.
E.g. }
{\tt routine TRY(routine R(integer X))
 integer J
 R(J) for J = 1, 1, 10
end  }
{\rm The routine TRY may now be called with a single parameter which must be the name of a routine which has
one integer parameter.  In this context the formal parameter names used to specify the parameters of a
procedure parameter are otherwise ignored.
Note : If the routine TRY is itself to be passed as a parameter the heading of the receiving routine would be
something like:  }
{\tt routine CHECK(routine P(routine Q(integer R)))  }
{\rm and the call would be:  }
{\tt CHECK (TRY)  }
{\rm {\bf GENERAL TYPE PARAMETERS  }}
{\rm In several situations it is useful to be able to pass to a procedure a reference to any type of variable. This is
done
by specifying an untyped name parameter.
E.g. }
{\tt routine WORK(name REF)  }
{\rm Such a parameter is intended for system-dependent interface procedures and has severely limited uses. In
particular it may only be passed on to another procedure requiring an untyped name parameter.
An example of the use of such a parameter is in the pre-declared READ routine which will accept an integer,
real, or string parameter.
E.g. }
{\tt integer Y
real Y
string (15) Z
READ(X);  READ(Y);  READ(Z)  }
\newline
\newpage
{\rm   29
The following is a complete list of formal parameter declarators:  }
{\tt integer
real
string({max})
integer name
real name
string({max})name
integer array name
real array name  string({max})array name
integer fn
real fn
string({max})fn
integer function
real function
string({max})function
integer map
real map
string({max})map
integer name array name
real name array name
string({max})name array name
record({fm})
record({fm})name
record({fm})array name
record({fm})name array name
record({fm})fn
record({fm})function
record({fm})map
routine
predicate
name
 }
{\rm {\bf  }}
\newpage
{\rm   30}
{\rm {\bf PROCEDURE SPECIFICATION  }}
{\rm In several situations it is necessary to use a procedure before it is possible (or desirable) to define it. For
example, where two or more procedures call each other (mutual recursion) or where a procedure is to be
defined externally (see External Linkage).
As all procedure identifiers must be declared before being used a procedure specification statement is
introduced.
This takes the form of the normal procedure heading with the keyword spec inserted before the procedure
identifier.
E.g. }
{\tt routine spec MAX(real SIZE)  }
{\rm This has no effect other than declaring the identifier to be a procedure of the specified type which takes the
given parameters. Except in the case of external procedure specifications the procedure must be defined later
on in the same block (but not any blocks defined therein).
For example: }
{\tt routine spec B(integer X)
routine A(integer Y)
 .
 B(Y-1)
 .
end
routine B(integer X)
 .
 A(X+3)
 .
end  }
{\rm Note that the spec statement and the procedure heading must correspond, that is, the type and form of the
statements must match, as must the type, form, order and number of any parameters. }
\newpage
{\rm   31}
{\rm {\bf CONTROL TRANSFER INSTRUCTIONS  }
{\bf {\it LABELS AND JUMPS  }}
Simple Labels }
{\rm Any statement, excluding declarations, may be given one or more simple labels, where a simple label is of the
form: {id} ':'
Each label is written to the left of the statement.  }
{\tt NEXT:  P = P+1 if P {\lt} 0
ERRORl:ERROR2: FAULTS = FAULTS+1  }
{\rm Control may be passed to a labelled statement by executing a jump instruction:  '-{\gt}' {id} }
{\tt -{\gt} NEXT
-{\gt} ERROR1 if DIVISOR = 0 }
{\rm Switch Vectors  }
{\rm A vector of labels may be declared in a similar manner to an array, using the declarator switch.  }
{\tt switch SW(4:9)
switch S1, S2(1:10), S3(11:20)  }
{\rm Note
a. The vector must be one-dimensional.
b. The bounds must be constants.
Once declared, switch labels may be used in the same way as simple labels.  }
{\tt SW(4):  CHECK VALUE(l)
SW(5):SW(6): ERROR FLAG = 1
LAST: SW(9): ! ALL FINISHED  }
{\rm A star (*) may be used in the definition of a switch label to locate any elements of the vector which would
otherwise be undefined.  }
{\tt switch LET('A':'Z')
.
.
LET('A'):LET('E'):LET('I'):LET('O'):LET('U'):
! DEAL WITH VOWELS
.
.
LET(*): ! ALL THE REST I.E. CONSONANTS }
{\rm The specific label to which a jump will be made is dependent on the value of an integer expression. }
{\tt -{\gt}SW(N) if N {\gt} 0
-{\gt}SW(100+N)
-{\gt}SW(6) }
{\rm Note:
1. Not all of the declared switch labels need be defined (in the previous example SW(7) and SW(8) are
undefined) but an error will occur at run time if an attempt is made to jump to a non-existent switch
label.
2. Simple labels are the only identifiers which may be used before they are declared/defined. }
\newpage
{\rm   32
3. The scope of both types of label is limited to the block in which they are defined, not including any
blocks defined therein.  That is, labels cannot be global to a block and therefore it is not possible to
jump into or out of a block.
4. The identifiers used for labels must not conflict with other local identifiers.
5. The results of entering a for loop with a jump and not through the for statement are undefined. }
{\rm {\bf OTHER CONTROL TRANSFER INSTRUCTIONS  }}
{\rm stop
Execution of the instruction stop causes control to be returned to the program which initiated the execution of
the current program. This is also the effect of reaching the statement end of program.
Control is transferred by signalling event zero (see Events).
monitor
This instruction causes the run-time diagnostic package to be invoked to produce diagnostic information.  If no
diagnostic package is available this instruction will be ignored (in some limited implementations the
production of diagnostics causes execution of the program to be terminated).
For convenience all other control transfer instructions are gathered here.
return   return from a routine.
result={exp}  return the result of a function
result=={reference} return the result of a map.
true   return from a predicate.
false   return from a predicate.
exit   jump out of the current cycle to the statement following the matching repeat.
continue   jump to the top of the current cycle.
signal event  see Events.  }
{\rm {\bf  }}
\newpage
{\rm   33}
{\rm {\bf EXTERNAL LINKAGE  }}
{\rm A complete program may be divided into several separately compiled modules which are linked together before
(or possibly while) the program is executed. This linkage is achieved by giving the external attribute to
relevant identifiers.
1. external DATA OBJECTS
An external variable is declared in the same way as an own variable with the keyword own replaced by
external  }
{\tt external integer CHOICE=4, WAIT = -5
external real array MEAN(-6:6)  }
{\rm The identifiers are then available for use by any program that references them. A separately compiled module
that requires to use any of these variables must first declare them using an external specification.  }
{\tt external integer spec WAIT, CHOICE
external real array spec MEAN(-6:6)  }
{\rm Note
1. No initialisation may be specified in an external specification.
2. External arrays must be one-dimensional and have constant bounds.
3. Even though all of the characters in the identifier of an external entity are significant to the
compiler, the system load software might impose constraints on the number of significant
characters.  Refer to the relevant appendix for system dependent restrictions.
2. external PROCEDURES
A procedure may be made available to other modules by prefixing the procedure heading with the keyword
external.  }
{\tt external routine TRIAL(string(63) S)  }
{\rm Such procedures must be compiled in a file comprising only external procedures (and possibly some non-
external procedures and own or external declarations).  The whole module is terminated by the statement end
of file
If a module requires to use an externally defined procedure it must first supply an external procedure
specification.  For example: }
{\tt external predicate spec LETTER(integer S) }
{\rm This is similar to a procedure specification but only requires the specified procedure to have been defined by
the time the module is executed.
The prefix external may be replaced by system or dynamic, the exact significance of which may vary from
machine to machine. }
{\rm {\bf  }}
\newpage
{\rm   34}
{\rm {\bf PREDEFINED PROCEDURES  }}
{\rm Every separately compiled module, whether a begin-end program block or a file of external procedures is
compiled within a conceptual 'outermost block' in which are declared a number of standard procedures such
as READ and WRITE. This means that these procedures are global to all parts of a program and so may be
used without having to be declared.  Note that as these procedures are global they may be redefined within the
program.
Further, own, constant or external identifiers may be declared in this outermost block and will be global to the
whole of  the file. }
{\tt own integer CALLS = 0
external routine DO SOMETHING
 CALLS = CALLS+l; ! RECORD TIMES ENTERED
 .
 .
end
external integer function ENTRIES
 result = CALLS
end
end of file }
{\rm Note that the function `ENTRIES is used to make the value of CALLS available to other modules without their
being able to change that value, even by mistake.
While the actual procedures which are predeclared may vary from machine to machine, the following are
standard and may be assumed present:  }
{\tt INPUT/OUTPUT
routine READSYMBOL(integer name S)
routine SKIPSYMBOL
integer function NEXTSYMBOL
routine READ(name N)
routine PRINTSYMBOL(integer N)
routine PRINTSTRING(string(255) S)
routine WRITE(integer N, PLACES)
routine NEWLINE
routine NEWLINES(integer N)
routine SPACE
routine SPACES(integer N)
routine SELECTINPUT(integer STREAM)
routine SELECTOUTPUT(integer STREAM)
STRING HANDLING
string(1) function TOSTRING(integer SYMBOL)
string(255) fn SUBSTRING(string(255)name S, integer F,T)
integer function CHARNO(string(255) S, integer N)
integer function LENGTH(string(255) S)
EVENT HANDLING (see Events)
integer function EVENT
integer function SUB EVENT
integer function EVENT INFO
STORE MAPPING
integer function ADDR(name V)
integer map INTEGER(integer ADDRESS)
real map REAL(integer ADDRESS)
string(255)map STRING(integer ADDRESS)  }
{\rm Refer to the relevant system library manual for detailed specifications of these and other standard procedures.  }
\newpage
{\rm   35}
{\rm {\bf EVENTS  }}
{\rm During the execution of a program several (synchronous) events may occur, such as resolution fails, array
bound faults etc. (see Faults).   Normally such events will cause the program to be terminated with an error
report and possibly diagnostic information. However events may be trapped and used to control the further
execution of the program.
The first non-declarative statements of any block may be of the form: }
{\tt on event {event list} start
! ON-BODY STATEMENTS
finish }
{\rm where (event list) is a list of integer constants representing the events to be trapped.
On entry to the block the on body is skipped and execution continues from the statements following the finish.
If an event specified in the (event list) is signalled during execution of the statements between the finish of the
on event group and the end of the block, control will be passed to the on-body (and may well pass through the
finish to the following statements). If the event is not trapped in the current block a 'return' is forced and the
event is signalled in the new block at the point from which the old block was entered. The process is repeated
until either the event is trapped or the outermost block of the program is reached in which case the event is
reported as a fault and the program terminates.
Three functions are available which give information about the last event to have been signalled.
4.   integer function EVENT - returns the class of the last event.
5.   integer function SUB EVENT - returns the sub-class of the last event.
6.   integer function EVENT INFO - returns any extra information passed with the event.
If no event has occurred each of these functions will return the value zero.  }
\newpage
{\rm   36
The classes of event and their sub-classes of them are:
{\bf EVENT   SUB-CLASS  MEANING (+EXTRA INFORMATION)  }
0
TERMINATION
-1
ABANDON PROGRAM WITHOUT
DIAGNOSTICS
0
NORMAL TERMINATION (stop)
{\gt}0
USER GENERATED ERROR
1
ARITHMETIC OVERFLOW
1
INTEGER OVERFLOW
2
REAL OVERFLOW
2
EXCESS RESOURCE
1
NOT ENOUGH STORE
3
DATA ERROR
1
SYMBOL IN DATA (+SYMBOL)
4
CORRUPT DATA
1
DATA TRANSMISSION ERROR
5
INVALID ARGUMENTS
1
ILLEGAL CYCLE
2
ILLEGAL EXPONENT (+EXPONENT)
3
ARRAY INSIDE-OUT
6
OUT OF RANGE
1
CAPACITY EXCEEDED
2
ARRAY BOUND FAULT (+INDEX)
3
NO SWITCH LABEL (+INDEX)
7
RESOLUTION FAILS
8
UNASSIGNED VARIABLE
9
INPUT ENDED
10
LIBRARY PROCEDURE ERROR
11-15
GENERAL PURPOSE
At any time during the execution of a program an event can be signalled by executing an instruction of the
form:  }
{\tt signal event {N}{QUAL}?  }
{\rm where:  }
{\tt {N} ::= an integer constant in the range 0 {\lt}= N {\lt}= 15
{qual} ::= ',' {sub event}{extra}?
{extra} ::= ',' {extra info} }
{\rm and {sub event} and {extra info} are integer expressions. }
\newpage
{\rm   37
The instruction causes event {n} to be signalled with sub-event (default zero) and extra information (default
zero). }
{\tt signal event 15;   ! event 15,0,0
signal event 14,7 if X {\lt} 0; ! event 14,7,0
signal event 13,1,Y if Y {\neq} 0; ! event 13,l,Y  }
{\rm Note:
1. In both the on and signal statements the keyword event is optional and may be omitted.
2. An event signalled inside an incarnation of an on-body will never be trapped into that incarnation.
Instead the search for a trap will start in the previous block. }
{\rm {\bf  }}
\newpage
{\rm   38}
{\rm {\bf APPENDIX 1  }
{\bf COMPILER ERROR MESSAGES  }}
{\rm Any errors detected by the compiler will generate messages of the form: }
{\tt * {message} }
{\rm In most cases a marker (|) will be output to indicate the position in the statement at which the error was
detected.
ACCESS
-  the statement cannot be reached.  This is not treated as an error but may
indicate another fault.
ATOM
-  unknown atomic element.
BOUNDS
-  invalid bounds for an array or switch declaration, or wrong number of constants
for an array initialisation.
CONTEXT
-  formally correct statement given in the wrong context.
COPY
-  attempt to redefine a local identifier.
FORM
-  incorrectly formed statement.
INDEX
-  switch label index out of bounds.
MATCH
-  procedure definition does not match a previous spec.
NAME
-  undeclared identifier.
ORDER
-  formally correct statement in wrong sequence.
SIZE
-  constant out of range.
TOO COMPLEX
-  statement too long or complex to analyse.
TYPE
-  variable of wrong type.
TYPE FOR {op}
-  operator {op} out of context.
%BEGIN MISSING
-  too many end statements
%CYCLE MISSING
-  a repeat has been given with no matching cycle.
%END MISSING
-  unterminated blocks remain at end of program or end of file.
%FINISH MISSING
-  outstanding start at end or repeat.
%REPEAT MISSING  -  outstanding cycle at end or finish.
RESULT MISSING
-  a function, map, or predicate can reach its end.
%START MISSING
-  a finish has been given with no matching start.
'{ID}' MISSING
-  undefined procedure or label.
 }
\newpage
{\rm   39}
{\rm {\bf APPENDIX 2  }
{\bf SAMPLE PROGRAM  LISTING }}
{\tt 1 %begin
   2  %begin
   3  %realname Q
   4  %integer VALUE, X, X
*                 ! COPY
   5  %string(256) S
*           ! SIZE
   6  %switch SA(1:4), SB(5:4)
*BOUNDS
   7  %routinespec CHECK
   8  %integerfnspec KEY(%integer X)
   9  %if X = 4 %STARY
*        ! ATOM
  10   VALUE = KEY
*       ! FORM
  11   VALUR = 0
*    ! NAME
  12 SA(5):
*       ! INDEX
  13   %exit
*%CYCLE MISSING
  14   %stop
  15   X = 0
*ACCESS
  16  %finish
*%START MISSING
  17  %on %event 4 %start
*ORDER
  18   %integerfunction KEY(%real X)
*MATCH
  19   %end
*RESULT MISSING
  20  Q == VALUE
*        ! TYPE
  21  X = Q{\amp}7
* TYPE FOR '{\amp}'
  22 %endofprogram
*%END MISSING
*%FINISH MISSING
*'CHECK' MISSING }
\newpage
{\rm   40}
{\rm {\bf APPENDIX 3  }
{\bf DATA PRECISION SPECIFICATION  }}
{\rm On some machines it is possible to offer a range of precisions for variables of type integer or real.  The
precision is specified by the use of one of the following prefixes:
short - smaller range than by default
long - larger range than by default
byte - large enough to hold a character (unsigned)
E.g. }
{\tt byte integer
short integer
long integer
long real }
{\rm If the machine on which the program is to be run cannot support the required precision the prefix will be
ignored.
E.g. on the IBM 360 (or ICL 4/75)
byte integer 8-bits unsigned
short integer 16-bits signed
integer  32-bits signed
real  32-bits
long real  64-bits
Note that checks may be applied to ensure that any quantity assigned to a variable is within the correct range
of values.
E.g. }
{\tt shortinteger S
integer X
X = 16\_FFFF
S = X }
{\rm will fail at run time, as '16\_FFFF' is a POSITIVE integer value, but a NEGATIVE short integer value.
The assignment operator '{\lt}-' may be used to force truncation if required (see Assignment). }
\newpage
{\rm   41}
{\rm {\bf APPENDIX 4  }
{\bf IMP KEYWORDS  }}
{\tt alias
and
array
begin
byte
c
comment  const
constant  continue  cycle
dynamic
else
end
event
exit
external
false
file
finish
fn
for
format
if
include  integer
like
list
long
map
monitor
name
not
on
of
or
own
predicate  program
real
record
repeat
result
return
routine
short
signal
spec
start
stop
string
switch
system
then
true
unless
until
while
 }
\newline
\newpage
{\rm   42}
{\rm {\bf APPENDIX 5  }
{\bf COMPARISON WITH EMAS IMP  }}
{\rm 1.  New Features
for
repeat until
continue
predicate
include
'==' in conditions
integer array (4) name
finish else if ...
else
lower case input
like
(*) in owns and switches
constant
function
alias
not
record function
record map
constant expressions
2.  Features not implemented
print text
until ... cycle
array format
reals long
reals normal
implied multiplication
3.  Changed Features
'AA' instead of M'AA'
16\_1A2 instead of X'1A2'
procedure parameter specification
record (F) R instead of record R (F)
SUBSTRING instead of FROMSTRING
termination of comments
'{\bslash}' or '{\bslash}{\bslash}' instead of '**' }
\newpage
{\rm   43
own initialisation
type checking for record operation
external .. spec instead of extri
events instead of fault trapping
/ gives a real result
string resolution }
\bye
