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

Memo: implementation of break and exceptions in python with goto in EO #618

Closed
dours opened this issue Apr 21, 2022 · 3 comments
Closed

Comments

@dours
Copy link
Contributor

dours commented Apr 21, 2022

Consider this python code

while True: break

We can translate it this way

goto
  [doBreak]
    TRUE.while
      doBreak.forward 0

Now consider this:

flag = 5
while True:
  try:
    break
  finally: flag = 10

From this section we know that "When a return, break or continue statement is executed in the try suite of a try…finally statement, the finally clause is also executed ‘on the way out.’".
So, our implementation of try by means of goto must catch both exceptions and the break. It then must execute finally and rethrow everything that was not processed by the except clause (including break). The break then will be caught again up the stack by the implementation of while.
Like so:

flag.write 5
goto
  [stackUp]
    TRUE.while
      write.
        resultOfTry
        goto
          [stackUp]
            stackUp.forward breakConstant // this is the break
      if.
        is-exception resultOfTry
        // here goes the implementation of except clause, which is BTW empty in our example
        0
      flag.write 10 // here goes the implementation of finally, so it is executed for exceptions and for the break
      if.
        is-break resultOfTry
        stackUp.forward resultOfTry  // redirect the break further up the stack
        0

We may also imagine a return that happens somewhere deep in the hierarchy of whiles and trys. This is a kind of semi-manual stack unwinding, IMHO.

@yegor256
Copy link
Member

@dours what is the question here? Is it a bug in EO?

@dours
Copy link
Contributor Author

dours commented Aug 24, 2022

@yegor256 It is not a question. It is an answer to you suggestion: you asked to describe how break and exception are implemented with the help of goto in py2eo as an issue.

@Graur
Copy link
Contributor

Graur commented Nov 14, 2022

@dours @yegor256 I believe we can close it

@Graur Graur closed this as completed Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants