forked from NesManrique/LenguajesII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblahsymtable.h
232 lines (196 loc) · 5.14 KB
/
blahsymtable.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef BLAHSYMTABLE
#define BLAHSYMTABLE
#include <string>
#include <vector>
#include <list>
#include <unordered_map>
#include <iostream>
class TVar;
typedef std::list<int> Sizelist;
typedef std::vector<std::pair<std::string,TVar*>> Fields;
class Tuple{
public:
std::string id;
int scope;
Tuple(std::string id,int scope):id(id),scope(scope){}
bool operator==(const Tuple a) const{
return id==a.id && scope==a.scope;
}
void print(std::ostream& os,int depth=0) const;
};
namespace std{
template<> class hash<Tuple>{
public:
size_t operator()(const Tuple &s) const {
return hash<string>()(s.id)+s.scope;
}
};
};
class TElement{
public:
std::string name;
bool isType;
TElement(std::string name,bool isType=false):name(name),isType(isType){}
virtual void print(std::ostream&,int d=0)=0;
virtual ~TElement(){}
};
//TYPES
class TType: public TElement {
public:
unsigned size;
unsigned alignment;
bool isArr;
bool isNumeric;
bool isStruct;
bool basic;
TType(std::string name,unsigned size,unsigned alignment,bool basic=false, bool numeric=false, bool struc=false,bool isArr=false):TElement(name,true),size(size),alignment(alignment),basic(basic),isNumeric(numeric),isStruct(struc),isArr(isArr){};
bool operator==(const TType &t2)const{
return name == t2.name;
}
bool operator!=(const TType &t2)const{
return name != t2.name;
}
void print(std::ostream&,int d=0);
};
//Basic Types
class TInteger: public TType{
public:
TInteger():TType("Integer",4,4,true,true){};
};
class TBool: public TType{
public:
TBool():TType("Bool",1,1,true){};
};
class TFloat: public TType{
public:
TFloat():TType("Float",4,4,true,true){};
};
class TError: public TType{
public:
TError():TType("error",0,1,true){};
};
class TChar: public TType{
public:
TChar():TType("Char",1,1,true,true){};
};
class TUndef: public TType{
public:
TUndef():TType("undef",0,1,true){};
};
class TVoid: public TType{
public:
TVoid():TType("Void",0,1,true){};
};
//Complex types
class Field{
public:
TType& type;
std::string& name;
Field(TType& type,std::string& name):type(type),name(name){}
};
class TStructured: public TType{
protected:
virtual void addField(TVar* type,std::string name)=0;
public:
std::unordered_map<std::string,TVar*> fields;
bool isUnion;
TStructured(std::string name,bool isUnion=false):TType(name,0,0,false,false,true),isUnion(isUnion){}
TVar* access(std::string name);
void print(std::ostream&,int d=0);
};
class TRegister: public TStructured{
public:
TRegister(std::string name,Fields fieldl);
void addField(TVar* var, std::string name);
};
class TUnion: public TStructured{
public:
TUnion(std::string name,Fields fieldl);
void addField(TVar* var,std::string name);
};
class TArray: public TType{
public:
TType* type;
int length;
TArray(TType* typ, int length):TType("Arrayof"+typ->name,typ->size*length,false,false,false,true),type(typ),length(length){}
TArray(TType*, Sizelist);
bool operator==(const TArray &t2)const{
return *type == *t2.type && length == t2.length;
}
};
//VARS AND FUNCTIONS
class TVar: public TElement{
public:
int scope;
TType& type;
int offset;
bool temporal;
bool isParam;
bool inMem;
std::list<int> regs;
TVar(std::string name,TType& type,bool isParam=false,bool temporal=false):isParam(isParam),type(type),TElement(name),scope(-1),temporal(temporal),inMem(true){}
void print(std::ostream&,int d=0);
bool operator==(const TVar &)const;
};
class TFunc: public TElement{
public:
TType& type;
int stacksize;
std::vector<TType*> args;
TFunc(std::string name, TType& type, std::vector<TType*> args):stacksize(0),TElement(name),type(type),args(args){}
std::string toStr();
void print(std::ostream&,int d=0);
};
//SYMTABLE
class Symtable {
private:
std::unordered_map<Tuple,TVar*> vars;
std::unordered_map<std::string,TFunc*> functions;
std::unordered_map<std::string,TType*> types;
std::list<int> scopeStack;
std::list<int> offsetStack;
int nextscope;
int paramoffset;
public:
int maxoffset;
Symtable():nextscope(1),paramoffset(0),maxoffset(0){
scopeStack.push_front(0);
offsetStack.push_front(0);
}
void insert(TType*);
void insert(TVar*,bool p =false);
void insert(TFunc*);
TVar* lookupVar(const std::string name);
TVar* lookupLocalVar(const std::string name);
TFunc* lookupFunc(const std::string name, const std::vector<TType*> args);
TType* lookupType(const std::string name);
int begFuncScope(){
paramoffset=0;
maxoffset=0;
offsetStack.push_front(0);
scopeStack.push_front(nextscope);
nextscope++;
}
int begScope(){
int t = offsetStack.front();
offsetStack.push_front(t);
scopeStack.push_front(nextscope);
nextscope++;
}
int endScope(){
offsetStack.pop_front();
scopeStack.pop_front();
}
int resetScope(){
nextscope=1;
scopeStack.clear();
scopeStack.push_front(0);
offsetStack.clear();
offsetStack.push_front(0);
}
void printVars(std::ostream& os);
void printFuncs(std::ostream& os);
void printTypes(std::ostream& os);
void print(std::ostream& os);
};
#endif