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

Make control flow stmts use Block instead of List<Stmt> for their bodies. #2226

Open
wants to merge 1 commit into
base: feature/qasm3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions compiler/qsc_qasm3/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ pub enum StmtKind {
Barrier(BarrierStmt),
Box(BoxStmt),
Break(BreakStmt),
Block(Box<Block>),
Block(Block),
Cal(CalibrationStmt),
CalibrationGrammar(CalibrationGrammarStmt),
ClassicalDecl(ClassicalDeclarationStmt),
Expand Down Expand Up @@ -419,16 +419,16 @@ impl Display for DefCalStmt {
pub struct IfStmt {
pub span: Span,
pub condition: Expr,
pub if_block: List<Stmt>,
pub else_block: Option<List<Stmt>>,
pub if_block: Block,
pub else_block: Option<Block>,
}

impl Display for IfStmt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln_header(f, "IfStmt", self.span)?;
writeln_field(f, "condition", &self.condition)?;
writeln_list_field(f, "if_block", &self.if_block)?;
write_opt_list_field(f, "else_block", self.else_block.as_ref())
writeln_field(f, "if_block", &self.if_block)?;
write_opt_field(f, "else_block", self.else_block.as_ref())
}
}

Expand Down Expand Up @@ -1323,8 +1323,8 @@ pub struct DefStmt {
pub span: Span,
pub name: Box<Ident>,
pub params: List<TypedParameter>,
pub body: Box<Block>,
pub return_type: Option<ScalarType>,
pub body: Block,
pub return_type: Option<Box<ScalarType>>,
}

impl Display for DefStmt {
Expand Down Expand Up @@ -1354,14 +1354,14 @@ impl Display for ReturnStmt {
pub struct WhileLoop {
pub span: Span,
pub while_condition: Expr,
pub block: List<Stmt>,
pub block: Block,
}

impl Display for WhileLoop {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln_header(f, "WhileLoop", self.span)?;
writeln_field(f, "condition", &self.while_condition)?;
write_list_field(f, "block", &self.block)
write_field(f, "block", &self.block)
}
}

Expand All @@ -1371,7 +1371,7 @@ pub struct ForStmt {
pub ty: ScalarType,
pub identifier: Identifier,
pub set_declaration: Box<EnumerableSet>,
pub block: List<Stmt>,
pub block: Block,
}

impl Display for ForStmt {
Expand All @@ -1380,7 +1380,7 @@ impl Display for ForStmt {
writeln_field(f, "variable_type", &self.ty)?;
writeln_field(f, "variable_name", &self.identifier)?;
writeln_field(f, "iterable", &self.set_declaration)?;
write_list_field(f, "block", &self.block)
write_field(f, "block", &self.block)
}
}

Expand Down
43 changes: 24 additions & 19 deletions compiler/qsc_qasm3/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,22 @@ fn disambiguate_ident(s: &mut ParserContext, indexed_ident: IndexedIdent) -> Res
#[allow(clippy::vec_box)]
pub(super) fn parse_many(s: &mut ParserContext) -> Result<Vec<Box<Stmt>>> {
many(s, |s| {
recovering(s, default, &[TokenKind::Semicolon], parse_block_or_stmt)
recovering(s, default, &[TokenKind::Semicolon], |s| {
parse_block_or_stmt(s).map(Box::new)
})
})
}

/// Grammar: `LBRACE statementOrScope* RBRACE`.
pub(super) fn parse_block(s: &mut ParserContext) -> Result<Box<Block>> {
pub(super) fn parse_block(s: &mut ParserContext) -> Result<Block> {
let lo = s.peek().span.lo;
token(s, TokenKind::Open(Delim::Brace))?;
let stmts = barrier(s, &[TokenKind::Close(Delim::Brace)], parse_many)?;
recovering_token(s, TokenKind::Close(Delim::Brace));
Ok(Box::new(Block {
Ok(Block {
span: s.span(lo),
stmts: stmts.into_boxed_slice(),
}))
})
}

#[allow(clippy::unnecessary_box_returns)]
Expand Down Expand Up @@ -454,7 +456,7 @@ fn parse_def(s: &mut ParserContext) -> Result<StmtKind> {
token(s, TokenKind::Open(Delim::Paren))?;
let (exprs, _) = seq(s, arg_def)?;
token(s, TokenKind::Close(Delim::Paren))?;
let return_type = opt(s, return_sig)?;
let return_type = opt(s, return_sig)?.map(Box::new);
let body = parse_block(s)?;
let kind = StmtKind::Def(DefStmt {
span: s.span(lo),
Expand Down Expand Up @@ -605,7 +607,7 @@ fn parse_gatedef(s: &mut ParserContext) -> Result<StmtKind> {
let ident = Box::new(prim::ident(s)?);
let params = opt(s, gate_params)?.unwrap_or_else(Vec::new);
let (qubits, _) = seq(s, prim::ident)?;
let body = parse_block(s)?;
let body = Box::new(parse_block(s)?);
Ok(StmtKind::QuantumGateDefinition(QuantumGateDefinition {
span: s.span(lo),
ident,
Expand Down Expand Up @@ -1122,7 +1124,7 @@ fn case_stmt(s: &mut ParserContext) -> Result<SwitchCase> {
s.push_error(Error::new(ErrorKind::MissingSwitchCaseLabels(s.span(lo))));
}

let block = parse_block(s).map(|block| *block)?;
let block = parse_block(s)?;

Ok(SwitchCase {
span: s.span(lo),
Expand All @@ -1134,27 +1136,30 @@ fn case_stmt(s: &mut ParserContext) -> Result<SwitchCase> {
/// Grammar: `DEFAULT scope`.
fn default_case_stmt(s: &mut ParserContext) -> Result<Block> {
token(s, TokenKind::Keyword(Keyword::Default))?;
parse_block(s).map(|block| *block)
parse_block(s)
}

/// Grammar: `statement | scope`.
fn parse_block_or_stmt(s: &mut ParserContext) -> Result<Box<Stmt>> {
fn parse_block_or_stmt(s: &mut ParserContext) -> Result<Stmt> {
if let Some(block) = opt(s, parse_block)? {
Ok(Box::new(Stmt {
Ok(Stmt {
span: block.span,
annotations: Default::default(),
kind: Box::new(StmtKind::Block(block)),
}))
})
} else {
Ok(parse(s)?)
Ok(*parse(s)?)
}
}

fn into_stmt_list(stmt: Stmt) -> List<Stmt> {
fn into_block(stmt: Stmt) -> Block {
if let StmtKind::Block(block) = *stmt.kind {
block.stmts
block
} else {
Box::new([Box::new(stmt)])
Block {
span: stmt.span,
stmts: Box::new([Box::new(stmt)]),
}
}
}

Expand All @@ -1167,9 +1172,9 @@ pub fn parse_if_stmt(s: &mut ParserContext) -> Result<IfStmt> {
let condition = expr::expr(s)?;
recovering_token(s, TokenKind::Close(Delim::Paren));

let if_block = into_stmt_list(*parse_block_or_stmt(s)?);
let if_block = into_block(parse_block_or_stmt(s)?);
let else_block = if opt(s, |s| token(s, TokenKind::Keyword(Keyword::Else)))?.is_some() {
Some(into_stmt_list(*parse_block_or_stmt(s)?))
Some(into_block(parse_block_or_stmt(s)?))
} else {
None
};
Expand Down Expand Up @@ -1236,7 +1241,7 @@ pub fn parse_for_loop(s: &mut ParserContext) -> Result<ForStmt> {
let identifier = Identifier::Ident(Box::new(prim::ident(s)?));
token(s, TokenKind::Keyword(Keyword::In))?;
let set_declaration = Box::new(for_loop_iterable_expr(s)?);
let block = into_stmt_list(*parse_block_or_stmt(s)?);
let block = into_block(parse_block_or_stmt(s)?);

Ok(ForStmt {
span: s.span(lo),
Expand All @@ -1255,7 +1260,7 @@ pub fn parse_while_loop(s: &mut ParserContext) -> Result<WhileLoop> {
token(s, TokenKind::Open(Delim::Paren))?;
let while_condition = expr::expr(s)?;
recovering_token(s, TokenKind::Close(Delim::Paren));
let block = into_stmt_list(*parse_block_or_stmt(s)?);
let block = into_block(parse_block_or_stmt(s)?);

Ok(WhileLoop {
span: s.span(lo),
Expand Down
16 changes: 8 additions & 8 deletions compiler/qsc_qasm3/src/parser/stmt/tests/for_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn simple_for_loop() {
Expr [19-20]: Lit: Int(1)
Expr [22-23]: Lit: Int(2)
Expr [25-26]: Lit: Int(3)
block:
block: Block [28-50]:
Stmt [38-44]:
annotations: <empty>
kind: AssignStmt [38-44]:
Expand All @@ -49,7 +49,7 @@ fn empty_for_loop() {
variable_name: Ident [8-9] "x"
iterable: DiscreteSet [13-15]:
values: <empty>
block: <empty>"#]],
block: Block [16-18]: <empty>"#]],
);
}

Expand All @@ -73,7 +73,7 @@ fn simple_for_loop_stmt_body() {
Expr [19-20]: Lit: Int(1)
Expr [22-23]: Lit: Int(2)
Expr [25-26]: Lit: Int(3)
block:
block: Block [36-42]:
Stmt [36-42]:
annotations: <empty>
kind: AssignStmt [36-42]:
Expand Down Expand Up @@ -103,7 +103,7 @@ fn for_loop_range() {
start: Expr [19-20]: Lit: Int(0)
step: Expr [21-22]: Lit: Int(2)
end: Expr [23-24]: Lit: Int(7)
block:
block: Block [26-48]:
Stmt [36-42]:
annotations: <empty>
kind: AssignStmt [36-42]:
Expand Down Expand Up @@ -133,7 +133,7 @@ fn for_loop_range_no_step() {
start: Expr [19-20]: Lit: Int(0)
step: <none>
end: Expr [21-22]: Lit: Int(7)
block:
block: Block [24-46]:
Stmt [34-40]:
annotations: <empty>
kind: AssignStmt [34-40]:
Expand All @@ -160,7 +160,7 @@ fn for_loop_expr() {
size: <none>
variable_name: Ident [13-14] "x"
iterable: Expr [18-20]: Ident [18-20] "xs"
block:
block: Block [21-43]:
Stmt [31-37]:
annotations: <empty>
kind: AssignStmt [31-37]:
Expand Down Expand Up @@ -192,7 +192,7 @@ fn for_loop_with_continue_stmt() {
Expr [19-20]: Lit: Int(1)
Expr [22-23]: Lit: Int(2)
Expr [25-26]: Lit: Int(3)
block:
block: Block [28-68]:
Stmt [38-44]:
annotations: <empty>
kind: AssignStmt [38-44]:
Expand Down Expand Up @@ -227,7 +227,7 @@ fn for_loop_with_break_stmt() {
Expr [19-20]: Lit: Int(1)
Expr [22-23]: Lit: Int(2)
Expr [25-26]: Lit: Int(3)
block:
block: Block [28-65]:
Stmt [38-44]:
annotations: <empty>
kind: AssignStmt [38-44]:
Expand Down
18 changes: 9 additions & 9 deletions compiler/qsc_qasm3/src/parser/stmt/tests/if_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ fn simple_if_stmt() {
op: Eq
lhs: Expr [9-10]: Ident [9-10] "x"
rhs: Expr [14-15]: Ident [14-15] "y"
if_block:
if_block: Block [17-39]:
Stmt [27-33]:
annotations: <empty>
kind: AssignStmt [27-33]:
lhs: IndexedIdent [27-28]:
name: Ident [27-28] "a"
indices: <empty>
rhs: Expr [31-32]: Lit: Int(0)
else_block:
else_block: Block [45-67]:
Stmt [55-61]:
annotations: <empty>
kind: AssignStmt [55-61]:
Expand Down Expand Up @@ -59,7 +59,7 @@ fn if_stmt_missing_else() {
op: Eq
lhs: Expr [9-10]: Ident [9-10] "x"
rhs: Expr [14-15]: Ident [14-15] "y"
if_block:
if_block: Block [17-39]:
Stmt [27-33]:
annotations: <empty>
kind: AssignStmt [27-33]:
Expand Down Expand Up @@ -98,47 +98,47 @@ fn nested_if_stmts() {
op: Eq
lhs: Expr [9-10]: Ident [9-10] "x"
rhs: Expr [14-15]: Ident [14-15] "y"
if_block:
if_block: Block [17-113]:
Stmt [27-107]:
annotations: <empty>
kind: IfStmt [27-107]:
condition: Expr [31-39]: BinaryOpExpr:
op: Eq
lhs: Expr [31-33]: Ident [31-33] "x1"
rhs: Expr [37-39]: Ident [37-39] "y1"
if_block:
if_block: Block [41-71]:
Stmt [55-61]:
annotations: <empty>
kind: AssignStmt [55-61]:
lhs: IndexedIdent [55-56]:
name: Ident [55-56] "a"
indices: <empty>
rhs: Expr [59-60]: Lit: Int(0)
else_block:
else_block: Block [77-107]:
Stmt [91-97]:
annotations: <empty>
kind: AssignStmt [91-97]:
lhs: IndexedIdent [91-92]:
name: Ident [91-92] "a"
indices: <empty>
rhs: Expr [95-96]: Lit: Int(1)
else_block:
else_block: Block [119-215]:
Stmt [129-209]:
annotations: <empty>
kind: IfStmt [129-209]:
condition: Expr [133-141]: BinaryOpExpr:
op: Eq
lhs: Expr [133-135]: Ident [133-135] "x2"
rhs: Expr [139-141]: Ident [139-141] "y2"
if_block:
if_block: Block [143-173]:
Stmt [157-163]:
annotations: <empty>
kind: AssignStmt [157-163]:
lhs: IndexedIdent [157-158]:
name: Ident [157-158] "a"
indices: <empty>
rhs: Expr [161-162]: Lit: Int(2)
else_block:
else_block: Block [179-209]:
Stmt [193-199]:
annotations: <empty>
kind: AssignStmt [193-199]:
Expand Down
22 changes: 11 additions & 11 deletions compiler/qsc_qasm3/src/parser/stmt/tests/invalid_stmts/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn assignment_in_if_condition() {
annotations: <empty>
kind: IfStmt [0-17]:
condition: Expr [4-5]: Ident [4-5] "x"
if_block:
if_block: Block [11-17]:
Stmt [13-15]:
annotations: <empty>
kind: ExprStmt [13-15]:
Expand Down Expand Up @@ -94,7 +94,7 @@ fn binary_op_assignment_in_if_condition() {
annotations: <empty>
kind: IfStmt [0-18]:
condition: Expr [4-5]: Ident [4-5] "x"
if_block:
if_block: Block [12-18]:
Stmt [14-16]:
annotations: <empty>
kind: ExprStmt [14-16]:
Expand Down Expand Up @@ -126,15 +126,15 @@ fn empty_if_block() {
parse,
"if (true);",
&expect![[r#"
Stmt [0-10]:
annotations: <empty>
kind: IfStmt [0-10]:
condition: Expr [4-8]: Lit: Bool(true)
if_block:
Stmt [9-10]:
annotations: <empty>
kind: Empty
else_block: <none>"#]],
Stmt [0-10]:
annotations: <empty>
kind: IfStmt [0-10]:
condition: Expr [4-8]: Lit: Bool(true)
if_block: Block [9-10]:
Stmt [9-10]:
annotations: <empty>
kind: Empty
else_block: <none>"#]],
);
}

Expand Down
Loading
Loading