trijezdci
2016-04-11 17:21:40 UTC
I ran into the following pathological scenario while working on the symbol table for M2C ...
PROCEDURE Outer;
VAR foo : CARDINAL;
PROCEDURE Inner;
BEGIN
foo := 0;
bar := 0;
...
END Inner;
...
VAR bar : INTEGER;
...
BEGIN
...
END Outer;
Apparently this is not strictly illegal in PIM.
At the point where variable bar is first used within procedure Inner, it has not been declared yet which means it is not present in the symbol table and the symbol table lookup then returns an unknown symbol response. If the above scenario is to be supported, it will complicate scope management because by the time variable bar is declared, scope Inner has already been closed. I discard scopes when closing them.
I found that I can get around this by first collecting all non-procedure declarations before proceeding with all the procedure declarations and do so in every scope.
However, I am not sure this is desirable. Clearly, such practise significantly diminishes the readability of the source code.
Can anyone think of any use cases that could justify this being allowed?
The only two use cases I can think of where use-before-declaration is desirable are:
(1) a type declaration with a reference to another type
TYPE T1 = POINTER TO T2;
TYPE T2 = ... ;
(2) a procedure referencing another procedure (either assignment or call) within its body.
PROCEDURE P1;
BEGIN
...
P2;
...
END P1;
PROCEDURE P2;
Any other scenarios of use-before-declaration do not seem to make sense to me.
Can anyone think of any other useful use cases where use-before-declaration semantics are desirable?
thanks in advance
PROCEDURE Outer;
VAR foo : CARDINAL;
PROCEDURE Inner;
BEGIN
foo := 0;
bar := 0;
...
END Inner;
...
VAR bar : INTEGER;
...
BEGIN
...
END Outer;
Apparently this is not strictly illegal in PIM.
At the point where variable bar is first used within procedure Inner, it has not been declared yet which means it is not present in the symbol table and the symbol table lookup then returns an unknown symbol response. If the above scenario is to be supported, it will complicate scope management because by the time variable bar is declared, scope Inner has already been closed. I discard scopes when closing them.
I found that I can get around this by first collecting all non-procedure declarations before proceeding with all the procedure declarations and do so in every scope.
However, I am not sure this is desirable. Clearly, such practise significantly diminishes the readability of the source code.
Can anyone think of any use cases that could justify this being allowed?
The only two use cases I can think of where use-before-declaration is desirable are:
(1) a type declaration with a reference to another type
TYPE T1 = POINTER TO T2;
TYPE T2 = ... ;
(2) a procedure referencing another procedure (either assignment or call) within its body.
PROCEDURE P1;
BEGIN
...
P2;
...
END P1;
PROCEDURE P2;
Any other scenarios of use-before-declaration do not seem to make sense to me.
Can anyone think of any other useful use cases where use-before-declaration semantics are desirable?
thanks in advance