home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / OB3.2D1.DMS / in.adf / Oberon-Report_English < prev    next >
Encoding:
Text File  |  1994-08-04  |  38.7 KB  |  1,050 lines

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