-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTTEnum.java
More file actions
142 lines (117 loc) · 5.17 KB
/
Copy pathASTTEnum.java
File metadata and controls
142 lines (117 loc) · 5.17 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ASTTEnum implements ASTType {
private final List<Map.Entry<String, List<ASTType>>> options;
public ASTTEnum(List<Map.Entry<String, List<ASTType>>> options) {
this.options = options;
}
public List<Map.Entry<String, List<ASTType>>> getOptions() {
return options;
}
@Override
public String toStr() {
StringBuilder sb = new StringBuilder("enum { ");
for (int i = 0; i < options.size(); i++) {
Map.Entry<String, List<ASTType>> entry = options.get(i);
sb.append(entry.getKey()).append(": (");
List<ASTType> types = entry.getValue();
for (int j = 0; j < types.size(); j++) {
sb.append(types.get(j).toStr());
if (j < types.size() - 1) {
sb.append(", ");
}
}
sb.append(")");
if (i < options.size() - 1) {
sb.append(", ");
}
}
sb.append(" }");
return sb.toString();
}
@Override
public boolean eqt(ASTType t) {
if (!(t instanceof ASTTEnum other))
return false;
if (this.options.size() != other.options.size())
return false;
for (Map.Entry<String, List<ASTType>> thisEntry : this.options) {
String tag = thisEntry.getKey();
List<ASTType> thisTypes = thisEntry.getValue();
List<ASTType> otherTypes = other.options.stream()
.filter(entry -> entry.getKey().equals(tag))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
if (otherTypes == null) return false;
if (thisTypes.size() != otherTypes.size()) return false;
for (int j = 0; j < thisTypes.size(); j++) {
if (!thisTypes.get(j).eqt(otherTypes.get(j)))
return false;
}
}
return true;
}
@Override
public boolean subtypeOf(ASTType t, Environment<ASTType> e, Set<String> seen) throws InterpreterError {
t = ASTTypeDef.unfold(t, e);
if (!(t instanceof ASTTEnum other)) return false;
for (Map.Entry<String, List<ASTType>> thisEntry : this.options) {
String tag = thisEntry.getKey();
List<ASTType> thisFields = thisEntry.getValue();
List<ASTType> otherFields = other.options.stream()
.filter(entry -> entry.getKey().equals(tag))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
if (otherFields == null) return false;
// Width check
if (thisFields.size() < otherFields.size()) return false;
// Depth check
for (int i = 0; i < otherFields.size(); i++) {
if (!thisFields.get(i).subtypeOf(otherFields.get(i), e, seen))
return false;
}
}
return true;
}
@Override
public ASTType computeLUB(ASTType other, Environment<ASTType> e) throws TypeCheckError, InterpreterError {
ASTType resolvedOther = ASTTypeDef.unfold(other, e);
if (resolvedOther instanceof ASTTEnum e2) {
List<Map.Entry<String, List<ASTType>>> lubOptions = new ArrayList<>();
for (Map.Entry<String, List<ASTType>> entry : this.options) {
lubOptions.add(new java.util.AbstractMap.SimpleEntry<>(entry.getKey(), new ArrayList<>(entry.getValue())));
}
for (Map.Entry<String, List<ASTType>> entry2 : e2.options) {
String tag2 = entry2.getKey();
List<ASTType> fields2 = entry2.getValue();
boolean isCommon = false;
for (Map.Entry<String, List<ASTType>> lubEntry : lubOptions) {
if (lubEntry.getKey().equals(tag2)) {
isCommon = true;
List<ASTType> fields1 = lubEntry.getValue();
List<ASTType> superFields = fields1.size() <= fields2.size() ? fields1 : fields2;
List<ASTType> subFields = fields1.size() <= fields2.size() ? fields2 : fields1;
for (int i = 0; i < superFields.size(); i++) {
ASTType f1 = superFields.get(i);
ASTType f2 = subFields.get(i);
if (!f1.subtypeOf(f2, e, new java.util.HashSet<>()) ||
!f2.subtypeOf(f1, e, new java.util.HashSet<>()))
throw new TypeCheckError("computeLUB: incompatible field types for tag " + tag2);
}
lubEntry.setValue(new ArrayList<>(superFields));
break;
}
}
if (!isCommon) {
lubOptions.add(new java.util.AbstractMap.SimpleEntry<>(tag2, new ArrayList<>(fields2)));
}
}
return new ASTTEnum(lubOptions);
}
return ASTType.super.computeLUB(other, e);
}
}