@@ -594,10 +594,7 @@ pub enum Instruction {
594594 UnaryOperation {
595595 op : Arg < UnaryOperator > ,
596596 } ,
597- BinaryOperation {
598- op : Arg < BinaryOperator > ,
599- } ,
600- BinaryOperationInplace {
597+ BinaryOp {
601598 op : Arg < BinaryOperator > ,
602599 } ,
603600 BinarySubscript ,
@@ -1096,30 +1093,136 @@ op_arg_enum!(
10961093 /// The possible Binary operators
10971094 /// # Examples
10981095 ///
1099- /// ```ignore
1100- /// use rustpython_compiler_core::Instruction::BinaryOperation ;
1101- /// use rustpython_compiler_core::BinaryOperator::Add ;
1102- /// let op = BinaryOperation {op: Add};
1096+ /// ```rust
1097+ /// use rustpython_compiler_core::Instruction::BinaryOp ;
1098+ /// use rustpython_compiler_core::BinaryOperator::BinaryOperator ;
1099+ /// let op = BinaryOp {op: BinaryOperator:: Add};
11031100 /// ```
1104- #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
1101+ ///
1102+ /// See also:
1103+ /// - [_PyEval_BinaryOps](https://github.com/python/cpython/blob/8183fa5e3f78ca6ab862de7fb8b14f3d929421e0/Python/ceval.c#L316-L343)
11051104 #[ repr( u8 ) ]
1105+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
11061106 pub enum BinaryOperator {
1107- Power = 0 ,
1108- Multiply = 1 ,
1109- MatrixMultiply = 2 ,
1110- Divide = 3 ,
1111- FloorDivide = 4 ,
1112- Modulo = 5 ,
1113- Add = 6 ,
1114- Subtract = 7 ,
1115- Lshift = 8 ,
1107+ /// `+`
1108+ Add = 0 ,
1109+ /// `&`
1110+ And = 1 ,
1111+ /// `//`
1112+ FloorDivide = 2 ,
1113+ /// `<<`
1114+ Lshift = 3 ,
1115+ /// `@`
1116+ MatrixMultiply = 4 ,
1117+ /// `*`
1118+ Multiply = 5 ,
1119+ /// `%`
1120+ Remainder = 6 ,
1121+ /// `|`
1122+ Or = 7 ,
1123+ /// `**`
1124+ Power = 8 ,
1125+ /// `>>`
11161126 Rshift = 9 ,
1117- And = 10 ,
1118- Xor = 11 ,
1119- Or = 12 ,
1127+ /// `-`
1128+ Subtract = 10 ,
1129+ /// `/`
1130+ TrueDivide = 11 ,
1131+ /// `^`
1132+ Xor = 12 ,
1133+ /// `+=`
1134+ InplaceAdd = 13 ,
1135+ /// `&=`
1136+ InplaceAnd = 14 ,
1137+ /// `//=`
1138+ InplaceFloorDivide = 15 ,
1139+ /// `<<=`
1140+ InplaceLshift = 16 ,
1141+ /// `@=`
1142+ InplaceMatrixMultiply = 17 ,
1143+ /// `*=`
1144+ InplaceMultiply = 18 ,
1145+ /// `%=`
1146+ InplaceRemainder = 19 ,
1147+ /// `|=`
1148+ InplaceOr = 20 ,
1149+ /// `**=`
1150+ InplacePower = 21 ,
1151+ /// `>>=`
1152+ InplaceRshift = 22 ,
1153+ /// `-=`
1154+ InplaceSubtract = 23 ,
1155+ /// `/=`
1156+ InplaceTrueDivide = 24 ,
1157+ /// `^=`
1158+ InplaceXor = 25 ,
11201159 }
11211160) ;
11221161
1162+ impl BinaryOperator {
1163+ /// Get the "inplace" version of the operator.
1164+ /// This has no effect if `self` is already an "inplace" operator.
1165+ ///
1166+ /// # Example
1167+ /// ```rust
1168+ /// assert_eq!(BinaryOperator::Power.as_inplace(), BinaryOperator::InplacePower);
1169+ ///
1170+ /// assert_eq!(BinaryOperator::InplaceSubtract.as_inplace(), BinaryOperator::InplaceSubtract);
1171+ /// ```
1172+ #[ must_use]
1173+ pub const fn as_inplace ( self ) -> Self {
1174+ match self {
1175+ Self :: Add => Self :: InplaceAdd ,
1176+ Self :: FloorDivide => Self :: InplaceFloorDivide ,
1177+ Self :: Lshift => Self :: InplaceLshift ,
1178+ Self :: MatrixMultiply => Self :: InplaceMatrixMultiply ,
1179+ Self :: Multiply => Self :: InplaceMultiply ,
1180+ Self :: Remainder => Self :: InplaceRemainder ,
1181+ Self :: Or => Self :: InplaceOr ,
1182+ Self :: Power => Self :: InplacePower ,
1183+ Self :: Rshift => Self :: InplaceRshift ,
1184+ Self :: Subtract => Self :: InplaceSubtract ,
1185+ Self :: TrueDivide => Self :: InplaceTrueDivide ,
1186+ Self :: Xor => Self :: InplaceXor ,
1187+ _ => self ,
1188+ }
1189+ }
1190+ }
1191+
1192+ impl fmt:: Display for BinaryOperator {
1193+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1194+ let op = match self {
1195+ Self :: Add => "+" ,
1196+ Self :: And => "&" ,
1197+ Self :: FloorDivide => "//" ,
1198+ Self :: Lshift => "<<" ,
1199+ Self :: MatrixMultiply => "@" ,
1200+ Self :: Multiply => "*" ,
1201+ Self :: Remainder => "%" ,
1202+ Self :: Or => "|" ,
1203+ Self :: Power => "**" ,
1204+ Self :: Rshift => ">>" ,
1205+ Self :: Subtract => "-" ,
1206+ Self :: TrueDivide => "/" ,
1207+ Self :: Xor => "^" ,
1208+ Self :: InplaceAdd => "+=" ,
1209+ Self :: InplaceAnd => "&=" ,
1210+ Self :: InplaceFloorDivide => "//=" ,
1211+ Self :: InplaceLshift => "<<=" ,
1212+ Self :: InplaceMatrixMultiply => "@=" ,
1213+ Self :: InplaceMultiply => "*=" ,
1214+ Self :: InplaceRemainder => "%=" ,
1215+ Self :: InplaceOr => "|=" ,
1216+ Self :: InplacePower => "**=" ,
1217+ Self :: InplaceRshift => ">>=" ,
1218+ Self :: InplaceSubtract => "-=" ,
1219+ Self :: InplaceTrueDivide => "/=" ,
1220+ Self :: InplaceXor => "^=" ,
1221+ } ;
1222+ write ! ( f, "{op}" )
1223+ }
1224+ }
1225+
11231226op_arg_enum ! (
11241227 /// The possible unary operators
11251228 #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
@@ -1514,7 +1617,7 @@ impl Instruction {
15141617 DeleteAttr { .. } => -1 ,
15151618 LoadConst { .. } => 1 ,
15161619 UnaryOperation { .. } => 0 ,
1517- BinaryOperation { .. } | BinaryOperationInplace { .. } | CompareOperation { .. } => -1 ,
1620+ BinaryOp { .. } | CompareOperation { .. } => -1 ,
15181621 BinarySubscript => -1 ,
15191622 CopyItem { .. } => 1 ,
15201623 Pop => -1 ,
@@ -1719,8 +1822,7 @@ impl Instruction {
17191822 DeleteAttr { idx } => w ! ( DeleteAttr , name = idx) ,
17201823 LoadConst { idx } => fmt_const ( "LoadConst" , arg, f, idx) ,
17211824 UnaryOperation { op } => w ! ( UnaryOperation , ?op) ,
1722- BinaryOperation { op } => w ! ( BinaryOperation , ?op) ,
1723- BinaryOperationInplace { op } => w ! ( BinaryOperationInplace , ?op) ,
1825+ BinaryOp { op } => write ! ( f, "BINARY_OP {}" , op. get( arg) ) ,
17241826 BinarySubscript => w ! ( BinarySubscript ) ,
17251827 LoadAttr { idx } => w ! ( LoadAttr , name = idx) ,
17261828 CompareOperation { op } => w ! ( CompareOperation , ?op) ,
0 commit comments