-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathType.h
More file actions
187 lines (137 loc) · 4.08 KB
/
Type.h
File metadata and controls
187 lines (137 loc) · 4.08 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma once
#include <ostream>
#include <string>
#include <vector>
struct Type {
virtual bool equals(Type* type) = 0;
virtual bool superset(Type* type) = 0;
virtual std::string toString() = 0;
std::ostream& operator<<(std::ostream& os) {
return (os << toString());
}
};
struct : Type {
bool equals(Type* type) override {
return this == type;
}
bool superset(Type* type) override {
return true;
}
virtual std::string toString() override {
return "any";
}
} Any;
struct FunctionType : Type {
std::vector<Type*> args;
Type* returnType;
FunctionType(std::vector<Type*> args, Type* returnType)
: args(args), returnType(returnType) {}
bool equals(Type* type) override {
if (FunctionType* functionType = dynamic_cast<FunctionType*>(type)) {
if (args.size() != functionType->args.size()) return false;
for (int i = 0; i < args.size(); ++i)
if (!(args[i]->equals(functionType->args[i]))) return false;
return returnType->equals(functionType->returnType);
}
return false;
}
bool superset(Type* type) override {
return equals(type);
}
virtual std::string toString() override {
std::string result = "(";
for (auto arg : args)
result += arg->toString() + ", ";
if (args.size() > 0) result.erase(result.size() - 2);
return result += ") -> " + returnType->toString();
}
};
struct UnionType : Type {
std::vector<Type*> types;
UnionType(std::vector<Type*> types) : types(types) {}
bool equals(Type* type) override {
if (UnionType* unionType = dynamic_cast<UnionType*>(type))
return superset(unionType) && unionType->superset(this);
return false;
}
bool superset(Type* type) override {
UnionType* unionType = dynamic_cast<UnionType*>(type);
if (!unionType) {
for (Type* t : types)
if (t->superset(type)) return true;
return false;
}
for (Type* t : unionType->types)
if (!superset(t)) return false;
return true;
}
virtual std::string toString() override {
if (types.empty()) return "";
std::string result = types[0]->toString();
for (int i = 1; i < types.size(); ++i)
result += " | " + types[i]->toString();
return result;
}
};
struct ArrayType : Type {
Type* elementsType;
ArrayType(Type* elementsType) : elementsType(elementsType) {}
bool equals(Type* type) override {
if (ArrayType* arrayType = dynamic_cast<ArrayType*>(type))
return elementsType->equals(arrayType->elementsType);
return false;
}
bool superset(Type* type) override {
if (ArrayType* arrayType = dynamic_cast<ArrayType*>(type))
return elementsType->equals(arrayType->elementsType);
return false;
}
virtual std::string toString() override {
if (UnionType* unionType = dynamic_cast<UnionType*>(elementsType))
return "(" + elementsType->toString() + ")[]";
return elementsType->toString() + "[]";
}
};
struct Primitive : Type {
std::string name;
Primitive(std::string name) : name(name) {}
bool equals(Type* type) override {
return this == type;
}
bool superset(Type* type) override {
return this == type;
}
virtual std::string toString() override {
return name;
}
};
inline void addType(std::vector<Type*>& types, Type* type) {
if (UnionType* u = dynamic_cast<UnionType*>(type)) {
for (auto t : u->types)
addType(types, t);
return;
}
for (auto it = types.begin(); it != types.end();) {
if ((*it)->superset(type)) return;
else if (type->superset(*it))
it = types.erase(it);
else
++it;
}
types.push_back(type);
}
inline Type* mergeTypes(const std::vector<Type*>& types) {
std::vector<Type*> collapsed = {};
for (auto type : types)
addType(collapsed, type);
if (collapsed.size() == 1) return collapsed[0];
return new UnionType(collapsed);
}
Type* ERROR_T = nullptr;
Type* ANY_T = &Any;
Type* INT_T = new Primitive("int");
Type* FLOAT_T = new Primitive("float");
Type* STRING_T = new Primitive("string");
Type* CHAR_T = new Primitive("char");
Type* BOOL_T = new Primitive("bool");
Type* NULL_T = new Primitive("null");