Examples of POINTER Declarations

In the following example, program 1 and program 2 are nearly identical. The only difference is found in the POINTER declaration at line 1000. In program 1, LOCALPOINTER is declared without a lex level restriction part, and the potential up-level pointer assignment at line 1200 of program 1 causes a syntax error. In program 2, LOCALPOINTER is declared with the lex level restriction part FOR GLOBALARRAY2, so the pointer assignment at line 1200 of program 2 cannot be an up-level pointer assignment and does not cause a syntax error. However, the restrictions imposed by the lex level restriction part cause a syntax error at line 1300 of program 2, where no error occurred in program 1.

 100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 200 %%%%%%%%%%%%%% PROGRAM 1 %%%%%%%%%%%%%%%
 300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 400 BEGIN                                        % LEX LEVEL 2
 500    POINTER GLOBALPOINTER;
 600    ARRAY GLOBALARRAY1,
 700          GLOBALARRAY2[0:9];
 800    GLOBALPOINTER := POINTER(GLOBALARRAY1);
 900    BEGIN                                     % LEX LEVEL 3
1000       POINTER LOCALPOINTER;
1100       ARRAY LOCALARRAY[0:9];
1200       GLOBALPOINTER := LOCALPOINTER;         % SYNTAX ERROR
1300       LOCALPOINTER := POINTER(LOCALARRAY);
1400    END;
1500 END.

 100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 200 %%%%%%%%%%%%%% PROGRAM 2 %%%%%%%%%%%%%%%
 300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 400 BEGIN                                        % LEX LEVEL 2
 500    POINTER GLOBALPOINTER;
 600    ARRAY GLOBALARRAY1,
 700          GLOBALARRAY2[0:9];
 800    GLOBALPOINTER := POINTER(GLOBALARRAY1);
 900    BEGIN                                     % LEX LEVEL 3
1000       POINTER LOCALPOINTER FOR GLOBALARRAY2;
1100       ARRAY LOCALARRAY[0:9];
1200       GLOBALPOINTER := LOCALPOINTER;
1300       LOCALPOINTER := POINTER(LOCALARRAY);   % SYNTAX ERROR
1400    END;
1500 END.

As the following example illustrates, a call-by-name formal pointer parameter cannot be assigned the value of any pointer other than itself, because there is no way for the compiler to determine the lex level of the actual pointer parameter passed to the call-by-name formal pointer parameter.

BEGIN
      POINTER P1, P2;                            % LEX LEVEL 2
      ARRAY A[0:9];
      PROCEDURE P(PTRA, PTRB);
         POINTER PTRA, PTRB;
         BEGIN
         PTRA := PTRA + 3;                       % OK
         REPLACE PTRA:PTRA BY PTRB:PTRB FOR 5;   % OK
         PTRA := PTRB;                           % SYNTAX ERROR
         PTRA := P2;                             % SYNTAX ERROR
         PTRB := POINTER(A);                     % SYNTAX ERROR
         REPLACE PTRA:PTRB BY "X";               % SYNTAX ERROR
         END;
      P2 := POINTER(A);
      P(P1, P2);
   END.

As the following example illustrates, to prevent up-level pointer assignments that can result from separate compilation of procedures with global parts, a pointer declared in the global part cannot be assigned the value of any pointer other than itself.

[POINTER PTRA,PTRB;]
PROCEDURE P;
   BEGIN
   ARRAY A[0:9];
   PTRA := PTRA + 2;     % OK
   PTRA := POINTER(A);   % SYNTAX ERROR  THIS IS AN
                         %   UP-LEVEL POINTER ASSIGNMENT.
   PTRA := PTRB;         % SYNTAX ERROR  THE LEX LEVELS
                         %   OF PTRA AND PTRB ARE NOT KNOWN,
                         %   SO THIS IS A POTENTIAL UP-LEVEL
                         %   POINTER ASSIGNMENT.
   END.