@@ -583,58 +583,68 @@ fn fold_constants(
583
583
Value :: String ( _) |
584
584
Value :: Function ( _) => None ,
585
585
586
- Value :: AddInt ( left, right) => fold_binary ( block, definitions, values, functions, * left, * right, |( _, left_value) , ( _, right_value) | {
587
- match ( left_value, right_value) {
588
- ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left + right) ) ,
589
- ( Value :: Int ( left) , _) if left. is_zero ( ) => Some ( right_value. clone ( ) ) ,
590
- ( _, Value :: Int ( right) ) if right. is_zero ( ) => Some ( left_value. clone ( ) ) ,
591
- _ => None ,
592
- }
593
- } ) ,
594
-
595
- Value :: SubInt ( left, right) => fold_binary ( block, definitions, values, functions, * left, * right, |( left, left_value) , ( right, right_value) | {
596
- if left == right {
597
- Some ( Value :: Int ( BigInt :: from ( 0 ) ) )
598
- } else {
586
+ & Value :: AddInt ( left, right) => {
587
+ fold_binary ( block, definitions, values, functions, left, right, |( _, left_value) , ( _, right_value) | {
599
588
match ( left_value, right_value) {
600
- ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left - right) ) ,
589
+ ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left + right) ) ,
590
+ ( Value :: Int ( left) , _) if left. is_zero ( ) => Some ( right_value. clone ( ) ) ,
601
591
( _, Value :: Int ( right) ) if right. is_zero ( ) => Some ( left_value. clone ( ) ) ,
602
592
_ => None ,
603
593
}
604
- }
605
- } ) ,
606
-
607
- Value :: MulInt ( left, right) => fold_binary ( block, definitions, values, functions, * left, * right, |( _, left_value) , ( _, right_value) | {
608
- match ( left_value, right_value) {
609
- ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left * right) ) ,
610
- ( Value :: Int ( left) , _) if left. is_zero ( ) => Some ( Value :: Int ( BigInt :: from ( 0 ) ) ) ,
611
- ( _, Value :: Int ( right) ) if right. is_zero ( ) => Some ( Value :: Int ( BigInt :: from ( 0 ) ) ) ,
612
- ( Value :: Int ( left) , _) if left. is_one ( ) => Some ( right_value. clone ( ) ) ,
613
- ( _, Value :: Int ( right) ) if right. is_one ( ) => Some ( left_value. clone ( ) ) ,
614
- _ => None ,
615
- }
616
- } ) ,
617
-
618
- Value :: DivInt ( left, right) => fold_binary ( block, definitions, values, functions, * left, * right, |( _, left_value) , ( _, right_value) | {
619
- match ( left_value, right_value) {
620
- ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left / right) ) ,
621
- ( Value :: Int ( left) , _) if left. is_zero ( ) => Some ( Value :: Int ( BigInt :: from ( 0 ) ) ) ,
622
- ( _, Value :: Int ( right) ) if right. is_one ( ) => Some ( left_value. clone ( ) ) ,
623
- _ => None ,
624
- }
625
- } ) ,
626
-
627
- Value :: AddString ( left, right) => fold_binary ( block, definitions, values, functions, * left, * right, |( _, left_value) , ( _, right_value) | {
628
- match ( left_value, right_value) {
629
- ( Value :: String ( left) , Value :: String ( right) ) => {
630
- let mut result = String :: with_capacity ( left. len ( ) + right. len ( ) ) ;
631
- result. push_str ( & left) ;
632
- result. push_str ( & right) ;
633
- Some ( Value :: String ( Rc :: new ( result) ) )
634
- } ,
635
- _ => None ,
636
- }
637
- } ) ,
594
+ } )
595
+ } ,
596
+
597
+ & Value :: SubInt ( left, right) => {
598
+ fold_binary ( block, definitions, values, functions, left, right, |( left, left_value) , ( right, right_value) | {
599
+ if left == right {
600
+ Some ( Value :: Int ( BigInt :: from ( 0 ) ) )
601
+ } else {
602
+ match ( left_value, right_value) {
603
+ ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left - right) ) ,
604
+ ( _, Value :: Int ( right) ) if right. is_zero ( ) => Some ( left_value. clone ( ) ) ,
605
+ _ => None ,
606
+ }
607
+ }
608
+ } )
609
+ } ,
610
+
611
+ & Value :: MulInt ( left, right) => {
612
+ fold_binary ( block, definitions, values, functions, left, right, |( _, left_value) , ( _, right_value) | {
613
+ match ( left_value, right_value) {
614
+ ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left * right) ) ,
615
+ ( Value :: Int ( left) , _) if left. is_zero ( ) => Some ( Value :: Int ( BigInt :: from ( 0 ) ) ) ,
616
+ ( _, Value :: Int ( right) ) if right. is_zero ( ) => Some ( Value :: Int ( BigInt :: from ( 0 ) ) ) ,
617
+ ( Value :: Int ( left) , _) if left. is_one ( ) => Some ( right_value. clone ( ) ) ,
618
+ ( _, Value :: Int ( right) ) if right. is_one ( ) => Some ( left_value. clone ( ) ) ,
619
+ _ => None ,
620
+ }
621
+ } )
622
+ } ,
623
+
624
+ & Value :: DivInt ( left, right) => {
625
+ fold_binary ( block, definitions, values, functions, left, right, |( _, left_value) , ( _, right_value) | {
626
+ match ( left_value, right_value) {
627
+ ( Value :: Int ( left) , Value :: Int ( right) ) => Some ( Value :: Int ( left / right) ) ,
628
+ ( Value :: Int ( left) , _) if left. is_zero ( ) => Some ( Value :: Int ( BigInt :: from ( 0 ) ) ) ,
629
+ ( _, Value :: Int ( right) ) if right. is_one ( ) => Some ( left_value. clone ( ) ) ,
630
+ _ => None ,
631
+ }
632
+ } )
633
+ } ,
634
+
635
+ & Value :: AddString ( left, right) => {
636
+ fold_binary ( block, definitions, values, functions, left, right, |( _, left_value) , ( _, right_value) | {
637
+ match ( left_value, right_value) {
638
+ ( Value :: String ( left) , Value :: String ( right) ) => {
639
+ let mut result = String :: with_capacity ( left. len ( ) + right. len ( ) ) ;
640
+ result. push_str ( & left) ;
641
+ result. push_str ( & right) ;
642
+ Some ( Value :: String ( Rc :: new ( result) ) )
643
+ } ,
644
+ _ => None ,
645
+ }
646
+ } )
647
+ } ,
638
648
639
649
Value :: Call ( function, arguments) => fold_call ( * function, arguments. to_vec ( ) , values, functions) ,
640
650
0 commit comments