LB Booster
Programming >> Compatibility with LB4 >> Labels in Subroutines
http://lbb.conforums.com/index.cgi?board=compatibility&action=display&num=1465114905

Labels in Subroutines
Post by Jack Kelly on Jun 5th, 2016, 08:21am

Labels do not seem to be local in LBB. They are in JB, and should be in my opinion.

Code:
goto [a]

sub x
[a]
print "sub a"
end sub

[a]
print "main a"

 

Re: Labels in Subroutines
Post by Richard Russell on Jun 5th, 2016, 09:15am

on Jun 5th, 2016, 08:21am, Jack Kelly wrote:
Labels do not seem to be local in LBB. They are in JB, and should be in my opinion.

Labels are local in LBB, for all practical purposes (many programs would not work if that was not the case). Try this:

Code:
call x
goto [a]
end

[a]
print "main a"
call x
end

sub x
goto [a]
exit sub

[a]
print "sub a"
end sub 

It's true that your example doesn't work in LBB, but that's because you're using a GOTO to 'jump over' a SUB, something that no sensible program would ever do.

There's a limit to the extent to which I can emulate the precise behaviour of labels in LBB, because I'm constrained by the way the underlying BBC BASIC works. But apart from 'contrived' examples you should find that local labels work as expected, and my LBB test suite includes programs that wouldn't run otherwise.

Richard.

Re: Labels in Subroutines
Post by Richard Russell on Oct 1st, 2016, 7:17pm

on Jun 5th, 2016, 09:15am, Richard Russell wrote:
apart from 'contrived' examples you should find that local labels work as expected, and my LBB test suite includes programs that wouldn't run otherwise.

It has been suggested that it would be helpful to provide more details about the circumstances in which LBB differs in the way labels work, which I am happy to do. The specific situation in which LBB won't behave the same as JB/LB is when the following conditions both apply:
In that case JB/LB will 'correctly' jump to the label in the main program, ignoring the label of the same name in the SUB/FUNCTION, but LBB will mistakenly jump to the 'local' label.

My assumption has always been that individually these conditions are unlikely, and that together they will virtually never happen. That's why I don't mention this rare special case in the LBB docs. If you want to ensure it never affects you, put all your SUBs and FUNCTIONs at the end, after the 'main program'.

Richard.