eo icon indicating copy to clipboard operation
eo copied to clipboard

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

Open dours opened this issue 2 years ago • 2 comments

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.

dours avatar Apr 21 '22 11:04 dours

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

yegor256 avatar Aug 23 '22 19:08 yegor256

@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.

dours avatar Aug 24 '22 21:08 dours

@dours @yegor256 I believe we can close it

Graur avatar Nov 14 '22 08:11 Graur