@@ -899,13 +899,36 @@ impl Format for ast::Expr {
899899 ast:: Expr :: XcrementOp { order : ast:: XcrementOpOrder :: Pre , op, var } => out. fmt ( ( op, var) ) ,
900900 ast:: Expr :: XcrementOp { order : ast:: XcrementOpOrder :: Post , op, var } => out. fmt ( ( var, op) ) ,
901901 ast:: Expr :: EnumConst { enum_name, ident } => out. fmt ( ( enum_name, "." , ident) ) ,
902- ast:: Expr :: LitInt { value : 0 , radix : ast:: IntRadix :: Bool } => out. fmt ( "false" ) ,
903- ast:: Expr :: LitInt { value : 1 , radix : ast:: IntRadix :: Bool } => out. fmt ( "true" ) ,
904- ast:: Expr :: LitInt { value, radix : ast:: IntRadix :: Bool } => out. fmt ( value) ,
905- ast:: Expr :: LitInt { value, radix : ast:: IntRadix :: Dec } => out. fmt ( value) ,
906- ast:: Expr :: LitInt { value, radix : ast:: IntRadix :: Hex } => out. fmt ( format_args ! ( "{:#x}" , value) ) ,
907- ast:: Expr :: LitInt { value, radix : ast:: IntRadix :: SignedHex } => out. fmt ( format_args ! ( "{:#x}" , SignedRadix ( * value) ) ) ,
908- ast:: Expr :: LitInt { value, radix : ast:: IntRadix :: Bin } => out. fmt ( format_args ! ( "{:#b}" , value) ) ,
902+ ast:: Expr :: LitInt { value, format } => match format {
903+ // These are the decimal formats
904+ & ast:: IntFormat :: SIGNED => out. fmt ( value) ,
905+ & ast:: IntFormat :: UNSIGNED => {
906+ out. fmt ( format_args ! ( "{}" , * value as u32 ) )
907+ } ,
908+
909+ & ast:: IntFormat { radix : ast:: IntRadix :: Hex , signed } => {
910+ match signed {
911+ false => out. fmt ( format_args ! ( "{:#x}" , value) ) ,
912+ true => out. fmt ( format_args ! ( "{:#x}" , SignedRadix ( * value) ) ) ,
913+ }
914+ } ,
915+
916+ & ast:: IntFormat { radix : ast:: IntRadix :: Bin , signed } => {
917+ match signed {
918+ false => out. fmt ( format_args ! ( "{:#b}" , value) ) ,
919+ true => out. fmt ( format_args ! ( "{:#b}" , SignedRadix ( * value) ) ) ,
920+ }
921+ } ,
922+
923+ & ast:: IntFormat { radix : ast:: IntRadix :: Bool , signed } => {
924+ match ( value, signed) {
925+ ( 0 , _) => out. fmt ( "false" ) ,
926+ ( 1 , _) => out. fmt ( "true" ) ,
927+ ( _, true ) => out. fmt ( value) ,
928+ ( _, false ) => out. fmt ( format_args ! ( "{:#x}" , * value as u32 ) ) ,
929+ }
930+ } ,
931+ } ,
909932 ast:: Expr :: LitFloat { value } => out. fmt ( value) ,
910933 ast:: Expr :: LitString ( x) => out. fmt ( x) ,
911934 ast:: Expr :: LabelProperty { label, keyword } => out. fmt ( ( keyword, "(" , label, ")" ) ) ,
@@ -1136,4 +1159,32 @@ mod tests {
11361159 assert ! ( reformat:: <ast:: ScriptFile >( 3 , r#"meta { x: 25 }"# ) . ends_with( "\n " ) ) ;
11371160 assert ! ( reformat:: <ast:: ScriptFile >( 9999 , r#" script lol { nop(); }"# ) . ends_with( "\n " ) ) ;
11381161 }
1162+
1163+ #[ test]
1164+ fn integer_formats ( ) {
1165+ use ast:: IntRadix as R ;
1166+
1167+ fn fmt_int ( value : i32 , signed : bool , radix : R ) -> String {
1168+ stringify ( & ast:: Expr :: LitInt {
1169+ value,
1170+ format : ast:: IntFormat { signed, radix }
1171+ } )
1172+ }
1173+
1174+ assert_eq ! ( fmt_int( 20 , true , R :: Dec ) , "20" ) ;
1175+ assert_eq ! ( fmt_int( -20 , true , R :: Dec ) , "-20" ) ;
1176+ assert_eq ! ( fmt_int( -0x30 , true , R :: Hex ) , "-0x30" ) ;
1177+ assert_eq ! ( fmt_int( -0x30 , false , R :: Hex ) , "0xffffffd0" ) ;
1178+ assert_eq ! ( fmt_int( -0b100 , true , R :: Bin ) , "-0b100" ) ;
1179+
1180+ assert_eq ! ( fmt_int( 0 , true , R :: Bool ) , "false" ) ;
1181+ assert_eq ! ( fmt_int( 0 , false , R :: Bool ) , "false" ) ;
1182+ assert_eq ! ( fmt_int( 1 , true , R :: Bool ) , "true" ) ;
1183+ assert_eq ! ( fmt_int( 1 , false , R :: Bool ) , "true" ) ;
1184+ assert_eq ! ( fmt_int( 2 , true , R :: Bool ) , "2" ) ;
1185+ assert_eq ! ( fmt_int( 2 , false , R :: Bool ) , "0x2" ) ;
1186+ assert_eq ! ( fmt_int( -2 , true , R :: Bool ) , "-2" ) ;
1187+ assert_eq ! ( fmt_int( -2 , false , R :: Bool ) , "0xfffffffe" ) ;
1188+ }
11391189}
1190+
0 commit comments