Algol 60 – Sample Implementation and Examples

Example for a hardware representation of Algol 60

 

Contents

 

Description of a hardware representation of Algol 60

The following description gives an example of a "hardware representation" of Algol 60 according to the Revised Report on the Algorithmic Language Algol 60 (CACM, Vol. 6, p. 1; The Computer Journal, Vol. 9, p. 349; Numerische Mathematik, Vol. 4, p. 420.).
 

1. Symbols

1.1. Letters

Only upper case letters are used:

    A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | 
    Q | R | S | T | U | V | W | X | Y | Z
 

1.2. Digits

    0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
 

1.3. Simple symbols

   + | - | * | / | , | . | 10 | : | ; | := | _ | ( | ) | [ | ] | "

The underscore ( _ ) is used for a blank space in strings for the symbol BLANK used in the Revised Report.
* is used for the symbol TIMES of the reference language.
The double quote (") is used for both string brackets (` '). The use of a single symbol limits the use of strigs in this sample implementation.

 

1.4. Keywords

As in most implementations any keywords of the language Algol 60 are enclosed in single quotes ('...'):

   'BEGIN' | 'END' |
   'IF' | 'THEN' | 'ELSE' | 'FOR' | 'DO' | 'GOTO' |
   'STEP' | 'UNTIL' | 'WHILE' | 'COMMENT' |
   'REAL' | 'INTEGER' | 'BOOLEAN' | 'ARRAY' | 'SWITCH' | 'PROCEDURE' |
   'LABEL' | 'STRING' | 'VALUE' |
   'TRUE' | 'FALSE' |
   
   'EQUAL' | 'NOTEQUAL' | 'LESS' | 'NOTLESS' | 'GREATER' | 'NOTGREATER' |
   'NOT' | 'AND' | 'OR' | 'IMPL' | 'EQUIV' |
   'POWER'

The second group of keywords consists of litteral translations of the symbols used in Algol 60 as a reference language (as described in the Revised Report).
These keywords refer to the symbols of the reference language as follows:

    'AND'     AND
 'EQUAL' =
 'EQUIV' EQUIVALENCE
 'GREATER' >
 'IMPL' IMPLICATION
 'LESS' <
 'NOT' ¬
 'NOTEQUAL' NOTEQUAL
 'NOTGREATER' NOTGREATER
 'NOTLESS' NOTGREATER
 'OR' OR
 'POWER' POWER

Other hardware representations may use other keywords like 'NOT EQUAL', 'NOT GREATER', 'NOT LESS' or ** for 'POWER'. Additional types as 'REAL2' for double precision or 'COMPLEX' for complex numbers may be implemented.

 

1.5. Names

Names of variables, labels, switches or procedures may be of any length where only the first 6 characters are of significance (giving a possible total of 1 617 038 306 unique names).

 

2. Standard functions

2.1. Arithmetical functions

As most implementations the hardware representation described incorporates a set of mathematical standard procedures:

    Procedure     Parameter     Result
 ABS(X) integer / real integer / real
 SQRT(X) integer / real real
 SIN(X) integer / real real
 COS(X) integer / real real
 ARCTAN(X) integer / real real
 LN(X) integer / real real
 EXP(X) integer / real real
 SIGN(X) integer / real integer
 ENTIER(X) real integer

Some implementations provide ARCTAN with an optional second argument for the quadrant.

 

2.2. Input - Output procedures

The "Report on Input-Output Procedures for Algol 60" (IFIP; Numerische Mathematik, Vol. 6 1964, 5) defines some procedures for input and output that are required for any hardware representation.

The following procedure reads any numerical input of type integer or real to a list of variables given as argument:

   READ(V1, V2, V3, ... Vn)

The following procedure prints a given list of expressions in a new line (with intermediate spaces):

   PRINT(E1, E2, E3, ... En)

The following procedure prints a given expression at the current printing position (without trailing space):

   TYPE(E)

The following procedure prints a string in a new line:

   WRITE("This_is_a_string")

 
 

Examples
 

E1: Simple I/O operations

'BEGIN'
   'REAL' A3, A2, A1, A0, X, P;
   READ (A3, A2, A1, A0, X);
   WRITE("POLYNOM: COEFFICIENTS");
   PRINT (A3, A2, A1, A0);
   WRITE ("ARGUMENT:");
   PRINT (X);
   P := 0;
   P := P * X + A3;
   P := P * X + A2;
   P := P * X + A1;
   P := P * X + A0;
   WRITE ("VALUE = ");
   TYPE (P);
'END'

E2: Condition

'BEGIN'
   'REAL' A1, A2, A3, A4, B1, B2 D;
   READ (A1, A2, A3, A4, B1, B2 D);
   D := A1 * A4, - A2 * A3;
   'IF' D 'NOTEQUAL' 0
      'THEN'
         'BEGIN'
            WRITE ("DEFINITE SOLUTION:");
            PRINT ((B1 * A4 - B2 * A2)/D);
            PRINT ((A1 * B2 - A3 * B1)/D)
         'END'
       'ELSE'
         'WRITE ("MATRIX IS SINGULAR.");
'END'

E3: Labels, goto-statemets

'BEGIN'
   'COMENT' POLYGON: CALCULATES THE SIDES OF A POLYGON
            FOR Z POINTS IN R2 WITH X AND Y VALUES.
            (Z IS THE FIRST INPUT VALUE FOLLOWED BY PAIRS
            OF VALUES FOR INDIVIDUAL POINTS)
            PRINTS FOR EACH POINT THE FOLLOWING VALUES:
            NUMBER, X, Y, LENGTH OF SIDE, TOTAL LENGTH.;
   'INTEGER' I, Z;
   'REAL' X0, Y0, XA, Y,A, XN, YN, TL, L;
   READ(Z); READ(XA, YA);
   X0:= XA; Y0:= YA;
   I:= 0; L:= 0.0;
   NEW:  READ(XN, YN);
   LAST: I:= I+1;
         TL:= SQRT((XN-XA)*(XN-XA) + (YN-YA)*(YN-YA));
         L:= L+TL;
         PRINT(I, XA, YA, TL, L);
         XA:= XN;
         YA:= YN;
         'IF' I 'LESS' Z-1 'THEN' 'GOTO' NEW;
         'IF' I 'EQUAL' Z-1 'THEN'
             'BEGIN'
                 XN:= X0;
                 YN:= Y0;
                 'GOTO' LAST;
             'END';
'END' POLYGON

E4: For-statement

'BEGIN'
   'COMENT' FACULTY;
   'INTEGER' I, N, F;
   'READ(N);
   NF:= 1; I:= 0;
   PRINT(I, NF);
   'FOR' I:= 1 'STEP' 1 'UNTIL' N 'DO'
      'BEGIN'
         NF:= NF*I;
         PRINT(I, NF);
     'END';
'END' FACULTY

E5: For-while-statement

'BEGIN'
   'COMMENT' ZERO VALUE OF A FUNCTION BY APPROXIMATION;
   'REAL' A, B, X, EPS;
   A := -1.5; B:= 0;
   READ(EPS);
   'FOR' X:= (A + B)/2 'WHILE'
      B - A 'GREATER' EPS 'AND'
      X 'NOTEQAUL' A 'AND'
      X 'NOTEQUAL' B 'DO'
         'IF' X + COS(X) 'LESS' 0 'THEN' A := X 'ELSE' B := X;
   PRINT(A,B);
'END'

E6: Simple array

'BEGIN'
   'INTEGER' I;
   'REAL' R;
   'ARRAY' A[1:10];
   R :=0;
   'FOR' I := 1 'STEP' 1 'UNTIL' 10 DO
   'BEGIN'
      READ(A[I]);
      R := R + A[I] * A[I];
   'END';
   'IF' R 'EQUAL' 0 'THEN' 'GOTO' L1;
   'COMMENT' NORMALIZE VALUES;
   'FOR' I := 1 'STEP' 1 'UNTIL' 10 DO
   'BEGIN'
      A[I] := A[I]/R;
      PRINT(A[I]);
   'END';
L1:
'END'

E7: Dynamic arrays, blocks

'BEGIN'
   'COMMENT' PRODUCT OF TWO N*N MATRICES;
   'INTEGER' N;
   READ(N);
   'BEGIN'
      'ARRAY' A, B, C[1:N, 1:N];
      'INTEGER' I, J, K;
      'REAL' S;
      'FOR' I:= 1 'STEP' 1 'UNTIL' N 'DO'
      'FOR' J:= 1 'STEP' 1 'UNTIL' N 'DO'
         READ(A[I,J)]);
      'FOR' I:= 1 'STEP' 1 'UNTIL' N 'DO'
      'FOR' J:= 1 'STEP' 1 'UNTIL' N 'DO'
         READ(B[I,J)]);
      'FOR' I:= 1 'STEP' 1 'UNTIL' N 'DO'
      'FOR' J:= 1 'STEP' 1 'UNTIL' N 'DO'
      'BEGIN'
         S := 0;
         'FOR' K:= 1 'STEP' 1 'UNTIL' N 'DO'
         S := S + A[I,K] * B[K,J];
         C[I,J] := S;
         PRINT(S);
      'END';
   'END';
'END'

E8: Switch

'BEGIN'
   'COMMENT' CONVERT A DIGIT (1-7) TO WEEKDAY;
   'INTEGER' I;
   'SWITCH' D := D1, D2, D3, D4, D5, D6, D7;
   READ(I);
   'IF' D 'GREATER' 7 'THEN' 'GOTO' FIN;
   'GOTO' D[I];
   D1: 'BEGIN'
          WRITE("MON"); 'GOTO' FIN;
       'END';
   D2: 'BEGIN'
          WRITE("TUE"); 'GOTO' FIN;
       'END';
   D3: 'BEGIN'
          WRITE("WEN"); 'GOTO' FIN;
       'END';
   D4: 'BEGIN'
          WRITE("THU"); 'GOTO' FIN;
       'END';
   D5: 'BEGIN'
          WRITE("FRI"); 'GOTO' FIN;
       'END';
   D6: 'BEGIN'
          WRITE("SAT"); 'GOTO' FIN;
       'END';
   D7: 'BEGIN'
          WRITE("SUN"); 'GOTO' FIN;
       'END';
FIN:
'END'

E9: Procedure

'BEGIN'
   'INTEGER' I, L, P;
   
   'COMMENT' SELECTS THE LEFT DIGIT OF THE
             DIGIT WITH THE POSITION K OF
             A NUMBER N WITH A LENGTH OF M;
   
   'INTEGER' 'PROCEDURE' SELECT(N,M,K);
      'VALUE' N, M K;
      'INTEGER' N, M, K;
      'BEGIN'
         'INTEGER' L;
         L := 10 'POWER'(M-K);
         SELECT := ENTIER(N/L) - ENTIER(N/L/10) * 10;
      'END';
   
   READ(I, L, P);
   PRINT(SELECT(I,L,P));
'END'

E10: Complex procedures

'BEGIN'
   'REAL' XBGN, XEND, EPS, INT;
   
   'REAL' 'PROCEDURE' TRAPEZION (XB, XE, FX);
      'VALUE' XB, XE;
      'REAL' 'PROCEDURE' FX;
      'BEGIN'
        'INTEGER' I, K;
        'REAL' DX, SUM, T1, T2; 
        DX := XE - XB;
        T2 := DX * 0.5 * (FX(XB) + FX(XE));
        K := 1;
  DOUB: K := K * 2;
        T1 := T2;
        SUM := 0.0;
        'FOR' I := 1 'STEP' 2 'UNTIL' K 'DO'
           SUM := SUM + FX(XB + DX/K * I);
        T2 := 0.5 * (SUM * DX/K *2.0 + T1);
        'IF' ABS ((T1 - T2)/T2) 'GREATER' EPS 'THEN' 'GOTO' DOUB;
      'END' TRAPEZION;
   
   'REAL' 'PROCEDURE' F(X);
      'REAL' X;
      F := EXP (-X*X);
   
   'COMMENT' MAIN;
LOOP: READ (XBGN, XEND, EPS);
      'IF' EPS 'EQUAL' 0.0 'THEN' 'GOTO' FIN;
      INT := TRAPEZION (XBGN, XEND, F);
      PRINT (XBGN, XEND, EPS, INT);
      'GOTO' LOOP;
FIN:
'END'

E11: Another complex procedure à la Jensen

'BEGIN'
   'REAL' X;
   
   'REAL' 'PROCEDURE' TRAPEZION(F, X, E);
   'VALUE' E;
   'REAL' F, X, E;
   'BEGIN'
      'INTEGER' I, D;
      'REAL' U, T, S;
      X := 0; D := 1; T := F;
      X := 1; T := (T + F)/2;
   M: S := 0;
      'FOR' I := 1 'STEP' 1 'UNTIL' D 'DO'
      'BEGIN'
         X := (2 * I - 1)/D/2;
         S := S + F
      'END';
      U := (T + S/D)/2;
      'IF' ABS(U - T) 'LESS' E 'THEN' TRAPEZION := U
      'ELSE'
      'BEGIN'
         T := U;
         D := D * 2;
         'GOTO' M
      'END'
   'END';
   
   PRINT(TRAPEZION((1 - X) / (1 + X * X), X, 10-6))
'END'

E12: Recursion

'BEGIN'
   'COMMENT' FACULTY (RECURSIVE);
   'INTEGER' N;
   
   'INTEGER' 'PROCEDURE' FAC(K);
      'VALUE' K;
      'INTEGER' K;
      'IF' K 'EQUAL' 0 'THEN' FAC := 1 'ELSE' FAC := K * FAC(K-1);
   
   READ(N);
   PRINT(N, FAC(N))
'END'

E13: A quite complex loop

'BEGIN'
   'COMMENT' WHAT WOULD X READ FOR ANY Y;
   'INTEGER' X, Y, V;
   X := 0;
   READ (Y);
   
   'FOR' V := 1, 2, 3 'IF' Y 'GREATER' 0 'THEN' 4 'ELSE' 5,
      7 'STEP' 2 'UNTIL' 12,
      15 'WHILE' X 'LESS' 100, 3
      'DO'
      X := X + V;
   
   WRITE("RESULT: ");
   TYPE(X)
'END'

 

Notes

Some of the examples are taken with only minor changes from the book "Einführung in das Programmieren mit ALGOL 60" by Götz Alefeld, Jürgen Herzberger and Otto Mayer (Mannheim 1972: Bibliographisches Institut AG).

The examples are not given in order to teach Algol 60 but with the intention to provide an impression of what an Algol program looks like.

This page was edited by N. Landsteiner (n.landsteiner@masswerk.at) 2002.

 


List of symbols by Algol 60 as a reference language and their representation:

[BLANK] A blank. Printed like a half box.
[10] The ten for the exponent in a real-type number. Printed as a small lowered ten.
[POWER] The power operator: an uparrow.
[TIMES] The times sign: a cross like an x.
[<] Simple: less than.
[NOTGREATER] Simple: less or equal.
[=] Simple: equal.
[NOTLESS] Simple: greater or equal.
[>] Simple: greater than.
[NOTEQUAL] Simple: not equal.
[EQUIVALENCE] Simple: logical equivalence.
[IMPLICATION] Simple: logical implication.
[OR] Simple: logical or.
[AND] Simple: logical and.
[¬] Simple: logical not.

 
Other AGOL 60 related documents to be found on this site:

   ^ top


mass:werk - media environments