Procedure variables and local procedures
On 2017-09-28 21:15, August Karlstrom wrote
On 2017-09-28 18:57, Andreas Pirklbauer wrote: Addendum: Another way to read the sentence “P must not be declared local to ANOTHER procedure” is that in fact “P may be declared local to the SAME procedure”.
Your interpretation seemed far fetched at first, but you are most likely right. I get it now.
-- August
In fact, with the latest revision of Oberon-07 one could rewrite the above language rule as “If a procedure P is assigned to a procedure variable v, both v and P must be visible in the scope where the assignment is made and when it is made”. Thus, Oberon-07 now no longer *require*s the enclosing scope of a local procedure P to be active when P is executed. P only needs to be “reachable somehow”, and this could be via a global variable, as shown below. However I would not recommend adopting such a programming pattern (the example is just meant to show what is now “legally possible”). This may be why this new “feature" is advertised anywhere. It can be seen a byproduct of the decision to restrict access to variables on the global or the strictly local level. This eliminated the need for a static link in implementations. And this in turn means that the enclosing scope doesn’t have to be around anymore when a procedure P is executed. -Andreas MODULE M; TYPE Proc = PROCEDURE; VAR GlobalVar: Proc; PROCEDURE GlobalProc1; VAR LocalVar1: Proc; PROCEDURE LocalProc1; BEGIN END LocalProc1; BEGIN (*GlobalProc1*) END GlobalProc1; PROCEDURE GlobalProc2; VAR LocalVar2: Proc; PROCEDURE LocalProc2; BEGIN END LocalProc2; BEGIN (*GlobalProc2*) GlobalVar := GlobalProc1; (*allowed*) GlobalVar := GlobalProc2; (*allowed*) GlobalVar := LocalProc2; (*allowed (LocalProc2 visible in current scope)*) LocalVar2 := GlobalProc1; (*allowed*) LocalVar2 := GlobalProc2; (*allowed*) LocalVar2 := LocalProc2; (*allowed (LocalProc2 visible in current scope)*) (* GlobalVar := LocalProc1; (*not allowed (LocalProc1 undefined)*) LocalVar1 := GlobalProc1; (*not allowed (LocalVar1 undefined)*) LocalVar1 := GlobalProc2; (*not allowed (LocalVar1 undefined)*) LocalVar1 := LocalProc1; (*not allowed (LocalVar1 and LocalProc1 undefined)*) LocalVar1 := LocalProc2; (*not allowed (LocalVar1 undefined)*) *) END GlobalProc2; BEGIN (*M*) GlobalVar := GlobalProc1; (*allowed*) GlobalVar := GlobalProc2; (*allowed*) GlobalVar; (*allowed*) GlobalProc2; (*allowed (assigns LocalProc2 to GlobalVar)*) GlobalVar; (*allowed (effectively executes LocalProc2) !!!*) (* GlobalVar := LocalProc1; (*not allowed (LocalProc1 undefined)*) GlobalVar := LocalProc2; (*not allowed (LocalProc2 undefined)*) LocalVar1 := GlobalProc1; (*not allowed (LocalVar1 undefined)*) LocalVar1 := GlobalProc2; (*not allowed (LocalVar1 undefined*) LocalVar1 := LocalProc1; (*not allowed (LocalVar1 and LocalProc1 undefined)*) LocalVar1 := LocalProc2; (*not allowed (LocalVar1 and LocalProc2 undefined)*) LocalVar2 := GlobalProc1; (*not allowed (LocalVar2 undefined)*) LocalVar2 := GlobalProc2; (*not allowed (LocalVar2 undefined*) LocalVar2 := LocalProc1; (*not allowed (LocalVar2 and LocalProc1 undefined)*) LocalVar2 := LocalProc2; (*not allowed (LocalVar2 and LocalProc2 undefined)*) *) END M.
________________________________________ From: Oberon [oberon-bounces@lists.inf.ethz.ch] on behalf of Andreas Pirklbauer [andreas_pirklbauer@yahoo.com] Sent: Friday, September 29, 2017 6:04 AM To: ETH Oberon and related systems Subject: [Oberon] Procedure variables and local procedures
And this in turn means that the enclosing scope doesn’t have to be around anymore when a procedure P is executed.
What happens when the enclosing procedure GlobalProc declares its local variables, which are then used within its local procedure? Local variables are allocated on the stack. Does the "counter" exist when the procedure IncCounter is called by a client module? Wojtek MODULE M; TYPE Proc* = PROCEDURE; VAR GlobalProc*: Proc; (*exported*) LocalProc: Proc; (*not exported*) PROCEDURE GlobalProc2; VAR LocalVar2: Proc; counter: INTEGER; (*allocated on the stack*) PROCEDURE ZeroCounter; BEGIN counter := 0 END ZeroCounter; PROCEDURE IncCounter; BEGIN counter := counter + 1 END IncCounter; BEGIN (*GlobalProc2*) GlobalProc := IncCounter; (*allowed (LocalProc2 visible in current scope)*) LocalProc := ZeroCounter; (*allowed (LocalProc2 visible in current scope)*) END GlobalProc2; BEGIN (*M*) GlobalProc2(); (*assign the variables*) ZeroCounter (); (*calling proc local to the module via proc variable*) (*IncCounter can now be called by clients. Can it be really?*) END M.
participants (2)
-
Andreas Pirklbauer -
Skulski, Wojciech