Skip to content

Commit f1e6290

Browse files
authored
Merge pull request #1353 from EvilBeaver/feature/issue-1327-adjustvalue
Приведение значения составного типа для 2.0
2 parents 5a92499 + 3d2a15c commit f1e6290

File tree

4 files changed

+544
-193
lines changed

4 files changed

+544
-193
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
using System;
8+
using System.Collections.Generic;
9+
using OneScript.Types;
10+
using OneScript.Values;
11+
12+
namespace OneScript.StandardLibrary.TypeDescriptions
13+
{
14+
internal class TypeComparer : IComparer<BslTypeValue>
15+
{
16+
private const string TYPE_BINARYDATA_NAME = "ДвоичныеДанные";
17+
private static readonly IDictionary<TypeDescriptor, int> primitives = new Dictionary<TypeDescriptor, int>();
18+
19+
public int Compare(BslTypeValue x, BslTypeValue y)
20+
{
21+
if (x.TypeValue.Equals(y)) return 0;
22+
23+
var primitiveX = PrimitiveIndex(x);
24+
var primitiveY = PrimitiveIndex(y);
25+
26+
if (primitiveX != -1)
27+
{
28+
if (primitiveY != -1)
29+
return primitiveX - primitiveY;
30+
31+
return -1;
32+
}
33+
34+
if (primitiveY != -1)
35+
return 1;
36+
37+
return x.TypeValue.Id.CompareTo(y.TypeValue.Id);
38+
}
39+
40+
private int PrimitiveIndex(BslTypeValue type)
41+
{
42+
if (StringComparer.CurrentCultureIgnoreCase.Equals(type.TypeValue.Name, TYPE_BINARYDATA_NAME))
43+
{
44+
// Пора двоичным данным стать примитивом
45+
return 1;
46+
}
47+
48+
if (primitives.TryGetValue(type.TypeValue, out var index))
49+
return index;
50+
51+
return -1;
52+
}
53+
54+
static TypeComparer()
55+
{
56+
primitives.Add(BasicTypes.Boolean, 0);
57+
primitives.Add(BasicTypes.String, 2);
58+
primitives.Add(BasicTypes.Date, 3);
59+
primitives.Add(BasicTypes.Null, 4);
60+
primitives.Add(BasicTypes.Number, 5);
61+
primitives.Add(BasicTypes.Type, 6);
62+
}
63+
64+
}
65+
}
66+

0 commit comments

Comments
 (0)