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

Session: executing "GOSUB line" after entering program → strange result #184

Closed
Marrin opened this issue Jun 21, 2022 · 4 comments
Closed
Assignees
Labels

Comments

@Marrin
Copy link

Marrin commented Jun 21, 2022

PC-BASIC version: 2.0.4

I get a strange result when entering a program in a Session and the GOSUB to a specific line. Here is a small test case:

#!/usr/bin/env python3
import pcbasic

SOURCE = """\
10 PRINT "Main"
30 PRINT 42
40 END
50 PRINT "After End"
60 PRINT "Sub"
70 RETURN
"""

def main():
    with pcbasic.Session() as session:
        session.execute(SOURCE)
        # session.execute("RUN")
        session.execute("GOSUB 60")


if __name__ == "__main__":
    main()

The output I get:

Sub
 42

What I expected was just the "Sub" without the 42 from line 30. And extra confusing is that "Main" is not printed. So why is the RETURN apparently jumping to line 20 instead of just return to the ”prompt” like it is done if the program and the GOSUB is entered manually?

@robhagemans
Copy link
Owner

Thanks, that's odd indeed. Your expectation is correct.
It does the wrong thing on the prompt as well so the problem is not specific to calling from the Session interface, it's a problem with the program pointer in the GOSUB or RETURN implementation itself in version 2.x. Version 1.x did the right thing here.

@robhagemans
Copy link
Owner

It looks like the runmode flag gets set on executing the GOSUB in direct mode, which means the RETURN jumps back to the program stream rather than the direct line. The weird return location arises because it records its stream position in the direct line on the jump (after a tokenised :GOSUB 60), then jumps to that position in the programme stream, which ends up on the second line of the program in this case.

@robhagemans
Copy link
Owner

The issue is that Interpreter.jump() sets Interpreter.run_mode, so the original runmode needs to be stored before executing the jump. I have a fix.

robhagemans added a commit that referenced this issue Jun 22, 2022
robhagemans added a commit that referenced this issue Jun 22, 2022
@robhagemans
Copy link
Owner

Fixed by commit 52e24aa. Thanks for reporting!

@robhagemans robhagemans self-assigned this Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants