eo
eo copied to clipboard
Memo: implementation of break and exceptions in python with goto in EO
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 what is the question here? Is it a bug in EO?
@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 @yegor256 I believe we can close it