-
Notifications
You must be signed in to change notification settings - Fork 170
Implement goto
in LPython
#1163
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
Conversation
|
||
|
||
def _make_code(code, codestring): | ||
return code.replace(co_code=codestring) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of manually calling types.Code(*args)
as in the original source code I am using .replace
method and passing the updated code. This makes it work with Python > 3.8 robustly since we don't depend on the internal order of arguments to PyCode_New
.
@certik This is ready and works for CPython, LLVM and C backend. The file src/runtime/ltypes/goto.py is taken from snoack/python-goto. I have the following questions,
Please let me know. Other than the above questions |
Is "UNLICENSE" equivalent to "PUBLIC DOMAIN?" If we have any other "derivative works" in our sources, how are we giving credit to the authors of the originals? |
Correct as far as I can tell.
I don't think we have any AFAICT. @certik Can you please confirm. |
I would say we do not need the original project to pull any of our changes because we will never support earlier versions of Python. Is there any residual benefit to them? What's our version floor? 3.7, 3.9, ... ? |
Looks like we're 3.9 and greater for LPython. |
@@ -173,10 +173,10 @@ stmt | |||
-- GoTo points to a GoToTarget with the corresponding target_id within | |||
-- the same procedure. We currently use `int` IDs to link GoTo with | |||
-- GoToTarget to avoid issues with serialization. | |||
| GoTo(int target_id) | |||
| GoTo(int target_id, identifier name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think GoTo should just reference using an ID. The name should go into GoToTarget.
| GoTo(int target_id, identifier name) | |
| GoTo(int target_id) |
Or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well in C backend we need the name to produce, label name_of_the_label
or goto name_of_the_label
. So we need the name to be stored in both GoTo and GoToTarget.
if( !ASR::is_a<ASR::stmt_t>(*tmp) ) { | ||
ASRUtils::EXPR(tmp); | ||
tmp = nullptr; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of these lines, both the original and the new ones?
Are we checking that the result is an expression? If so, you can use an assert for ASR::is_a
(or perhaps down_cast
, since is_a
might require a particular class).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the scene is that goto.end
is represented as [(Expr (Attribute (Name goto Load) end Load))]
at AST level. So, now when we will complete visiting Attribute
, tmp
will either point to a ASR expression or a ASR statement (because GoTo
in ASR is a statement). So in case we get something like, goto.label
then tmp
will point to a statement in which case we should exit the function naturally and the transform_stmts
before this node will include that GoTo statement. However if tmp
is not pointing to a statement then I replaced ASRUtils::EXPR(tmp)
with an LFORTRAN_ASSERT
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so, you can use an assert for
ASR::is_a
(or perhapsdown_cast
, sinceis_a
might require a particular class).
Assert with ASR::is_a
works fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please document this as a comment? Next person to see that code will have the same question, including me tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good enough to merge as is. But there are issues to resolve. One is the ASR relationship of GoTo and GoToTarget. It looks like what is needed is a direct link (in C++) from goto to the target, so that one can read off the name if needed easily.
It seems the ASR.asdl structure does not easily allow a way to do it. Storing both the number and a string is redundant.
Regarding where to maintain the CPython implementation, yes, we should maintain it ourselves and ensure it actually works in all modern Python versions. |
The source code of src/runtime/ltypes/goto.py is coming from https://github.com/snoack/python-goto/blob/acbe736221d2238df3d09beab457d0bb19d05812/goto.py. The license of the python-goto project at the time of inclusion in LPython is available at https://github.com/snoack/python-goto/blob/acbe736221d2238df3d09beab457d0bb19d05812/LICENSE.
Co-authored-by: Ron Nuriel <[email protected]>
Co-authored-by: Ron Nuriel <[email protected]>
There are two ways in my mind to deal with this,
|
I like this idea, so I opened up a dedicated issue for this: #1167 |
No description provided.