1111
1212using System ;
1313using System . Collections . Generic ;
14- using System . IO ;
1514using System . Linq ;
16- using System . Net ;
1715using Shrinker . Parser . SyntaxNodes ;
1816
1917namespace Shrinker . Parser . Optimizations
@@ -42,6 +40,7 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
4240 var userDefinedNames = rootNode . FindUserDefinedNames ( ) ;
4341 var defineMap = new Dictionary < string , string [ ] >
4442 {
43+ { "float" , new [ ] { "F" , "f" , "_f" } } ,
4544 { "abs" , new [ ] { "A" } } ,
4645 { "acos" , new [ ] { "AC" } } ,
4746 { "acosh" , new [ ] { "ACH" } } ,
@@ -63,7 +62,7 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
6362 { "distance" , new [ ] { "DST" } } ,
6463 { "dot" , new [ ] { "D" } } ,
6564 { "faceforward" , new [ ] { "FF" } } ,
66- { "floor" , new [ ] { "F " } } ,
65+ { "floor" , new [ ] { "FL " } } ,
6766 { "fract" , new [ ] { "FC" } } ,
6867 { "fwidth" , new [ ] { "FW" } } ,
6968 { "greaterThan" , new [ ] { "GT" } } ,
@@ -122,13 +121,12 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
122121
123122 var keywordNodes =
124123 rootNode . TheTree
125- . OfType < GlslFunctionCallSyntaxNode > ( )
126- . Where ( o => defineMap . ContainsKey ( o . Name ) )
124+ . Where ( o => o is IRenamable or VariableDeclarationSyntaxNode && defineMap . ContainsKey ( GetSupportedBuiltinName ( o ) ) )
127125 . ToList ( ) ;
128126 foreach ( var keyword in defineMap . Keys )
129127 {
130128 // How many occurrences are in the code?
131- var nodes = keywordNodes . Where ( o => o . Name == keyword ) . ToList ( ) ;
129+ var nodes = keywordNodes . Where ( o => GetSupportedBuiltinName ( o ) == keyword ) . ToList ( ) ;
132130 if ( nodes . Count <= 1 )
133131 continue ; // Not worth it.
134132
@@ -144,7 +142,13 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
144142 continue ; // Could replace with #define, but not worth it.
145143
146144 // We'll get a space saving - Replace the nodes...
147- nodes . ForEach ( o => o . Rename ( replacement ) ) ;
145+ foreach ( var node in nodes )
146+ {
147+ if ( node is IRenamable r )
148+ r . Rename ( replacement ) ;
149+ else
150+ ( ( VariableDeclarationSyntaxNode ) node ) . RenameType ( replacement ) ;
151+ }
148152
149153 // ...and add a #define.
150154 var existingDefine = rootNode . Children . OfType < PragmaDefineSyntaxNode > ( ) . FirstOrDefault ( ) ;
@@ -153,6 +157,21 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
153157 }
154158 }
155159
160+ private static string GetSupportedBuiltinName ( SyntaxNode node )
161+ {
162+ if ( node is IRenamable n )
163+ return n . Name ;
164+ if ( node is VariableDeclarationSyntaxNode decl )
165+ {
166+ var name = decl . VariableType . Content ;
167+ if ( name . StartsWith ( "const" ) )
168+ name = name . Substring ( 5 ) . TrimStart ( ) ;
169+ return name ;
170+ }
171+
172+ return null ;
173+ }
174+
156175 private static string NameWithoutDotSuffix ( string name ) => name . Split ( '.' ) . First ( ) ;
157176
158177 private static Dictionary < string , string > BuildGolfRenameMap ( SyntaxNode rootNode )
0 commit comments