forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecimal.bal
71 lines (62 loc) · 1.99 KB
/
decimal.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Implementation specific to basic type decimal.
public type DecimalSubtype readonly & record {|
boolean allowed;
decimal[] values;
|};
public function decimalConst(decimal value) returns ComplexSemType {
DecimalSubtype st = { allowed: true, values: [value] };
return basicSubtype(BT_DECIMAL, st);
}
function decimalSubtypeSingleValue(SubtypeData d) returns decimal? {
if d is boolean {
return ();
}
DecimalSubtype s = <DecimalSubtype>d;
if !s.allowed {
return ();
}
decimal[] values = s.values;
if values.length() != 1 {
return ();
}
return values[0];
}
function decimalSubtypeContains(SubtypeData d, decimal f) returns boolean {
if d is boolean {
return d;
}
DecimalSubtype v = <DecimalSubtype>d;
foreach decimal val in v.values {
if val == f {
return v.allowed;
}
}
return !v.allowed;
}
function decimalSubtypeUnion(SubtypeData d1, SubtypeData d2) returns SubtypeData {
decimal[] values = [];
boolean allowed = enumerableSubtypeUnion(<DecimalSubtype>d1, <DecimalSubtype>d2, values);
return createDecimalSubtype(allowed, values);
}
function decimalSubtypeIntersect(SubtypeData d1, SubtypeData d2) returns SubtypeData {
decimal[] values = [];
boolean allowed = enumerableSubtypeIntersect(<DecimalSubtype>d1, <DecimalSubtype>d2, values);
return createDecimalSubtype(allowed, values);
}
function decimalSubtypeComplement(SubtypeData d) returns SubtypeData {
DecimalSubtype s = <DecimalSubtype>d;
return createDecimalSubtype(!s.allowed, s.values);
}
function createDecimalSubtype(boolean allowed, decimal[] values) returns SubtypeData {
if values.length() == 0 {
return !allowed;
}
DecimalSubtype res = { allowed, values: values.cloneReadOnly() };
return res;
}
final BasicTypeOps decimalOps = {
union: decimalSubtypeUnion,
intersect: decimalSubtypeIntersect,
complement: decimalSubtypeComplement,
isEmpty: notIsEmpty
};