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 implementation of switch to ANTLR and reference AST #492

Merged
merged 6 commits into from
Dec 20, 2023

Conversation

jakelishman
Copy link
Contributor

Summary

This adds a simple implementation in both ANTLR and the Python reference AST (and parser) of the switch statement proposed by gh-463.

Details and comments

This is an implementation of, as I understand things, gh-463 with the addition of "Option 2" from Lev's comment: #463 (comment).

Notably, this only allows one case per block with multiple values distinguished by commas, no colon after the values, and the braces are required. I'm happy to update this PR if any of those fairly minor syntactic items change or I misunderstood the intent.

This adds a simple implementation in both ANTLR and the Python reference
AST (and parser) of the `switch` statement propsed by openqasmgh-463.
Copy link
Contributor

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

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

I had a fairly thorough look through this and didn't find anything that looked wrong or to nitpick over.

@Swordcat
Copy link
Contributor

Looks good, We at Atlantic Quantum are looking forward to using this capability in our compiler soon.

I was a bit puzzled over not visiting the CompoundStatement directly when printing out the SwitchStatement until I realized that a printer node vistor for CompoundStatement hasn't been implemented yet. I took the liberty to implement one [gh-497] 😄

Copy link
Contributor

@braised-babbage braised-babbage left a comment

Choose a reason for hiding this comment

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

Overall this looks good to me. Some changes requested re: the AST.

"""

target: Expression
cases: Dict[Tuple[int, ...], CompoundStatement]
Copy link
Contributor

@braised-babbage braised-babbage Dec 4, 2023

Choose a reason for hiding this comment

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

I think we need to allow for actual OpenQASM AST as the keys, rather than just Python int tuples. Unfortunately the Python AST objects are not hashable, so this will need to be a list of pairs.

We won't allow generic expressions in a switch case, but we probably will want to support something more than just integer literals. At the very least, it would be reasonable to allow const expressions (this is akin to C style switches using #define constants, and this is the only way that nontrivial switch statements will be readable). I think this is what is suggested by "integer constant" in the proposal #463. Since a constant is basically anything built from literals or references to const declarations through arithmetic or const externs, my suggestion is that we just allow an arbitrary tuple[Expression, ...] and then it is up to later stages in a compiler or interpreter to check that the switch cases are valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd read "integer constant" to mean specifically "integer literal", mostly because there weren't any examples in the text of using constant identifiers, but I think you're right - "constant expression" is the same terminology the C99 spec uses, and 1 + 2 is a valid label in C. It might be worth adding some examples of constant expressions to the spec text to make it clear as well.

A list of pairs is completely fine for the AST, imo. I made it a dict originally only because it was a minor convenience without sacrifice in Python 3.7+, since dict retains insertion order, so it's a proxy for a list. It's no trouble to make it only a list, and I don't think that anything operating on the AST will really care.

Modified in ceac60f. As a side effect, we largely have to lose the duplicate-case detection at this point in AST generation, since we can only reliably do that after constant-propagation, which this generator doesn't do.

Copy link
Contributor

Choose a reason for hiding this comment

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

Re: const expressions, this came up at the last TSC meeting and the consensus was that they should get supported. The version of the spec update that got merged mentions it explicitly ("The integer-constant-list-expression is a sequence of one or more integer const expressions separated by comma literals") but I agree that it's worth an example, so I've opened this #499

source/openqasm/openqasm3/parser.py Show resolved Hide resolved
@jakelishman
Copy link
Contributor Author

Thanks for the reviews, guys. Erik - your suggestions all sounded good and sensible to me, and hopefully match what you were after now.

Copy link
Contributor

@braised-babbage braised-babbage left a comment

Choose a reason for hiding this comment

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

This looks good to me.

@blakejohnson blakejohnson merged commit bd67f35 into openqasm:main Dec 20, 2023
9 checks passed
@jakelishman jakelishman deleted the switch-antlr branch December 20, 2023 14:29
@jakelishman
Copy link
Contributor Author

@braised-babbage: with this now merged, we probably want to cut a new release of the Python package, but I don't want to get in the way of anything you guys have got going on with it. I'm happy if you want to do it whenever is convenient for you, or if it won't cause problems, I can do it.

@braised-babbage
Copy link
Contributor

@braised-babbage: with this now merged, we probably want to cut a new release of the Python package, but I don't want to get in the way of anything you guys have got going on with it. I'm happy if you want to do it whenever is convenient for you, or if it won't cause problems, I can do it.

I think it should be fine to make a new release. I will let you have the honor.

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

5 participants