-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scope.h
208 lines (207 loc) · 4.65 KB
/
Scope.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
#pragma once
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<assert.h>
#include"Log.h"
#include"Ast.h"
using namespace std;
namespace yfpeg {
class FuncArg {
public:
FuncArg(string name,string type)
:index(-1),argName(name),argType(type) {};
string argName;
string argType;
int index;
};
class FuncRet {
public:
FuncRet(){};
string retType;
};
class FuncInfo {
public:
FuncInfo():addr(-1) {};
int addr;
vector<FuncArg*> args;
FuncRet funcRet;
void addArg(string name,string type) {
FuncArg* arg = new FuncArg(name,type);
arg->index = args.size();
args.push_back(arg);
};
};
class Var{
public:
Var(string name,string type = "") {
this->name = name;
this->type = type;
funcInfo = nullptr;
};
void addFuncArg(string name,string type) {
if (funcInfo == nullptr) {
funcInfo = new FuncInfo();
}
funcInfo->addArg(name,type);
};
void setFuncAddr(int addr) {
if (!funcInfo) {
funcInfo = new FuncInfo();
}
funcInfo->addr = addr;
};
void setFuncRetType(string type) {
if (!funcInfo) {
funcInfo = new FuncInfo();
}
funcInfo->funcRet.retType = type;
};
string name;
string type;
FuncInfo* funcInfo;
};
class OneScope {
public:
OneScope():curVarName(""), curVarType("") {};
Var* addVar(string name,string type = "") {
Var* var = new Var(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;
}
return nullptr;
}
void addChild(OneScope* child) {
children.push_back(child);
child->parent = this;
}
int getOffsetOfVar(string name) {
for (int i = vars.size()-1 ; i >= 0; --i) {
if ((vars[i])->name == name) {
return i;
}
}
return -1;
}
int checkTypeAssignLegalByOffset(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) {
return true;
}
else {
if (legalConverts[ltype] == rtype) {
return true;
}
else {
LOG("Error:ilegal type convertion:can't assign %s to %s",rtype.c_str(),ltype.c_str());
assert(false);
return false;
}
}
}
else {
assert(false);
}
}
void walk(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++);
}
}
void setCurVarName(string name) {
curVarName = name;
_checkVar();
}
void setCurVarType(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;
};
class Scope
{
public:
Scope() {};
OneScope* newChild() {
if (!curScope) {
OneScope* one = new OneScope();
curScope = one;
root = curScope;
return one;
}
else {
OneScope* one = new OneScope();
curScope->addChild(one);
curScope = one;
return one;
}
}
void revert() {
curScope = curScope->parent;
}
void revertAndRemove() {
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;
}
}
void addVar(string name,string type = "") {
curScope->addVar(name,type);
}
void push(OneScope* scope) {
stack.push_back(scope);
curScope = scope;
};
void pop() {
curScope = stack.back();
stack.pop_back();
};
int findEntrance(string entranceName) {
Var* entrance = root->getVar(entranceName);
return entrance->funcInfo->addr;
};
OneScope* curScope;
OneScope* root;
vector<OneScope*> stack;
};
}