@@ -115,11 +115,6 @@ pub enum ConstantValue {
115115#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
116116pub enum ConstExpression {
117117 Value ( ConstantValue ) ,
118- Binary {
119- left : Box < Self > ,
120- operation : HighLevelOperation ,
121- right : Box < Self > ,
122- } ,
123118 MathExpr ( MathExpr , Vec < Self > ) ,
124119}
125120
@@ -140,11 +135,7 @@ impl TryFrom<Expression> for ConstExpression {
140135 Expression :: Binary { left, operation, right } => {
141136 let left_expr = Self :: try_from ( * left) ?;
142137 let right_expr = Self :: try_from ( * right) ?;
143- Ok ( Self :: Binary {
144- left : Box :: new ( left_expr) ,
145- operation,
146- right : Box :: new ( right_expr) ,
147- } )
138+ Ok ( Self :: MathExpr ( MathExpr :: Binary ( operation) , vec ! [ left_expr, right_expr] ) )
148139 }
149140 Expression :: MathExpr ( math_expr, args) => {
150141 let mut const_args = Vec :: new ( ) ;
@@ -185,9 +176,6 @@ impl ConstExpression {
185176 {
186177 match self {
187178 Self :: Value ( value) => func ( value) ,
188- Self :: Binary { left, operation, right } => {
189- Some ( operation. eval ( left. eval_with ( func) ?, right. eval_with ( func) ?) )
190- }
191179 Self :: MathExpr ( math_expr, args) => {
192180 let mut eval_args = Vec :: new ( ) ;
193181 for arg in args {
@@ -270,6 +258,7 @@ pub enum Expression {
270258/// For arbitrary compile-time computations
271259#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
272260pub enum MathExpr {
261+ Binary ( HighLevelOperation ) ,
273262 Log2Ceil ,
274263 NextMultipleOf ,
275264 SaturatingSub ,
@@ -278,6 +267,7 @@ pub enum MathExpr {
278267impl Display for MathExpr {
279268 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
280269 match self {
270+ Self :: Binary ( op) => write ! ( f, "{op}" ) ,
281271 Self :: Log2Ceil => write ! ( f, "log2_ceil" ) ,
282272 Self :: NextMultipleOf => write ! ( f, "next_multiple_of" ) ,
283273 Self :: SaturatingSub => write ! ( f, "saturating_sub" ) ,
@@ -288,13 +278,18 @@ impl Display for MathExpr {
288278impl MathExpr {
289279 pub fn num_args ( & self ) -> usize {
290280 match self {
281+ Self :: Binary ( _) => 2 ,
291282 Self :: Log2Ceil => 1 ,
292283 Self :: NextMultipleOf => 2 ,
293284 Self :: SaturatingSub => 2 ,
294285 }
295286 }
296287 pub fn eval ( & self , args : & [ F ] ) -> F {
297288 match self {
289+ Self :: Binary ( op) => {
290+ assert_eq ! ( args. len( ) , 2 ) ;
291+ op. eval ( args[ 0 ] , args[ 1 ] )
292+ }
298293 Self :: Log2Ceil => {
299294 assert_eq ! ( args. len( ) , 1 ) ;
300295 let value = args[ 0 ] ;
@@ -722,9 +717,6 @@ impl Display for ConstExpression {
722717 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
723718 match self {
724719 Self :: Value ( value) => write ! ( f, "{value}" ) ,
725- Self :: Binary { left, operation, right } => {
726- write ! ( f, "({left} {operation} {right})" )
727- }
728720 Self :: MathExpr ( math_expr, args) => {
729721 let args_str = args. iter ( ) . map ( |arg| format ! ( "{arg}" ) ) . collect :: < Vec < _ > > ( ) . join ( ", " ) ;
730722 write ! ( f, "{math_expr}({args_str})" )
0 commit comments