- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.h
More file actions
Latest commit
208 lines (207 loc) · 4.65 KB
/
Copy pathScope.h
File metadata and controls
208 lines (207 loc) · 4.65 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#pragma once
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<assert.h>
#include"Log.h"
#include"Ast.h"
usingnamespacestd;
namespaceyfpeg {
classFuncArg {
public:
FuncArg(string name,string type)
:index(-1),argName(name),argType(type) {};
string argName;
string argType;
int index;
};
classFuncRet {
public:
FuncRet(){};
string retType;
};
classFuncInfo {
public:
FuncInfo():addr(-1) {};
int addr;
vector<FuncArg*> args;
FuncRet funcRet;
voidaddArg(string name,string type) {
FuncArg* arg = newFuncArg(name,type);
arg->index = args.size();
args.push_back(arg);
};
};
classVar{
public:
Var(string name,string type = "") {
this->name = name;
this->type = type;
funcInfo = nullptr;
};
voidaddFuncArg(string name,string type) {
if (funcInfo == nullptr) {
funcInfo = newFuncInfo();
}
funcInfo->addArg(name,type);
};
voidsetFuncAddr(int addr) {
if (!funcInfo) {
funcInfo = newFuncInfo();
}
funcInfo->addr = addr;
};
voidsetFuncRetType(string type) {
if (!funcInfo) {
funcInfo = newFuncInfo();
}
funcInfo->funcRet.retType = type;
};
string name;
string type;
FuncInfo* funcInfo;
};
classOneScope {
public:
OneScope():curVarName(""), curVarType("") {};
Var* addVar(string name,string type = "") {
Var* var = newVar(name,type);
vars.push_back(var);
return var;
}
Var* getVar(string name) {
OneScope* cur = this;
while (cur) {
for (int i = cur->vars.size()-1 ; i >= 0; --i) {
if ((cur->vars[i])->name == name) {
return cur->vars[i];
}
}
cur = cur->parent;
}
returnnullptr;
}
voidaddChild(OneScope* child) {
children.push_back(child);
child->parent = this;
}
intgetOffsetOfVar(string name) {
for (int i = vars.size()-1 ; i >= 0; --i) {
if ((vars[i])->name == name) {
return i;
}
}
return -1;
}
intcheckTypeAssignLegalByOffset(int l_offset,int r_offset) {
static map<string, string> legalConverts = {
{"int","float"},
{"float","int"},
{"vec2","ivec2"},
{"vec3","ivec3"},
{"vec4","ivec4"},
};
if (l_offset < vars.size() && r_offset < vars.size()) {
string& ltype = vars[l_offset]->type;
string& rtype = vars[r_offset]->type;
if (ltype == rtype) {
returntrue;
}
else {
if (legalConverts[ltype] == rtype) {
returntrue;
}
else {
LOG("Error:ilegal type convertion:can't assign %s to %s",rtype.c_str(),ltype.c_str());
assert(false);
returnfalse;
}
}
}
else {
assert(false);
}
}
voidwalk(int scope_level = 0) {
for (vector<Var*>::iterator it = vars.begin(); it != vars.end(); ++it) {
Var* var = *it;
LOG("%s%s:%s",getIndent(scope_level).c_str(),var->name.c_str(),var->type.c_str());
}
for (vector<OneScope*>::iterator it = children.begin(); it != children.end(); ++it) {
OneScope* one = *it;
one->walk(scope_level++);
}
}
voidsetCurVarName(string name) {
curVarName = name;
_checkVar();
}
voidsetCurVarType(string type) {
curVarType = type;
_checkVar();
}
void_checkVar() {
if (curVarName != "" && curVarType != "") {
addVar(curVarName,curVarType);
curVarName = "";
curVarType = "";
}
}
string curVarName;
string curVarType;
vector<Var*> vars;
vector<OneScope*> children;
OneScope* parent;
};
classScope
{
public:
Scope() {};
OneScope* newChild() {
if (!curScope) {
OneScope* one = newOneScope();
curScope = one;
root = curScope;
return one;
}
else {
OneScope* one = newOneScope();
curScope->addChild(one);
curScope = one;
return one;
}
}
voidrevert() {
curScope = curScope->parent;
}
voidrevertAndRemove() {
OneScope* parent = curScope->parent;
if (parent) {
vector<OneScope*>::iterator it = find(parent->children.begin(), parent->children.end(), curScope);
if (it != parent->children.end()) {
parent->children.erase(it);
}
curScope = curScope->parent;
}
}
voidaddVar(string name,string type = "") {
curScope->addVar(name,type);
}
voidpush(OneScope* scope) {
stack.push_back(scope);
curScope = scope;
};
voidpop() {
curScope = stack.back();
stack.pop_back();
};
intfindEntrance(string entranceName) {
Var* entrance = root->getVar(entranceName);
return entrance->funcInfo->addr;
};
OneScope* curScope;
OneScope* root;
vector<OneScope*> stack;
};
}