-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmcontext.cc
More file actions
203 lines (184 loc) · 5.49 KB
/
vmcontext.cc
File metadata and controls
203 lines (184 loc) · 5.49 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
#include "vmcontext.hpp"
bool IsFunctionOverwriteEnabled(const std::string& name);
namespace Interpreter {
struct BuiltinValue {
std::string Name;
Value val;
};
BuiltinValue g_builtinVar[] = {
{"false", Value(0l)},
{"true", Value(1l)},
{"nil", Value()},
};
#define COUNT_OF(a) (sizeof(a) / sizeof(a[0]))
VMContext::VMContext(Type type, VMContext* Parent) : mFlags(0), mIsEnableWarning(false) {
mParent = Parent;
mType = type;
mDeepth = 0;
if (mParent != NULL) {
mDeepth = mParent->mDeepth + 1;
mIsEnableWarning = mParent->mIsEnableWarning;
}
LoadBuiltinVar();
}
VMContext::~VMContext() {}
bool VMContext::IsBuiltinVarName(const std::string& name) {
for (int i = 0; i < COUNT_OF(g_builtinVar); i++) {
if (g_builtinVar[i].Name == name) {
return false;
}
}
return true;
}
void VMContext::LoadBuiltinVar() {
for (int i = 0; i < COUNT_OF(g_builtinVar); i++) {
mVars[g_builtinVar[i].Name] = g_builtinVar[i].val;
}
}
void VMContext::BreakExecuted() {
if (!IsBreakAvaliable()) {
throw RuntimeException("break only avaliable in for or switch statement");
}
mFlags |= BREAK_FLAG;
}
void VMContext::ContinueExecuted() {
if (!IsContinueAvaliable()) {
throw RuntimeException("continue only avaliable in for statement");
}
mFlags |= CONTINUE_FLAG;
}
void VMContext::ExitExecuted(Value exitCode) {
VMContext* seek = this;
while (seek) {
seek->mFlags |= EXIT_FLAG;
seek->mReturnValue = exitCode;
seek = seek->mParent;
}
}
void VMContext::ReturnExecuted(Value retVal) {
if (!IsInFunctionContext()) {
throw RuntimeException("return only avaliable in function statement");
}
VMContext* seek = this;
while (seek) {
seek->mFlags |= RETURN_FLAG;
seek->mReturnValue = retVal;
if (seek->mType == Function) {
break;
}
seek = seek->mParent;
}
}
bool VMContext::IsInFunctionContext() {
VMContext* seek = this;
while (seek) {
if (seek->mType == Function) {
return true;
}
seek = seek->mParent;
}
return false;
}
void VMContext::AddVar(const std::string& name) {
if (!IsBuiltinVarName(name)) {
throw RuntimeException("variable name is builtin :" + name);
}
if (mIsEnableWarning && IsShadowName(name)) {
LOG("variable name shadow :" + name);
}
std::map<std::string, Value>::iterator iter = mVars.find(name);
if (iter == mVars.end()) {
mVars[name] = Value();
return;
}
throw RuntimeException("variable already exist name:" + name);
}
void VMContext::SetVarValue(const std::string& name, Value value) {
if (!IsBuiltinVarName(name)) {
throw RuntimeException("variable not changable,because name is builtin :" + name);
}
VMContext* ctx = this;
while (ctx != NULL) {
std::map<std::string, Value>::iterator iter = ctx->mVars.find(name);
if (iter != ctx->mVars.end()) {
iter->second = value;
return;
}
ctx = ctx->mParent;
}
if (mIsEnableWarning) {
LOG("variable <" + name + "> not found, so new one.");
}
mVars[name] = value;
}
bool VMContext::IsShadowName(const std::string& name) {
VMContext* ctx = this;
while (ctx != NULL) {
std::map<std::string, Value>::iterator iter = ctx->mVars.find(name);
if (iter != ctx->mVars.end()) {
return true;
}
ctx = ctx->mParent;
}
return false;
}
bool VMContext::GetVarValue(const std::string& name, Value& val) {
VMContext* ctx = this;
while (ctx != NULL) {
std::map<std::string, Value>::iterator iter = ctx->mVars.find(name);
if (iter != ctx->mVars.end()) {
val = iter->second;
return true;
}
ctx = ctx->mParent;
}
return false;
throw RuntimeException("variable not found :" + name);
}
Value VMContext::GetVarValue(const std::string& name) {
Value ret;
if (!GetVarValue(name, ret)) {
throw RuntimeException("variable not found :" + name);
}
return ret;
}
void VMContext::AddFunction(const Instruction* obj) {
if (mType != File) {
throw RuntimeException("function declaration must in the top block name:" + obj->Name);
}
if (!IsFunctionOverwriteEnabled(obj->Name)) {
throw RuntimeException("exit function can't overwrite");
}
//LOG("add function:"+obj->Name);
std::map<std::string, const Instruction*>::iterator iter = mFunctions.find(obj->Name);
if (iter == mFunctions.end()) {
mFunctions[obj->Name] = obj;
return;
}
throw RuntimeException("function already exist name:" + obj->Name);
}
const Instruction* VMContext::GetFunction(const std::string& name) {
VMContext* ctx = this;
while (ctx->mParent != NULL) {
ctx = ctx->mParent;
}
std::map<std::string, const Instruction*>::iterator iter = ctx->mFunctions.find(name);
if (iter == ctx->mFunctions.end()) {
return NULL;
}
return iter->second;
}
Value VMContext::GetTotalFunction() {
VMContext* ctx = this;
while (ctx->mParent != NULL) {
ctx = ctx->mParent;
}
std::vector<Value> functions;
std::map<std::string, const Instruction*>::iterator iter = ctx->mFunctions.begin();
while (iter != ctx->mFunctions.end()) {
functions.push_back(Value(iter->first));
iter++;
}
return Value(functions);
}
} // namespace Interpreter