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

Implementing python's global and nonlocal #1735

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Implementing python's global and nonlocal #1735

wants to merge 7 commits into from

Conversation

oxisto
Copy link
Member

@oxisto oxisto commented Sep 26, 2024

This PR tries to implement python's global keyword and tries to replicate python variable resolution as good as possible.

I had to add a ReferenceScopeModifierStatement node, which is able to solve both global and nonlocal, but I am not 100 % happy with the solution. Another approach would be to store this scope modification in the scope itself, so this PR would need some discussion.

nonlocal cannot be tested until we actually support parsing nested functions #1736

@oxisto oxisto added the python label Sep 26, 2024
@oxisto oxisto marked this pull request as ready for review September 26, 2024 23:19
@@ -90,6 +90,9 @@ abstract class Scope(
*/
@Transient var wildcardImports: MutableSet<ImportDeclaration> = mutableSetOf()

/** A list of scope modifications. */
@Transient var scopeModifications: MutableMap<Name, Scope> = mutableMapOf()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be the alternative approach to the node. Depending on what we decide, this would be removed

This was linked to issues Sep 26, 2024
@oxisto oxisto changed the title Implementing python's global Implementing python's global and nonlocal Sep 27, 2024
Copy link
Contributor

@maximiliankaul maximiliankaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd appreciate to have two PRs:

  • one for the CPG changes (with it's own tests, so that we can see how it's supposed to work in general and not only in the specific python use-case)
  • one making use of the first PR by implementing the Python stuff

I get why you modify the scope of a Reference as this is how the Python front-end works. However, don't we need a scope modifier for the declaration (I get that this will be problematic with the Python "add declarations pass"?

@@ -329,3 +330,22 @@ fun MetadataProvider.newDefaultStatement(rawNode: Any? = null): DefaultStatement
log(node)
return node
}

/**
* Creates a new [DefaultStatement]. The [MetadataProvider] receiver will be used to fill different
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReferenceScopeModifierStatement


// There should be three variable declarations, two local and one global
var cVariables = result.variables("c")
assertEquals(3, cVariables.size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the global one. But where is the second local one? I only see the c = 3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the error one.

@oxisto
Copy link
Member Author

oxisto commented Sep 27, 2024

I'd appreciate to have two PRs:

  • one for the CPG changes (with it's own tests, so that we can see how it's supposed to work in general and not only in the specific python use-case)
  • one making use of the first PR by implementing the Python stuff

Done. #1742

I get why you modify the scope of a Reference as this is how the Python front-end works. However, don't we need a scope modifier for the declaration (I get that this will be problematic with the Python "add declarations pass"?

The declaration's scope is not modified. Only the "lookup scope" of the symbol that you are trying to resolve in the current scope is modified (e.g., from the local to the global scope).

The trick is that the add-declaration-pass will then create the variable in the lookup-scope (if it does not exist). So if we have a global c and a c = 2 in the block, there will be a global lookup and if it fails, we will create the node in the global scope. I will improve this logic in this PR a little bit after #1742 is merged in.

This PR adds a new node type `LookupScopeStatement`, which can be used to adjust the lookup scope of symbols that are resolved in the current scope. Most prominent examples are Python's `global` and `nonlocal` statements.

Support for python will be added in a later PR. This will only add the node and provides the basic functionality in the lookup.
This PR tries to implement python's `global` keyword and tries to replicate python variable resolution as good as possible.
Copy link

sonarcloud bot commented Sep 27, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle nonlocal keyword Handle global keyword
2 participants