@@ -204,7 +204,7 @@ impl Display for MeasureExpr {
204
204
}
205
205
}
206
206
/// A binary operator.
207
- #[ derive( Clone , Debug ) ]
207
+ #[ derive( Clone , Copy , Debug ) ]
208
208
pub enum BinOp {
209
209
/// Addition: `+`.
210
210
Add ,
@@ -273,8 +273,8 @@ impl Display for BinOp {
273
273
}
274
274
275
275
/// A unary operator.
276
- #[ derive( Clone , Debug ) ]
277
- pub enum UnOp {
276
+ #[ derive( Clone , Copy , Debug ) ]
277
+ pub enum UnaryOp {
278
278
/// Negation: `-`.
279
279
Neg ,
280
280
/// Bitwise NOT: `~`.
@@ -283,12 +283,12 @@ pub enum UnOp {
283
283
NotL ,
284
284
}
285
285
286
- impl Display for UnOp {
286
+ impl Display for UnaryOp {
287
287
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
288
288
match self {
289
- UnOp :: Neg => write ! ( f, "Neg" ) ,
290
- UnOp :: NotB => write ! ( f, "NotB" ) ,
291
- UnOp :: NotL => write ! ( f, "NotL" ) ,
289
+ UnaryOp :: Neg => write ! ( f, "Neg" ) ,
290
+ UnaryOp :: NotB => write ! ( f, "NotB" ) ,
291
+ UnaryOp :: NotL => write ! ( f, "NotL" ) ,
292
292
}
293
293
}
294
294
}
@@ -637,7 +637,7 @@ impl Display for AliasStmt {
637
637
#[ derive( Clone , Debug ) ]
638
638
pub struct ExprStmt {
639
639
pub span : Span ,
640
- pub expr : Box < Expr > ,
640
+ pub expr : Expr ,
641
641
}
642
642
643
643
impl Display for ExprStmt {
@@ -667,7 +667,7 @@ impl Display for Expr {
667
667
#[ derive( Clone , Debug ) ]
668
668
pub struct DiscreteSet {
669
669
pub span : Span ,
670
- pub values : List < ExprStmt > ,
670
+ pub values : Box < [ Expr ] > ,
671
671
}
672
672
673
673
impl Display for DiscreteSet {
@@ -685,9 +685,9 @@ impl Display for DiscreteSet {
685
685
#[ derive( Clone , Debug ) ]
686
686
pub struct RangeDefinition {
687
687
pub span : Span ,
688
- pub start : Option < ExprStmt > ,
689
- pub end : Option < ExprStmt > ,
690
- pub step : Option < ExprStmt > ,
688
+ pub start : Option < Expr > ,
689
+ pub end : Option < Expr > ,
690
+ pub step : Option < Expr > ,
691
691
}
692
692
693
693
#[ derive( Clone , Debug ) ]
@@ -1584,17 +1584,20 @@ impl Display for ClassicalAssignment {
1584
1584
1585
1585
#[ derive( Clone , Debug , Default ) ]
1586
1586
pub enum ExprKind {
1587
+ Assign ( AssignExpr ) ,
1588
+ AssignOp ( AssignOpExpr ) ,
1587
1589
/// An expression with invalid syntax that can't be parsed.
1588
1590
#[ default]
1589
1591
Err ,
1590
1592
Ident ( Ident ) ,
1591
- UnaryExpr ( UnaryExpr ) ,
1592
- BinaryExpr ( BinaryExpr ) ,
1593
+ UnaryOp ( UnaryOpExpr ) ,
1594
+ BinaryOp ( BinaryOpExpr ) ,
1593
1595
Lit ( Lit ) ,
1594
1596
FunctionCall ( FunctionCall ) ,
1595
1597
Cast ( Cast ) ,
1596
1598
Concatenation ( Concatenation ) ,
1597
1599
IndexExpr ( IndexExpr ) ,
1600
+ Paren ( Expr ) ,
1598
1601
}
1599
1602
1600
1603
impl Display for ExprKind {
@@ -1603,40 +1606,68 @@ impl Display for ExprKind {
1603
1606
match self {
1604
1607
ExprKind :: Err => write ! ( f, "Err" ) ,
1605
1608
ExprKind :: Ident ( id) => write ! ( f, "{id}" ) ,
1606
- ExprKind :: UnaryExpr ( expr) => write ! ( f, "{expr}" ) ,
1607
- ExprKind :: BinaryExpr ( expr) => display_bin_op ( indent, expr) ,
1609
+ ExprKind :: UnaryOp ( expr) => write ! ( f, "{expr}" ) ,
1610
+ ExprKind :: BinaryOp ( expr) => display_bin_op ( indent, expr) ,
1608
1611
ExprKind :: Lit ( lit) => write ! ( f, "{lit}" ) ,
1609
1612
ExprKind :: FunctionCall ( call) => write ! ( f, "{call}" ) ,
1610
- ExprKind :: Cast ( cast) => write ! ( f , "{ cast}" ) ,
1613
+ ExprKind :: Cast ( cast) => display_cast ( indent , cast) ,
1611
1614
ExprKind :: Concatenation ( concat) => write ! ( f, "{concat}" ) ,
1612
1615
ExprKind :: IndexExpr ( index) => write ! ( f, "{index}" ) ,
1616
+ ExprKind :: Assign ( expr) => write ! ( f, "{expr}" ) ,
1617
+ ExprKind :: AssignOp ( expr) => write ! ( f, "{expr}" ) ,
1618
+ ExprKind :: Paren ( expr) => display_paren ( indent, expr) ,
1613
1619
}
1614
1620
}
1615
1621
}
1616
1622
1617
1623
#[ derive( Clone , Debug ) ]
1618
- pub struct UnaryExpr {
1619
- pub span : Span ,
1624
+ pub struct AssignExpr {
1625
+ pub lhs : Expr ,
1626
+ pub rhs : Expr ,
1627
+ }
1628
+
1629
+ impl Display for AssignExpr {
1630
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
1631
+ let indent = set_indentation ( indented ( f) , 0 ) ;
1632
+ display_assign ( indent, & self . lhs , & self . rhs )
1633
+ }
1634
+ }
1635
+
1636
+ #[ derive( Clone , Debug ) ]
1637
+ pub struct AssignOpExpr {
1638
+ pub op : BinOp ,
1639
+ pub lhs : Expr ,
1640
+ pub rhs : Expr ,
1641
+ }
1642
+
1643
+ impl Display for AssignOpExpr {
1644
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
1645
+ let indent = set_indentation ( indented ( f) , 0 ) ;
1646
+ display_assign_op ( indent, self . op , & self . lhs , & self . rhs )
1647
+ }
1648
+ }
1649
+
1650
+ #[ derive( Clone , Debug ) ]
1651
+ pub struct UnaryOpExpr {
1620
1652
pub op : UnaryOp ,
1621
- pub expr : Box < Expr > ,
1653
+ pub expr : Expr ,
1622
1654
}
1623
1655
1624
- impl Display for UnaryExpr {
1656
+ impl Display for UnaryOpExpr {
1625
1657
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
1626
1658
let indent = set_indentation ( indented ( f) , 0 ) ;
1627
1659
display_un_op ( indent, self . op , & self . expr )
1628
1660
}
1629
1661
}
1630
1662
1631
1663
#[ derive( Clone , Debug ) ]
1632
- pub struct BinaryExpr {
1633
- pub span : Span ,
1634
- pub op : BinaryOp ,
1635
- pub lhs : ExprStmt ,
1636
- pub rhs : ExprStmt ,
1664
+ pub struct BinaryOpExpr {
1665
+ pub op : BinOp ,
1666
+ pub lhs : Expr ,
1667
+ pub rhs : Expr ,
1637
1668
}
1638
1669
1639
- impl Display for BinaryExpr {
1670
+ impl Display for BinaryOpExpr {
1640
1671
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
1641
1672
let indent = set_indentation ( indented ( f) , 0 ) ;
1642
1673
display_bin_op ( indent, self )
@@ -1647,7 +1678,7 @@ impl Display for BinaryExpr {
1647
1678
pub struct FunctionCall {
1648
1679
pub span : Span ,
1649
1680
pub name : Identifier ,
1650
- pub args : List < ExprStmt > ,
1681
+ pub args : List < Expr > ,
1651
1682
}
1652
1683
1653
1684
impl Display for FunctionCall {
@@ -1665,8 +1696,8 @@ impl Display for FunctionCall {
1665
1696
#[ derive( Clone , Debug ) ]
1666
1697
pub struct Cast {
1667
1698
pub span : Span ,
1668
- pub r#type : ScalarType ,
1669
- pub arg : ExprStmt ,
1699
+ pub r#type : TypeDef ,
1700
+ pub arg : Expr ,
1670
1701
}
1671
1702
1672
1703
impl Display for Cast {
@@ -1678,7 +1709,7 @@ impl Display for Cast {
1678
1709
#[ derive( Clone , Debug ) ]
1679
1710
pub struct IndexExpr {
1680
1711
pub span : Span ,
1681
- pub collection : ExprStmt ,
1712
+ pub collection : Expr ,
1682
1713
pub index : IndexElement ,
1683
1714
}
1684
1715
@@ -1692,65 +1723,6 @@ impl Display for IndexExpr {
1692
1723
}
1693
1724
}
1694
1725
1695
- #[ derive( Clone , Copy , Debug ) ]
1696
- pub enum UnaryOp {
1697
- NegB ,
1698
- NegL ,
1699
- NegN ,
1700
- }
1701
-
1702
- impl Display for UnaryOp {
1703
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
1704
- match self {
1705
- UnaryOp :: NegB => write ! ( f, "NegB" ) ,
1706
- UnaryOp :: NegL => write ! ( f, "NegL" ) ,
1707
- UnaryOp :: NegN => write ! ( f, "NegN" ) ,
1708
- }
1709
- }
1710
- }
1711
-
1712
- #[ derive( Clone , Copy , Debug ) ]
1713
- pub enum BinaryOp {
1714
- /// `>`
1715
- Gt ,
1716
- /// `<`
1717
- Lt ,
1718
- /// `>=`
1719
- Gte ,
1720
- /// `<=`
1721
- Lte ,
1722
- /// `==`
1723
- Eq ,
1724
- /// `!=`
1725
- Neq ,
1726
- /// `&&`
1727
- AndL ,
1728
- /// `||`
1729
- OrL ,
1730
- /// `|`
1731
- OrB ,
1732
- /// `^`
1733
- XorB ,
1734
- /// `&`
1735
- AndB ,
1736
- /// `<<`
1737
- ShL ,
1738
- /// `>>`
1739
- ShR ,
1740
- /// `+`
1741
- Add ,
1742
- /// `-`
1743
- Sub ,
1744
- /// `*`
1745
- Mul ,
1746
- /// `/`
1747
- Div ,
1748
- /// `%`
1749
- Mod ,
1750
- /// `**`
1751
- Exp ,
1752
- }
1753
-
1754
1726
#[ derive( Clone , Debug ) ]
1755
1727
pub struct Lit {
1756
1728
pub span : Span ,
@@ -1860,10 +1832,19 @@ impl Display for IndexElement {
1860
1832
}
1861
1833
}
1862
1834
1863
- #[ derive( Clone , Debug ) ]
1835
+ #[ derive( Clone , Debug , Default ) ]
1864
1836
pub enum IndexSetItem {
1865
1837
RangeDefinition ( RangeDefinition ) ,
1866
- Expr ( ExprStmt ) ,
1838
+ Expr ( Expr ) ,
1839
+ #[ default]
1840
+ Err ,
1841
+ }
1842
+
1843
+ /// This is needed to able to use `IndexSetItem` in the `seq` combinator.
1844
+ impl WithSpan for IndexSetItem {
1845
+ fn with_span ( self , _span : Span ) -> Self {
1846
+ self
1847
+ }
1867
1848
}
1868
1849
1869
1850
impl Display for IndexSetItem {
@@ -1872,13 +1853,14 @@ impl Display for IndexSetItem {
1872
1853
match self {
1873
1854
IndexSetItem :: RangeDefinition ( range) => display_range ( indent, range) ,
1874
1855
IndexSetItem :: Expr ( expr) => write ! ( f, "IndexSetItem {expr}" ) ,
1856
+ IndexSetItem :: Err => write ! ( f, "Err" ) ,
1875
1857
}
1876
1858
}
1877
1859
}
1878
1860
1879
1861
#[ derive( Clone , Debug ) ]
1880
1862
pub enum AssignmentOp {
1881
- BinaryOp ( BinaryOp ) ,
1863
+ BinaryOp ( BinOp ) ,
1882
1864
/// `OpenQASM3` has the `~=` assignment operator.
1883
1865
/// This enum variant is meant to capture that.
1884
1866
UnaryOp ( UnaryOp ) ,
@@ -1986,7 +1968,7 @@ fn display_assign(mut indent: Indented<Formatter>, lhs: &Expr, rhs: &Expr) -> fm
1986
1968
1987
1969
fn display_assign_op (
1988
1970
mut indent : Indented < Formatter > ,
1989
- op : BinaryOp ,
1971
+ op : BinOp ,
1990
1972
lhs : & Expr ,
1991
1973
rhs : & Expr ,
1992
1974
) -> fmt:: Result {
@@ -1997,7 +1979,7 @@ fn display_assign_op(
1997
1979
Ok ( ( ) )
1998
1980
}
1999
1981
2000
- fn display_bin_op ( mut indent : Indented < Formatter > , expr : & BinaryExpr ) -> fmt:: Result {
1982
+ fn display_bin_op ( mut indent : Indented < Formatter > , expr : & BinaryOpExpr ) -> fmt:: Result {
2001
1983
write ! ( indent, "BinOp ({:?}):" , expr. op) ?;
2002
1984
indent = set_indentation ( indent, 1 ) ;
2003
1985
write ! ( indent, "\n {}" , expr. lhs) ?;
@@ -2012,6 +1994,20 @@ fn display_un_op(mut indent: Indented<Formatter>, op: UnaryOp, expr: &Expr) -> f
2012
1994
Ok ( ( ) )
2013
1995
}
2014
1996
1997
+ fn display_paren ( mut indent : Indented < Formatter > , expr : & Expr ) -> fmt:: Result {
1998
+ write ! ( indent, "Paren:" ) ?;
1999
+ indent = set_indentation ( indent, 1 ) ;
2000
+ write ! ( indent, "\n {expr}" ) ?;
2001
+ Ok ( ( ) )
2002
+ }
2003
+ fn display_cast ( mut indent : Indented < Formatter > , cast : & Cast ) -> fmt:: Result {
2004
+ let Cast { span, r#type, arg } = cast;
2005
+ write ! ( indent, "Cast {span}:" ) ?;
2006
+ indent = set_indentation ( indent, 1 ) ;
2007
+ write ! ( indent, "\n {type}\n {arg}" ) ?;
2008
+ Ok ( ( ) )
2009
+ }
2010
+
2015
2011
fn display_while ( mut indent : Indented < Formatter > , cond : & Expr , block : & Block ) -> fmt:: Result {
2016
2012
write ! ( indent, "While:" ) ?;
2017
2013
indent = set_indentation ( indent, 1 ) ;
0 commit comments