Pascal Library

The following is an example of a Pascal library.

library lib; usage(sharing = sharedbyall);
     interface
        type vect = array [1..30] of integer;
        procedure sum (vector1, vector2: vect);
        function fact (n: integer): integer;
        function sin (r: real): real;
     end;

     library mylib (title = 'OBJECT/ARITHLIB');
     procedure sum;
        var i: integer;
        begin
        for i:= 1 to 30 do
           vector[i]:= vector1[i] + vector2[i];
        end;
     function sin; mylib;
     function fact;
        begin
        if n < 1 then
           fact:= 1
        else
           fact:= n * fact(n-1);
        end;

     begin
     freeze;
     end.

This library exports the procedure sum and the functions fact and sin, all of which appear in the interface part. Sum and fact are completely declared in the library block. Sin is imported from another library.

When an ALGOL library procedure uses an EBCDIC array that was passed from a Pascal client program, the library procedure should specify the starting index of the array. This precaution is necessary because ALGOL processes the following statements slightly differently:

REPLACE A BY “HI”;
REPLACE A[0] BY “HI”;

The following statement causes the value “HI” to be correctly assigned to element 0 of array A:

REPLACE A[0] BY “HI”;

The following ALGOL library illustrates the use of these types of statements:

$SHARING = PRIVATE
BEGIN
PROCEDURE ALGOLDISPLAY(S1, S2);
  EBCDIC ARRAY S1[*], S2[*]
  BEGIN
  REPLACE S1[0] BY “HI”; % THESE TWO STATEMENTS ARE CORRECT
  DISPLAY(S1[0]);

  REPLACE S2 BY “HI”     % THESE TWO STATEMENTS ARE ALLOWED,
  DISPLAY(S2);          % BUT WILL NOT GIVE THE EXPECTED RESULTS.

  END;

  EXPORT ALGOLDISPLAY;
  FREEZE(TEMPORARY);
END.

The following example illustrates how an ALGOL client program should pass an unbounded array to a Pascal library. The ALGOL client program declares the imported procedure PASCPROC with an unbounded array parameter A. The ALGOL client program declares another array, called MYARRAY, for use as the actual parameter. MYARRAY is declared with a lower bound of 0 because a Pascal library always assumes the first array element to be at index 0.

BEGIN
   LIBRARY MYLIB;
   PROCEDURE PASCPROC(A);
      EBCDIC ARRAY A[*];
      LIBRARY MYLIB;
   EBCDIC ARRAY MYARRAY [0:5];

   REPLACE MYARRAY BY “DATA”;
   PASCPROC(MYARRAY);

END.

The following Pascal program invokes the procedure ALGOLDISPLAY in an ALGOL library called OBJECT/ALGOLLIB.

program p;
  type
    stringbyte = packed array [1..20] of char;

  library mylib(title= 'OBJECT/ALGOLLIB');
  procedure algoldisplay(s1, s2: stringtype); mylib;

  var
    s1, s2: stringtype;

  begin

  s1:= 'abcdefghijklmnopqrst';
  s2:= '12345678901234567890';

  algoldisplay(s1, s2);
  end.