694 lines
57 KiB
Plaintext
694 lines
57 KiB
Plaintext
|
<?xml encoding = "windows-1252"?>
|
|||
|
<FictionBook xmlns:l="http://www.w3.org/1999/xlink" xmlns="http://www.gribuser.ru/xml/fictionbook/2.0">
|
|||
|
<description></description>
|
|||
|
<body>
|
|||
|
<section><title><p>The Programming Language Oberon</p><p>Revision 22.9.2011</p><p>Niklaus Wirth</p></title>
|
|||
|
<epigraph><p>Make it as simple as possible, but not simpler.</p><text-author>(A. Einstein)</text-author></epigraph>
|
|||
|
<p>Table of Contents</p>
|
|||
|
<empty-line/>
|
|||
|
<p><a l:href="#1">1. Introduction</a></p>
|
|||
|
<p><a l:href="#2">2. Syntax</a></p>
|
|||
|
<p><a l:href="#3">3. Vocabulary</a></p>
|
|||
|
<p><a l:href="#4">4. Declarations and scope rules</a></p>
|
|||
|
<p><a l:href="#5">5. Constant declarations</a></p>
|
|||
|
<p><a l:href="#6">6. Type declarations</a></p>
|
|||
|
<p><a l:href="#7">7. Variable declarations</a></p>
|
|||
|
<p><a l:href="#8">8. Expressions</a></p>
|
|||
|
<p><a l:href="#9">9. Statements</a></p>
|
|||
|
<p><a l:href="#10">10. Procedure declarations</a></p>
|
|||
|
<p><a l:href="#11">11. Modules</a></p>
|
|||
|
<p><a l:href="#app">Appendix: The Syntax of Oberon</a></p>
|
|||
|
<section id="1"><title><p>1. Introduction</p></title>
|
|||
|
<p>Oberon is a general-purpose programming language that evolved from Modula-2. Its principal new feature is the concept of type extension. It permits the construction of new data types on the basis of existing ones and to relate them.</p>
|
|||
|
<p>This report is not intended as a programmer's tutorial. It is intentionally kept concise. Its function is to serve as a reference for programmers, implementors, and manual writers. What remains unsaid is mostly left so intentionally, either because it is derivable from stated rules of the language, or because it would unnecessarily restrict the freedom of implementors.</p>
|
|||
|
<p>This document describes the language defined in 1988/90 as revised in 2007/11.</p>
|
|||
|
</section>
|
|||
|
<section id="2"><title><p>2. Syntax</p></title>
|
|||
|
<p>A language is an infinite set of sentences, namely the sentences well formed according to its syntax. In Oberon, these sentences are called compilation units. Each unit is a finite sequence of <emphasis>symbols</emphasis> from a finite vocabulary. The vocabulary of Oberon consists of identifiers, numbers, strings, operators, delimiters, and comments. They are called <emphasis>lexical symbols</emphasis> and are composed of sequences of <emphasis>characters</emphasis>. (Note the distinction between symbols and characters.)</p>
|
|||
|
<p>To describe the syntax, an extended Backus-Naur Formalism called EBNF is used. Brackets [ and ] denote optionality of the enclosed sentential form, and braces { and } denote its repetition (possibly 0 times). Syntactic entities (non-terminal symbols) are denoted by English words expressing their intuitive meaning. Symbols of the language vocabulary (terminal symbols) are denoted by strings enclosed in quote marks or by words in capital letters.</p>
|
|||
|
</section>
|
|||
|
<section id="3"><title><p>3. Vocabulary</p></title>
|
|||
|
<p>The following lexical rules must be observed when composing symbols. Blanks and line breaks must not occur within symbols (except in comments, and blanks in strings). They are ignored unless they are essential to separate two consecutive symbols. Capital and lower-case letters are considered as being distinct.</p>
|
|||
|
<p><emphasis>Identifiers</emphasis> are sequences of letters and digits. The first character must be a letter.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ident = letter {letter | digit}.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Examples:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> x scan Oberon GetSymbol firstLetter</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><emphasis>Numbers</emphasis> are (unsigned) integers or real numbers. Integers are sequences of digits and may be followed by a suffix letter. If no suffix is specified, the representation is decimal. The suffix H indicates hexadecimal representation.</p>
|
|||
|
<p>A <emphasis>real number</emphasis> always contains a decimal point. Optionally it may also contain a decimal scale factor. The letter E is pronounced as "times ten to the power of". A real number is of type REAL, unless it contains a scale factor with the letter D, in which case it is of type LONGREAL.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> number = integer | real.</code></p>
|
|||
|
<p><code> integer = digit {digit} | digit {hexDigit} "H".</code></p>
|
|||
|
<p><code> real = digit {digit} "." {digit} [ScaleFactor].</code></p>
|
|||
|
<p><code> ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.</code></p>
|
|||
|
<p><code> hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".</code></p>
|
|||
|
<p><code> digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Examples:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> 1987</code></p>
|
|||
|
<p><code> 100H = 256</code></p>
|
|||
|
<p><code> 12.3</code></p>
|
|||
|
<p><code> 4.567E8 = 456700000</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><emphasis>Strings</emphasis> are sequences of characters enclosed in quote marks ("). A string cannot contain the delimiting quote mark. Alternatively, a single-character string may be specified by the ordinal number of the character in hexadecimal notation followed by an "X". The number of characters in a string is called the <emphasis>length</emphasis> of the string.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> string = """ {character} """ | digit {hexdigit} "X" .</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Examples:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> "OBERON" "Don't worry!" 22X</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><emphasis>Operators</emphasis> and <emphasis>delimiters</emphasis> are the special characters, character pairs, or reserved words listed below. These reserved words consist exclusively of capital letters and cannot be used in the role of identifiers.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> + := ARRAY IMPORT THEN</code></p>
|
|||
|
<p><code> - ^ BEGIN IN TO</code></p>
|
|||
|
<p><code> * = BY IS TRUE</code></p>
|
|||
|
<p><code> / # CASE MOD TYPE</code></p>
|
|||
|
<p><code> ~ < CONST MODULE UNTIL</code></p>
|
|||
|
<p><code> & > DIV NIL VAR</code></p>
|
|||
|
<p><code> . <= DO OF WHILE</code></p>
|
|||
|
<p><code> , >= ELSE OR</code></p>
|
|||
|
<p><code> ; .. ELSIF POINTER</code></p>
|
|||
|
<p><code> | : END PROCEDURE</code></p>
|
|||
|
<p><code> ( ) FALSE RECORD</code></p>
|
|||
|
<p><code> [ ] FOR REPEAT</code></p>
|
|||
|
<p><code> { } IF RETURN</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><emphasis>Comments</emphasis> may be inserted between any two symbols in a program. They are arbitrary character sequences opened by the bracket (* and closed by *). Comments do not affect the meaning of a program. They may be nested.</p>
|
|||
|
</section>
|
|||
|
<section id="4"><title><p>4. Declarations and scope rules</p></title>
|
|||
|
<p>Every identifier occurring in a program must be introduced by a declaration, unless it is a predefined identifier. Declarations also serve to specify certain permanent properties of an object, such as whether it is a constant, a type, a variable, or a procedure.</p>
|
|||
|
<p>The identifier is then used to refer to the associated object. This is possible in those parts of a program only which are within the <emphasis>scope</emphasis> of the declaration. No identifier may denote more than one object within a given scope. The scope extends textually from the point of the declaration to the end of the block (procedure or module) to which the declaration belongs and hence to which the object is local. The scope rule has the following amendments:</p>
|
|||
|
<p>1. If a type <emphasis>T</emphasis> is defined as POINTER TO T1 <a l:href="#6.4">(see 6.4)</a>, the identifier <emphasis>T1</emphasis> can be declared textually following the declaration of <emphasis>T</emphasis>, but it must lie within the same scope.</p>
|
|||
|
<p>2. Field identifiers of a record declaration <a l:href="#6.3">(see 6.3)</a> are valid in field designators only.</p>
|
|||
|
<p>In its declaration, an identifier in the global scope may be followed by an export mark (*) to indicate that it be <emphasis>exported</emphasis> from its declaring module. In this case, the identifier may be used in other modules, if they import the declaring module. The identifier is then prefixed by the identifier designating its module <a l:href="#11">(see Ch. 11)</a>. The prefix and the identifier are separated by a period and together are called a <emphasis>qualified identifier</emphasis>.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> qualident = [ident "."] ident.</code></p>
|
|||
|
<p><code> identdef = ident ["*"].</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The following identifiers are predefined; their meaning is defined in section <a l:href="#6.1">6.1</a> (types) or <a l:href="#10.2">10.2</a> (procedures):</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ABS ASR ASSERT BOOLEAN CHAR</code></p>
|
|||
|
<p><code> CHR COPY DEC EXCL FLOOR</code></p>
|
|||
|
<p><code> FLT INC INCL INTEGER LEN</code></p>
|
|||
|
<p><code> LSL LONG LONGREAL NEW ODD</code></p>
|
|||
|
<p><code> ORD PACK REAL ROR SET</code></p>
|
|||
|
<p><code> SHORT UNPK</code></p>
|
|||
|
</section>
|
|||
|
<section id="5"><title><p>5. Constant declarations</p></title>
|
|||
|
<p>A constant declaration associates an identifier with a constant value.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ConstantDeclaration = identdef "=" ConstExpression.</code></p>
|
|||
|
<p><code> ConstExpression = expression.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>A constant expression can be evaluated by a mere textual scan without actually executing the program. Its operands are constants <a l:href="#8">(see Ch. 8)</a>. Examples of constant declarations are:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> N = 100</code></p>
|
|||
|
<p><code> limit = 2*N - 1</code></p>
|
|||
|
<p><code> all = {0 .. WordSize-1}</code></p>
|
|||
|
<p><code> name = "Oberon"</code></p>
|
|||
|
</section>
|
|||
|
<section id="6"><title><p>6. Type declarations</p></title>
|
|||
|
<p>A data type determines the set of values which variables of that type may assume, and the operators that are applicable. A type declaration is used to associate an identifier with a type. The types define the structure of variables of this type and, by implication, the operators that are applicable to components. There are two different structures, namely arrays and records, with different component selectors.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> TypeDeclaration = identdef "=" StrucType.</code></p>
|
|||
|
<p><code> StrucType = ArrayType | RecordType | PointerType | ProcedureType.</code></p>
|
|||
|
<p><code> type = qualident | StrucType.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Examples:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> Table = ARRAY N OF REAL</code></p>
|
|||
|
<p><code> Tree = POINTER TO Node</code></p>
|
|||
|
<p><code> Node = RECORD</code></p>
|
|||
|
<p><code> key: INTEGER;</code></p>
|
|||
|
<p><code> left, right: Tree</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
<p><code> CenterNode = RECORD (Node)</code></p>
|
|||
|
<p><code> name: ARRAY 32 OF CHAR;</code></p>
|
|||
|
<p><code> subnode: Tree</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
<p><code> Function = PROCEDURE (x: INTEGER): INTEGER</code></p>
|
|||
|
<section id="6.1"><title><p>6.1. Basic types</p></title>
|
|||
|
<p>The following basic types are denoted by predeclared identifiers. The associated operators are defined in <a l:href="#8.2">8.2</a>, and the predeclared function procedures in <a l:href="#10.2">10.2</a>. The values of a given basic type are the following:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> BOOLEAN the truth values TRUE and FALSE</code></p>
|
|||
|
<p><code> CHAR the characters of a standard character set</code></p>
|
|||
|
<p><code> INTEGER the integers</code></p>
|
|||
|
<p><code> REAL real numbers</code></p>
|
|||
|
<p><code> LONGREAL real numbers</code></p>
|
|||
|
<p><code> SET the sets of integers between 0 and 31</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The type LONGREAL is intended to represent real numbers with a higher number of digits than REAL. However, the two types may be identical.</p>
|
|||
|
</section>
|
|||
|
<section id="6.2"><title><p>6.2. Array types</p></title>
|
|||
|
<p>An array is a structure consisting of a fixed number of elements which are all of the same type, called the <emphasis>element type</emphasis>. The number of elements of an array is called its <emphasis>length</emphasis>. The elements of the array are designated by indices, which are integers between 0 and the length minus 1.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ArrayType = ARRAY length {"," length} OF type.</code></p>
|
|||
|
<p><code> length = ConstExpression.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>A declaration of the form</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ARRAY N0, N1, ... , Nk OF T</code></p>
|
|||
|
<p></p><empty-line/>is understood as an abbreviation of the declaration<empty-line/>
|
|||
|
<p><code> ARRAY N0 OF</code></p>
|
|||
|
<p><code> ARRAY N1 OF</code></p>
|
|||
|
<p><code> ...</code></p>
|
|||
|
<p><code> ARRAY Nk OF T</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Examples of array types:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ARRAY N OF INTEGER</code></p>
|
|||
|
<p><code> ARRAY 10, 20 OF REAL</code></p>
|
|||
|
</section>
|
|||
|
<section id="6.3"><title><p>6.3. Record types</p></title>
|
|||
|
<p>A record type is a structure consisting of a fixed number of elements of possibly different types. The record type declaration specifies for each element, called <emphasis>field</emphasis>, its type and an identifier which denotes the field. The scope of these field identifiers is the record definition itself, but they are also visible within field designators <a l:href="#8.1">(see 8.1)</a> referring to elements of record variables.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> RecordType = RECORD ["(" BaseType ")"] [FieldListSequence] END.</code></p>
|
|||
|
<p><code> BaseType = qualident.</code></p>
|
|||
|
<p><code> FieldListSequence = FieldList {";" FieldList}.</code></p>
|
|||
|
<p><code> FieldList = IdentList ":" type.</code></p>
|
|||
|
<p><code> IdentList = identdef {"," identdef}.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>If a record type is exported, field identifiers that are to be visible outside the declaring module must be marked. They are called <emphasis>public fields</emphasis>; unmarked fields are called <emphasis>private fields</emphasis>.</p>
|
|||
|
<p>Record types are extensible, i.e. a record type can be defined as an extension of another record type. In the examples above, <emphasis>CenterNode</emphasis> (directly) extends <emphasis>Node</emphasis>, which is the (direct) base type of <emphasis>CenterNode</emphasis>. More specifically, <emphasis>CenterNode</emphasis> extends <emphasis>Node</emphasis> with the fields <emphasis>name</emphasis> and <emphasis>subnode</emphasis>.</p>
|
|||
|
<p><emphasis>Definition</emphasis>: A type <emphasis>T</emphasis> extends a type <emphasis>T0</emphasis>, if it equals <emphasis>T0</emphasis>, or if it directly extends an extension of <emphasis>T0</emphasis>. Conversely, a type <emphasis>T0</emphasis> is a base type of <emphasis>T</emphasis>, if it equals <emphasis>T</emphasis>, or if it is the direct base type of a base type of <emphasis>T</emphasis>.</p>
|
|||
|
<p>Examples of record types:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> RECORD day, month, year: INTEGER</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
<p><code> RECORD</code></p>
|
|||
|
<p><code> name, firstname: ARRAY 32 OF CHAR;</code></p>
|
|||
|
<p><code> age: INTEGER;</code></p>
|
|||
|
<p><code> salary: REAL</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
</section>
|
|||
|
<section id="6.4"><title><p>6.4. Pointer types</p></title>
|
|||
|
<p>Variables of a pointer type <emphasis>P</emphasis> assume as values pointers to variables of some type <emphasis>T</emphasis>. It must be a record type. The pointer type <emphasis>P</emphasis> is said to be <emphasis>bound to T</emphasis>, and <emphasis>T</emphasis> is the <emphasis>pointer base type of P</emphasis>. Pointer types inherit the extension relation of their base types, if there is any. If a type <emphasis>T</emphasis> is an extension of <emphasis>T0</emphasis> and <emphasis>P</emphasis> is a pointer type bound to <emphasis>T</emphasis>, then <emphasis>P</emphasis> is also an extension of <emphasis>P0, the pointer type bound to T0</emphasis>.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> PointerType = POINTER TO type.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>If <emphasis>p</emphasis> is a variable of type P = POINTER TO T, then a call of the predefined procedure NEW(p) has the following effect <a l:href="#10.2">(see 10.2)</a>: A variable of type <emphasis>T</emphasis> is allocated in free storage, and a pointer to it is assigned to <emphasis>p</emphasis>. This pointer <emphasis>p</emphasis> is of type <emphasis>P</emphasis> and the referenced variable <emphasis>p^</emphasis> is of type <emphasis>T</emphasis>. Failure of allocation results in <emphasis>p</emphasis> obtaining the value <emphasis>NIL</emphasis>. Any pointer variable may be assigned the value <emphasis>NIL</emphasis>, which points to no variable at all.</p>
|
|||
|
</section>
|
|||
|
<section id="6.5"><title><p>6.5. Procedure types</p></title>
|
|||
|
<p>Variables of a procedure type <emphasis>T</emphasis> have a procedure (or NIL) as value. If a procedure <emphasis>P</emphasis> is assigned to a procedure variable of type <emphasis>T</emphasis>, the (types of the) formal parameters of <emphasis>P</emphasis> must be the same as those indicated in the formal parameters of <emphasis>T</emphasis>. The same holds for the result type in the case of a function procedure <a l:href="#10.1">(see 10.1)</a>. <emphasis>P</emphasis> must not be declared local to another procedure, and neither can it be a standard procedure.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ProcedureType = PROCEDURE [FormalParameters].</code></p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="7"><title><p>7. Variable declarations</p></title>
|
|||
|
<p>Variable declarations serve to introduce variables and associate them with identifiers that must be unique within the given scope. They also serve to associate fixed data types with the variables.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> VariableDeclaration = IdentList ":" type.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Variables whose identifiers appear in the same list are all of the same type. Examples of variable declarations (refer to examples in <a l:href="#6">Ch. 6</a>):</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> i, j, k: INTEGER</code></p>
|
|||
|
<p><code> x, y: REAL</code></p>
|
|||
|
<p><code> p, q: BOOLEAN</code></p>
|
|||
|
<p><code> s: SET</code></p>
|
|||
|
<p><code> f: Function</code></p>
|
|||
|
<p><code> a: ARRAY 100 OF REAL</code></p>
|
|||
|
<p><code> w: ARRAY 16 OF</code></p>
|
|||
|
<p><code> RECORD</code></p>
|
|||
|
<p><code> ch: CHAR;</code></p>
|
|||
|
<p><code> count: INTEGER</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
<p><code> t: Tree</code></p>
|
|||
|
</section>
|
|||
|
<section id="8"><title><p>8. Expressions</p></title>
|
|||
|
<p>Expressions are constructs denoting rules of computation whereby constants and current values of variables are combined to derive other values by the application of operators and function procedures. Expressions consist of operands and operators. Parentheses may be used to express specific associations of operators and operands.</p>
|
|||
|
<section id="8.1"><title><p>8.1. Operands</p></title>
|
|||
|
<p>With the exception of sets and literal constants, i.e. numbers and strings, operands are denoted by <emphasis>designators</emphasis>. A designator consists of an identifier referring to the constant, variable, or procedure to be designated. This identifier may possibly be qualified by module identifiers <a l:href="#4">(see Ch. 4</a> and <a l:href="#11">11)</a>, and it may be followed by selectors, if the designated object is an element of a structure.</p>
|
|||
|
<p>If A designates an array, then <emphasis>A[E]</emphasis> denotes that element of <emphasis>A</emphasis> whose index is the current value of the expression <emphasis>E</emphasis>. The type of <emphasis>E</emphasis> must be of type INTEGER. A designator of the form <emphasis>A[E1, E2, ..., En]</emphasis> stands for <emphasis>A[E1][E2] ... [En]</emphasis>. If <emphasis>p</emphasis> designates a pointer variable, <emphasis>p^</emphasis> denotes the variable which is referenced by <emphasis>p</emphasis>. If <emphasis>r</emphasis> designates a record, then <emphasis>r.f</emphasis> denotes the field <emphasis>f</emphasis> of <emphasis>r</emphasis>. If <emphasis>p</emphasis> designates a pointer, <emphasis>p.f</emphasis> denotes the field <emphasis>f</emphasis> of the record <emphasis>p^</emphasis>, i.e. the dot implies dereferencing and <emphasis>p.f</emphasis> stands for <emphasis>p^.f</emphasis>.</p>
|
|||
|
<p>The <emphasis>typeguard v(T0)</emphasis> asserts that <emphasis>v</emphasis> is of type <emphasis>T0</emphasis>, i.e. it aborts program execution, if it is not of type <emphasis>T0</emphasis>. The guard is applicable, if</p>
|
|||
|
<p>1. <emphasis>T0</emphasis> is an extension of the declared type <emphasis>T</emphasis> of <emphasis>v</emphasis>, and if</p>
|
|||
|
<p>2. <emphasis>v</emphasis> is a variable parameter of record type, or <emphasis>v</emphasis> is a pointer.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> designator = qualident {selector}.</code></p>
|
|||
|
<p><code> selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".</code></p>
|
|||
|
<p><code> ExpList = expression {"," expression}.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>If the designated object is a variable, then the designator refers to the variable's current value. If the object is a procedure, a designator without parameter list refers to that procedure. If it is followed by a (possibly empty) parameter list, the designator implies an activation of the procedure and stands for the value resulting from its execution. The (types of the) actual parameters must correspond to the formal parameters as specified in the procedure's declaration <a l:href="#10">(see Ch. 10)</a>.</p>
|
|||
|
<p>Examples of designators <a l:href="#7">(see examples in Ch. 7)</a>:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> i (INTEGER)</code></p>
|
|||
|
<p><code> a[i] (REAL)</code></p>
|
|||
|
<p><code> w[3].ch (CHAR)</code></p>
|
|||
|
<p><code> t.key (INTEGER)</code></p>
|
|||
|
<p><code> t.left.right (Tree)</code></p>
|
|||
|
<p><code> t(CenterNode).subnode (Tree)</code></p>
|
|||
|
</section>
|
|||
|
<section id="8.2"><title><p>8.2. Operators</p></title>
|
|||
|
<p>The syntax of expressions distinguishes between four classes of operators with different precedences (binding strengths). The operator ~ has the highest precedence, followed by multiplication operators, addition operators, and relations. Operators of the same precedence associate from left to right. For example, <emphasis>x-y-z</emphasis> stands for <emphasis>(x-y)-z</emphasis>.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> expression = SimpleExpression [relation SimpleExpression].</code></p>
|
|||
|
<p><code> relation = "=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.</code></p>
|
|||
|
<p><code> SimpleExpression = ["+"|"-"] term {AddOperator term}.</code></p>
|
|||
|
<p><code> AddOperator = "+" | "-" | OR.</code></p>
|
|||
|
<p><code> term = factor {MulOperator factor}.</code></p>
|
|||
|
<p><code> MulOperator = "*" | "/" | DIV | MOD | "&" .</code></p>
|
|||
|
<p><code> factor = number | string | NIL | TRUE | FALSE |</code></p>
|
|||
|
<p><code> set | designator [ActualParameters] | "(" expression ")" | "~" factor.</code></p>
|
|||
|
<p><code> set = "{" [element {"," element}] "}".</code></p>
|
|||
|
<p><code> element = expression [".." expression].</code></p>
|
|||
|
<p><code> ActualParameters = "(" [ExpList] ")" .</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The available operators are listed in the following tables. In some instances, several different operations are designated by the same operator symbol. In these cases, the actual operation is identified by the type of the operands.</p>
|
|||
|
<section id="8.2.1"><title><p><emphasis>8.2.1. Logical operators</emphasis></p></title>
|
|||
|
<p><code> symbol result</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> OR logical disjunction</code></p>
|
|||
|
<p><code> & logical conjunction</code></p>
|
|||
|
<p><code> ~ negation</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>These operators apply to BOOLEAN operands and yield a BOOLEAN result.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> p OR q stands for "if p then TRUE, else q"</code></p>
|
|||
|
<p><code> p & q stands for "if p then q, else FALSE"</code></p>
|
|||
|
<p><code> ~ p stands for "not p"</code></p>
|
|||
|
</section>
|
|||
|
<section id="8.2.2"><title><p><emphasis>8.2.2. Arithmetic operators</emphasis></p></title>
|
|||
|
<p><code> symbol result</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> + sum</code></p>
|
|||
|
<p><code> - difference</code></p>
|
|||
|
<p><code> * product</code></p>
|
|||
|
<p><code> / quotient</code></p>
|
|||
|
<p><code> DIV integer quotient</code></p>
|
|||
|
<p><code> MOD modulus</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The operators +, -, *, and / apply to operands of numeric types. Both operands must be of the same type, which is also the type of the result. When used as unary operators, - denotes sign inversion and + denotes the identity operation.</p>
|
|||
|
<p>The operators DIV and MOD apply to integer operands only. Let q = x DIV y, and r = x MOD y. Then quotient <emphasis>q</emphasis> and remainder <emphasis>r</emphasis> are defined by the equation</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> x = q*y + r 0 <= r < y</code></p>
|
|||
|
</section>
|
|||
|
<section id="8.2.3"><title><p><emphasis>8.2.3. Set operators</emphasis></p></title>
|
|||
|
<p><code> symbol result</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> + union</code></p>
|
|||
|
<p><code> - difference</code></p>
|
|||
|
<p><code> * intersection</code></p>
|
|||
|
<p><code> / symmetric set difference</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>When used with a single operand of type SET, the minus sign denotes the set complement.</p>
|
|||
|
</section>
|
|||
|
<section id="8.2.4"><title><p><emphasis>8.2.4. Relations</emphasis></p></title>
|
|||
|
<p><code> symbol relation</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> = equal</code></p>
|
|||
|
<p><code> # unequal</code></p>
|
|||
|
<p><code> < less</code></p>
|
|||
|
<p><code> <= less or equal</code></p>
|
|||
|
<p><code> > greater</code></p>
|
|||
|
<p><code> >= greater or equal</code></p>
|
|||
|
<p><code> IN set membership</code></p>
|
|||
|
<p><code> IS type test</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Relations are Boolean. The ordering relations <, <=, >, >= apply to the numeric types, CHAR, and character arrays. The relations = and # also apply to the types BOOLEAN and SET, and to pointer and procedure types. The relations <= and >= denote inclusion when applied to sets.</p>
|
|||
|
<p><emphasis>x IN s</emphasis> stands for "x is an element of s". <emphasis>x</emphasis> must be of type INTEGER, and <emphasis>s</emphasis> of type SET.</p>
|
|||
|
<p><emphasis>v IS T</emphasis> stands for "v is of type T" and is called a <emphasis>type test</emphasis>. It is applicable, if</p>
|
|||
|
<p>1. T is an extension of the declared type T0 of v, and if</p>
|
|||
|
<p>2. v is a variable parameter of record type or v is a pointer.</p>
|
|||
|
<p>Assuming, for instance, that T is an extension of T0 and that v is a designator declared of type T0, then the test <emphasis>v IS T</emphasis> determines whether the actually designated variable is (not only a T0, but also) a T. The value of <emphasis>NIL IS T</emphasis> is undefined.</p>
|
|||
|
<p>Examples of expressions (refer to examples in <a l:href="#7">Ch. 7</a>):</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> 1987 (INTEGER)</code></p>
|
|||
|
<p><code> i DIV 3 (INTEGER)</code></p>
|
|||
|
<p><code> ~p OR q (BOOLEAN)</code></p>
|
|||
|
<p><code> (i+j) * (i-j) (INTEGER)</code></p>
|
|||
|
<p><code> s - {8, 9, 13} (SET)</code></p>
|
|||
|
<p><code> a[i+j] * a[i-j] (REAL)</code></p>
|
|||
|
<p><code> (0<=i) & (i<100) (BOOLEAN)</code></p>
|
|||
|
<p><code> t.key = 0 (BOOLEAN)</code></p>
|
|||
|
<p><code> k IN {i .. j-1} (BOOLEAN)</code></p>
|
|||
|
<p><code> t IS CenterNode (BOOLEAN)</code></p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="9"><title><p>9. Statements</p></title>
|
|||
|
<p>Statements denote actions. There are elementary and structured statements. Elementary statements are not composed of any parts that are themselves statements. They are the assignment and the procedure call. Structured statements are composed of parts that are themselves statements. They are used to express sequencing and conditional, selective, and repetitive execution. A statement may also be empty, in which case it denotes no action. The empty statement is included in order to relax punctuation rules in statement sequences.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> statement = [assignment | ProcedureCall | IfStatement | CaseStatement |</code></p>
|
|||
|
<p><code> WhileStatement | RepeatStatement | ForStatement].</code></p>
|
|||
|
<section id="9.1"><title><p>9.1. Assignments</p></title>
|
|||
|
<p>The assignment serves to replace the current value of a variable by a new value specified by an expression. The assignment operator is written as ":=" and pronounced as <emphasis>becomes</emphasis>.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> assignment = designator ":=" expression.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>If a value parameter is structured (of array or record type), no assignment to it or to its elements are permitted. Neither may assignments be made to imported variables.</p>
|
|||
|
<p>The type of the expression must be the same as that of the designator. The following exceptions hold:</p>
|
|||
|
<p>1. The constant NIL can be assigned to variables of any pointer or procedure type.</p>
|
|||
|
<p>2. Strings can be assigned to any array of characters, provided the number of characters in the string is not greater than that of the array. If it is less, a null character (0X) is appended. Singlecharacter strings can also be assigned to variables of type CHAR.</p>
|
|||
|
<p>3. In the case of records, the type of the source must be an extension of the type of the destination. Examples of assignments <a l:href="#7">(see examples in Ch. 7)</a>:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> i := 0</code></p>
|
|||
|
<p><code> p := i = j</code></p>
|
|||
|
<p><code> x := FLT(i + 1)</code></p>
|
|||
|
<p><code> k := (i + j) DIV 2</code></p>
|
|||
|
<p><code> f := log2</code></p>
|
|||
|
<p><code> s := {2, 3, 5, 7, 11, 13}</code></p>
|
|||
|
<p><code> a[i] := (x+y) * (x-y)</code></p>
|
|||
|
<p><code> t.key := i</code></p>
|
|||
|
<p><code> w[i+1].ch := "A"</code></p>
|
|||
|
</section>
|
|||
|
<section id="9.2"><title><p>9.2. Procedure calls</p></title>
|
|||
|
<p>A procedure call serves to activate a procedure. The procedure call may contain a list of actual parameters which are substituted in place of their corresponding formal parameters defined in the procedure declaration <a l:href="#10">(see Ch. 10)</a>. The correspondence is established by the positions of the parameters in the lists of actual and formal parameters respectively. There exist two kinds of parameters: <emphasis>variable</emphasis> and <emphasis>value</emphasis> parameters.</p>
|
|||
|
<p>In the case of variable parameters, the actual parameter must be a designator denoting a variable. If it designates an element of a structured variable, the selector is evaluated when the formal/actual parameter substitution takes place, i.e. before the execution of the procedure. If the parameter is a value parameter, the corresponding actual parameter must be an expression. This expression is evaluated prior to the procedure activation, and the resulting value is assigned to the formal parameter which now constitutes a local variable <a l:href="#10.1">(see also 10.1.)</a>.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ProcedureCall = designator [ActualParameters].</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Examples of procedure calls:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ReadInt(i) <a l:href="#10">(see Ch. 10)</a></code></p>
|
|||
|
<p><code> WriteInt(2*j + 1, 6)</code></p>
|
|||
|
<p><code> INC(w[k].count)</code></p>
|
|||
|
</section>
|
|||
|
<section id="9.3"><title><p>9.3. Statement sequences</p></title>
|
|||
|
<p>Statement sequences denote the sequence of actions specified by the component statements which are separated by semicolons.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> StatementSequence = statement {";" statement}.</code></p>
|
|||
|
</section>
|
|||
|
<section id="9.4"><title><p>9.4. If statements</p></title>
|
|||
|
<p><code> IfStatement = IF expression THEN StatementSequence</code></p>
|
|||
|
<p><code> {ELSIF expression THEN StatementSequence}</code></p>
|
|||
|
<p><code> [ELSE StatementSequence]</code></p>
|
|||
|
<p><code> END.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>If statements specify the conditional execution of guarded statements. The Boolean expression preceding a statement is called its <emphasis>guard</emphasis>. The guards are evaluated in sequence of occurrence, until one evaluates to TRUE, whereafter its associated statement sequence is executed. If no guard is satisfied, the statement sequence following the symbol ELSE is executed, if there is one.</p>
|
|||
|
<p>Example:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> IF (ch >= "A") & (ch <= "Z") THEN ReadIdentifier</code></p>
|
|||
|
<p><code> ELSIF (ch >= "0") & (ch <= "9") THEN ReadNumber</code></p>
|
|||
|
<p><code> ELSIF ch = 22X THEN ReadString</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
</section>
|
|||
|
<section id="9.5"><title><p>9.5. Case statements</p></title>
|
|||
|
<p>Case statements specify the selection and execution of a statement sequence according to the value of an expression. First the case expression is evaluated, then the statement sequence is executed whose case label list contains the obtained value. The case expression must be of type INTEGER or CHAR, and all labels must be integers or single-character strings, respectively.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> CaseStatement = CASE expression OF case {"|" case} END.</code></p>
|
|||
|
<p><code> case = [CaseLabelList ":" StatementSequence].</code></p>
|
|||
|
<p><code> CaseLabelList = LabelRange {"," LabelRange}.</code></p>
|
|||
|
<p><code> LabelRange = label [".." label].</code></p>
|
|||
|
<p><code> label = integer | string | ident.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Example:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> CASE k OF</code></p>
|
|||
|
<p><code> 0: x := x + y</code></p>
|
|||
|
<p><code> | 1: x := x - y</code></p>
|
|||
|
<p><code> | 2: x := x * y</code></p>
|
|||
|
<p><code> | 3: x := x / y</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
</section>
|
|||
|
<section id="9.6"><title><p>9.6. While statements</p></title>
|
|||
|
<p>While statements specify repetition. If any of the Boolean expressions (guards) yields TRUE, the corresponding statement sequence is executed. The expression evaluation and the statement execution are repeated until none of the Boolean expressions yields TRUE.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> WhileStatement = WHILE expression DO StatementSequence</code></p>
|
|||
|
<p><code> {ELSIF expression DO StatementSequence} END.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Examples:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> WHILE j > 0 DO</code></p>
|
|||
|
<p><code> j := j DIV 2; i := i+1</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
<p><code> WHILE (t # NIL) & (t.key # i) DO</code></p>
|
|||
|
<p><code> t := t.left</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
<p><code> WHILE m > n DO m := m - n</code></p>
|
|||
|
<p><code> ELSIF n > m DO n := n - m</code></p>
|
|||
|
<p><code> END</code></p>
|
|||
|
</section>
|
|||
|
<section id="9.7"><title><p>9.7. Repeat Statements</p></title>
|
|||
|
<p>A repeat statement specifies the repeated execution of a statement sequence until a condition is satisfied. The statement sequence is executed at least once.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> RepeatStatement = REPEAT StatementSequence UNTIL expression.</code></p>
|
|||
|
</section>
|
|||
|
<section id="9.8"><title><p>9.8. For statements</p></title>
|
|||
|
<p>A for statement specifies the repeated execution of a statement sequence for a given number of times, while a progression of values is assigned to an integer variable called the <emphasis>control variable</emphasis> of the for statement.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ForStatement =</code></p>
|
|||
|
<p><code> FOR ident ":=" expression TO expression [BY ConstExpression] DO</code></p>
|
|||
|
<p><code> StatementSequence END .</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The for statement</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> FOR v := beg TO end BY inc DO S END</code></p>
|
|||
|
<empty-line/>
|
|||
|
<empty-line/>is, if <emphasis>inc</emphasis> > 0, equivalent to
|
|||
|
<empty-line/>
|
|||
|
<p><code> v := beg; lim := end;</code></p>
|
|||
|
<p><code> WHILE v <= lim DO S; v := v + inc END</code></p>
|
|||
|
<empty-line/>
|
|||
|
<empty-line/>and if <emphasis>inc</emphasis> < 0 it is equivalent to
|
|||
|
<empty-line/>
|
|||
|
<p><code> v := beg; lim := end;</code></p>
|
|||
|
<p><code> WHILE v >= lim DO S; v := v + inc END</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The types of <emphasis>v</emphasis>, <emphasis>beg</emphasis> and <emphasis>end</emphasis> must be INTEGER, and <emphasis>inc</emphasis> must be an integer (constant expression). If the step is not specified, it is assumed to be 1.</p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="10"><title><p>10. Procedure declarations</p></title>
|
|||
|
<p>Procedure declarations consist of a procedure heading and a procedure body. The heading specifies the procedure identifier, the formal parameters, and the result type (if any). The body contains declarations and statements. The procedure identifier is repeated at the end of the procedure declaration.</p>
|
|||
|
<p>There are two kinds of procedures, namely proper procedures and function procedures. The latter are activated by a function designator as a constituent of an expression, and yield a result that is an operand in the expression. Proper procedures are activated by a procedure call. A function procedure is distinguished in the declaration by indication of the type of its result following the parameter list. Its body must end with a RETURN clause which defines the result of the function procedure.</p>
|
|||
|
<p>All constants, variables, types, and procedures declared within a procedure body are local to the procedure. The values of local variables are undefined upon entry to the procedure. Since procedures may be declared as local objects too, procedure declarations may be nested.</p>
|
|||
|
<p>In addition to its formal parameters and locally declared objects, the objects declared in the environment of the procedure are also visible in the procedure (with the exception of variables and of those objects that have the same name as an object declared locally).</p>
|
|||
|
<p>The use of the procedure identifier in a call within its declaration implies recursive activation of the procedure.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident.</code></p>
|
|||
|
<p><code> ProcedureHeading = PROCEDURE identdef [FormalParameters].</code></p>
|
|||
|
<p><code> ProcedureBody = DeclarationSequence [BEGIN StatementSequence]</code></p>
|
|||
|
<p><code> [RETURN expression] END.</code></p>
|
|||
|
<p><code> DeclarationSequence = [CONST {ConstantDeclaration ";"}]</code></p>
|
|||
|
<p><code> [TYPE {TypeDeclaration ";"}] [VAR {VariableDeclaration ";"}]</code></p>
|
|||
|
<p><code> {ProcedureDeclaration ";"}.</code></p>
|
|||
|
<section id="10.1"><title><p>10.1. Formal parameters</p></title>
|
|||
|
<p>Formal parameters are identifiers which denote actual parameters specified in the procedure call. The correspondence between formal and actual parameters is established when the procedure is called. There are two kinds of parameters, namely <emphasis>value</emphasis> and <emphasis>variable</emphasis> parameters. A variable parameter corresponds to an actual parameter that is a variable, and it stands for that variable. A value parameter corresponds to an actual parameter that is an expression, and it stands for its value, which cannot be changed by assignment. However, if a value parameter is of a scalar type, it represents a local variable to which the value of the actual expression is initially assigned.</p>
|
|||
|
<p>The kind of a parameter is indicated in the formal parameter list: Variable parameters are denoted by the symbol VAR and value parameters by the absence of a prefix.</p>
|
|||
|
<p>A function procedure without parameters must have an empty parameter list. It must be called by a function designator whose actual parameter list is empty too.</p>
|
|||
|
<p>Formal parameters are local to the procedure, i.e. their scope is the program text which constitutes the procedure declaration.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> FormalParameters = "(" [FPSection {";" FPSection}] ")" [":" qualident].</code></p>
|
|||
|
<p><code> FPSection = [VAR] ident {"," ident} ":" FormalType.</code></p>
|
|||
|
<p><code> FormalType = {ARRAY OF} qualident.</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The type of each formal parameter is specified in the parameter list. For variable parameters, it must be identical to the corresponding actual parameter's type, except in the case of a record, where it must be a base type of the corresponding actual parameter's type.</p>
|
|||
|
<p>If the formal parameter's type is specified as</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ARRAY OF T</code></p>
|
|||
|
<empty-line/>
|
|||
|
<empty-line/>the parameter is said to be an <emphasis>open array</emphasis>, and the corresponding actual parameter may be of arbitrary length.
|
|||
|
<p>If a formal parameter specifies a procedure type, then the corresponding actual parameter must be either a procedure declared globally, or a variable (or parameter) of that procedure type. It cannot be a predefined procedure. The result type of a procedure can be neither a record nor an array.</p>
|
|||
|
<p>Examples of procedure declarations:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> PROCEDURE ReadInt(VAR x: INTEGER);</code></p>
|
|||
|
<p><code> VAR i : INTEGER; ch: CHAR;</code></p>
|
|||
|
<p><code> BEGIN i := 0; Read(ch);</code></p>
|
|||
|
<p><code> WHILE ("0" <= ch) & (ch <= "9") DO</code></p>
|
|||
|
<p><code> i := 10*i + (ORD(ch)-ORD("0")); Read(ch)</code></p>
|
|||
|
<p><code> END ;</code></p>
|
|||
|
<p><code> x := i</code></p>
|
|||
|
<p><code> END ReadInt</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> PROCEDURE WriteInt(x: INTEGER); (* 0 <= x < 10^5 *)</code></p>
|
|||
|
<p><code> VAR i: INTEGER;</code></p>
|
|||
|
<p><code> buf: ARRAY 5 OF INTEGER;</code></p>
|
|||
|
<p><code> BEGIN i := 0;</code></p>
|
|||
|
<p><code> REPEAT buf[i] := x MOD 10; x := x DIV 10; INC(i) UNTIL x = 0;</code></p>
|
|||
|
<p><code> REPEAT DEC(i); Write(CHR(buf[i] + ORD("0"))) UNTIL i = 0</code></p>
|
|||
|
<p><code> END WriteInt</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> PROCEDURE log2(x: INTEGER): INTEGER;</code></p>
|
|||
|
<p><code> VAR y: INTEGER; (*assume x>0*)</code></p>
|
|||
|
<p><code> BEGIN y := 0;</code></p>
|
|||
|
<p><code> WHILE x > 1 DO x := x DIV 2; INC(y) END ;</code></p>
|
|||
|
<p><code> RETURN y</code></p>
|
|||
|
<p><code> END log2</code></p>
|
|||
|
</section>
|
|||
|
<section id="10.2"><title><p>10.2. Predefined procedures</p></title>
|
|||
|
<p>The following table lists the predefined procedures. Some are generic procedures, i.e. they apply to several types of operands. v stands for a variable, x and n for expressions, and T for a type.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> <emphasis>Function procedures:</emphasis></code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> Name Argument type Result type Function</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ABS(x) numeric type type of x absolute value</code></p>
|
|||
|
<p><code> ODD(x) INTEGER BOOLEAN x MOD 2 = 1</code></p>
|
|||
|
<p><code> LEN(v) v: array INTEGER the length of v</code></p>
|
|||
|
<p><code> LSL(x, n) x, n: INTEGER type of x logical shift left, x * 2<sup>n</sup></code></p>
|
|||
|
<p><code> ASR(x, n) x, n: INTEGER type of x signed shift right, x DIV 2<sup>n</sup></code></p>
|
|||
|
<p><code> ROR(x, n) x. n: INTEGER type of x x rotated right by n bits</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> <emphasis>Type conversion functions:</emphasis></code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> Name Argument type Result type Function</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> FLOOR(x) REAL, LONGREAL INTEGER largest integer <= x</code></p>
|
|||
|
<p><code> FLT(x) INTEGER REAL identity</code></p>
|
|||
|
<p><code> ORD(x) CHAR, BOOLEAN, SET INTEGER ordinal number of x</code></p>
|
|||
|
<p><code> CHR(x) INTEGER CHAR character with ordinal number x</code></p>
|
|||
|
<p><code> LONG(x) REAL LONGREAL x</code></p>
|
|||
|
<p><code> SHORT(x) LONGREAL REAL x</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> <emphasis>Proper procedures:</emphasis></code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> Name Argument types Function</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> INC(v) INTEGER v := v + 1</code></p>
|
|||
|
<p><code> INC(v, n) INTEGER v := v + n</code></p>
|
|||
|
<p><code> DEC(v) INTEGER v := v - 1</code></p>
|
|||
|
<p><code> DEC(v, n) INTEGER v := v - n</code></p>
|
|||
|
<p><code> INCL(v, x) v: SET; x: INTEGER v := v + {x}</code></p>
|
|||
|
<p><code> EXCL(v, x) v: SET; x: INTEGER v := v - {x}</code></p>
|
|||
|
<p><code> COPY(x, v) x: character array, string v := x</code></p>
|
|||
|
<p><code> v: character array</code></p>
|
|||
|
<p><code> NEW(v) pointer type allocate v^</code></p>
|
|||
|
<p><code> ASSERT(b) BOOLEAN abort, if ~b</code></p>
|
|||
|
<p><code> ASSERT(b, n) BOOLEAN, INTEGER</code></p>
|
|||
|
<p><code> PACK(x, y) REAL; INTEGER pack x and y into x</code></p>
|
|||
|
<p><code> UNPK(x, y) REAL; INTEGER unpack x into x and y</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>Procedures INC and DEC may have an explicit increment or decrement. It must be a constant. Also for INCL and EXCL, <emphasis>x</emphasis> must be a constant. The second parameter <emphasis>n</emphasis> of ASSERT is a value transmitted to the system as an abort parameter.</p>
|
|||
|
<p>The parameter <emphasis>y</emphasis> of PACK represents the exponent of <emphasis>x</emphasis>. PACK(x, y) is equivalent to x := x * 2<sup>y</sup>. UNPK is the reverse operation of PACK. The resulting <emphasis>x</emphasis> is normalized, i.e. 1.0 <= x < 2.0.</p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="11"><title><p>11. Modules</p></title>
|
|||
|
<p>A module is a collection of declarations of constants, types, variables, and procedures, and a sequence of statements for the purpose of assigning initial values to the variables. A module typically constitutes a text that is compilable as a unit.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> module = MODULE ident ";" [ImportList ";"] DeclarationSequence</code></p>
|
|||
|
<p><code> [BEGIN StatementSequence] END ident "." .</code></p>
|
|||
|
<p><code> ImportList = IMPORT import {"," import} ";" .</code></p>
|
|||
|
<p><code> Import = ident [":=" ident].</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p>The import list specifies the modules of which the module is a client. If an identifier x is exported from a module M, and if M is listed in a module's import list, then x is referred to as M.x. If the form "M := M1" is used in the import list, an exported object x declared within M1 is referenced in the importing module as M.x .</p>
|
|||
|
<p>Identifiers that are to be visible in client modules, i.e. which are to be exported, must be marked by an asterisk (export mark) in their declaration. Variables cannot be exported, with the exception of those of scalar types in read-only mode.</p>
|
|||
|
<p>The statement sequence following the symbol BEGIN is executed when the module is added to a system (loaded). Individual (parameterless) procedures can thereafter be activated from the system, and these procedures serve as commands.</p>
|
|||
|
<p>Example:</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> MODULE Out; (*exported procedures: Write, WriteInt, WriteLn*)</code></p>
|
|||
|
<p><code> IMPORT Texts, Oberon;</code></p>
|
|||
|
<p><code> VAR W: Texts.Writer;</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> PROCEDURE Write*(ch: CHAR);</code></p>
|
|||
|
<p><code> BEGIN Texts.Write(W, ch)</code></p>
|
|||
|
<p><code> END ;</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> PROCEDURE WriteInt*(x, n: INTEGER);</code></p>
|
|||
|
<p><code> VAR i: INTEGER; a: ARRAY 16 OF CHAR;</code></p>
|
|||
|
<p><code> BEGIN i := 0;</code></p>
|
|||
|
<p><code> IF x < 0 THEN Texts.Write(W, "-"); x := -x END ;</code></p>
|
|||
|
<p><code> REPEAT a[i] := CHR(x MOD 10 + ORD("0")); x := x DIV 10; INC(i) UNTIL x = 0;</code></p>
|
|||
|
<p><code> REPEAT Texts.Write(W, " "); DEC(n) UNTIL n <= i;</code></p>
|
|||
|
<p><code> REPEAT DEC(i); Texts.Write(W, a[i]) UNTIL i = 0</code></p>
|
|||
|
<p><code> END WriteInt;</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> PROCEDURE WriteLn*;</code></p>
|
|||
|
<p><code> BEGIN Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)</code></p>
|
|||
|
<p><code> END WriteLn;</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> BEGIN Texts.OpenWriter(W)</code></p>
|
|||
|
<p><code> END Out.</code></p>
|
|||
|
<section id="11.1"><title><p>11.1 The Module SYSTEM</p></title>
|
|||
|
<p>The optional module SYSTEM contains definitions that are necessary to program low-level operations referring directly to resources particular to a given computer and/or implementation. These include for example facilities for accessing devices that are controlled by the computer, and perhaps facilities to break the data type compatibility rules otherwise imposed by the language definition. It is strongly recommended to restrict their use to specific low-level modules, as such modules are inherently non-portable and not "type-safe". However, they are easily recognized due to the identifier SYSTEM appearing in their import lists. The subsequent definitions are generally applicable. However, individual implementations may include in their module SYSTEM additional definitions that are particular to the specific, underlying computer. In the following, <emphasis>v</emphasis> stands for a variable, <emphasis>x</emphasis>, <emphasis>a</emphasis>, and <emphasis>n</emphasis> for expressions.</p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> <emphasis>Function procedures:</emphasis></code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> Name Argument types Result type Function</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> ADR(v) any INTEGER address of variable v</code></p>
|
|||
|
<p><code> SIZE(T) any type INTEGER size in bytes</code></p>
|
|||
|
<p><code> BIT(a, n) a, n: INTEGER BOOLEAN bit n of mem[a]</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> <emphasis>Proper procedures:</emphasis></code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> Name Argument types Function</code></p>
|
|||
|
<empty-line/>
|
|||
|
<p><code> GET(a, v) a: INTEGER; v: any basic type v := mem[a]</code></p>
|
|||
|
<p><code> PUT(a, x) a: INTEGER; x: any basic type mem[a] := x</code></p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="app"><title><p>Appendix</p><p>The Syntax of Oberon</p></title>
|
|||
|
<p><code> letter = "A" | "B" | <20> | "Z" | "a" | "b" | <20> | "z".</code></p>
|
|||
|
<p><code> digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".</code></p>
|
|||
|
<p><code> hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".</code></p>
|
|||
|
<p><code> ident = letter {letter | digit}.</code></p>
|
|||
|
<p><code> qualident = [ident "."] ident.</code></p>
|
|||
|
<p><code> identdef = ident ["*"].</code></p>
|
|||
|
<p><code> integer = digit {digit} | digit {hexDigit} "H".</code></p>
|
|||
|
<p><code> real = digit {digit} "." {digit} [ScaleFactor].</code></p>
|
|||
|
<p><code> ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.</code></p>
|
|||
|
<p><code> number = integer | real.</code></p>
|
|||
|
<p><code> string = """ {character} """ | digit {hexDigit} "X".</code></p>
|
|||
|
<p><code> ConstantDeclaration = identdef "=" ConstExpression.</code></p>
|
|||
|
<p><code> ConstExpression = expression.</code></p>
|
|||
|
<p><code> TypeDeclaration = identdef "=" StrucType.</code></p>
|
|||
|
<p><code> StrucType = ArrayType | RecordType | PointerType | ProcedureType.</code></p>
|
|||
|
<p><code> type = qualident | StrucType.</code></p>
|
|||
|
<p><code> ArrayType = ARRAY length {"," length} OF type.</code></p>
|
|||
|
<p><code> length = ConstExpression.</code></p>
|
|||
|
<p><code> RecordType = RECORD ["(" BaseType ")"] [FieldListSequence] END.</code></p>
|
|||
|
<p><code> BaseType = qualident.</code></p>
|
|||
|
<p><code> FieldListSequence = FieldList {";" FieldList}.</code></p>
|
|||
|
<p><code> FieldList = IdentList ":" type.</code></p>
|
|||
|
<p><code> IdentList = identdef {"," identdef}.</code></p>
|
|||
|
<p><code> PointerType = POINTER TO type.</code></p>
|
|||
|
<p><code> ProcedureType = PROCEDURE [FormalParameters].</code></p>
|
|||
|
<p><code> VariableDeclaration = IdentList ":" type.</code></p>
|
|||
|
<p><code> expression = SimpleExpression [relation SimpleExpression].</code></p>
|
|||
|
<p><code> relation = "=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.</code></p>
|
|||
|
<p><code> SimpleExpression = ["+" | "-"] term {AddOperator term}.</code></p>
|
|||
|
<p><code> AddOperator = "+" | "-" | OR.</code></p>
|
|||
|
<p><code> term = factor {MulOperator factor}.</code></p>
|
|||
|
<p><code> MulOperator = "*" | "/" | DIV | MOD | "&".</code></p>
|
|||
|
<p><code> factor = number | string | NIL | TRUE | FALSE |</code></p>
|
|||
|
<p><code> set | designator [ActualParameters] | "(" expression ")" | "~" factor.</code></p>
|
|||
|
<p><code> designator = qualident {selector}.</code></p>
|
|||
|
<p><code> selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".</code></p>
|
|||
|
<p><code> set = "{" [element {"," element}] "}".</code></p>
|
|||
|
<p><code> element = expression [".." expression].</code></p>
|
|||
|
<p><code> ExpList = expression {"," expression}.</code></p>
|
|||
|
<p><code> ActualParameters = "(" [ExpList] ")" .</code></p>
|
|||
|
<p><code> statement = [assignment | ProcedureCall | IfStatement | CaseStatement |</code></p>
|
|||
|
<p><code> WhileStatement | RepeatStatement | ForStatement].</code></p>
|
|||
|
<p><code> assignment = designator ":=" expression.</code></p>
|
|||
|
<p><code> ProcedureCall = designator [ActualParameters].</code></p>
|
|||
|
<p><code> StatementSequence = statement {";" statement}.</code></p>
|
|||
|
<p><code> IfStatement = IF expression THEN StatementSequence</code></p>
|
|||
|
<p><code> {ELSIF expression THEN StatementSequence}</code></p>
|
|||
|
<p><code> [ELSE StatementSequence] END.</code></p>
|
|||
|
<p><code> CaseStatement = CASE expression OF case {"|" case} END.</code></p>
|
|||
|
<p><code> case = [CaseLabelList ":" StatementSequence].</code></p>
|
|||
|
<p><code> CaseLabelList = LabelRange {"," LabelRange}.</code></p>
|
|||
|
<p><code> LabelRange = label [".." label].</code></p>
|
|||
|
<p><code> label = integer | string | ident.</code></p>
|
|||
|
<p><code> WhileStatement = WHILE expression DO StatementSequence</code></p>
|
|||
|
<p><code> {ELSIF expression DO StatementSequence} END.</code></p>
|
|||
|
<p><code> RepeatStatement = REPEAT StatementSequence UNTIL expression.</code></p>
|
|||
|
<p><code> ForStatement = FOR ident ":=" expression TO expression [BY ConstExpression]</code></p>
|
|||
|
<p><code> DO StatementSequence END.</code></p>
|
|||
|
<p><code> ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident.</code></p>
|
|||
|
<p><code> ProcedureHeading = PROCEDURE identdef [FormalParameters].</code></p>
|
|||
|
<p><code> ProcedureBody = DeclarationSequence [BEGIN StatementSequence]</code></p>
|
|||
|
<p><code> [RETURN expression] END.</code></p>
|
|||
|
<p><code> DeclarationSequence = [CONST {ConstDeclaration ";"}]</code></p>
|
|||
|
<p><code> [TYPE {TypeDeclaration ";"}]</code></p>
|
|||
|
<p><code> [VAR {VariableDeclaration ";"}]</code></p>
|
|||
|
<p><code> {ProcedureDeclaration ";"}.</code></p>
|
|||
|
<p><code> FormalParameters = "(" [FPSection {";" FPSection}] ")" [":" qualident].</code></p>
|
|||
|
<p><code> FPSection = [VAR] ident {"," ident} ":" FormalType.</code></p>
|
|||
|
<p><code> FormalType = {ARRAY OF} qualident.</code></p>
|
|||
|
<p><code> module = MODULE ident ";" [ImportList] DeclarationSequence</code></p>
|
|||
|
<p><code> [BEGIN StatementSequence] END ident "." .</code></p>
|
|||
|
<p><code> ImportList = IMPORT import {"," import} ";".</code></p>
|
|||
|
<p><code> import = ident [":=" ident].</code></p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
</body>
|
|||
|
</FictionBook>
|
|||
|
|
|||
|
|
|||
|
|