-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluator.cpp
360 lines (295 loc) · 7.23 KB
/
Evaluator.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "Evaluator.h"
#include "Function.h"
#include "Matcher.h"
#include "VarContext.h"
#include "PPrint.h"
#include <QDebug>
#include <QCoreApplication>
RuntimeResult::RuntimeResult(QList<Token> result)
{
_result = result;
}
RuntimeResult::RuntimeResult(QString message)
{
_errorMessage = message;
_success = false;
}
RuntimeResult &RuntimeResult::operator =(const RuntimeResult &other)
{
_errorMessage = other._errorMessage;
_success = other._success;
_result = other._result;
return *this;
}
RuntimeResult RuntimeResult::operator +(const RuntimeResult &other)
{
RuntimeResult res;
if (_success)
{
res._success = other._success;
res._result = _result;
res._result.append(other._result);
res._errorMessage = other._errorMessage;
}
else
{
res = *this;
}
return res;
}
RuntimeResult &RuntimeResult::operator +=(const RuntimeResult &other)
{
*this = *this + other;
return *this;
}
bool RuntimeResult::success() const
{
return _success;
}
QString RuntimeResult::message() const
{
return _errorMessage;
}
QList<Token> RuntimeResult::result() const
{
return _result;
}
RuntimeResult::operator QString() const
{
return _errorMessage + pprint(_result);
}
Evaluator::Evaluator()
{
Function buryFn("Br");
buryFn.addNativeSentence("s.Name '=' e.Expr", [this](VarContext args)
{
Token name = args.singleVar("Name");
if (name.type() != Token::IDENT)
rtError("Invalid argument", "First argument to <Br> must be an identifier, received " + pprint(name));
bury(name.name(), args.expressionVar("Expr"));
return QList<Token>();
});
addFunction(buryFn);
Function digFn("Dg");
digFn.addNativeSentence("s.Name", [this](VarContext args)
{
Token name = args.singleVar("Name");
if (name.type() != Token::IDENT)
rtError("Invalid argument", "First argument to <Dg> must be an identifier, received " + pprint(name));
return dig(name.name());
});
addFunction(digFn);
Function copyFn("Cp");
copyFn.addNativeSentence("s.Name", [this](VarContext args)
{
Token name = args.singleVar("Name");
if (name.type() != Token::IDENT)
rtError("Invalid argument", "First argument to <Cp> must be an identifier, received " + pprint(name));
return copy(name.name());
});
addFunction(copyFn);
Function undefFn("Undef");
undefFn.addNativeSentence("s.Name", [this](VarContext args)
{
Token name = args.singleVar("Name");
if (name.type() != Token::IDENT)
rtError("Invalid argument", "First argument to <Undef> must be an identifier, received " + pprint(name));
clearFunction(name.name());
return QList<Token>();
});
addFunction(undefFn);
}
void Evaluator::addFunction(Function func)
{
_functions[func.name()] = func;
}
void Evaluator::clearFunction(QString name)
{
_functions.remove(name);
}
RuntimeResult Evaluator::evaluate(AstNode node, VarContext ctx, int recursionDepth)
{
if (recursionDepth > _recursionLimit)
{
throw StackOverflowException(node);
}
if (node.isSym())
{
return RuntimeResult(QList<Token>{Token(node.symbol())});
}
else if (node.isIdent())
{
return RuntimeResult(QList<Token>{Token(node.name())});
}
else if (node.isInteger())
{
return RuntimeResult(QList<Token>{Token::fromInteger(node.integer())});
}
else if (node.isVar())
{
if (!ctx.exists(node.name()) || ctx.exists(node.name()) != node.symbol())
return RuntimeResult("Variable " + node + " is not defined");
if (node.symbol() == 'e')
{
return RuntimeResult(ctx.expressionVar(node.name()));
}
else
{
return RuntimeResult(QList<Token>{
ctx.singleVar(node.name())
});
}
}
else if (node.isParen())
{
QList<Token> result;
for (const AstNode &n : node.parenContent())
{
RuntimeResult internalResult = evaluate(n, ctx);
if (!internalResult.success())
return internalResult;
result.append(internalResult.result());
}
return RuntimeResult(QList<Token>{
Token(result)
});
}
else if (node.isFunc())
{
QList<Token> args;
for (const AstNode &arg : node.funcArgs())
{
RuntimeResult internalResult = evaluate(arg, ctx, recursionDepth + 1);
if (!internalResult.success())
return internalResult;
args.append(internalResult.result());
}
return callFunction(node.name(), args, recursionDepth + 1);
}
return RuntimeResult("#TYPE_ERROR");
}
RuntimeResult Evaluator::callFunction(QString name, QList<Token> args, int recursionDepth)
{
if (!_functions.contains(name))
return RuntimeResult("Function " + name + " is not defined.");
Function func = _functions[name];
for (const Sentence &sentence : func.sentences())
{
MatchResult res = match(args, sentence.pattern(), VarContext());
if (!res.success)
continue;
if (sentence.isExternal())
{
return RuntimeResult(sentence.externResult(res));
}
QList<Token> final;
for (const AstNode &node : sentence.result())
{
RuntimeResult argRes = evaluate(node, res.context, recursionDepth);
if (!argRes.success())
return argRes;
final.append(argRes.result());
}
return RuntimeResult(final);
}
return RuntimeResult("Function " + name + " had no matching sentences for input");
}
void Evaluator::quit()
{
throw EvalQuitException();
}
void Evaluator::reset()
{
_vars = {};
_functions = {};
_shouldContinue = true;
}
QList<Token> Evaluator::dig(QString name)
{
if (_vars.contains(name))
{
QList<Token> value = _vars[name].pop();
if (_vars[name].empty())
{
_vars.remove(name);
}
return value;
}
else
{
return {};
}
}
QList<Token> Evaluator::copy(QString name)
{
if (_vars.contains(name))
{
return _vars[name].last();
}
else
{
return {};
}
}
void Evaluator::bury(QString name, QList<Token> expression)
{
if (!_vars.contains(name))
{
_vars[name] = QStack<QList<Token>>();
}
_vars[name].push(expression);
}
void rtError(QString brief, QString details)
{
throw AssertionException(brief + "\n" + details);
}
void EvalQuitException::raise() const
{
throw *this;
}
EvalQuitException *EvalQuitException::clone() const
{
return new EvalQuitException(*this);
}
StackOverflowException::StackOverflowException(AstNode failedAt)
: QException()
{
_failedAt = failedAt;
}
AstNode StackOverflowException::failedAt() const
{
return _failedAt;
}
void StackOverflowException::raise() const
{
throw *this;
}
StackOverflowException *StackOverflowException::clone() const
{
return new StackOverflowException(*this);
}
StackOverflowException::operator QString() const
{
return "StackOverflowException: at " + pprint(_failedAt);
}
AssertionException::AssertionException(QString message)
: QException()
{
_message = message;
}
QString AssertionException::message() const
{
return _message;
}
void AssertionException::raise() const
{
throw *this;
}
AssertionException *AssertionException::clone() const
{
return new AssertionException(*this);
}
AssertionException::operator QString() const
{
return "AssertionException: " + _message;
}