forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.bal
72 lines (63 loc) · 2.05 KB
/
float.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
72
// Implementation specific to basic type float.
public type FloatSubtype readonly & record {|
boolean allowed;
float[] values;
|};
public function floatConst(float value) returns ComplexSemType {
FloatSubtype st = { allowed: true, values: [value] };
return basicSubtype(BT_FLOAT, st);
}
function floatSubtypeSingleValue(SubtypeData d) returns float? {
if d is boolean {
return ();
}
FloatSubtype f = <FloatSubtype>d;
if !f.allowed {
return ();
}
float[] values = f.values;
if values.length() != 1 {
return ();
}
return values[0];
}
// XXX should this be generified and moved to enumerable?
function floatSubtypeContains(SubtypeData d, float f) returns boolean {
if d is boolean {
return d;
}
FloatSubtype v = <FloatSubtype>d;
int? index = v.values.indexOf(f);
if index != () {
return v.allowed;
}
return !v.allowed;
}
function floatSubtypeUnion(ProperSubtypeData d1, ProperSubtypeData d2) returns SubtypeData {
float[] values = [];
boolean allowed = enumerableSubtypeUnion(<FloatSubtype>d1, <FloatSubtype>d2, values);
return createFloatSubtype(allowed, values);
}
function floatSubtypeIntersect(ProperSubtypeData d1, ProperSubtypeData d2) returns SubtypeData {
float[] values = [];
boolean allowed = enumerableSubtypeIntersect(<FloatSubtype>d1, <FloatSubtype>d2, values);
return createFloatSubtype(allowed, values);
}
function floatSubtypeComplement(ProperSubtypeData d) returns SubtypeData {
FloatSubtype s = <FloatSubtype>d;
return createFloatSubtype(!s.allowed, s.values);
}
function createFloatSubtype(boolean allowed, float[] values) returns SubtypeData {
if values.length() == 0 {
return !allowed;
}
FloatSubtype res = { allowed, values: values.cloneReadOnly() };
return res;
}
final BasicTypeOps floatOps = {
union: floatSubtypeUnion,
intersect: floatSubtypeIntersect,
complement: floatSubtypeComplement,
// Empty float sets don't use subtype representation.
isEmpty: notIsEmpty
};