home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / oberon / obrept.asc < prev    next >
Text File  |  1991-02-24  |  40KB  |  1,247 lines

  1.  
  2.  
  3.  
  4.     THE PROGRAMMING LANGUAGE OBERON
  5.  
  6.  
  7.  
  8.             (Revision 1. 10. 90)
  9.             N.Wirth
  10.  
  11.             Make it as simple as possible, but not simpler.
  12.                 --A. Einstein
  13.  
  14.  
  15.  
  16.  
  17. 1. Introduction
  18.  
  19. Oberon is a general-purpose programming language that evolved from Modula-2. 
  20. Its principal new feature is the concept of type extension. It permits the 
  21. construction of new data types on the basis of existing ones and to relate 
  22. them.
  23.  
  24. This report is not intended as a programmer's tutorial. It is intentionally 
  25. kept concise. Its function is to serve as a reference for programmers, 
  26. implementors, and manual writers. What remains unsaid is mostly left so 
  27. intentionally, either because it is derivable from stated rules of the 
  28. language, or because it would require to commit the definition when a 
  29. general commitment appears as unwise.
  30.  
  31.  
  32.  
  33. 2. Syntax
  34.  
  35. A language is an infinite set of sentences, namely the sentences well formed 
  36. according to its syntax. In Oberon, these sentences are called compilation 
  37. units. Each unit is a finite sequence of symbols from a finite vocabulary. 
  38. The vocabulary of Oberon consists of identifiers, numbers, strings, operators, 
  39. delimiters, and comments. They are called lexical symbols and are composed of 
  40. sequences of characters. (Note the distinction between symbols and characters.)
  41.  
  42. To describe the syntax, an extended Backus-Naur Formalism called EBNF is used. 
  43. Brackets [ and ] denote optionality of the enclosed sentential form, and 
  44. braces { and } denote its repetition (possibly 0 times). Syntactic entities 
  45. (non-terminal symbols) are denoted by English words expressing their intuitive 
  46. meaning. Symbols of the language vocabulary (terminal symbols) are denoted 
  47. by strings enclosed in quote marks or words written in capital letters, 
  48. so-called reserved words. Syntactic rules (productions) are marked by 
  49. a $ sign at the left margin of the line.
  50.  
  51.  
  52.  
  53. 3. Vocabulary and representation
  54.  
  55. The representation of symbols in terms of characters is defined using the 
  56. ASCII set. Symbols are identifiers, numbers, strings, operators, delimiters, 
  57. and comments. The following lexical rules must be observed. Blanks and line 
  58. breaks must not occur within symbols (except in comments, and blanks in 
  59. strings). They are ignored unless they are essential to separate two 
  60. consecutive symbols. Capital and lower-case letters are considered as 
  61. being distinct.
  62.  
  63.  
  64. 3.1. Identifiers are sequences of letters and digits. The first character 
  65. must be a letter.
  66.  
  67. $    ident  =  letter {letter | digit}.
  68.  
  69. Examples: x   scan   Oberon   GetSymbol   firstLetter
  70.  
  71.  
  72. 3.2. Numbers are (unsigned) integers or real numbers. Integers are sequences 
  73. of digits and may be followed by a suffix letter. The type is the minimal 
  74. type to which the number belongs (see 6.1.). If no suffix is specified, the
  75. representation is decimal. The suffix H indicates hexadecimal representation.
  76.  
  77. A real number always contains a decimal point. Optionally it may also contain 
  78. a decimal scale factor. The letter E (or D) is pronounced as  "times ten to 
  79. the power of". A real number is of type REAL, unless it has a scale factor 
  80. containing the letter D; in this case it is of type LONGREAL.
  81.  
  82.  
  83. $    number  =  integer | real.
  84. $    integer  =  digit {digit} | digit {hexDigit} "H" .
  85. $    real  =  digit {digit} "." {digit} [ScaleFactor].
  86. $    ScaleFactor  =  ("E" | "D") ["+" | "-"] digit {digit}.
  87. $    hexDigit  =  digit | "A" | "B" | "C" | "D" | "E" | "F".
  88. $    digit  =  "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
  89.  
  90.  
  91. Examples:
  92.  
  93. 1987
  94. 100H    = 256
  95. 12.3
  96. 4.567E8    = 456700000
  97. 0.57712566D-6    = 0.00000057712566
  98.  
  99.  
  100.  
  101. 3.3. Character constants are either denoted by a single character enclosed 
  102. in quote marks or by the ordinal number of the character in hexadecimal 
  103. notation followed by the letter X.
  104.  
  105.  
  106. $    CharConstant  = """ character """ | digit {hexDigit} "X".
  107.  
  108.  
  109.  
  110. 3.4. Strings are sequences of characters enclosed in quote marks ("). 
  111. A string cannot contain a quote mark. The number of characters in a 
  112. string is called the length of the string. Strings can be assigned to 
  113. and compared with arrays of characters (see 9.1 and 8.2.4).
  114.  
  115. $    string  =  """ {character} """ .
  116.  
  117. Examples: "OBERON"    "Don't worry!"
  118.  
  119.  
  120.  
  121. 3.5. Operators and delimiters are the special characters, character 
  122. pairs, or reserved words listed below. These reserved words consist 
  123. exclusively of capital letters and cannot be used in the role of 
  124. identifiers.
  125.  
  126. +     :=    ARRAY    IS    TO
  127. -     ^    BEGIN    LOOP    TYPE
  128. *     =    CASE    MOD    UNTIL
  129. /     #    CONST    MODULE    VAR
  130. ~    <    DIV    NIL    WHILE
  131. &     >    DO    OF    WITH
  132. .     <=    ELSE    OR
  133. ,     >=    ELSIF    POINTER
  134. ;     ..    END    PROCEDURE
  135. |     :    EXIT    RECORD
  136. (    )     IF    REPEAT
  137. [    ]    IMPORT    RETURN
  138. {     }    IN    THEN    
  139.  
  140.  
  141.  
  142.  
  143. 3.6. Comments may be inserted between any two symbols in a program. 
  144. They are arbitrary character sequences opened by the bracket (* and 
  145. closed by *). Comments do not affect the meaning of a program.
  146.  
  147.  
  148.  
  149.  
  150.  
  151. 4. Declarations and scope rules
  152.  
  153.  
  154. Every identifier occurring in a program must be introduced by a 
  155. declaration, unless it is a predefined identifier. Declarations also 
  156. serve to specify certain permanent properties of an object, such as 
  157. whether it is a constant, a type, a variable, or a procedure.
  158.  
  159. The identifier is then used to refer to the associated object. This 
  160. is possible in those parts of a program only which are within the 
  161. scope of the declaration. No identifier may denote more than one 
  162. object within a given scope. The scope extends textually from the 
  163. point of the declaration to the end of the block (procedure or module) 
  164. to which the declaration belongs and hence to which the object is local. 
  165. The scope rule has the following amendments:
  166.  
  167.     1. If a type T is defined as POINTER TO T1 (see 6.4), the 
  168.     identifier T1 can be declared textually following the 
  169.     declaration of T, but it must lie within the same scope.
  170.  
  171.     2. Field identifiers of a record declaration (see 6.3) 
  172.     are valid in field designators only.
  173.  
  174. In its declaration, an identifier in the global scope may be followed 
  175. by an export mark (*) to indicate that it be exported from its declaring 
  176. module. In this case, the identifier may be used in other modules, if 
  177. they import the declaring module. The identifier is then prefixed by 
  178. the identifier designating its module (see Ch. 11). The prefix and the 
  179. identifier are separated by a period and together are called a 
  180. qualified identifier.
  181.  
  182.  
  183. $    qualident = [ident "."] ident.
  184. $    identdef = ident ["*"].
  185.  
  186.  
  187. The following identifiers are predefined; their meaning is defined 
  188. in the indicated sections:
  189.  
  190. ABS          (10.2)    LEN    (10.2)
  191. ASH        (10.2)    LONG    (10.2)
  192. BOOLEAN      (6.1)    LONGINT    (6.1)
  193. BYTE        (6.1)    LONGREAL(6.1)
  194. CAP          (10.2)    MAX    (10.2)
  195. CHAR         (6.1)    MIN    (10.2)
  196. CHR          (10.2)    NEW    (6.4)
  197. DEC          (10.2)    ODD    (10.2)
  198. ENTIER        (10.2)    ORD     (10.2)
  199. EXCL        (10.2)    REAL    (6.1)
  200. FALSE        (6.1)    SET       (6.1)
  201. HALT         (10.2)    SHORT    (10.2)
  202. INC         (10.2)    SHORTINT(6.1)
  203. INCL        (10.2)    SIZE    (10.2)
  204. INTEGER     (6.1)    TRUE    (6.1)
  205.  
  206.  
  207.  
  208.  
  209. 5. Constant declarations
  210.  
  211.  
  212. A constant declaration associates an identifier with a constant value. 
  213.  
  214. $    ConstantDeclaration  =  identdef "=" ConstExpression.
  215. $    ConstExpression  =  expression.
  216.  
  217. A constant expression can be evaluated by a mere textual scan without 
  218. actually executing the program.  Its operands are constants  (see Ch. 8). 
  219. Examples of constant declarations are
  220.  
  221. N    =    100
  222. limit    =    2*N -1
  223. all    =    {0 .. WordSize-1}
  224.  
  225.  
  226.  
  227.  
  228.  
  229. 6. Type declarations
  230.  
  231.  
  232. A data type determines the set of values which variables of that type 
  233. may assume, and the operators that are applicable. A type declaration
  234. is used to associate an identifier with the type.  Such association 
  235. may be with unstructured (basic) types, or it may be with structured 
  236. types, in which case it defines the structure of variables of this 
  237. type and, by implication, the operators that are applicable to the 
  238. components. 
  239.  
  240. There are two different structures, namely arrays and records, with 
  241. different component selectors.
  242.  
  243.  
  244. $    TypeDeclaration  =  identdef "=" type.
  245. $    type  =  qualident | ArrayType | RecordType | 
  246. $            PointerType | ProcedureType.
  247.  
  248.  
  249.  
  250. Examples:
  251.  
  252. Table    =    ARRAY N OF REAL
  253. Tree    =    POINTER TO Node
  254. Node    =    RECORD key: INTEGER;
  255.             left, right: Tree
  256.         END
  257.         
  258. CenterNode =    RECORD (Node)
  259.            name: ARRAY 32 OF CHAR;
  260.            subnode: Tree
  261.          END
  262.          
  263. Function* =    PROCEDURE (x: INTEGER): INTEGER
  264.  
  265.  
  266.  
  267.  
  268. 6.1. Basic types
  269.  
  270. The following basic types are denoted by predeclared identifiers. 
  271. The associated operators are defined in 8.2, and the predeclared 
  272. function procedures in 10.2. The values of a given basic type 
  273. are the following:
  274.  
  275. 1. BOOLEAN    the truth values TRUE and FALSE.
  276. 2. CHAR        the characters of the extended ASCII set (0X ... 0FFX).
  277. 3. SHORTINT    the integers between -128 and 127.
  278. 4. INTEGER    the integers between MIN(INTEGER) and MAX(INTEGER).
  279. 5. LONGINT    the integers between MIN(LONGINT) and MAX(LONGINT).
  280. 6. REAL        real numbers between MIN(REAL) and MAX(REAL).
  281. 7. LONGREAL    real numbers between MIN(LONGREAL) and MAX(LONGREAL).
  282. 8. SET        the sets of integers between 0 and MAX(SET).
  283.  
  284. Types 3 to 5 are integer types, 6 and 7 are real types, and together 
  285. they are called numeric types. They form a hierarchy; the larger type 
  286. includes (the values of) the smaller type:
  287.  
  288. LONGREAL  >=  REAL  >=  LONGINT  >=  INTEGER  >=  SHORTINT
  289.  
  290.  
  291.  
  292. 6.2. Array types
  293.  
  294. An array is a structure consisting of a fixed number of elements which 
  295. are all of the same type, called the element type. The number of 
  296. elements of an array is called its length. The elements of the array
  297. are designated by indices, which are integers between 0 and the 
  298. length minus 1.
  299.  
  300.  
  301. $    ArrayType  =  ARRAY length {"," length} OF type.
  302. $    length  =  ConstExpression.
  303.  
  304.  
  305. A declaration of the form ARRAY N0, N1, ... , Nk OF T is understood 
  306. as an abbreviation of the declaration 
  307.  
  308.     ARRAY N0 OF
  309.         ARRAY N1 OF
  310.         ...
  311.             ARRAY Nk OF T
  312.  
  313.  
  314.     
  315. Examples of array types:
  316.  
  317. ARRAY N OF INTEGER
  318. ARRAY 10, 20 OF REAL
  319.  
  320.  
  321.  
  322.  
  323. 6.3. Record types
  324.  
  325. A record type is a structure consisting of a fixed number of elements 
  326. of possibly different types. The record type declaration specifies 
  327. for each element, called field, its type and an identifier which 
  328. denotes the field. The scope of these field identifiers is the record 
  329. definition itself, but they are also visible within field designators 
  330. (see 8.1) referring to elements of record variables.
  331.  
  332.  
  333. $    RecordType  =  RECORD ["(" BaseType ")"] FieldListSequence END.
  334. $    BaseType  =  qualident.
  335. $    FieldListSequence  =  FieldList {";" FieldList}.
  336. $    FieldList  =  [IdentList ":" type].
  337. $    IdentList  =  identdef {"," identdef}.
  338.  
  339.  
  340. If a record type is exported, field identifiers that are to be visible 
  341. outside the declaring module must be marked. They are called public 
  342. fields; unmarked fields are called private fields.
  343.  
  344. Record types are extensible, i.e. a record type can be defined as an 
  345. extension of another record type. In the examples above, CenterNode 
  346. (directly) extends Node, which is the (direct) base type of CenterNode. 
  347. More specifically, CenterNode extends Node with the fields name 
  348. and subnode.
  349.  
  350.  
  351. Definition: A type T0 extends a type T, if it equals T, or if it 
  352. directly extends an extension of T. Conversely, a type T is a base 
  353. type of T0, if it equals T0, or if it is the direct base type of a 
  354. base type of T0.
  355.  
  356.  
  357. Examples of record types:
  358.  
  359. RECORD day, month, year: INTEGER
  360. END
  361.  
  362. RECORD
  363.     name, firstname: ARRAY 32 OF CHAR;
  364.     age: INTEGER;
  365.     salary: REAL
  366. END
  367.  
  368.  
  369.  
  370. 6.4. Pointer types
  371.  
  372. Variables of a pointer type P assume as values pointers to variables 
  373. of some type T. The pointer type P is said to be bound to T, and T 
  374. is the pointer base type of P. T must be a record or array type. 
  375. Pointer types inherit the extension relation of their base types. 
  376. If a type T0 is an extension of T and P0 is a pointer type bound 
  377. to T0, then P0 is also an extension of P.
  378.  
  379. $    PointerType  =  POINTER TO type.
  380.  
  381. If p is a variable of type P = POINTER TO T, then a call of the 
  382. predefined procedure NEW(p) has the following effect (see 10.2): 
  383. A variable of type T is allocated in free storage, and a pointer 
  384. to it is assigned to p. This pointer p is of type P; the referenced 
  385. variable p^ is of type T. Failure of allocation results in p 
  386. obtaining the value NIL. 
  387.  
  388. Any pointer variable may be assigned the value NIL, which points
  389. to no variable at all. 
  390.  
  391.  
  392.  
  393. 6.5. Procedure types
  394.  
  395. Variables of a procedure type T have a procedure (or NIL) as value. 
  396. If a procedure P is assigned to a procedure variable of type T, the 
  397. (types of the) formal parameters of P must be the same as those 
  398. indicated in the formal parameters of T. The same holds for the 
  399. result type in the case of a function procedure (see 10.1). P must 
  400. not be declared local to another procedure, and neither can it be a 
  401. predefined procedure. 
  402.  
  403. $    ProcedureType = PROCEDURE [FormalParameters].
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411. 7. Variable declarations
  412.  
  413.  
  414. Variable declarations serve to introduce variables and associate 
  415. them with identifiers that must be unique within the given scope. 
  416. They also serve to associate fixed data types with the variables.
  417.  
  418.  
  419. $    VariableDeclaration  =  IdentList ":" type.
  420.  
  421.  
  422. Variables whose identifiers appear in the same list are all of the 
  423. same type. 
  424.  
  425. Examples of variable declarations (refer to examples in Ch. 6):
  426.  
  427. i, j, k:INTEGER
  428. x, y:    REAL
  429. p, q:    BOOLEAN
  430. s:      SET
  431. f:      Function
  432. a:      ARRAY 100 OF REAL
  433. w:      ARRAY 16 OF
  434.            RECORD ch: CHAR;
  435.              count: INTEGER
  436.            END
  437. t:    Tree
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444. 8. Expressions
  445.  
  446.  
  447. Expressions are constructs denoting rules of computation whereby 
  448. constants and current values of variables are combined to derive 
  449. other values by the application of operators and function procedures. 
  450. Expressions consist of operands and operators. Parentheses may be 
  451. used to express specific associations of operators and operands. 
  452.  
  453.  
  454. 8.1. Operands
  455.  
  456. With the exception of sets and literal constants, i.e. numbers and 
  457. character strings, operands are denoted by designators. A designator 
  458. consists of an identifier referring to the constant, variable, or 
  459. procedure to be designated. This identifier may possibly be qualified 
  460. by module identifiers (see Ch. 4 and 11), and it may be followed by 
  461. selectors, if the designated object is an element of a structure.
  462.  
  463. If A designates an array, then A[E] denotes that element of A whose 
  464. index is the current value of the expression E. The type of E must be 
  465. an integer type. A designator of the form  A[E1, E2, ... , En] stands  
  466. for  A[E1][E2] ... [En]. If p designates a pointer variable, p^ denotes 
  467. the variable which is referenced by p. If r designates a record, 
  468. then r.f denotes the field f of r. If p designates a pointer, 
  469. p.f denotes the field f of the record p^, i.e. the dot implies 
  470. dereferencing and p.f stands for p^.f, and p[E] denotes the element 
  471. of p^ with index E.
  472.  
  473. The typeguard v(T0) asserts that v is of type T0, i.e. it aborts 
  474. program execution, if it is not of type T0. The guard is applicable, if
  475.  
  476.     1. T0 is an extension of the declared type T of v, and if
  477.     2. v is a variable parameter of record type or v is a pointer.
  478.  
  479.  
  480.  
  481.  
  482. $    designator  =  qualident {"." ident | "[" ExpList "]" | 
  483. $            "(" qualident ")" | "^" }.
  484. $    ExpList  =  expression {"," expression}.
  485.  
  486.  
  487.  
  488.  
  489. If the designated object is a variable, then the designator refers 
  490. to the variable's current value. If the object is a procedure, 
  491. a designator without parameter list refers to that procedure. If it 
  492. is followed by a (possibly empty) parameter list, the designator 
  493. implies an activation of the procedure and stands for the value 
  494. resulting from its execution. The (types of the) actual parameters
  495. must correspond to the formal parameters as specified in the 
  496. procedure's declaration (see Ch. 10).
  497.  
  498. Examples of designators (see examples in Ch. 7):
  499.  
  500. i            (INTEGER)
  501. a[i]            (REAL)
  502. w[3].ch            (CHAR)
  503. t.key            (INTEGER)
  504. t.left.right        (Tree)
  505. t(CenterNode).subnode    (Tree)
  506.  
  507.  
  508. 8.2. Operators
  509.  
  510. The syntax of expressions distinguishes between four classes of 
  511. operators with different precedences (binding strengths). The 
  512. operator ~ has the highest precedence, followed by multiplication 
  513. operators, addition operators, and relations. Operators of the 
  514. same precedence associate from left to right. For example, x-y-z 
  515. stands for (x-y)-z.
  516.  
  517. $    expression  =  SimpleExpression [relation SimpleExpression].
  518. $    relation  =  "=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.
  519. $    SimpleExpression  =  ["+"|"-"] term {AddOperator term}.
  520. $    AddOperator  =  "+" | "-" | OR .
  521. $    term  =  factor {MulOperator factor}.
  522. $    MulOperator  =  "*" | "/" | DIV | MOD | "&" .
  523. $    factor  =  number | CharConstant | string | NIL | set |
  524. $        designator [ActualParameters] | "(" expression ")" | "~" factor.
  525. $    set  =  "{" [element {"," element}] "}".
  526. $    element  =  expression [".." expression].
  527. $    ActualParameters  =  "(" [ExpList] ")" .
  528.  
  529. The available operators are listed in the following tables.  In some 
  530. instances, several different operations are designated by the same 
  531. operator symbol.  In these cases, the actual operation is identified 
  532. by the type of the operands.
  533.  
  534.  
  535.  
  536. 8.2.1. Logical operators
  537.  
  538. symbol    result 
  539.  
  540. OR      logical disjunction
  541. &     logical conjunction
  542. ~     negation
  543.  
  544. These operators apply to BOOLEAN operands and yield a BOOLEAN result.   
  545.  
  546. p OR q    stands for    "if p then TRUE, else q"
  547. p & q    stands for    "if p then q, else FALSE"
  548. ~ p    stands for    "not p"
  549.  
  550.  
  551. 8.2.2. Arithmetic operators
  552.  
  553. symbol    result 
  554.      
  555. +      sum
  556. -      difference
  557. *      product
  558. /    quotient
  559. DIV     integer quotient
  560. MOD     modulus
  561.  
  562. The operators +, -, *, and / apply to operands of numeric types.  
  563. The type of the result is that operand's type which includes the other 
  564. operand's type, except for division (/), where the result is the real 
  565. type which includes both operand types. When used as operators with 
  566. a single operand, - denotes sign inversion and + denotes the 
  567. identity operation.
  568.  
  569. The operators DIV and MOD apply to integer operands only. They are 
  570. related by the following formulas defined for any dividend x and 
  571. positive divisors y:
  572.  
  573. x  =  (x DIV y) * y  +  (x MOD y)
  574. 0 <= (x MOD y) < y.
  575.  
  576.  
  577. 8.2.3.  Set operators
  578.  
  579. symbol    result        
  580. +     union
  581. -     difference
  582. *     intersection
  583. /     symmetric set difference
  584.  
  585. The monadic minus sign denotes the complement of x, i.e. -x denotes 
  586. the set of integers between 0 and MAX(SET) which are not elements of x. 
  587.  
  588. x - y    =  x * (-y)
  589. x / y    =  (x-y) + (y-x)
  590.  
  591.  
  592.  
  593. 8.2.4. Relations
  594.  
  595. symbol    relation
  596.  
  597. =      equal
  598. #      unequal
  599. <      less
  600. <=     less or equal
  601. >      greater
  602. >=     greater or equal
  603. IN     set membership
  604. IS    type test
  605.  
  606. Relations are Boolean. The ordering relations <, <=, >, and >= apply 
  607. to the numeric types, CHAR, and character arrays (strings). The 
  608. relations = and # also apply to the type BOOLEAN and to set, pointer, 
  609. and procedure types. x IN s  stands for "x is an element of s". 
  610. x must be of an integer type, and s of type SET. 
  611.  
  612. v IS T stands for "v is of type T" and is called a type test. It is 
  613. applicable, if
  614.  
  615.     1. T is an extension of the declared type T0 of v, and if
  616.     2. v is a variable parameter of record type or v is a pointer.
  617.  
  618.  
  619. Assuming, for instance, that T is an extension of T0 and that v is 
  620. a designator declared of type T0, then the test "v IS T" determines 
  621. whether the actually designated variable is (not only a T0, but 
  622. also) a T. The value of NIL IS T is undefined.
  623.  
  624. Examples of expressions (refer to examples in Ch. 7):
  625.  
  626. 1987            (INTEGER)
  627. i DIV 3            (INTEGER)
  628. ~p OR q         (BOOLEAN)
  629. (i+j) * (i-j)         (INTEGER)
  630. s - {8, 9, 13}         (SET)
  631. i + x             (REAL)
  632. a[i+j] * a[i-j]     (REAL)
  633. (0<=i) & (i<100)    (BOOLEAN)
  634. t.key = 0         (BOOLEAN)
  635. k IN {i .. j-1}        (BOOLEAN)
  636. t IS CenterNode        (BOOLEAN)
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643. 9. Statements
  644.  
  645. Statements denote actions. There are elementary and structured statements. 
  646.  
  647. Elementary statements are not composed of any parts that are themselves 
  648. statements. They are the assignment, the procedure call, and the return 
  649. and exit statements. 
  650.  
  651. Structured statements are composed of parts that are themselves statements. 
  652. They are used to express sequencing and conditional, selective, and 
  653. repetitive execution. 
  654.  
  655. A statement may also be empty, in which case it denotes no action.  The 
  656. empty statement is included in order to relax punctuation rules in 
  657. statement sequences. 
  658.  
  659.  
  660. $  statement  =  [assignment | ProcedureCall |
  661. $    IfStatement | CaseStatement | WhileStatement | RepeatStatement |
  662. $    LoopStatement | WithStatement | EXIT | RETURN [expression] ].
  663.  
  664.  
  665.  
  666. 9.1. Assignments
  667.  
  668. The assignment serves to replace the current value of a variable by 
  669. a new value specified by an expression. The assignment operator 
  670. is written as ":=" and pronounced as becomes. 
  671.  
  672. $    assignment  =  designator ":=" expression.
  673.  
  674. The type of the expression must be included by the type of the variable, 
  675. or it must extend the type of the variable. The following exceptions hold:
  676.  
  677.     1.The constant NIL can be assigned to variables of any 
  678.     pointer or procedure type.
  679.  
  680.     2. Strings can be assigned to any variable whose type is 
  681.     an array of characters, provided the length of the string 
  682.     is less than that of the array. If a string s of length n 
  683.     is assigned to an array a , the result is  
  684.     a[i] = si  for i = 0 ... n-1, and a[n] = 0X.
  685.  
  686.  
  687.  
  688. Examples of assignments (see examples in Ch. 7):
  689.  
  690. i := 0
  691. p := i = j
  692. x := i + 1
  693. k := log2(i+j)
  694. F := log2
  695. s := {2, 3, 5, 7, 11, 13}
  696. a[i] := (x+y) * (x-y)
  697. t.key := i
  698. w[i+1].ch := "A"
  699.  
  700.  
  701.  
  702. 9.2. Procedure calls
  703.  
  704. A procedure call serves to activate a procedure. The procedure call 
  705. may contain a list of actual parameters which are substituted in place 
  706. of their corresponding formal parameters defined in the procedure 
  707. declaration (see Ch. 10). The correspondence is established by the 
  708. positions of the parameters in the lists of actual and formal parameters 
  709. respectively. There exist two kinds of parameters: variable and 
  710. value parameters.
  711.  
  712. In the case of variable parameters, the actual parameter must be 
  713. a designator denoting a variable. If it designates an element of 
  714. a structured variable, the selector is evaluated when the formal/actual 
  715. parameter substitution takes place, i.e. before the execution of the 
  716. procedure. If the parameter is a value parameter, the corresponding 
  717. actual parameter must be an expression. This expression is evaluated 
  718. prior to the procedure activation, and the resulting value is assigned 
  719. to the formal parameter which now constitutes a local variable 
  720. (see also 10.1.).
  721.  
  722.  
  723. $    ProcedureCall  =  designator [ActualParameters].
  724.  
  725.  
  726. Examples of procedure calls:
  727.  
  728. ReadInt(i)    (see Ch. 10)
  729. WriteInt(j*2+1, 6)
  730. INC(w[k].count)
  731.  
  732.  
  733.  
  734.  
  735. 9.3. Statement sequences
  736.  
  737. Statement sequences denote the sequence of actions specified by the 
  738. component statements which are separated by semicolons. 
  739.  
  740.  
  741. $    StatementSequence  =  statement {";" statement}.
  742.  
  743.  
  744.  
  745.  
  746. 9.4. If statements
  747.  
  748.  
  749. $    IfStatement  =    IF expression THEN StatementSequence
  750. $        {ELSIF expression THEN StatementSequence}
  751. $        [ELSE StatementSequence]
  752. $        END.
  753.  
  754. If statements specify the conditional execution of guarded statements. 
  755. The Boolean expression preceding a statement is called its guard. 
  756. The guards are evaluated in sequence of occurrence, until one evaluates 
  757. to TRUE, whereafter its associated statement sequence is executed. If no 
  758. guard is satisfied, the statement sequence following the symbol ELSE is 
  759. executed, if there is one.
  760.  
  761. Example:
  762.  
  763. IF (ch >= "A") & (ch <= "Z") THEN ReadIdentifier
  764. ELSIF (ch >= "0") & (ch <= "9") THEN ReadNumber
  765. ELSIF ch = 22X THEN ReadString
  766. END
  767.  
  768.  
  769.  
  770.  
  771. 9.5. Case statements
  772.  
  773.  
  774. Case statements specify the selection and execution of a statement sequence 
  775. according to the value of an expression. First the case expression 
  776. is evaluated, then the statement sequence is executed whose case label list 
  777. contains the obtained value. The case expression and all labels must be 
  778. of the same type, which must be an integer type or CHAR. Case labels are 
  779. constants, and no value must occur more than once. If the value of 
  780. the expression does not occur as a label of any case, the statement sequence 
  781. following the symbol ELSE is selected, if there is one. Otherwise it is 
  782. considered as an error.
  783.  
  784.  
  785. $    CaseStatement = CASE expression OF case {"|" case} 
  786. $            [ELSE StatementSequence] END.
  787. $    case  =    [CaseLabelList ":" StatementSequence].
  788. $    CaseLabelList  = CaseLabels {"," CaseLabels}.
  789. $    CaseLabels  =    ConstExpression [".." ConstExpression].
  790.  
  791.  
  792. Example:
  793.  
  794. CASE ch OF
  795.      "A" .. "Z": ReadIdentifier
  796.    | "0" .. "9": ReadNumber
  797.    | 22X :    ReadString
  798.    ELSE    SpecialCharacter
  799. END
  800.  
  801.  
  802.  
  803.  
  804. 9.6. While statements
  805.  
  806.  
  807. While statements specify repetition. If the Boolean expression (guard) 
  808. yields TRUE, the statement sequence is executed. The expression 
  809. evaluation and the statement execution are repeated as long as the 
  810. Boolean expression yields TRUE.
  811.  
  812.  
  813. $    WhileStatement  = WHILE expression DO StatementSequence END.
  814.  
  815.  
  816. Examples:
  817.  
  818. WHILE j > 0 DO
  819.     j := j DIV 2; i := i+1
  820. END
  821.  
  822. WHILE (t # NIL) & (t.key # i) DO
  823.     t := t.left
  824. END
  825.  
  826.  
  827.  
  828. 9.7. Repeat Statements
  829.  
  830. A repeat statement specifies the repeated execution of a statement sequence 
  831. until a condition is satisfied. The statement sequence is executed at 
  832. least once.
  833.  
  834.  
  835. $    RepeatStatement  = REPEAT StatementSequence UNTIL expression.
  836.  
  837.  
  838.  
  839.  
  840.  
  841. 9.8. Loop statements
  842.  
  843. A loop statement specifies the repeated execution of a statement sequence.  
  844. It is terminated by the execution of any exit statement within that 
  845. sequence (see 9.9). 
  846.  
  847.  
  848. $    LoopStatement  =  LOOP StatementSequence END.
  849.  
  850.  
  851.  
  852. Example:
  853.  
  854. LOOP
  855.    IF t1 = NIL THEN EXIT END ;
  856.    IF k < t1.key THEN t2 := t1.left; p := TRUE
  857.    ELSIF k > t1.key THEN t2 := t1.right; p := FALSE
  858.    ELSE EXIT
  859.    END
  860. END ;
  861.  
  862.    
  863. Although while and repeat statements can be expressed by loop statements 
  864. containing a single exit statement, the use of while and repeat statements 
  865. is recommended in the most frequently occurring situations, where 
  866. termination depends on a single condition determined either at the 
  867. beginning or the end of the repeated statement sequence. The loop statement 
  868. is useful to express cases with several termination conditions and points.
  869.  
  870.  
  871.  
  872. 9.9. Return and exit statements
  873.  
  874. A return statement consists of the symbol RETURN, possibly followed by 
  875. an expression. It indicates the termination of a procedure, and the 
  876. expression  specifies the result of a function procedure. Its type 
  877. must be identical to the result type specified in the procedure 
  878. heading (see Ch. 10).
  879.  
  880. Function procedures require the presence of a return statement 
  881. indicating the result value. There may be several, although only one 
  882. will be executed. In proper procedures, a return statement is implied 
  883. by the end of the procedure body. An explicit return statement therefore 
  884. appears as an additional (probably exceptional) termination point.
  885.  
  886. An exit statement consists of the symbol EXIT. It specifies termination 
  887. of the enclosing loop statement and continuation with the statement 
  888. following that loop statement. Exit statements are contextually, although 
  889. not syntactically bound to the loop statement which contains them.
  890.  
  891.  
  892.  
  893. 9.10. With statements
  894.  
  895. If a pointer variable or a variable parameter with record structure is of 
  896. a type T0, it may be designated in the heading of a with clause together 
  897. with a type T that is an extension of T0. Then the variable is guarded 
  898. within the with statement as if it had been declared of type T. The 
  899. with statement assumes a role similar to the type guard, extending the 
  900. guard over an entire statement sequence. It may be regarded as a 
  901. regional type guard.
  902.  
  903.  
  904. $  WithStatement  =  WITH qualident ":" qualident DO StatementSequence END .
  905.  
  906.  
  907.  
  908. Example:
  909.  
  910. WITH t: CenterNode DO name := t.name; L := t.subnode END
  911.  
  912.  
  913.  
  914.  
  915. 10. Procedure declarations
  916.  
  917. Procedure declarations consist of a procedure heading and a 
  918. procedure body. The heading specifies the procedure identifier, 
  919. the formal parameters, and the result type (if any). The body 
  920. contains declarations and statements. The procedure identifier 
  921. is repeated at the end of the procedure declaration.
  922.  
  923. There are two kinds of procedures, namely proper procedures and 
  924. function procedures. The latter are activated by a function designator 
  925. as a constituent of an expression, and yield a result that is 
  926. an operand in the expression. Proper procedures are activated 
  927. by a procedure call. The function procedure is distinguished 
  928. in the declaration by indication of the type of its result following 
  929. the parameter list. Its body must contain a RETURN statement 
  930. which defines the result of the function procedure.
  931.  
  932. All constants, variables, types, and procedures declared within 
  933. a procedure body are local to the procedure. The values of local variables 
  934. are undefined upon entry to the procedure. Since procedures may 
  935. be declared as local objects too, procedure declarations may be nested.
  936.  
  937. In addition to its formal parameters and locally declared objects, 
  938. the objects declared in the environment of the procedure are also 
  939. visible in the procedure (with the exception of those objects that 
  940. have the same name as an object declared locally).
  941.  
  942. The use of the procedure identifier in a call within its declaration 
  943. implies recursive activation of the procedure.
  944.  
  945.  
  946. $    ProcedureDeclaration  =  ProcedureHeading ";" ProcedureBody ident.
  947. $    ProcedureHeading  =  PROCEDURE ["*"] identdef [FormalParameters].
  948. $    ProcedureBody  =  DeclarationSequence [BEGIN StatementSequence] END.
  949. $    ForwardDeclaration  =  PROCEDURE "^" identdef [FormalParameters].
  950. $    DeclarationSequence  =  {CONST {ConstantDeclaration ";"} |
  951. $        TYPE {TypeDeclaration ";"} | VAR {VariableDeclaration ";"}}
  952. $        {ProcedureDeclaration ";" | ForwardDeclaration ";"}.
  953.  
  954. A forward declaration serves to allow forward references to a procedure 
  955. that appears later in the text in full. The actual declaration - which 
  956. specifies the body - must indicate the same parameters and result 
  957. type (if any) as the forward declaration, and it must be within the 
  958. same scope. An asterisk following the symbol PROCEDURE is a hint to the 
  959. compiler and specifies that the procedure is to be usable as parameter 
  960. and assignable to variables of a compatible procedure type.
  961.  
  962.  
  963.  
  964. 10.1. Formal parameters
  965.  
  966. Formal parameters are identifiers which denote actual parameters specified 
  967. in the procedure call. The correspondence between formal and actual 
  968. parameters is established when the procedure is called. There are 
  969. two kinds of parameters, namely value and variable parameters. The kind 
  970. is indicated in the formal parameter list. Value parameters stand for 
  971. local variables to which the result of the evaluation of the corresponding 
  972. actual parameter is assigned as initial value. Variable parameters correspond 
  973. to actual parameters that are variables, and they stand for these variables. 
  974. Variable parameters are indicated by the symbol VAR, value parameters by the 
  975. absence of the symbol VAR. A function procedure without parameters must have 
  976. an empty parameter list.  It must be called by a function designator 
  977. whose actual parameter list is empty too.
  978.  
  979. Formal parameters are local to the procedure, i.e. their scope is the 
  980. program text which constitutes the procedure declaration.
  981.  
  982.  
  983. $    FormalParameters  =  "(" [FPSection {";" FPSection}] ")" [":" qualident].
  984. $    FPSection  =  [VAR] ident  {"," ident} ":" FormalType.
  985. $    FormalType  =  {ARRAY OF} (qualident | ProcedureType).
  986.  
  987.  
  988. The type of each formal parameter is specified in the parameter list.  
  989. For variable parameters, it must be identical to the corresponding 
  990. actual parameter's type, except in the case of a record, where it must be 
  991. a base type of the corresponding actual parameter's type. For value parameters, 
  992. the rule of assignment holds (see 9.1). If the formal parameter's type 
  993. is specified as
  994.  
  995.     ARRAY OF T
  996.     
  997. the parameter is said to be an open array parameter, and the corresponding 
  998. actual parameter may be any array with element type T.
  999.  
  1000. If a formal parameter specifies a procedure type, then the corresponding 
  1001. actual parameter must be either a procedure declared at level 0 or 
  1002. a variable (or parameter) of that procedure type. It cannot be a 
  1003. predefined procedure. The result type of a procedure can be neither 
  1004. a record nor an array.
  1005.  
  1006.  
  1007. Examples of procedure declarations:
  1008.  
  1009. PROCEDURE ReadInt(VAR x: INTEGER);
  1010. VAR i : INTEGER; ch: CHAR;
  1011. BEGIN i := 0; Read(ch);
  1012.    WHILE ("0" <= ch) & (ch <= "9") DO
  1013.       i := 10*i + (ORD(ch)-ORD("0")); Read(ch)
  1014.    END ;
  1015.    x := i
  1016. END ReadInt
  1017.   
  1018.   
  1019. PROCEDURE WriteInt(x: INTEGER);  (* 0 <= x < 10^5 *)
  1020. VAR i: INTEGER;
  1021.     buf: ARRAY 5 OF INTEGER;
  1022. BEGIN i := 0;
  1023.    REPEAT buf[i] := x MOD 10;  x := x DIV 10;  INC(i) UNTIL x = 0;
  1024.    REPEAT DEC(i); Write(CHR(buf[i] + ORD("0"))) UNTIL i = 0
  1025. END WriteInt
  1026.  
  1027.  
  1028. PROCEDURE log2(x: INTEGER): INTEGER;
  1029. VAR y: INTEGER;  (*assume x>0*)
  1030. BEGIN y := 0;
  1031.    WHILE x > 1 DO x := x DIV 2; INC(y) END ;
  1032.    RETURN y
  1033. END log2
  1034.  
  1035.  
  1036.  
  1037.  
  1038. 10.2. Predefined procedures
  1039.  
  1040. The following table lists the predefined procedures.  Some are
  1041. generic procedures, i.e. they apply to several types of operands.  
  1042. v stands for a variable, x and n for expressions, and T for a type.
  1043.  
  1044.  
  1045. Function procedures:
  1046.  
  1047. Name    Argument type    Result type    Function
  1048.  
  1049. ABS(x)   numeric type    type of x    absolute value
  1050. ODD(x)     integer type    BOOLEAN        x MOD 2 = 1
  1051. CAP(x)   CHAR        CHAR        corresponding capital letter
  1052. ASH(x,n) x, n: integer    LONGINT        x * 2n,  arithmetic shift
  1053. LEN(v,n) v: array    LONGINT        the length of v in dimension n
  1054.      n: integer type
  1055. LEN(v)                     is equivalent to LEN(v, 0)
  1056. MAX(T)     T = basic type    T        maximum value of type T
  1057.      T = SET    INTEGER        maximum element of sets
  1058. MIN(T)     T = basic type    T        minimum value of type T
  1059.      T = SET    INTEGER        0
  1060. SIZE(T)     T = any type    integer type    no. of bytes required by T
  1061.  
  1062.  
  1063.     
  1064. Type conversion procedures:
  1065.  
  1066. Name    Argument type    Result type    Function
  1067.  
  1068. ORD(x)   CHAR        INTEGER        ordinal number of x
  1069. CHR(x)   integer type    CHAR        character with ordinal number x
  1070. SHORT(x) LONGINT    INTEGER        identity
  1071.      INTEGER    SHORTINT
  1072.      LONGREAL    REAL (truncation possible)
  1073. LONG(x)  SHORTINT    INTEGER        identity
  1074.      INTEGER    LONGINT
  1075.      REAL        LONGREAL
  1076. ENTIER(x) real type    LONGINT        largest integer not greater than x
  1077.  
  1078.                     Note that  ENTIER(i/j)  =  i DIV j
  1079.  
  1080.  
  1081.  
  1082. Proper procedures:
  1083.  
  1084. Name    Argument types            Function    
  1085.  
  1086. INC(v)    integer type            v := v+1
  1087. INC(v,x)  integer type            v := v+x
  1088. DEC(v)    integer type            v := v-1
  1089. DEC(v,x)  integer type            v := v-x
  1090. INCL(v,x) v: SET; 
  1091.       x: integer type           v := v + {x}
  1092. EXCL(v,x) v: SET; 
  1093.       x: integer type           v := v - {x}
  1094. COPY(x,v) x: character array, string
  1095.       v: character array        v := x
  1096. NEW(v)      pointer type            allocate v^  
  1097. HALT(x)      integer constant        terminate program execution
  1098.  
  1099.  
  1100. The second parameter of INC and DEC may be omitted, in which case 
  1101. its default value is 1. In HALT(x), x is a parameter whose interpretation 
  1102. is left to the underlying system implementation.
  1103.  
  1104.  
  1105.  
  1106. 11. Modules
  1107.  
  1108. A module is a collection of declarations of constants, types, variables, 
  1109. and procedures, and a sequence of statements for the purpose of 
  1110. assigning initial values to the variables. A module typically constitutes 
  1111. a text that is compilable as a unit.
  1112.  
  1113.  
  1114. $    module  =  MODULE ident ";"  [ImportList] DeclarationSequence
  1115. $        [BEGIN StatementSequence] END ident "." .
  1116. $    ImportList  =  IMPORT import {"," import} ";" .
  1117. $    import  =  ident [":=" ident].
  1118.  
  1119.  
  1120. The import list specifies the modules of which the module is a client. 
  1121. If an identifier x is exported from a module M, and if M is listed in 
  1122. a module's import list, then x is referred to as M.x. 
  1123. If the form "M := M1" is used in the import list, that object declared 
  1124. within M1 is referenced as  M.x .
  1125.  
  1126. Identifiers that are to be visible in client modules, i.e. outside 
  1127. the declaring module, must be marked by an export mark in 
  1128. their declaration.
  1129.  
  1130. The statement sequence following the symbol BEGIN is executed when 
  1131. the module is added to a system (loaded). Individual (parameterless) 
  1132. procedures can thereafter be activated from the system, and these 
  1133. procedures serve as commands.
  1134.  
  1135.  
  1136. Example:
  1137.  
  1138.  
  1139. MODULE Out;
  1140.  
  1141. (*exported procedures:  Write, WriteInt, WriteLn*)
  1142.  
  1143. IMPORT Texts, Oberon;
  1144.         
  1145. VAR W: Texts.Writer;
  1146.     
  1147. PROCEDURE Write*(ch: CHAR);
  1148. BEGIN Texts.Write(W, ch)
  1149. END Write;    
  1150.  
  1151. PROCEDURE WriteInt*(x, n: LONGINT);
  1152. VAR i: INTEGER; a: ARRAY 16 OF CHAR;
  1153. BEGIN i := 0;
  1154.   IF x < 0 THEN Texts.Write(W, "-"); x := -x END ;
  1155.   REPEAT a[i] := CHR(x MOD 10 + ORD("0")); x := x DIV 10; INC(i) UNTIL x = 0;
  1156.   REPEAT Texts.Write(W, " "); DEC(n) UNTIL n <= i;
  1157.   REPEAT DEC(i); Texts.Write(W, a[i]) UNTIL i = 0
  1158. END WriteInt;    
  1159.  
  1160. PROCEDURE WriteLn*;
  1161. BEGIN Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
  1162. END WriteLn;
  1163.  
  1164. BEGIN Texts.OpenWriter(W)
  1165. END Out.
  1166.  
  1167.  
  1168.  
  1169. 12. The Module SYSTEM
  1170.  
  1171. The module SYSTEM contains definitions that are necessary 
  1172. to program low-level operations referring directly to resources 
  1173. particular to a given computer and/or implementation. These include 
  1174. for example facilities for accessing devices that are controlled by the 
  1175. computer, and facilities to break the data type compatibility rules 
  1176. otherwise imposed by the language definition. It is recommended 
  1177. to restrict their use to specific low-level modules. Such modules 
  1178. are inherently non-portable, but easily recognized due to the 
  1179. identifier SYSTEM appearing in their import lists. The subsequent 
  1180. definitions are applicable to most modern computers; however, individual 
  1181. implementations may include in this module definitions that are particular 
  1182. to the specific, underlying computer.
  1183.  
  1184. Module SYSTEM exports the data type BYTE. No representation of values 
  1185. is specified. Instead, certain compatibility rules with other types 
  1186. are given:
  1187.  
  1188.     1. The type BYTE is compatible with CHAR and SHORTINT.
  1189.  
  1190.     2. If a formal parameter is of type ARRAY OF BYTE, then 
  1191.     the corresponding actual parameter may be of any type.
  1192.     
  1193.     
  1194. The procedures contained in module SYSTEM are listed in the following 
  1195. tables. They correspond to single instructions compiled as in-line code. 
  1196. For details, the reader is referred to the processor manual. 
  1197. v stands for a variable, x, y, a, and n for expressions, and T for a type.
  1198.  
  1199.  
  1200. Function procedures:
  1201.  
  1202. Name    Argument types        Result type    Function
  1203.  
  1204. ADR(v)      any            LONGINT        address of variable v
  1205. BIT(a, n) a: LONGINT        BOOLEAN        bit n of Mem[a]
  1206.       n: integer type
  1207. CC(n)      integer constant     BOOLEAN        Condition  n  (0 <= n < 16)
  1208. LSH(x,n)  x:integer type or SET    type of x    logical shift
  1209.       n:integer type
  1210. ROT(x, n) x:integer type or SET    type of x    rotation
  1211.       n: integer type
  1212. VAL(T, x) T, x: any type    T        x interpreted as of type T
  1213.  
  1214.  
  1215. Proper procedures:
  1216.  
  1217. Name        Argument types            Function
  1218.  
  1219. GET(a,v)    a: LONGINT; v: any basic type    v := Mem[a]
  1220. PUT(a, x)    a: LONGINT; x: any basic type    Mem[a] := x
  1221. MOVE(s, d, n)    s, d: LONGINT; n: integer type    Mem[d] ... Mem[d+n-1]
  1222.                              := Mem[s] ... Mem[s+n-1]
  1223. NEW(v, n)    v: any pointer type        allocate storage block 
  1224.         n: integer type            of n bytes,
  1225.                         assign its address to v
  1226.     
  1227.  
  1228.  
  1229.     
  1230. -------------
  1231.  
  1232. Oberon Language Report
  1233.  
  1234. Oberon-M(tm) for the MSDOS Environment
  1235. For specific MSDOS information, see the README file in the
  1236. Oberon-M distribution package.
  1237.  
  1238. E. R. Videki
  1239. P.O. Box 58
  1240. Morgan Hill, California 95038
  1241. U.S.A.
  1242.  
  1243. erv@k2.everest.tandem.com
  1244.  
  1245.  
  1246.  
  1247.