-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.cpp
More file actions
226 lines (220 loc) · 6.42 KB
/
script.cpp
File metadata and controls
226 lines (220 loc) · 6.42 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "models.h"
#include "lexer.h"
#include "parser.h"
#include "script.h"
#include "functional"
#include "iostream"
#define BINARY_NUMBER(opt, lval, rval, type) \
{ \
Entry *res = new Entry(); \
res->numberData = ((type)lval->getNumber())opt((type)rval->getNumber()); \
return res; \
}
#define BINARY_COMP(opt, lval, rval) \
{ \
Entry *res = new Entry(); \
res->boolData = (lval->getNumber())opt(rval->getNumber()); \
return res; \
}
#define BINARY_LOGIC(opt, lval, rval) \
{ \
Entry *res = new Entry(); \
res->boolData = (lval->getBool())opt(rval->getBool()); \
return res; \
}
Entry *primaryExpression(string text);
Entry *unaryExpression(string opt, Entry *val);
Entry *binaryExpression(string opt, Entry *lval, Entry *rval);
Script::Script()
{
kc = new Context(10);
}
void Script::process(string code)
{
auto tokens = tokenize(code);
auto node = parser(tokens);
auto res = evaluate(node, "");
if (res != nullptr && verbose)
cout << "result:" << res->getString() << endl;
}
Entry *Script::evaluate(ASTNode *node, string indent)
{
indent += "\t";
Entry *res = nullptr;
// cout << indent << "evaluate: " << ToString(node->type) << " " << node->text << endl;
switch (node->type)
{
case ASTNodeType::Function:
{
res = new Entry();
res->flag = PrimaryFlag::methodFlag;
res->methodData = new Method();
res->methodData->stackLink = kc->frame;
res->methodData->params = node->params;
res->methodData->funcBody = node->children[0]; //block节点指针
kc->set(node->text, res);
}
break;
case ASTNodeType::Block:
case ASTNodeType::Program:
{
//定义先执行
for (auto child : node->children)
{
if (child->type == ASTNodeType::VarDeclaration || child->type == ASTNodeType::Function)
evaluate(child, indent);
}
for (auto child : node->children)
{
if (child->type == ASTNodeType::VarDeclaration || child->type == ASTNodeType::Function)
continue;
res = evaluate(child, indent);
if (child->type == ASTNodeType::Return)
break;
}
}
break;
case ASTNodeType::Return:
if (node->children.size())
res = evaluate(node->children[0], indent);
break;
case ASTNodeType::Primary:
res = primaryExpression(node->text);
break;
case ASTNodeType::Unary:
{
auto val = evaluate(node->children[0], indent);
res = unaryExpression(node->text, val);
}
break;
case ASTNodeType::Binary:
{
auto lval = evaluate(node->children[0], indent);
auto rval = evaluate(node->children[1], indent);
res = binaryExpression(node->text, lval, rval);
}
break;
case ASTNodeType::Call:
{
//解析出所有参数节点
Entry *methodEntry = kc->get(node->text);
if (methodEntry == nullptr || methodEntry->isUndefined())
throw "not found function " + node->text;
vector<Entry *> params;
for (auto paramNode : node->children)
params.push_back(evaluate(paramNode, indent));
res = callFunction(methodEntry, params, indent);
}
break;
case ASTNodeType::NumberLiteral:
res = new Entry();
res->flag = PrimaryFlag::numberFlag;
res->numberData = stod(node->text);
break;
case ASTNodeType::Identifier:
res = kc->get(node->text);
break;
case ASTNodeType::AssignmentStmt:
case ASTNodeType::VarDeclaration:
{
Entry *rval;
if (node->children.size()) //如果没有就只是变量声明
rval = evaluate(node->children[0], indent);
else
{
rval = new Entry();
rval->flag = PrimaryFlag::undefinedFlag;
}
kc->set(node->text, rval);
res = rval;
}
break;
}
return res;
};
Entry *Script::callFunction(Entry *methodEntry, vector<Entry *> params, string indent)
{
kc->pushFrame(new StackFrame(methodEntry->methodData->stackLink, kc->globalScope));
//实参设置到当前的作用域中
auto vParams = methodEntry->methodData->params;
for (int i = 0; i < vParams.size(); i++)
kc->set(vParams[i], params[i]);
//开始执行函数语句
for (auto child : methodEntry->methodData->funcBody->children)
{
Entry *res = evaluate(child, indent);
if (child->type == ASTNodeType::Return)
return res;
}
return nullptr;
};
Entry *primaryExpression(string text)
{
Entry *res = new Entry();
res->flag = PrimaryFlag::undefinedFlag;
if (text == "true" || text == "false")
{
res->flag = PrimaryFlag::boolFlag;
res->boolData = text == "true";
}
return res;
}
Entry *unaryExpression(string opt, Entry *val)
{
Entry *res = new Entry();
if (opt == "!")
{
res->flag = PrimaryFlag::boolFlag;
res->boolData = !val->getBool();
return res;
}
else if (opt == "~")
{
res->flag = PrimaryFlag::numberFlag;
res->numberData = ~((int)val->getNumber());
return res;
}
delete res;
return nullptr;
}
extern unordered_map<string, function<Entry *(Entry *lval, Entry *rval)> > optMap;
Entry *binaryExpression(string opt, Entry *lval, Entry *rval)
{
if (opt == "+")
BINARY_NUMBER(+, lval, rval, double);
if (opt == "-")
BINARY_NUMBER(-, lval, rval, double);
if (opt == "*")
BINARY_NUMBER(*, lval, rval, double);
if (opt == "/")
BINARY_NUMBER(/, lval, rval, double);
if (opt == "%")
BINARY_NUMBER(%, lval, rval, long);
if (opt == ">>")
BINARY_NUMBER(>>, lval, rval, long);
if (opt == "<<")
BINARY_NUMBER(<<, lval, rval, long);
if (opt == "<")
BINARY_COMP(<, lval, rval);
if (opt == ">")
BINARY_COMP(>, lval, rval);
if (opt == "<=")
BINARY_COMP(<=, lval, rval);
if (opt == ">=")
BINARY_COMP(>=, lval, rval);
if (opt == "==")
BINARY_COMP(==, lval, rval);
if (opt == "!=")
BINARY_COMP(!=, lval, rval);
if (opt == "||")
BINARY_LOGIC(||, lval, rval);
if (opt == "&&")
BINARY_LOGIC(&&, lval, rval);
if (opt == "|")
BINARY_NUMBER(|, lval, rval, long);
if (opt == "^")
BINARY_NUMBER(^, lval, rval, long);
if (opt == "&")
BINARY_NUMBER(&, lval, rval, long);
return nullptr;
}