ALGOL-M LANGUAGE REFERENCE MANUAL This section describes the various elements of the ALGOL-M language. The format of the element will be shown, followed by a description and examples of use. The following notation is used: Braces, {}, indicate an optional entry. A vertical bar, |, indicates alternate choices, one of which must appear. Elipses, "...", indicate that the preceding item may be optionally repeated. Reserved words are indicated by capital let- ters. Reserved words and other special symbols must appear as shown. Items appearing in small letters are elements of the language which are defined and explained elsewhere in the language manual. ***** ELEMENT arithmetic expression FORMAT integer|decimal variable {(} arithmetic expression binary operator arithmetic expression {)} {(} unary operator arithmetic expression {)} ALGOL-M USER'S MANUAL Page 2 DESCRIPTION Operators in ALGOL-M have an implied precedence which is used to determine the manner in which operators and operands are grouped. A-B/C causes the result of B divided by C to be subtracted from A. In this case B is considered to be "bound" to the "/" operator instead of the "-" operator which causes the division to be performed first. The implied precedence binds operands to the adjacent of highest precedence. The implied precedence of oper- ators is as follows: unary -,+ ** *,/ +,- Parentheses can be used to override the implied precedence in the same way as they are used in ordinary algebra. Thus the expression (A-B)/C will cause B to be subtracted from A and the result divided by C. EXAMPLE (X + Y) * (Z * Y + X) ** 2 X + Y + Z * X * Y * Z / 5.456 + I ALGOL-M USER'S MANUAL Page 3 ELEMENT ARRAY declaration FORMAT INTEGER|DECIMAL|STRING {(expression)} ARRAY identifier ... bound pair list {,identifier} DESCRIPTION The array declaration dynamically allocates storage for arrays. The optional integer expression indi- cates the length of each array element. For strings, the maximum length is 255 characters and for decimals the maximum length is 18 digits. Inte- ger lengths are not specified since storage adequate to represent all integer values between -16,383 and +16,383 is automatically allocated. Arrays are not automatically initialized to zero. EXAMPLE INTEGER ARRAY X[0:5,0:5]; DECIMAL(10) ARRAY X,Y[3:6,5:10]; STRING ARRAY WORDS[Y+3:12]; STRING(20) WORD[0:X+5,5:10,15:20]; ALGOL-M USER'S MANUAL Page 4 ELEMENT assignment statement FORMAT variable := {variable :=} ... expression DESCRIPTION The expression is evaluated and stored into the variable. The types of permissible assignments are indicated by the following table: expression integer decimal string integer yes no no variable decimal yes yes no string no no yes Multiple assignments are allowed with the expression being assigned to all of the listed variables. EXAMPLE X := Y + Z; Y[1] := Y[2] := 50; ALGOL-M USER'S MANUAL Page 5 ELEMENT assignment on the fly FORMAT ( variable := { variable := } ... expression ) DESCRIPTION Algol-m allows a special form of the assignment statement known as assignment on the fly. This form of an assignment statement can be used anywhere an expression is allowed. The expression to the right of the := symbol is evaluated and then stored into the variable on the left. The value of the entire expression is the same as that of it's right half. The only difference is the side-effect of storing the intermediate result into the variable on the left. These intermediate results can then be used at a later point in the program without re-calcula- ting them. EXAMPLE A:= (B:=C + 5) * 2; IF (B:=C:=D+5) > 10 THEN WRITE(B); ALGOL-M USER'S MANUAL Page 6 ELEMENT balanced statement FORMAT {label definition} simple statement {label definition} IF boolean expression THEN bal- anced statement ELSE balanced statement DESCRIPTION If the boolean expression is true, the balanced statement to the left of the ELSE is executed. If the boolean expression is false, the balanced state- ment to the right of the ELSE is executed. EXAMPLE IF A < B THEN A := 1 ELSE A := 2; IF B = C THEN BEGIN WRITE(B); B := B + 1; END ELSE BEGIN WRITE(C); C := C + 1; END; PROGRAMMING NOTE A semicolon is not allowed after the statement im- mediately preceding the ELSE. ALGOL-M USER'S MANUAL Page 7 ELEMENT block FORMAT BEGIN {declaration;} ... statement; ... END; DESCRIPTION The block is the foundation of the ALGOL-M language. Each time a new block is entered new variables may be declared. These variables are unique in the sense that a variable X declared in two different blocks represents two different variables. All storage within a block is dynamic and allocated when the block is entered and de-allocated when the block is departed. A block can be used any place a simple statement can be used. EXAMPLE BEGIN X := 1; Y := 2; END; IF X = Y THEN BEGIN INTEGER X,Y; X := 3; Y := 4; END; PROGRAMMING NOTE Declarations may not appear in case blocks. The final END, which matches the initial program BEGIN, is not followed by a semicolon. ALGOL-M USER'S MANUAL PAGE 8 ELEMENT boolean expression FORMAT NOT boolean expression boolean expression OR boolean expression boolean expression AND boolean expression {(} expression =|<|>|>=|<=|<> expression {)} DESCRIPTION Integer-integer, decimal-integer, decimal-decimal, integer-decimal, and string-string comparisons are allowed in ALGOL-M. For integer-decimal and decimal-integer comparisons the integer value is converted to a decimal value prior to comparison. The result of a comparison of numerical values is based on the size of the numbers. The result of a comparison of string values depends on a character- by-character comparison where the first instance of a non-equal character establishes the boolean result. The collating sequence of the ASCII character set is used for string comparisons. Generally, numbers are followed by upper case letters which are followed by lower case letters. EXAMPLE X > Y OR Y < Z; (X = Y) AND (Y = Z OR Z = 10); IF NOT X = 1 THEN WRITE("HELLO"); ALGOL-M USER'S MANUAL Page 9 ELEMENT bound pair list FORMAT [expression : expression{,expression : expression} ...] DESCRIPTION Expressions in the bound pair list must be of type integer and greater than or equal to zero. There can be no more than 255 dimensions. EXAMPLE [1:7,0:5] [3:6,x:y] [y*3:z,1:12] ALGOL-M USER'S MANUAL Page 10 ELEMENT CASE statement FORMAT CASE expression OF BEGIN statement; ... END; DESCRIPTION The CASE statement allows the programmer to choose one of several statements to be executed. The statement chosen depends on the value of the integer expression. The first statement is executed if the expression evaluates to zero. If the value of the expression is greater than the number of statements in the case block, the resulting action is undefined. EXAMPLE CASE X+Y OF BEGIN WRITE("CASE 0"); WRITE("CASE 1"); END; ALGOL-M USER'S MANUAL Page 11 ELEMENT constant FORMAT integer|decimal|string DESCRIPTION A constant may be either an integer, decimal, or string constant. Integer constants are numbers with no decimal point ranging from -16383 to +16383. Decimal constants are numbers with a decimal point and may not exceed 18 digits in length (13 digits to the left and 5 digits to the right of the decimal point). String constants may be composed of any combination of alpha-numeric and special characters and may be up to 255 characters in length. Strings entered from the console or disk may be either enclosed in quotation marks or delimited with commas. Strings used as constants in the program must be enclosed in double quotation marks. EXAMPLE 10 10.5678 "EXAMPLE ONE" PROGRAMMING NOTE The maximum precision for decimal numbers used in arithmetic operations is 5 digits to the right of the decimal point(anything greater is truncated). ALGOL-M USER'S MANUAL Page 12 ELEMENT declaration DESCRIPTION See FILE declaration, ARRAY declaration, simple declaration, procedure declaration, and function declaration. ***** ELEMENT expression FORMAT boolean expression|arithmetic expression DESCRIPTION See arithmetic expression and boolean expression. ALGOL-M USER'S MANUAL Page 13 ELEMENT FILE declaration FORMAT FILE identifier {(expression)} {,identifier {(expression)}} ... DESCRIPTION The identifiers used in the FILE declaration are file identifiers which reference actual file names. The actual file names may be assigned at compile- time or at run-time. If they are to be assigned at run time the name must be read into a STRING variable and then assigned to the file identifier through an assignment statement. The file name entered at the console must be in upper case. The optional integer expression following the file identifier is used to specify the record length in bytes for blocked records. EXAMPLE FILE TAPE1, TAPE2[128]; PROGRAMMING NOTE Blocked records are implemented in a manner very similar to that used in CBASIC. See sample program BLKTEST.ALG. ALGOL-M USER'S MANUAL Page 14 ELEMENT FOR statement FORMAT {label definition} FOR assignment statement STEP expression UNTIL expression DO simple statement DESCRIPTION Execution of all statements within the simple statement are repeated until the indexing variable is greater than or equal to the value of the UNTIL expression. The indexing variable is incremented by the amount specified in the STEP expression and must be incremented by a positive amount. The UNTIL and STEP expressions are evaluated on each loop. EXAMPLE FOR I := 1 STEP 1 UNTIL 10 DO X := Y; FOR INDEX := X+Y STEP 2 UNTIL X*Y DO BEGIN A := A + B; WRITE(A); END; ALGOL-M USER'S MANUAL Page 15 ELEMENT function call FORMAT identifier {(expression {,expression} ...)} DESCRIPTION Functions may appear as primary elements in arithmetic or boolean expressions. Parameter passing is by value. Functions may be called recursively with no limit to the number of recursive calls allowed. EXAMPLE X := RAND; Y := SQRT(5.6); C := FUNC * RND(2); ALGOL-M USER'S MANUAL Page 16 ELEMENT function declaration FORMAT INTEGER|DECIMAL|STRING FUNCTION identifier {(identifier {,identifier} ...)} {declaration; ...} simple statement; DESCRIPTION A function declaration may or may not include parameters. If parameters are included they must be declared before the simple statement which represents the body of the function. Parameters are passed by value and may be of type integer, decimal, or string. Functions return a value to the point of call. The value to be returned is assigned to the function name (which is used as a simple variable within the function) prior to the end of the function. Functions may be called recursively with no limit set as to the number of recursive calls which can be made. Variables may be declared within functions and are considered local to the function. EXAMPLE INTEGER FUNCTION VALUE(X); INTEGER X; BEGIN X := (X * 5) + (X * 2 ); VALUE := X; END; ALGOL-M USER'S MANUAL Page 17 ELEMENT GOTO statement FORMAT label definition GO TO identifier|integer label definition GOTO identifier|integer DESCRIPTION Execution continues at the statement labeled with the identifier or integer following the GOTO or GO TO statement. EXAMPLE NEXT: GO TO 100; 100: GOTO NEXT; PROGRAMMING NOTE GOTO statements can only be used to branch within the current block or to an outer block. GOTO statements branching out of subroutines can cause unknown results. ALGOL-M USER'S MANUAL Page 18 ELEMENT identifier FORMAT letter {letter|number} ... DESCRIPTION Identifiers begin with a letter and are continued with any alphanumeric characters. Although identifiers up to 255 characters may be used, only the first 31 characters are actually used to distinguish the identifiers. EXAMPLE A NAME COUNTER1 ALGOL-M USER'S MANUAL Page 19 ELEMENT IF statement DESCRIPTION See balanced statement or unbalanced statement. ***** ELEMENT if expression FORMAT IF boolean expression THEN expression ELSE expres- sion DESCRIPTION The if expression allows a conditional expression evaluation. If the boolean expression is true the value of the if expression is the value of the expression following the word THEN. If the boolean expression is false the value of the if expression is the value of the expression following the word ELSE. EXAMPLE WRITE( IF A > B THEN A ELSE B ); A:= IF B=C THEN B ELSE D; ALGOL-M USER'S MANUAL Page 20 ELEMENT label definition FORMAT identifier|integer : DESCRIPTION Label definitions are optional on all balanced or unbalanced statements. EXAMPLE FINISH: 100: ***** ELEMENT procedure call FORMAT identifier {(expression {,expression} ...)} DESCRIPTION Procedures can be called with or without parameters. Parameter passing is by value. Procedures can be called recursively with no limit set as to the number of recursive calls. EXAMPLE COMPUTE; COMPARE("AAA", WORD); COUNT(1, 2, 3); ALGOL-M USER'S MANUAL Page 21 ELEMENT procedure declaration FORMAT PROCEDURE identifier {(identifier {,identifier}...)} {declaration; ...} simple statement DESCRIPTION A procedure declaration may or may not include para- meters. If parameters are included they must be declared before the simple statement which repre- sents the body of the procedure. Parameters are passed by value and may be of type integer, decimal or string. Procedures do not return a value to the point of call. Procedures can be called recursive- ly. Procedures are considered separate blocks with- in which local variables may be declared. EXAMPLE PROCEDURE OUTPUT; WRITE ("HELLO"); PROCEDURE COMPARE(X,Y); INTEGER X,Y; BEGIN WRITE("THE LARGEST INTEGER IS "); IF X > Y THEN WRITE(X); ELSE WRITE(Y); END; ALGOL-M USER'S MANUAL Page 22 ELEMENT READ statement FORMAT READ {file identifier} {,integer|identifier} (variable {,variable}...) {ONENDFILE procedure call} DESCRIPTION If the form of the READ statement is READ, then the input device is the console. Otherwise a file option must be specified and the input device is the disk. If the input device is a disk then an option- al record number may be specified if the file con- tains blocked records. A READ statement reads one or more variables at a time. The optional ONENDFILE section indicates action to be taken when the end of the specified file is reached. When this occurs an end of file warning will be printed and control will pass to the procedure listed following the ONEND- FILE. The procedure call must contain a parameter which may or may not be used in the procedure. EXAMPLE READ(WORDONE, X, VALUE2); READ FILE3 (WORDONE, X, VALUE2); READ INPUTFILE(X) ONENDFILE SUBR(1); READ FILE1,1(Y) ONENDFILE SUBR(2); READ TESTFILE,X(Z) ONENDFILE ENDFILE(1); PROGRAMMING NOTE The file identifier must have a CP/M file name as- signed to it prior to reading from that file. When this file name is assigned in an assignment state- ment it also opens the file. All files are automa- tically closed at the end of the block in which they are declared. A file close error will occurr if a file is declared and never opened. ALGOL-M USER'S MANUAL Page 23 ELEMENT reserved word list FORMAT letter {letter} ... DESCRIPTION The following words are reserved by ALGOL-M and may not be used as identifiers: AND ARRAY BEGIN CASE CLOSE DECIMAL DO ELSE END FILE FUNCTION GO GOTO IF INTEGER NOT OF ONENDFILE OR PROCEDURE READ STEP STRING TAB THEN TO UNTIL WHILE WRITE WRITEON Reserved words must be preceeded and followed by either a special character or a space. Spaces may not be embedded within reserved words. ALGOL-M USER'S MANUAL Page 24 ELEMENT simple statement FORMAT block|assignment statement|for statement| case statement|goto statement| while statement|read statement|write statement| procedure call|identifier DESCRIPTION All ALGOL-M statements are free form and must be separated by semicolons. ***** ELEMENT simple declaration FORMAT INTEGER|DECIMAL|STRING {(identifier|integer)} identifier {,identifier} ... DESCRIPTION Simple integer variables may be any value between - 16,383 and +16,383. Simple decimal variables can be specified as any length from one to 18 digits with a default length of 12 digits (7 digits to the left and 5 digits to the right of the decimal point). Simple string variables can be specified as any length from one to 255 characters with a default length of 10 characters. EXAMPLE INTEGER X; DECIMAL(15) X,Y; STRING(33) WORDONE, WORDTWO, WORDTHREE; ALGOL-M USER'S MANUAL Page 25 ELEMENT special characters DESCRIPTION The following special characters are used by ALGOL- M: ) close parenthesis * asterisk + plus - minus : colon ; semicolon < less-than > greater-than = equal , comma [ open bracket ] close bracket := assigned equal ** exponentiation(integer only) % percentage Any special character in the ASCII character set may appear in a string. Special characters, other than those listed above, will cause an error condition if used outside of a string. ALGOL-M USER'S MANUAL Page 26 ELEMENT TAB expression FORMAT TAB expression DESCRIPTION TAB is optionally used in a WRITE statement to cause spacing on the output line. The amount of spacing is specified by the integer expression following TAB. EXAMPLE WRITE("NEXT NAME", TAB 5, NAME[I]); PROGRAMMING NOTE TAB expressions are only used when writing to the console. ALGOL-M USER'S MANUAL Page 27 ELEMENT unbalanced statement FORMAT {label definition} IF boolean expression THEN state- ment {label definition} IF boolean expression THEN bal- anced statement ELSE unbalanced statement DESCRIPTION Unlike the balanced statement that will always have a balanced statement on either side of the ELSE in an IF THEN ELSE structure, an unbalanced statement may not even include the ELSE portion of the state- ment. EXAMPLE IF S > Y THEN WRITE(X); IF X < Y THEN IF Z > Y THEN WRITE(Z) ELSE WRITE(X); PROGRAMMING NOTE A semicolon is not allowed after the statement im- mediately preceding an ELSE. ALGOL-M USER'S MANUAL Page 28 ELEMENT variable FORMAT identifier {[bound pair list]} DESCRIPTION A variable in ALGOL-M may be simple or subscripted and of type INTEGER, DECIMAL, or STRING. EXAMPLE X VALUE[2] Z[1,X * Y] ***** ELEMENT WHILE statement FORMAT WHILE boolean expression DO simple statement DESCRIPTION WHILE statements continue executing the simple statement following the DO for as long as the boolean expression is true. EXAMPLE WHILE I > 0 DO I := I - 1; WHILE X > 5 AND Y <> 8 DO BEGIN X := X / 3; WRITE(X); END; ALGOL-M USER'S MANUAL Page 29 ELEMENT WRITE statement FORMAT WRITE|WRITEON {file option} (expression|tab expression|string {,expression|tab expression|string} ...) DESCRIPTION The WRITE option indicates the output will start printing on a new line, while the WRITEON option will continue printing on the same line. If the form of the statement is WRITE (or WRITEON), the output device is the console. Otherwise, a file option must be specified and the output device is the disk. When writing to a blocked disk file you may optionally specify a record number and output will go to that record in the file. EXAMPLE WRITE(X); WRITE("THE NUMBER IS",X + Y); WRITE("ANSWER", TAB 5, X * Y); WRITE FILE("hello"); WRITE FILEA,5(X,Y); WRITE FILE2,Z(X);