Illegal use of GOTO and GOSUB

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Illegal use of GOTO and GOSUB

Post by BorisTheOld »

In view of some of the recent discussions on the forums, I would like to suggest the following new feature:

The compiler should flag the illegal use of GOTO and GOSUB statements, as it would for the illegal use of any other statement.

No matter what one thinks of the GOTO and GOSUB statements, they are a valid part of the language. It seems very strange to me that the compiler tests the correct usage of other statements, but leaves it to the programmer to get it right for these two statements.

For example, the compiler does not flag this as an error:

Code: Select all

x.l = 0
Select x
  Case 1
    Goto IllegalJump
  Case 2
EndSelect
IllegalJump:
And the information in the help file is totally inadequate:
This command is used to transfer the program directly to the labels position. Be cautious when using this function, as incorrect use could cause a program to crash.
What is incorrect use, and under what conditions could incorrect use cause a program to crash?

The help entry might just as well be:
We have provided the GOTO and GOSUB statements, but we're not documenting all the rules for their use, and the compiler will not test that you are using them correctly. Good luck and have a nice day.
:)
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Illegal use of GOTO and GOSUB

Post by User_Russian »

BorisTheOld wrote:For example, the compiler does not flag this as an error:
There is no error, from the point of view of the compiler.
And I'll tell you more, the code works fine.

Code: Select all

x.l = 1
Select x
  Case 1
    FakeReturn
    Goto IllegalJump
  Case 2
EndSelect
IllegalJump:
PS.
Earlier there was command FakeSelect, but it some reason excluded.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Illegal use of GOTO and GOSUB

Post by BorisTheOld »

@User_Russian

That's my point.

There have been several discussions recently about using "Goto" to escape from Select and loop structures, and how the stack gets corrupted. The compiler needs to detect and report incorrect usage. Vague statements in the documentation is not acceptable, and it shouldn't be necessary to look at ASM listings to track down bugs.

The only reference to "FakeReturn" in the documentation is in relation to "Gosub". There is no reference to it being applicable in any other situation.

If "FakeReturn" is not coded and there is a jump to a location outside the subroutine, then the compiler should flag it as an error.

If "FakeReturn" can be used in many situations, then this should be documented and the compiler should check for its correct use.
Last edited by BorisTheOld on Wed Oct 02, 2013 5:50 pm, edited 1 time in total.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
NicknameFJ
User
User
Posts: 90
Joined: Tue Mar 17, 2009 6:36 pm
Location: Germany

Re: Illegal use of GOTO and GOSUB

Post by NicknameFJ »

BorisTheOld wrote:
The compiler should flag the illegal use of GOTO and GOSUB statements, as it would for the illegal use of any other statement.
No, the compiler should clean up the stack so the program doesn´t crash und then jump out of the Select/case block.

As far as I can remember: In PB 5.20, I don´t know which beta, Fred tried to make the goto command bullet proof but it was buggy, so he removed it.

Oh yes, there was a Fakereturn command, anybody knows why it was removed?

Edit: I have just seen that fakereturn is still there

NicknameFJ
PS: Sorry for my weird english, but english is not my native language.



Image
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Illegal use of GOTO and GOSUB

Post by BorisTheOld »

NicknameFJ wrote:No, the compiler should clean up the stack so the program doesn´t crash und then jump out of the Select/case block.
That would be a solution, but opens up a can of worms related to structured code, etc.

It's probably best to flag jumps that go to places outside the current scope or structure.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Illegal use of GOTO and GOSUB

Post by heartbone »

Illegal?
Show me the law.

This request seems a bit control freakish to me.
Why not allow the legacy behavior?
If I'm hacking a quick snippet to demonstrate a bug,
I really don't want to bother with all of the extra test control structures needed to "properly" exit a particular loop.

Boris, perhaps a compile time warning might be appropriate,
but to your suggestion of a prohibition... NO.
Keep it BASIC.
User avatar
NicknameFJ
User
User
Posts: 90
Joined: Tue Mar 17, 2009 6:36 pm
Location: Germany

Re: Illegal use of GOTO and GOSUB

Post by NicknameFJ »

BorisTheOld wrote:
[...] but opens up a can of worms related to structured code, etc.
You are right but:

using goto <> structured code :mrgreen:

If someone want to use goto/gosub - let him. Anyone has to think about it by himself if he wants to use such commands and produce (maybe) unstructured code.

If someone decides to use that commands (which are part of the language, because it is BASIC) then the compiler should support the programer as good as the compiler can do and clean up the stack internal.

Maybe a compiler WARNING could be the solution, combined with cleaning up the stack internal.

Just my 2 cents


NicknameFJ
Last edited by NicknameFJ on Wed Oct 02, 2013 7:47 pm, edited 1 time in total.
PS: Sorry for my weird english, but english is not my native language.



Image
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Illegal use of GOTO and GOSUB

Post by BorisTheOld »

heartbone wrote:This request seems a bit control freakish to me. Why not allow the legacy behavior?
I'm a hacker from way back and tend to push languages beyond their limits. :) However, I think there should be some rules established, no matter what PB statement we're talking about.

If certain uses of a statement are known to cause stack problems, or the like, then I don't think it's unreasonable for the compiler to complain. After all, one of the reasons for a compiler is to detect and report on invalid code. To selectively not report known issues is of no help to programmers who make a coding error and spend hours or days trying to track down the problem.
NicknameFJ wrote:You are right but: using goto <> structured code :mrgreen:
I realize that, but to allow a jump from deep inside a complex nest of procedure calls, select structures, and loop structures, to an arbitrary point in another complex nest of procedure calls, select structures, and loop structures, might just create a few stack problems for the compiler to sort out.

That's why I think there should be some established rules that will cater to both the structured programming types and the hackers (like me and heartbone :) )
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Illegal use of GOTO and GOSUB

Post by idle »

There are macro replacements for goto and gosub here

http://www.purebasic.fr/english/viewtop ... 12&t=55242
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Illegal use of GOTO and GOSUB

Post by Demivec »

I agree that a warning may be appropriate to give someone a heads up on what might go wrong but still allow any use. Some better documentation that alerts a programmer to some specific cases would be useful as well.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Illegal use of GOTO and GOSUB

Post by BorisTheOld »

idle wrote:There are macro replacements for goto and gosub.....
Although these are useful, they still rely on the programmer using them in the correct context without any checking by the compiler.

The reason I made this request is that I believe all PB statements should have rules, that if broken, should be trapped by the compiler for the protection of regular coders. A simple set of rules for GOTO and GOSUB, that are reported by the compiler if broken, will protect most PB users from misplaced statements or incorrect usage.

Even though I tend to write "interesting" code, I still do it within the rules of the language and would want the compiler to prevent me from breaking the rules. But if I knowingly want to break the rules I do it via macros and ASM at my own risk.

The hackers have alternate ways of writing code if they need more flexibility. And that's the way it's always been.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Illegal use of GOTO and GOSUB

Post by BorisTheOld »

As well as using PB, we use two other mainline BASIC products to write DLL/SO libraries for stuff that PB can't handle.

Because of the interest in this request, I thought it might be useful to describe how other BASIC products treat GOTO and GOSUB. All products are fully featured and support OOP.

Basic #1 (Windows only):
a) GOTO and GOSUB use must be local to the mainline code or to a procedure/method/property
b) no restrictions on jumping out of select or loop structures

Basic #2 (cross platform)
a) full feature set: GOSUB is not allowed, GOTO use is the same as Basic #1 <-- correction
b) reduced feature set to support old code: GOTO and GOSUB use is the same as Basic #1

Basic #3 (Linux only) <-- added for completeness
a) GOSUB is not available, GOTO use is the same as Basic #1
b) GOTO not allowed to jump into select or loop structures

This isn't an exhaustive list, but it shows how each BASIC product has specific, but different, rules for using GOTO and GOSUB. And although they all support structured programming and OOP, they all support GOTO. There is obviously no doubt about the place of GOTO in the BASIC language. However, all three dialects have strict rules for its use.
Last edited by BorisTheOld on Thu Oct 03, 2013 6:10 pm, edited 2 times in total.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Illegal use of GOTO and GOSUB

Post by heartbone »

OK.
Now, for a more reasonable approach, how about reading this GOTO man page directly from the source?
http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgGoto
No OOBS.
Keep it BASIC.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Illegal use of GOTO and GOSUB

Post by RichAlgeni »

BorisTheOld wrote:Because of the interest in this request....
Completely and wholeheartedly disagree with this request. It is a waste of time and resource that is better spent elsewhere. As someone who's been coding for some time, you know the use of Goto's create unstructured code.

Use them as you wish since they are still provided, just don't use them inside a code block, such as If/Then/Else, etc.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Illegal use of GOTO and GOSUB

Post by BorisTheOld »

RichAlgeni wrote:As someone who's been coding for some time, you know the use of Goto's create unstructured code........
That must explain why my programs don't have bugs -- I've often wondered about that.

I also wonder why PB has no published rules about using Goto and Gosub, and why the compiler doesn't check for dangerous usage. I don't mind for myself, because apparently the Gods protect me from harm. But I am concerned that lesser mortals deserve more than a vague recommendation to be careful. :D
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Post Reply