-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathType.cpp
211 lines (175 loc) · 4.23 KB
/
Type.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "Type.h"
//#include "miint.tab.h"
using namespace std;
/*
*
* TYPE
*
* */
Type::Type(yytokentype id) : id(id) {}
int Type::getId() {
return id;
}
std::size_t Type::size() {
float typeSize = realSize();
return std::ceil(typeSize / 4.f) * 4;
}
/*
*
* TUPLETYPE
*
* */
TupleType::TupleType(yytokentype id) : Type(id) {}
bool TupleType::isTuple() {
return true;
}
std::string TupleType::toString() {
std::string str = "(";
for (auto &type : types)
str += type->toString() + ',';
return str + ')';
}
Type *TupleType::getSubType(int pos) {
return types[types.size() - pos - 1];
}
TupleType *TupleType::add(Type *other) {
types.push_back(other);
return this;
}
bool TupleType::equals(Type *type) {
if (!type->isTuple()) return false;
auto *other = (TupleType *) type;
if (other->types.size() != types.size()) return false;
for (int i = 0; i < types.size(); i++)
if (!types[i]->equals(other->types[i])) return false;
return true;
}
yytokentype TupleType::getType() {
return TUPLE;
}
/*
*
* PRIMITIVETYPE
*
* */
template<typename T>
std::string numberToString(T Number) {
std::ostringstream ss;
ss << Number;
return ss.str();
}
PrimitiveType::PrimitiveType(yytokentype type) : Type(type), type(type) {
switch (id) {
case INT:
_size = 4;
break;
case LONG:
_size = 8;
break;
case FLOAT:
_size = 8;
break;
case DOUBLE:
_size = 8;
break;
case CHAR:
_size = 1;
break;
case BOOL:
_size = 1;
break;
case STRING:
_size = 1;
break;
case VOID:
_size = 0;
break;
default:
fprintf(stderr, "Tipo no reconocido: %d\n", type);
//exit(-1);
}
}
std::string PrimitiveType::toString() {
return numberToString(id);
}
TupleType *PrimitiveType::add(Type *other) {
auto *tuple = new TupleType(id);
tuple->add(this);
tuple->add(other);
return tuple;
}
bool PrimitiveType::equals(Type *type) {
return !type->isTuple() && ((PrimitiveType *) type)->id == id;
}
bool PrimitiveType::isTuple() {
return false;
}
yytokentype PrimitiveType::getType() {
return type;
}
std::size_t TupleType::realSize() {
std::size_t size = 0;
for (auto &type : types)
size += type->size();
return size;
}
std::size_t PrimitiveType::realSize() {
return _size;
}
Type *TupleType::clone() {
return new TupleType(*this);
}
int TupleType::length() {
return types.size();
}
std::size_t TupleType::offsetOf(int pos) {
std::size_t size = 0;
//for(int i = 0; i < pos; i++)
for (int i = types.size() - 1; i > pos; i--)
size += getSubType(i)->size();
return size;
}
Type *PrimitiveType::clone() {
return new PrimitiveType(*this);
}
PrimitiveType::PrimitiveType(yytokentype id, std::size_t size) : Type(id), type(id), _size(size) {}
std::ostream &operator<<(std::ostream &os, yytokentype const &yytoken) {
os << [](yytokentype const &yytoken) {
switch (yytoken) {
case yytokentype::VALOR_INT:
case yytokentype::INT:
return "INT";
case yytokentype::VALOR_BOOL:
case yytokentype::BOOL:
return "BOOL";
case yytokentype::VALOR_CHAR:
case yytokentype::CHAR:
return "CHAR";
case yytokentype::VALOR_DOUBLE:
case yytokentype::DOUBLE:
return "DOUBLE";
case yytokentype::VALOR_FLOAT:
case yytokentype::FLOAT:
return "FLOAT";
case yytokentype::VALOR_LONG:
case yytokentype::LONG:
return "LONG";
case yytokentype::VALOR_STRING:
case yytokentype::STRING:
return "STRING";
}
return "UNKNOWN";
}(yytoken) << '(' << (int) yytoken << ')';
return os;
}
bool isNumberType(Type *tipo) {
switch (tipo->getType()) {
case INT:
case LONG:
case FLOAT:
case DOUBLE:
return true;
default:
return false;
}
}