1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using OneScript . Exceptions ;
5+ using OneScript . Types ;
46using OneScript . Values ;
57using ScriptEngine . Machine ;
68
@@ -88,6 +90,8 @@ public TypeDescriptionBuilder AddQualifier(IValue qualifier, int nParam = 0)
8890 public TypeDescription Build ( )
8991 {
9092 _types = new List < BslTypeValue > ( _types . Distinct ( ) ) ;
93+ _types . RemoveAll ( type => type . TypeValue . ImplementingClass == typeof ( BslUndefinedValue ) ) ;
94+ _types . Sort ( new TypeComparer ( ) ) ;
9195 var hasNumber = _types . Contains ( TypeDescription . TypeNumber ( ) ) ;
9296 var hasString = _types . Contains ( TypeDescription . TypeString ( ) ) ;
9397 var hasDate = _types . Contains ( TypeDescription . TypeDate ( ) ) ;
@@ -112,5 +116,55 @@ public static TypeDescription Build(BslTypeValue type, IValue qualifier = null)
112116 var builder = new TypeDescriptionBuilder ( ) ;
113117 return builder . AddTypes ( new [ ] { type } ) . AddQualifier ( qualifier ) . Build ( ) ;
114118 }
119+
120+ private class TypeComparer : IComparer < BslTypeValue >
121+ {
122+ private static readonly IDictionary < TypeDescriptor , int > primitives = new Dictionary < TypeDescriptor , int > ( ) ;
123+ public int Compare ( BslTypeValue x , BslTypeValue y )
124+ {
125+ if ( x . TypeValue . Equals ( y ) ) return 0 ;
126+
127+ var primitiveX = PrimitiveIndex ( x ) ;
128+ var primitiveY = PrimitiveIndex ( y ) ;
129+
130+ if ( primitiveX != - 1 )
131+ {
132+ if ( primitiveY != - 1 )
133+ return primitiveX - primitiveY ;
134+
135+ return - 1 ;
136+ }
137+
138+ if ( primitiveY != - 1 )
139+ return 1 ;
140+
141+ return x . TypeValue . Id . CompareTo ( y . TypeValue . Id ) ;
142+ }
143+
144+ private int PrimitiveIndex ( BslTypeValue type )
145+ {
146+ if ( StringComparer . CurrentCultureIgnoreCase . Equals ( type . TypeValue . Name , TYPE_BINARYDATA_NAME ) )
147+ {
148+ // Пора двоичным данным стать примитивом
149+ return 1 ;
150+ }
151+
152+ if ( primitives . ContainsKey ( type . TypeValue ) )
153+ return primitives [ type . TypeValue ] ;
154+
155+ return - 1 ;
156+ }
157+
158+ static TypeComparer ( )
159+ {
160+ primitives . Add ( BasicTypes . Boolean , 0 ) ;
161+ primitives . Add ( BasicTypes . String , 2 ) ;
162+ primitives . Add ( BasicTypes . Date , 3 ) ;
163+ primitives . Add ( BasicTypes . Null , 4 ) ;
164+ primitives . Add ( BasicTypes . Number , 5 ) ;
165+ primitives . Add ( BasicTypes . Type , 6 ) ;
166+ }
167+
168+ }
115169 }
116170}
0 commit comments