Author |
Topic: Labels in Subroutines (Read 470 times) |
|
Jack Kelly
Full Member
member is offline
Gender:
Posts: 106
|
|
Labels in Subroutines
« Thread started 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"
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Labels in Subroutines
« Reply #1 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.
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Labels in Subroutines
« Reply #2 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:
The program attempts to GOTO or GOSUB a label in the main program that appears after one or more SUB or FUNCTION definitions The destination label happens to be the same as a local label in one of those SUBs or FUNCTIONs. 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.
|
|
|
|
|