Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. <?xml encoding = "windows-1252"?>
  2. <FictionBook xmlns:l="http://www.w3.org/1999/xlink" xmlns="http://www.gribuser.ru/xml/fictionbook/2.0">
  3. <description></description>
  4. <body>
  5. <section><title><p>The Programming Language Oberon</p><p>Revision 22.9.2011</p><p>Niklaus Wirth</p></title>
  6. <epigraph><p>Make it as simple as possible, but not simpler.</p><text-author>(A. Einstein)</text-author></epigraph>
  7. <p>Table of Contents</p>
  8. <empty-line/>
  9. <p><a l:href="#1">1. Introduction</a></p>
  10. <p><a l:href="#2">2. Syntax</a></p>
  11. <p><a l:href="#3">3. Vocabulary</a></p>
  12. <p><a l:href="#4">4. Declarations and scope rules</a></p>
  13. <p><a l:href="#5">5. Constant declarations</a></p>
  14. <p><a l:href="#6">6. Type declarations</a></p>
  15. <p><a l:href="#7">7. Variable declarations</a></p>
  16. <p><a l:href="#8">8. Expressions</a></p>
  17. <p><a l:href="#9">9. Statements</a></p>
  18. <p><a l:href="#10">10. Procedure declarations</a></p>
  19. <p><a l:href="#11">11. Modules</a></p>
  20. <p><a l:href="#app">Appendix: The Syntax of Oberon</a></p>
  21. <section id="1"><title><p>1. Introduction</p></title>
  22. <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>
  23. <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>
  24. <p>This document describes the language defined in 1988/90 as revised in 2007/11.</p>
  25. </section>
  26. <section id="2"><title><p>2. Syntax</p></title>
  27. <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>
  28. <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>
  29. </section>
  30. <section id="3"><title><p>3. Vocabulary</p></title>
  31. <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>
  32. <p><emphasis>Identifiers</emphasis> are sequences of letters and digits. The first character must be a letter.</p>
  33. <empty-line/>
  34. <p><code>        ident = letter {letter | digit}.</code></p>
  35. <empty-line/>
  36. <p>Examples:</p>
  37. <empty-line/>
  38. <p><code>        x scan Oberon GetSymbol firstLetter</code></p>
  39. <empty-line/>
  40. <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>
  41. <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>
  42. <empty-line/>
  43. <p><code>        number = integer | real.</code></p>
  44. <p><code>        integer = digit {digit} | digit {hexDigit} "H".</code></p>
  45. <p><code>        real = digit {digit} "." {digit} [ScaleFactor].</code></p>
  46. <p><code>        ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.</code></p>
  47. <p><code>        hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".</code></p>
  48. <p><code>        digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".</code></p>
  49. <empty-line/>
  50. <p>Examples:</p>
  51. <empty-line/>
  52. <p><code>        1987</code></p>
  53. <p><code>        100H               = 256</code></p>
  54. <p><code>        12.3</code></p>
  55. <p><code>        4.567E8            = 456700000</code></p>
  56. <empty-line/>
  57. <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>
  58. <empty-line/>
  59. <p><code>        string = """ {character} """ | digit {hexdigit} "X" .</code></p>
  60. <empty-line/>
  61. <p>Examples:</p>
  62. <empty-line/>
  63. <p><code>        "OBERON" "Don't worry!" 22X</code></p>
  64. <empty-line/>
  65. <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>
  66. <empty-line/>
  67. <p><code>        +   :=   ARRAY   IMPORT      THEN</code></p>
  68. <p><code>        -   ^    BEGIN   IN          TO</code></p>
  69. <p><code>        *   =    BY      IS          TRUE</code></p>
  70. <p><code>        /   #    CASE    MOD         TYPE</code></p>
  71. <p><code>        ~   &lt;    CONST   MODULE      UNTIL</code></p>
  72. <p><code>        &amp;   &gt;    DIV     NIL         VAR</code></p>
  73. <p><code>        .   &lt;=   DO      OF          WHILE</code></p>
  74. <p><code>        ,   &gt;=   ELSE    OR</code></p>
  75. <p><code>        ;   ..   ELSIF   POINTER</code></p>
  76. <p><code>        |   :    END     PROCEDURE</code></p>
  77. <p><code>        (   )    FALSE   RECORD</code></p>
  78. <p><code>        [   ]    FOR     REPEAT</code></p>
  79. <p><code>        {   }    IF      RETURN</code></p>
  80. <empty-line/>
  81. <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>
  82. </section>
  83. <section id="4"><title><p>4. Declarations and scope rules</p></title>
  84. <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>
  85. <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>
  86. <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>
  87. <p>2. Field identifiers of a record declaration <a l:href="#6.3">(see 6.3)</a> are valid in field designators only.</p>
  88. <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>
  89. <empty-line/>
  90. <p><code>        qualident = [ident "."] ident.</code></p>
  91. <p><code>        identdef = ident ["*"].</code></p>
  92. <empty-line/>
  93. <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>
  94. <empty-line/>
  95. <p><code>        ABS     ASR    ASSERT     BOOLEAN   CHAR</code></p>
  96. <p><code>        CHR     COPY   DEC        EXCL      FLOOR</code></p>
  97. <p><code>        FLT     INC    INCL       INTEGER   LEN</code></p>
  98. <p><code>        LSL     LONG   LONGREAL   NEW       ODD</code></p>
  99. <p><code>        ORD     PACK   REAL       ROR       SET</code></p>
  100. <p><code>        SHORT   UNPK</code></p>
  101. </section>
  102. <section id="5"><title><p>5. Constant declarations</p></title>
  103. <p>A constant declaration associates an identifier with a constant value.</p>
  104. <empty-line/>
  105. <p><code>        ConstantDeclaration = identdef "=" ConstExpression.</code></p>
  106. <p><code>        ConstExpression = expression.</code></p>
  107. <empty-line/>
  108. <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>
  109. <empty-line/>
  110. <p><code>        N      =  100</code></p>
  111. <p><code>        limit  =  2*N - 1</code></p>
  112. <p><code>        all    =  {0 .. WordSize-1}</code></p>
  113. <p><code>        name   =  "Oberon"</code></p>
  114. </section>
  115. <section id="6"><title><p>6. Type declarations</p></title>
  116. <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>
  117. <empty-line/>
  118. <p><code>        TypeDeclaration = identdef "=" StrucType.</code></p>
  119. <p><code>        StrucType = ArrayType | RecordType | PointerType | ProcedureType.</code></p>
  120. <p><code>        type = qualident | StrucType.</code></p>
  121. <empty-line/>
  122. <p>Examples:</p>
  123. <empty-line/>
  124. <p><code>        Table       =  ARRAY N OF REAL</code></p>
  125. <p><code>        Tree        =  POINTER TO Node</code></p>
  126. <p><code>        Node        =  RECORD</code></p>
  127. <p><code>                         key: INTEGER;</code></p>
  128. <p><code>                         left, right: Tree</code></p>
  129. <p><code>                       END</code></p>
  130. <p><code>        CenterNode  =  RECORD (Node)</code></p>
  131. <p><code>                         name: ARRAY 32 OF CHAR;</code></p>
  132. <p><code>                         subnode: Tree</code></p>
  133. <p><code>                       END</code></p>
  134. <p><code>        Function    =  PROCEDURE (x: INTEGER): INTEGER</code></p>
  135. <section id="6.1"><title><p>6.1. Basic types</p></title>
  136. <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>
  137. <empty-line/>
  138. <p><code>        BOOLEAN     the truth values TRUE and FALSE</code></p>
  139. <p><code>        CHAR        the characters of a standard character set</code></p>
  140. <p><code>        INTEGER     the integers</code></p>
  141. <p><code>        REAL        real numbers</code></p>
  142. <p><code>        LONGREAL    real numbers</code></p>
  143. <p><code>        SET         the sets of integers between 0 and 31</code></p>
  144. <empty-line/>
  145. <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>
  146. </section>
  147. <section id="6.2"><title><p>6.2. Array types</p></title>
  148. <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>
  149. <empty-line/>
  150. <p><code>        ArrayType = ARRAY length {"," length} OF type.</code></p>
  151. <p><code>        length = ConstExpression.</code></p>
  152. <empty-line/>
  153. <p>A declaration of the form</p>
  154. <empty-line/>
  155. <p><code>        ARRAY N0, N1, ... , Nk OF T</code></p>
  156. <p></p><empty-line/>is understood as an abbreviation of the declaration<empty-line/>
  157. <p><code>        ARRAY N0 OF</code></p>
  158. <p><code>                ARRAY N1 OF</code></p>
  159. <p><code>                        ...</code></p>
  160. <p><code>                        ARRAY Nk OF T</code></p>
  161. <empty-line/>
  162. <p>Examples of array types:</p>
  163. <empty-line/>
  164. <p><code>        ARRAY N OF INTEGER</code></p>
  165. <p><code>        ARRAY 10, 20 OF REAL</code></p>
  166. </section>
  167. <section id="6.3"><title><p>6.3. Record types</p></title>
  168. <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>
  169. <empty-line/>
  170. <p><code>        RecordType = RECORD ["(" BaseType ")"] [FieldListSequence] END.</code></p>
  171. <p><code>        BaseType = qualident.</code></p>
  172. <p><code>        FieldListSequence = FieldList {";" FieldList}.</code></p>
  173. <p><code>        FieldList = IdentList ":" type.</code></p>
  174. <p><code>        IdentList = identdef {"," identdef}.</code></p>
  175. <empty-line/>
  176. <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>
  177. <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>
  178. <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>
  179. <p>Examples of record types:</p>
  180. <empty-line/>
  181. <p><code>        RECORD day, month, year: INTEGER</code></p>
  182. <p><code>        END</code></p>
  183. <p><code>        RECORD</code></p>
  184. <p><code>                name, firstname: ARRAY 32 OF CHAR;</code></p>
  185. <p><code>                age: INTEGER;</code></p>
  186. <p><code>                salary: REAL</code></p>
  187. <p><code>        END</code></p>
  188. </section>
  189. <section id="6.4"><title><p>6.4. Pointer types</p></title>
  190. <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>
  191. <empty-line/>
  192. <p><code>        PointerType = POINTER TO type.</code></p>
  193. <empty-line/>
  194. <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>
  195. </section>
  196. <section id="6.5"><title><p>6.5. Procedure types</p></title>
  197. <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>
  198. <empty-line/>
  199. <p><code>        ProcedureType = PROCEDURE [FormalParameters].</code></p>
  200. </section>
  201. </section>
  202. <section id="7"><title><p>7. Variable declarations</p></title>
  203. <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>
  204. <empty-line/>
  205. <p><code>        VariableDeclaration = IdentList ":" type.</code></p>
  206. <empty-line/>
  207. <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>
  208. <empty-line/>
  209. <p><code>        i, j, k: INTEGER</code></p>
  210. <p><code>        x, y: REAL</code></p>
  211. <p><code>        p, q: BOOLEAN</code></p>
  212. <p><code>        s: SET</code></p>
  213. <p><code>        f: Function</code></p>
  214. <p><code>        a: ARRAY 100 OF REAL</code></p>
  215. <p><code>        w: ARRAY 16 OF</code></p>
  216. <p><code>                RECORD</code></p>
  217. <p><code>                        ch: CHAR;</code></p>
  218. <p><code>                        count: INTEGER</code></p>
  219. <p><code>                END</code></p>
  220. <p><code>        t: Tree</code></p>
  221. </section>
  222. <section id="8"><title><p>8. Expressions</p></title>
  223. <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>
  224. <section id="8.1"><title><p>8.1. Operands</p></title>
  225. <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>
  226. <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>
  227. <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>
  228. <p>1. <emphasis>T0</emphasis> is an extension of the declared type <emphasis>T</emphasis> of <emphasis>v</emphasis>, and if</p>
  229. <p>2. <emphasis>v</emphasis> is a variable parameter of record type, or <emphasis>v</emphasis> is a pointer.</p>
  230. <empty-line/>
  231. <p><code>        designator = qualident {selector}.</code></p>
  232. <p><code>        selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".</code></p>
  233. <p><code>        ExpList = expression {"," expression}.</code></p>
  234. <empty-line/>
  235. <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>
  236. <p>Examples of designators <a l:href="#7">(see examples in Ch. 7)</a>:</p>
  237. <empty-line/>
  238. <p><code>        i                        (INTEGER)</code></p>
  239. <p><code>        a[i]                     (REAL)</code></p>
  240. <p><code>        w[3].ch                  (CHAR)</code></p>
  241. <p><code>        t.key                    (INTEGER)</code></p>
  242. <p><code>        t.left.right             (Tree)</code></p>
  243. <p><code>        t(CenterNode).subnode    (Tree)</code></p>
  244. </section>
  245. <section id="8.2"><title><p>8.2. Operators</p></title>
  246. <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>
  247. <empty-line/>
  248. <p><code>        expression = SimpleExpression [relation SimpleExpression].</code></p>
  249. <p><code>        relation = "=" | "#" | "&lt;" | "&lt;=" | "&gt;" | "&gt;=" | IN | IS.</code></p>
  250. <p><code>        SimpleExpression = ["+"|"-"] term {AddOperator term}.</code></p>
  251. <p><code>        AddOperator = "+" | "-" | OR.</code></p>
  252. <p><code>        term = factor {MulOperator factor}.</code></p>
  253. <p><code>        MulOperator = "*" | "/" | DIV | MOD | "&amp;" .</code></p>
  254. <p><code>        factor = number | string | NIL | TRUE | FALSE |</code></p>
  255. <p><code>                set | designator [ActualParameters] | "(" expression ")" | "~" factor.</code></p>
  256. <p><code>        set = "{" [element {"," element}] "}".</code></p>
  257. <p><code>        element = expression [".." expression].</code></p>
  258. <p><code>        ActualParameters = "(" [ExpList] ")" .</code></p>
  259. <empty-line/>
  260. <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>
  261. <section id="8.2.1"><title><p><emphasis>8.2.1. Logical operators</emphasis></p></title>
  262. <p><code>        symbol      result</code></p>
  263. <empty-line/>
  264. <p><code>        OR          logical disjunction</code></p>
  265. <p><code>        &amp;           logical conjunction</code></p>
  266. <p><code>        ~           negation</code></p>
  267. <empty-line/>
  268. <p>These operators apply to BOOLEAN operands and yield a BOOLEAN result.</p>
  269. <empty-line/>
  270. <p><code>        p OR q      stands for     "if p then TRUE, else q"</code></p>
  271. <p><code>        p &amp; q       stands for     "if p then q, else FALSE"</code></p>
  272. <p><code>        ~ p         stands for     "not p"</code></p>
  273. </section>
  274. <section id="8.2.2"><title><p><emphasis>8.2.2. Arithmetic operators</emphasis></p></title>
  275. <p><code>        symbol      result</code></p>
  276. <empty-line/>
  277. <p><code>        +           sum</code></p>
  278. <p><code>        -           difference</code></p>
  279. <p><code>        *           product</code></p>
  280. <p><code>        /           quotient</code></p>
  281. <p><code>        DIV         integer quotient</code></p>
  282. <p><code>        MOD         modulus</code></p>
  283. <empty-line/>
  284. <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>
  285. <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>
  286. <empty-line/>
  287. <p><code>        x = q*y + r    0 &lt;= r &lt; y</code></p>
  288. </section>
  289. <section id="8.2.3"><title><p><emphasis>8.2.3. Set operators</emphasis></p></title>
  290. <p><code>        symbol      result</code></p>
  291. <empty-line/>
  292. <p><code>        +           union</code></p>
  293. <p><code>        -           difference</code></p>
  294. <p><code>        *           intersection</code></p>
  295. <p><code>        /           symmetric set difference</code></p>
  296. <empty-line/>
  297. <p>When used with a single operand of type SET, the minus sign denotes the set complement.</p>
  298. </section>
  299. <section id="8.2.4"><title><p><emphasis>8.2.4. Relations</emphasis></p></title>
  300. <p><code>        symbol      relation</code></p>
  301. <empty-line/>
  302. <p><code>        =           equal</code></p>
  303. <p><code>        #           unequal</code></p>
  304. <p><code>        &lt;           less</code></p>
  305. <p><code>        &lt;=          less or equal</code></p>
  306. <p><code>        &gt;           greater</code></p>
  307. <p><code>        &gt;=          greater or equal</code></p>
  308. <p><code>        IN          set membership</code></p>
  309. <p><code>        IS          type test</code></p>
  310. <empty-line/>
  311. <p>Relations are Boolean. The ordering relations &lt;, &lt;=, &gt;, &gt;= 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 &lt;= and &gt;= denote inclusion when applied to sets.</p>
  312. <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>
  313. <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>
  314. <p>1. T is an extension of the declared type T0 of v, and if</p>
  315. <p>2. v is a variable parameter of record type or v is a pointer.</p>
  316. <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>
  317. <p>Examples of expressions (refer to examples in <a l:href="#7">Ch. 7</a>):</p>
  318. <empty-line/>
  319. <p><code>        1987                 (INTEGER)</code></p>
  320. <p><code>        i DIV 3              (INTEGER)</code></p>
  321. <p><code>        ~p OR q              (BOOLEAN)</code></p>
  322. <p><code>        (i+j) * (i-j)        (INTEGER)</code></p>
  323. <p><code>        s - {8, 9, 13}       (SET)</code></p>
  324. <p><code>        a[i+j] * a[i-j]      (REAL)</code></p>
  325. <p><code>        (0&lt;=i) &amp; (i&lt;100)     (BOOLEAN)</code></p>
  326. <p><code>        t.key = 0            (BOOLEAN)</code></p>
  327. <p><code>        k IN {i .. j-1}      (BOOLEAN)</code></p>
  328. <p><code>        t IS CenterNode      (BOOLEAN)</code></p>
  329. </section>
  330. </section>
  331. </section>
  332. <section id="9"><title><p>9. Statements</p></title>
  333. <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>
  334. <empty-line/>
  335. <p><code>        statement = [assignment | ProcedureCall | IfStatement | CaseStatement |</code></p>
  336. <p><code>                WhileStatement | RepeatStatement | ForStatement].</code></p>
  337. <section id="9.1"><title><p>9.1. Assignments</p></title>
  338. <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>
  339. <empty-line/>
  340. <p><code>        assignment = designator ":=" expression.</code></p>
  341. <empty-line/>
  342. <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>
  343. <p>The type of the expression must be the same as that of the designator. The following exceptions hold:</p>
  344. <p>1. The constant NIL can be assigned to variables of any pointer or procedure type.</p>
  345. <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>
  346. <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>
  347. <empty-line/>
  348. <p><code>        i := 0</code></p>
  349. <p><code>        p := i = j</code></p>
  350. <p><code>        x := FLT(i + 1)</code></p>
  351. <p><code>        k := (i + j) DIV 2</code></p>
  352. <p><code>        f := log2</code></p>
  353. <p><code>        s := {2, 3, 5, 7, 11, 13}</code></p>
  354. <p><code>        a[i] := (x+y) * (x-y)</code></p>
  355. <p><code>        t.key := i</code></p>
  356. <p><code>        w[i+1].ch := "A"</code></p>
  357. </section>
  358. <section id="9.2"><title><p>9.2. Procedure calls</p></title>
  359. <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>
  360. <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>
  361. <empty-line/>
  362. <p><code>        ProcedureCall = designator [ActualParameters].</code></p>
  363. <empty-line/>
  364. <p>Examples of procedure calls:</p>
  365. <empty-line/>
  366. <p><code>        ReadInt(i) <a l:href="#10">(see Ch. 10)</a></code></p>
  367. <p><code>        WriteInt(2*j + 1, 6)</code></p>
  368. <p><code>        INC(w[k].count)</code></p>
  369. </section>
  370. <section id="9.3"><title><p>9.3. Statement sequences</p></title>
  371. <p>Statement sequences denote the sequence of actions specified by the component statements which are separated by semicolons.</p>
  372. <empty-line/>
  373. <p><code>        StatementSequence = statement {";" statement}.</code></p>
  374. </section>
  375. <section id="9.4"><title><p>9.4. If statements</p></title>
  376. <p><code>        IfStatement = IF expression THEN StatementSequence</code></p>
  377. <p><code>                {ELSIF expression THEN StatementSequence}</code></p>
  378. <p><code>                [ELSE StatementSequence]</code></p>
  379. <p><code>                END.</code></p>
  380. <empty-line/>
  381. <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>
  382. <p>Example:</p>
  383. <empty-line/>
  384. <p><code>        IF (ch &gt;= "A") &amp; (ch &lt;= "Z") THEN ReadIdentifier</code></p>
  385. <p><code>        ELSIF (ch &gt;= "0") &amp; (ch &lt;= "9") THEN ReadNumber</code></p>
  386. <p><code>        ELSIF ch = 22X THEN ReadString</code></p>
  387. <p><code>        END</code></p>
  388. </section>
  389. <section id="9.5"><title><p>9.5. Case statements</p></title>
  390. <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>
  391. <empty-line/>
  392. <p><code>        CaseStatement = CASE expression OF case {"|" case} END.</code></p>
  393. <p><code>        case = [CaseLabelList ":" StatementSequence].</code></p>
  394. <p><code>        CaseLabelList = LabelRange {"," LabelRange}.</code></p>
  395. <p><code>        LabelRange = label [".." label].</code></p>
  396. <p><code>        label = integer | string | ident.</code></p>
  397. <empty-line/>
  398. <p>Example:</p>
  399. <empty-line/>
  400. <p><code>        CASE k OF</code></p>
  401. <p><code>        0: x := x + y</code></p>
  402. <p><code>        | 1: x := x - y</code></p>
  403. <p><code>        | 2: x := x * y</code></p>
  404. <p><code>        | 3: x := x / y</code></p>
  405. <p><code>        END</code></p>
  406. </section>
  407. <section id="9.6"><title><p>9.6. While statements</p></title>
  408. <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>
  409. <empty-line/>
  410. <p><code>        WhileStatement = WHILE expression DO StatementSequence</code></p>
  411. <p><code>                {ELSIF expression DO StatementSequence} END.</code></p>
  412. <empty-line/>
  413. <p>Examples:</p>
  414. <empty-line/>
  415. <p><code>        WHILE j &gt; 0 DO</code></p>
  416. <p><code>                j := j DIV 2; i := i+1</code></p>
  417. <p><code>        END</code></p>
  418. <p><code>        WHILE (t # NIL) &amp; (t.key # i) DO</code></p>
  419. <p><code>                t := t.left</code></p>
  420. <p><code>        END</code></p>
  421. <p><code>        WHILE m &gt; n DO m := m - n</code></p>
  422. <p><code>        ELSIF n &gt; m DO n := n - m</code></p>
  423. <p><code>        END</code></p>
  424. </section>
  425. <section id="9.7"><title><p>9.7. Repeat Statements</p></title>
  426. <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>
  427. <empty-line/>
  428. <p><code>        RepeatStatement = REPEAT StatementSequence UNTIL expression.</code></p>
  429. </section>
  430. <section id="9.8"><title><p>9.8. For statements</p></title>
  431. <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>
  432. <empty-line/>
  433. <p><code>        ForStatement =</code></p>
  434. <p><code>                FOR ident ":=" expression TO expression [BY ConstExpression] DO</code></p>
  435. <p><code>                StatementSequence END .</code></p>
  436. <empty-line/>
  437. <p>The for statement</p>
  438. <empty-line/>
  439. <p><code>        FOR v := beg TO end BY inc DO S END</code></p>
  440. <empty-line/>
  441. <empty-line/>is, if <emphasis>inc</emphasis> &gt; 0, equivalent to
  442. <empty-line/>
  443. <p><code>        v := beg; lim := end;</code></p>
  444. <p><code>        WHILE v &lt;= lim DO S; v := v + inc END</code></p>
  445. <empty-line/>
  446. <empty-line/>and if <emphasis>inc</emphasis> &lt; 0 it is equivalent to
  447. <empty-line/>
  448. <p><code>        v := beg; lim := end;</code></p>
  449. <p><code>        WHILE v &gt;= lim DO S; v := v + inc END</code></p>
  450. <empty-line/>
  451. <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>
  452. </section>
  453. </section>
  454. <section id="10"><title><p>10. Procedure declarations</p></title>
  455. <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>
  456. <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>
  457. <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>
  458. <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>
  459. <p>The use of the procedure identifier in a call within its declaration implies recursive activation of the procedure.</p>
  460. <empty-line/>
  461. <p><code>        ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident.</code></p>
  462. <p><code>        ProcedureHeading = PROCEDURE identdef [FormalParameters].</code></p>
  463. <p><code>        ProcedureBody = DeclarationSequence [BEGIN StatementSequence]</code></p>
  464. <p><code>                [RETURN expression] END.</code></p>
  465. <p><code>        DeclarationSequence = [CONST {ConstantDeclaration ";"}]</code></p>
  466. <p><code>                [TYPE {TypeDeclaration ";"}] [VAR {VariableDeclaration ";"}]</code></p>
  467. <p><code>                {ProcedureDeclaration ";"}.</code></p>
  468. <section id="10.1"><title><p>10.1. Formal parameters</p></title>
  469. <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>
  470. <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>
  471. <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>
  472. <p>Formal parameters are local to the procedure, i.e. their scope is the program text which constitutes the procedure declaration.</p>
  473. <empty-line/>
  474. <p><code>        FormalParameters = "(" [FPSection {";" FPSection}] ")" [":" qualident].</code></p>
  475. <p><code>        FPSection = [VAR] ident {"," ident} ":" FormalType.</code></p>
  476. <p><code>        FormalType = {ARRAY OF} qualident.</code></p>
  477. <empty-line/>
  478. <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>
  479. <p>If the formal parameter's type is specified as</p>
  480. <empty-line/>
  481. <p><code>        ARRAY OF T</code></p>
  482. <empty-line/>
  483. <empty-line/>the parameter is said to be an <emphasis>open array</emphasis>, and the corresponding actual parameter may be of arbitrary length.
  484. <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>
  485. <p>Examples of procedure declarations:</p>
  486. <empty-line/>
  487. <p><code>        PROCEDURE ReadInt(VAR x: INTEGER);</code></p>
  488. <p><code>                VAR i : INTEGER; ch: CHAR;</code></p>
  489. <p><code>        BEGIN i := 0; Read(ch);</code></p>
  490. <p><code>                WHILE ("0" &lt;= ch) &amp; (ch &lt;= "9") DO</code></p>
  491. <p><code>                        i := 10*i + (ORD(ch)-ORD("0")); Read(ch)</code></p>
  492. <p><code>                END ;</code></p>
  493. <p><code>                x := i</code></p>
  494. <p><code>        END ReadInt</code></p>
  495. <empty-line/>
  496. <p><code>        PROCEDURE WriteInt(x: INTEGER); (* 0 &lt;= x &lt; 10^5 *)</code></p>
  497. <p><code>                VAR i: INTEGER;</code></p>
  498. <p><code>                        buf: ARRAY 5 OF INTEGER;</code></p>
  499. <p><code>        BEGIN i := 0;</code></p>
  500. <p><code>                REPEAT buf[i] := x MOD 10; x := x DIV 10; INC(i) UNTIL x = 0;</code></p>
  501. <p><code>                REPEAT DEC(i); Write(CHR(buf[i] + ORD("0"))) UNTIL i = 0</code></p>
  502. <p><code>        END WriteInt</code></p>
  503. <empty-line/>
  504. <p><code>        PROCEDURE log2(x: INTEGER): INTEGER;</code></p>
  505. <p><code>                VAR y: INTEGER; (*assume x&gt;0*)</code></p>
  506. <p><code>        BEGIN y := 0;</code></p>
  507. <p><code>                WHILE x &gt; 1 DO x := x DIV 2; INC(y) END ;</code></p>
  508. <p><code>                RETURN y</code></p>
  509. <p><code>        END log2</code></p>
  510. </section>
  511. <section id="10.2"><title><p>10.2. Predefined procedures</p></title>
  512. <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>
  513. <empty-line/>
  514. <p><code>        <emphasis>Function procedures:</emphasis></code></p>
  515. <empty-line/>
  516. <p><code>        Name        Argument type        Result type   Function</code></p>
  517. <empty-line/>
  518. <p><code>        ABS(x)      numeric type         type of x     absolute value</code></p>
  519. <p><code>        ODD(x)      INTEGER              BOOLEAN       x MOD 2 = 1</code></p>
  520. <p><code>        LEN(v)      v: array             INTEGER       the length of v</code></p>
  521. <p><code>        LSL(x, n)   x, n: INTEGER        type of x     logical shift left, x * 2<sup>n</sup></code></p>
  522. <p><code>        ASR(x, n)   x, n: INTEGER        type of x     signed shift right, x DIV 2<sup>n</sup></code></p>
  523. <p><code>        ROR(x, n)   x. n: INTEGER        type of x     x rotated right by n bits</code></p>
  524. <empty-line/>
  525. <p><code>        <emphasis>Type conversion functions:</emphasis></code></p>
  526. <empty-line/>
  527. <p><code>        Name        Argument type        Result type   Function</code></p>
  528. <empty-line/>
  529. <p><code>        FLOOR(x)    REAL, LONGREAL       INTEGER       largest integer &lt;= x</code></p>
  530. <p><code>        FLT(x)      INTEGER              REAL          identity</code></p>
  531. <p><code>        ORD(x)      CHAR, BOOLEAN, SET   INTEGER       ordinal number of x</code></p>
  532. <p><code>        CHR(x)      INTEGER              CHAR          character with ordinal number x</code></p>
  533. <p><code>        LONG(x)     REAL                 LONGREAL      x</code></p>
  534. <p><code>        SHORT(x)    LONGREAL             REAL          x</code></p>
  535. <empty-line/>
  536. <p><code>        <emphasis>Proper procedures:</emphasis></code></p>
  537. <empty-line/>
  538. <p><code>        Name           Argument types                Function</code></p>
  539. <empty-line/>
  540. <p><code>        INC(v)         INTEGER                       v := v + 1</code></p>
  541. <p><code>        INC(v, n)      INTEGER                       v := v + n</code></p>
  542. <p><code>        DEC(v)         INTEGER                       v := v - 1</code></p>
  543. <p><code>        DEC(v, n)      INTEGER                       v := v - n</code></p>
  544. <p><code>        INCL(v, x)     v: SET; x: INTEGER            v := v + {x}</code></p>
  545. <p><code>        EXCL(v, x)     v: SET; x: INTEGER            v := v - {x}</code></p>
  546. <p><code>        COPY(x, v)     x: character array, string    v := x</code></p>
  547. <p><code>                       v: character array</code></p>
  548. <p><code>        NEW(v)         pointer type                  allocate v^</code></p>
  549. <p><code>        ASSERT(b)      BOOLEAN                       abort, if ~b</code></p>
  550. <p><code>        ASSERT(b, n)   BOOLEAN, INTEGER</code></p>
  551. <p><code>        PACK(x, y)     REAL; INTEGER                 pack x and y into x</code></p>
  552. <p><code>        UNPK(x, y)     REAL; INTEGER                 unpack x into x and y</code></p>
  553. <empty-line/>
  554. <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>
  555. <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 &lt;= x &lt; 2.0.</p>
  556. </section>
  557. </section>
  558. <section id="11"><title><p>11. Modules</p></title>
  559. <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>
  560. <empty-line/>
  561. <p><code>        module = MODULE ident ";" [ImportList ";"] DeclarationSequence</code></p>
  562. <p><code>                [BEGIN StatementSequence] END ident "." .</code></p>
  563. <p><code>        ImportList = IMPORT import {"," import} ";" .</code></p>
  564. <p><code>        Import = ident [":=" ident].</code></p>
  565. <empty-line/>
  566. <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>
  567. <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>
  568. <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>
  569. <p>Example:</p>
  570. <empty-line/>
  571. <p><code>        MODULE Out; (*exported procedures: Write, WriteInt, WriteLn*)</code></p>
  572. <p><code>                IMPORT Texts, Oberon;</code></p>
  573. <p><code>                VAR W: Texts.Writer;</code></p>
  574. <empty-line/>
  575. <p><code>                PROCEDURE Write*(ch: CHAR);</code></p>
  576. <p><code>                BEGIN Texts.Write(W, ch)</code></p>
  577. <p><code>                END ;</code></p>
  578. <empty-line/>
  579. <p><code>                PROCEDURE WriteInt*(x, n: INTEGER);</code></p>
  580. <p><code>                        VAR i: INTEGER; a: ARRAY 16 OF CHAR;</code></p>
  581. <p><code>                BEGIN i := 0;</code></p>
  582. <p><code>                        IF x &lt; 0 THEN Texts.Write(W, "-"); x := -x END ;</code></p>
  583. <p><code>                        REPEAT a[i] := CHR(x MOD 10 + ORD("0")); x := x DIV 10; INC(i) UNTIL x = 0;</code></p>
  584. <p><code>                        REPEAT Texts.Write(W, " "); DEC(n) UNTIL n &lt;= i;</code></p>
  585. <p><code>                        REPEAT DEC(i); Texts.Write(W, a[i]) UNTIL i = 0</code></p>
  586. <p><code>                END WriteInt;</code></p>
  587. <empty-line/>
  588. <p><code>                PROCEDURE WriteLn*;</code></p>
  589. <p><code>                BEGIN Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)</code></p>
  590. <p><code>                END WriteLn;</code></p>
  591. <empty-line/>
  592. <p><code>        BEGIN Texts.OpenWriter(W)</code></p>
  593. <p><code>        END Out.</code></p>
  594. <section id="11.1"><title><p>11.1 The Module SYSTEM</p></title>
  595. <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>
  596. <empty-line/>
  597. <p><code>        <emphasis>Function procedures:</emphasis></code></p>
  598. <empty-line/>
  599. <p><code>        Name          Argument types    Result type    Function</code></p>
  600. <empty-line/>
  601. <p><code>        ADR(v)        any               INTEGER        address of variable v</code></p>
  602. <p><code>        SIZE(T)       any type          INTEGER        size in bytes</code></p>
  603. <p><code>        BIT(a, n)     a, n: INTEGER     BOOLEAN        bit n of mem[a]</code></p>
  604. <empty-line/>
  605. <p><code>        <emphasis>Proper procedures:</emphasis></code></p>
  606. <empty-line/>
  607. <p><code>        Name          Argument types                   Function</code></p>
  608. <empty-line/>
  609. <p><code>        GET(a, v)     a: INTEGER; v: any basic type    v := mem[a]</code></p>
  610. <p><code>        PUT(a, x)     a: INTEGER; x: any basic type    mem[a] := x</code></p>
  611. </section>
  612. </section>
  613. <section id="app"><title><p>Appendix</p><p>The Syntax of Oberon</p></title>
  614. <p><code>        letter = "A" | "B" | … | "Z" | "a" | "b" | … | "z".</code></p>
  615. <p><code>        digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".</code></p>
  616. <p><code>        hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".</code></p>
  617. <p><code>        ident = letter {letter | digit}.</code></p>
  618. <p><code>        qualident = [ident "."] ident.</code></p>
  619. <p><code>        identdef = ident ["*"].</code></p>
  620. <p><code>        integer = digit {digit} | digit {hexDigit} "H".</code></p>
  621. <p><code>        real = digit {digit} "." {digit} [ScaleFactor].</code></p>
  622. <p><code>        ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.</code></p>
  623. <p><code>        number = integer | real.</code></p>
  624. <p><code>        string = """ {character} """ | digit {hexDigit} "X".</code></p>
  625. <p><code>        ConstantDeclaration = identdef "=" ConstExpression.</code></p>
  626. <p><code>        ConstExpression = expression.</code></p>
  627. <p><code>        TypeDeclaration = identdef "=" StrucType.</code></p>
  628. <p><code>        StrucType = ArrayType | RecordType | PointerType | ProcedureType.</code></p>
  629. <p><code>        type = qualident | StrucType.</code></p>
  630. <p><code>        ArrayType = ARRAY length {"," length} OF type.</code></p>
  631. <p><code>        length = ConstExpression.</code></p>
  632. <p><code>        RecordType = RECORD ["(" BaseType ")"] [FieldListSequence] END.</code></p>
  633. <p><code>        BaseType = qualident.</code></p>
  634. <p><code>        FieldListSequence = FieldList {";" FieldList}.</code></p>
  635. <p><code>        FieldList = IdentList ":" type.</code></p>
  636. <p><code>        IdentList = identdef {"," identdef}.</code></p>
  637. <p><code>        PointerType = POINTER TO type.</code></p>
  638. <p><code>        ProcedureType = PROCEDURE [FormalParameters].</code></p>
  639. <p><code>        VariableDeclaration = IdentList ":" type.</code></p>
  640. <p><code>        expression = SimpleExpression [relation SimpleExpression].</code></p>
  641. <p><code>        relation = "=" | "#" | "&lt;" | "&lt;=" | "&gt;" | "&gt;=" | IN | IS.</code></p>
  642. <p><code>        SimpleExpression = ["+" | "-"] term {AddOperator term}.</code></p>
  643. <p><code>        AddOperator = "+" | "-" | OR.</code></p>
  644. <p><code>        term = factor {MulOperator factor}.</code></p>
  645. <p><code>        MulOperator = "*" | "/" | DIV | MOD | "&amp;".</code></p>
  646. <p><code>        factor = number | string | NIL | TRUE | FALSE |</code></p>
  647. <p><code>                set | designator [ActualParameters] | "(" expression ")" | "~" factor.</code></p>
  648. <p><code>        designator = qualident {selector}.</code></p>
  649. <p><code>        selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".</code></p>
  650. <p><code>        set = "{" [element {"," element}] "}".</code></p>
  651. <p><code>        element = expression [".." expression].</code></p>
  652. <p><code>        ExpList = expression {"," expression}.</code></p>
  653. <p><code>        ActualParameters = "(" [ExpList] ")" .</code></p>
  654. <p><code>        statement = [assignment | ProcedureCall | IfStatement | CaseStatement |</code></p>
  655. <p><code>                WhileStatement | RepeatStatement | ForStatement].</code></p>
  656. <p><code>        assignment = designator ":=" expression.</code></p>
  657. <p><code>        ProcedureCall = designator [ActualParameters].</code></p>
  658. <p><code>        StatementSequence = statement {";" statement}.</code></p>
  659. <p><code>        IfStatement = IF expression THEN StatementSequence</code></p>
  660. <p><code>                {ELSIF expression THEN StatementSequence}</code></p>
  661. <p><code>                [ELSE StatementSequence] END.</code></p>
  662. <p><code>        CaseStatement = CASE expression OF case {"|" case} END.</code></p>
  663. <p><code>        case = [CaseLabelList ":" StatementSequence].</code></p>
  664. <p><code>        CaseLabelList = LabelRange {"," LabelRange}.</code></p>
  665. <p><code>        LabelRange = label [".." label].</code></p>
  666. <p><code>        label = integer | string | ident.</code></p>
  667. <p><code>        WhileStatement = WHILE expression DO StatementSequence</code></p>
  668. <p><code>                {ELSIF expression DO StatementSequence} END.</code></p>
  669. <p><code>        RepeatStatement = REPEAT StatementSequence UNTIL expression.</code></p>
  670. <p><code>        ForStatement = FOR ident ":=" expression TO expression [BY ConstExpression]</code></p>
  671. <p><code>                DO StatementSequence END.</code></p>
  672. <p><code>        ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident.</code></p>
  673. <p><code>        ProcedureHeading = PROCEDURE identdef [FormalParameters].</code></p>
  674. <p><code>        ProcedureBody = DeclarationSequence [BEGIN StatementSequence]</code></p>
  675. <p><code>                [RETURN expression] END.</code></p>
  676. <p><code>        DeclarationSequence = [CONST {ConstDeclaration ";"}]</code></p>
  677. <p><code>                [TYPE {TypeDeclaration ";"}]</code></p>
  678. <p><code>                [VAR {VariableDeclaration ";"}]</code></p>
  679. <p><code>                {ProcedureDeclaration ";"}.</code></p>
  680. <p><code>        FormalParameters = "(" [FPSection {";" FPSection}] ")" [":" qualident].</code></p>
  681. <p><code>        FPSection = [VAR] ident {"," ident} ":" FormalType.</code></p>
  682. <p><code>        FormalType = {ARRAY OF} qualident.</code></p>
  683. <p><code>        module = MODULE ident ";" [ImportList] DeclarationSequence</code></p>
  684. <p><code>                [BEGIN StatementSequence] END ident "." .</code></p>
  685. <p><code>        ImportList = IMPORT import {"," import} ";".</code></p>
  686. <p><code>        import = ident [":=" ident].</code></p>
  687. </section>
  688. </section>
  689. </body>
  690. </FictionBook>
  691.  
  692.  
  693.  
  694.