Nested procedures

Topics related to the use of Oberon language features
augustk
Posts: 52
Joined: Mon Sep 05, 2011 5:21 pm
Location: Sweden

Re: Nested procedures

Post by augustk » Wed Dec 11, 2013 1:57 pm

If I understand it correctly even simple recursion in a local procedure like

Code: Select all

PROCEDURE P;
	PROCEDURE Q;
	BEGIN
		...
		Q
	END Q;
BEGIN
	Q
END P
is not allowed in Oberon-07/13.

cfbsoftware
Site Admin
Posts: 405
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Nested procedures

Post by cfbsoftware » Thu Dec 12, 2013 12:09 pm

I can see why you ask - but it is allowed. While it is true that Q is declared locally in P, the identifier (in this case Q) that represents the name of a procedure is also visible in its own scope. Note that if that weren't true, the following would be possible:

Code: Select all

PROCEDURE Q;
   VAR Q: INTEGER;
BEGIN
   ...
   Q := 0;
END Q;

augustk
Posts: 52
Joined: Mon Sep 05, 2011 5:21 pm
Location: Sweden

Re: Nested procedures

Post by augustk » Fri Dec 13, 2013 9:38 am

OK, I thought every identifier belonged to exactly one scope. Your example does make sense if that was the case.

cfbsoftware
Site Admin
Posts: 405
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Nested procedures

Post by cfbsoftware » Fri Dec 20, 2013 11:51 pm

After working with Prof Wirth's latest Oberon for FPGA compiler, and receiving replies to a couple of questions I realise that my last reply, and probably some of my earlier posts in this discussion, were incorrect.

The following is actually legal Oberon code because PROCEDURE Q is a local variable in the outer scope:

Code: Select all

PROCEDURE Q;
   VAR Q: INTEGER;
BEGIN
   ...
   Q := 0;
The consequence of this is that you would not be able to call Q recursively in this example.

Similarly (though unlikely to be seen in the real world) the following is also legal:

Code: Select all

PROCEDURE P2;
  PROCEDURE P2; 
  BEGIN 
  ...
  END P2;
BEGIN 
  P2
END P2;
Again the call to P2 is not recursive as it refers to the local P2.

Therefore, for now, I am now planning to keep the behaviour of nested procedures and local objects the same as it was in Astrobe v4.5.

Locked