Qiskit / openqasm3_parser

Parser and semantic analyzer for the OpenQASM3 language
Apache License 2.0
11 stars 12 forks source link

Implement `extern` #130

Open jlapeyre opened 8 months ago

jlapeyre commented 8 months ago

For example this fails to parse without errors, although it is valid OQ3.

extern f(int x);

Implementing extern requires several steps, as do most or all language features. However there is a model that you can follow closely at every step.

AST

It will be just about the same as the entry for Def, but of course, the block will be absent.

These methods will be called from syntax_to_semantics.rs to translate the AST to ASG.

substituting extern for def. And https://github.com/Qiskit/openqasm3_parser/blob/ea3ab9c002eb314241f3c7ee7af1727eca18b135/crates/oq3_parser/src/grammar/items.rs#L339-L357

ASG

Follow this model: https://github.com/Qiskit/openqasm3_parser/blob/ea3ab9c002eb314241f3c7ee7af1727eca18b135/crates/oq3_semantics/src/asg.rs#L180

https://github.com/Qiskit/openqasm3_parser/blob/ea3ab9c002eb314241f3c7ee7af1727eca18b135/crates/oq3_semantics/src/asg.rs#L622-L628

AST -> ASG

Follow this model: https://github.com/Qiskit/openqasm3_parser/blob/ea3ab9c002eb314241f3c7ee7af1727eca18b135/crates/oq3_semantics/src/syntax_to_semantics.rs#L368-L390

Done

And it's as easy as that! Now enjoy parsing extern statements !

RasmitDevkota commented 6 months ago

I would like to work on this issue as a start for contributing to #119!

RasmitDevkota commented 6 months ago

I see in openqasm3_parser/crates/oq3_parser/src/grammar/items.rs that there are kinds defined in openqasm3_parser/crates/oq3_parser/src/syntax_kind/syntax_kind_enum.rs, but I don't see one specifically for EXTERN, only one for EXTERN_KW. I assume this is needed, so does this need to be defined manually or was it supposed to be generated at some point? Also, although it wasn't mentioned in the tasks, would it be correct to add Extern to each of the casting-related functions for Stmt and AnyHasName?