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

Add anonymous scope blocks #487

Merged
merged 5 commits into from
Nov 8, 2023

Conversation

braised-babbage
Copy link
Contributor

@braised-babbage braised-babbage commented Nov 7, 2023

Summary

This generalizes the existing block scoping to allow for anonymous blocks in the OpenQASM grammar. In the reference AST, these correspond to CompoundStatement objects when they are not already associated with a list of statements (e.g. in the bodies of loops)

Details and comments

OpenQASM supports a limited form of block scoping, where new blocks may be introduced in the specific context of control flow statements like for or if, or box statements. The restriction of block scoping to these particular statements is unnecessary, in my opinion. It does not save implementation effort, since the scoping can still be arbitrarily nested (e.g. through nested loops). It is also a bit of a nuisance, in that it limits the expressiveness of the language. Scope can be used to express programmer intent. Anonymous scope blocks are also convenient for code generators or processors which may need to safely introduce names at some program point, since shadowing provides a legible mechanism for handling collisions (in an earlier discussion, I have cited function inlining as one example of such a transformation).

This PR updates the grammar so that a scope block may be used wherever a statement is expected. In the reference AST, some of these scope blocks get translated to a new CompoundStatement.

The current implementation is backwards compatible. Here I have chosen to minimize breakage of existing code at the expense of uniformity of the AST classes. For example, the block of a ForInLoop still has type List[Statement]. The new CompoundStatement is only constructed for blocks that are not associated with a box or control flow statement.

Backwards compatibility keeps the changes here as narrow as possible. If there is general buy-in for the extensions here, but people prefer to break backwards compatibility (using CompoundStatement more generally in the AST) we can do that either in this or in a separate PR.

@braised-babbage braised-babbage marked this pull request as ready for review November 7, 2023 22:27
@braised-babbage braised-babbage merged commit f558486 into openqasm:main Nov 8, 2023
9 checks passed
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

Successfully merging this pull request may close these issues.

None yet

3 participants