Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ON GOSUB may be slightly off. #38

Closed
flagxor opened this issue Sep 27, 2018 · 3 comments
Closed

ON GOSUB may be slightly off. #38

flagxor opened this issue Sep 27, 2018 · 3 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@flagxor
Copy link
Collaborator

flagxor commented Sep 27, 2018

ON GOSUB unconditionally pushes a return address onto the stack, even when out of bounds.
Unsure if this matches canonical behavior.
We should confirm and add a test.

@flagxor flagxor added help wanted Extra attention is needed good first issue Good for newcomers labels Sep 27, 2018
@pauwell
Copy link
Collaborator

pauwell commented Sep 27, 2018

I found the following article really interesting:
https://www.atarimagazines.com/compute/issue87/The_Power_Of_ON-GOTO_And_ON-GOSUB.php

It says the following about on..gosubs behaviour in this case:

10 PRINT "ENTER NUMBER : "; : INPUT D
20 PRINT "D = "; D; " : ";
30 ON D GOSUB 40, 50, 60, 70 : GOSUB 80 : GOTO 10
40 PRINT "CAME TO LINE 40 : "; : RETURN
50 PRINT "CAME TO LINE 50 : " : RETURN
60 PRINT "CAME TO LINE 60 : "; : RETURN
70 PRINT "CAME TO LINE 70 : "; : RETURN
80 PRINT "CAME TO LINE 80 : "; : RETURN

When you run this program, it asks you to enter a number. If you enter 1, the program displays the message Came to line 40. After it returns from the subroutine at line 40, notice that the program does not proceed to the routines in lines 40–70. Instead, it proceeds to the next BASIC statement in the line, GOSUB 80. This statement is always performed, allowing you to note the difference between ON-GOSUB and a normal GOSUB command.

Now try entering a number smaller than 1 or greater than 4. Notice how ON-GOSUB responds. In this case, the program skips all of the possible destination lines listed in the ON-GOSUB statement. ON-GOSUB does not cause any branches, and the program proceeds to the next statement (GOSUB 80). For example, if you enter 0 at the prompt, the program prints Came to line 80.

Is this how it should handled here? I read that in some versions of BASIC it results in an error but in most versions it just gets skipped if it is out of bounds.

@flagxor
Copy link
Collaborator Author

flagxor commented Sep 27, 2018

My offhand reaction is that until we have a more unified place to put compat flags / modes of operation that we'd want to support the most permissive thing (fallthru) as working programs are unlikely to meaningfully rely on erroring in this case.

But this might be a good place for a TODO: Make this behavior configurable once we have BASIC version flags.

In terms of the return behavior, also note that:

10 PRINT "ENTER NUMBER : "; : INPUT D
20 PRINT "D = "; D; " : ";
30 ON D GOSUB 40, 50, 60, 70 : GOSUB 80 : GOTO 10
40 PRINT "CAME TO LINE 40 : "; : RETURN
50 PRINT "CAME TO LINE 50 : " : RETURN
60 PRINT "CAME TO LINE 60 : "; : RETURN
70 PRINT "CAME TO LINE 70 : "; : RETURN
80 PRINT "CAME TO LINE 80 : "; : RETURN

Emits:

hi
hi
there

Where one might have expected:

hi
there

@flagxor
Copy link
Collaborator Author

flagxor commented Sep 30, 2018

Resolved in:
#39

Thanks pauwell!

@flagxor flagxor closed this as completed Sep 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants