Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
orif
, orbox
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 aForInLoop
still has typeList[Statement]
. The newCompoundStatement
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.