-
Notifications
You must be signed in to change notification settings - Fork 3
/
type.h
179 lines (136 loc) · 3.73 KB
/
type.h
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
#ifndef __TAB_TYPE_H
#define __TAB_TYPE_H
namespace tab {
struct Type {
enum types_t {
ATOM,
TUP,
ARR,
MAP,
SEQ,
NONE
};
enum atom_types_t {
INT,
UINT,
REAL,
STRING
};
types_t type;
atom_types_t atom;
std::shared_ptr< std::vector<Type> > tuple;
std::shared_ptr< Atom > literal;
Type(types_t t = NONE) : type(t), atom(INT) {}
Type(atom_types_t a) : type(ATOM), atom(a) {}
Type(const Atom& a) : type(ATOM) {
switch (a.which) {
case INT: atom = INT; break;
case UINT: atom = UINT; break;
case REAL: atom = REAL; break;
case STRING: atom = STRING; break;
}
}
Type(types_t t, const std::initializer_list<Type>& tup) :
type(t),
tuple(std::make_shared< std::vector<Type> >(tup)) {}
bool operator!=(const Type& t) const {
if (t.type != type) return true;
if (type == ATOM) {
if (atom != t.atom)
return true;
return false;
}
if (tuple && t.tuple) {
if (tuple->size() != t.tuple->size())
return true;
for (size_t i = 0; i < tuple->size(); ++i) {
if ((*tuple)[i] != (*t.tuple)[i])
return true;
}
return false;
} else if (!tuple && !t.tuple) {
return false;
} else {
return true;
}
}
bool operator==(const Type& t) const {
return !(t != *this);
}
static std::string print(const Type& t) {
std::string ret;
switch (t.type) {
case NONE: ret += "None"; break;
case ATOM:
switch (t.atom) {
case INT: ret += "Int"; break;
case UINT: ret += "UInt"; break;
case REAL: ret += "Real"; break;
case STRING: ret += "String"; break;
}
break;
case TUP: ret += "("; break;
case ARR: ret += "Arr["; break;
case MAP: ret += "Map["; break;
case SEQ: ret += "Seq["; break;
}
if (t.tuple) {
bool first = true;
for (const Type& tt : *(t.tuple)) {
if (first) {
first = false;
} else {
ret += ", ";
}
ret += print(tt);
}
switch (t.type) {
case TUP:
ret += ")"; break;
case ARR:
case MAP:
case SEQ:
ret += "]"; break;
default:
break;
}
}
return ret;
}
Type& push(const Type& t) {
if (!tuple) {
tuple = std::make_shared< std::vector<Type> >();
}
tuple->push_back(t);
return tuple->back();
}
void pop_front() {
if (tuple && tuple->size() > 1) {
tuple = std::make_shared< std::vector<Type> >(tuple->begin()+1, tuple->end());
}
}
};
} // namespace tab
namespace std {
template <>
struct hash<tab::Type> {
size_t operator()(const tab::Type& t) const {
size_t r = hash<size_t>()(t.type);
if (t.type == tab::Type::ATOM) {
r += hash<size_t>()(t.atom);
} else if (t.tuple) {
for (auto i : (*t.tuple)) {
r += (*this)(i);
}
}
return r;
}
};
template <>
struct hash< std::pair<tab::String, tab::Type> > {
size_t operator()(const std::pair<tab::String, tab::Type>& x) const {
return hash<size_t>()(x.first.ix) + hash<tab::Type>()(x.second);
}
};
} // namespace std
#endif