-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
324 lines (265 loc) · 8.36 KB
/
main.cpp
File metadata and controls
324 lines (265 loc) · 8.36 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
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
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QDebug>
#include "Matcher.h"
#include "Token.h"
#include "AstNode.h"
#include "Parser.h"
#include "Evaluator.h"
#include "VarContext.h"
#ifdef INCLUDE_CLI
#include "Repl.h"
#endif
#include "PPrint.h"
#ifdef NO_IDE
using Application = QCoreApplication;
#else
#include "ide/IdeMain.h"
#endif
int g_numFailed = 0;
void testEval(QString function, QString expression, QString expected)
{
Evaluator eval;
Parser funcParser(function),
exprParser(expression),
resParser(expected);
Function func;
QList<AstNode> expr;
ParseResult exprRet = exprParser.parseMany<AstNode>(&expr);
QList<Token> res;
ParseResult resRet = resParser.parseMany<Token>(&res);
ParseResult funcRet;
QList<Token> result;
exprParser.skip();
resParser.skip();
while ((funcRet = funcParser.parseFunctionDefinition(&func)))
{
eval.addFunction(func);
}
funcParser.skip();
if (!exprRet || !resRet || funcRet.status() == ParseResult::INCOMPLETE)
{
g_numFailed++;
qDebug() << "\n\033[31mTEST FAILS:\033[0m";
qDebug() << "Failed to fully parse expression, function or result";
qDebug() << "Function:";
sout(pprint(funcRet, funcParser));
qDebug() << "Expression:";
sout(pprint(exprRet, exprParser));
qDebug() << "Result:";
sout(pprint(resRet, resParser));
goto end;
}
for (const AstNode &node : expr)
{
RuntimeResult rr = eval.evaluate(node, VarContext());
if (!rr.success())
{
g_numFailed++;
qDebug() << "\n\033[31mTEST FAILS:\033[0m";
qDebug() << "Runtime error while evaluating" << node;
qDebug() << rr;
goto end;
}
result.append(rr.result());
}
if (result != res)
{
g_numFailed++;
qDebug() << "\n\033[31mTEST FAILS:\033[0m";
qDebug() << "Expected result" << res;
qDebug() << "Got" << result;
}
end:
qDebug() << "\033[36mEvaluate\033[0m" << function << expression << "->" << pprint(result);
}
void testEval(QString function, QString expression, QList<Token> expected)
{
testEval(function, expression, pprint(expected));
}
void testEval(QString expression, QString expected)
{
testEval("", expression, expected);
}
void testMatch(const QString &test, bool shouldBe, const MatchResult &result)
{
if (result.success != shouldBe)
{
g_numFailed++;
qDebug() << "\n\033[31mTEST FAILS:\033[0m";
qDebug() << "with context" << result.context;
}
qDebug() << "\033[36mMatchResult\033[0m" << test << result.success;
if (result.success != shouldBe)
{
qDebug() << "";
}
}
void testMatch(QString data, QString pattern, bool shouldBe = true)
{
Parser dataParser(data),
patternParser(pattern);
QList<Token> parsedData, parsedPattern;
dataParser.parseMany<Token>(&parsedData);
patternParser.parseMany<Token>(&parsedPattern);
testMatch(pattern + " = " + data, shouldBe,
match(parsedData, parsedPattern, VarContext()));
}
void testParseAst(QString string, QList<AstNode> equals = {})
{
Parser parser{string};
QList<AstNode> result;
parser.parseMany<AstNode>(&result);
if (!equals.empty() && result != equals)
{
g_numFailed++;
qDebug() << "\n\033[31mTEST FAILS:\033[0m";
qDebug() << "Expected" << pprint(equals);
}
qDebug() << "\033[36mParse\033[0m" << string << pprint(result);
}
void testParseFunc(QString string)
{
Parser parser{string};
ParseResult ret;
Function func;
if (!(ret = parser.parseFunctionDefinition(&func)))
{
g_numFailed++;
qDebug() << "\n\033[31mTEST FAILS:\033[0m";
sout(pprint(ret, parser));
qDebug() << string;
}
else
{
qDebug() << "\033[36mFunction\033[0m";
qDebug().noquote() << func;
}
}
int testResults()
{
if (g_numFailed == 0)
{
qDebug() << "\033[32mALL TESTS SUCCEEDED\033[0m";
}
else
{
qDebug().nospace() << "\033[31m" << g_numFailed << " TESTS FAILED\033[0m";
}
return g_numFailed;
}
void testAllMatches()
{
testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext()));
testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext()));
LTok sameTwo = {Token('s', "a"), Token('s', "a")};
testMatch("s.a s.a = aa", true, match({Token('a'), Token('a')}, sameTwo, VarContext()));
testMatch("s.a s.a = ab", false, match({Token('a'), Token('b')}, sameTwo, VarContext()));
LTok sameStartEnd = {
Token('s', "a"),
Token('e', "middle"),
Token('s', "a")};
testMatch("s.a e.middle s.a = aea", true,
match({Token('a'), Token('e'), Token('a')}, sameStartEnd, VarContext()));
testMatch("s.a e.middle s.a = aef Hi a", true,
match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('a')}, sameStartEnd, VarContext()));
testMatch("s.a e.middle s.a = aef Hi c", false,
match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('c')}, sameStartEnd, VarContext()));
LTok parenthesized = {
Token(LTok({Token('s', "a")})),
Token('e', "Middle"),
Token('s', "a"),
};
LTok parenTest1 = {
Token(LTok({Token('y')})),
Token('f'),
Token("MiddleStuff"),
Token('y')};
testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true,
match(parenTest1, parenthesized, VarContext()));
// testMatch("(y)f Middle-stuff y", "(s.a) e.Middle s.a");
testMatch("(a)", "(a)");
testMatch("hello", "s.A e.Rest");
testMatch("123", "123");
testMatch("(123)", "t.a");
testMatch("(123)", "(s.a)");
}
void testAllParses()
{
testParseAst("all symbols");
testParseAst("Identifier symbols Identifier");
testParseAst("s.A");
testParseAst("(s.A) Variable-quoted");
testParseAst("<Func-name a b (c)>");
testParseAst("<Prout hi>");
testParseAst("(Prout hi)");
testParseAst("(<Prout hi>)");
testParseAst("<If T Then (<Prout hi>) Else (<Prout sorry>)>");
testParseAst("(s.a) e.Middle s.a");
testParseAst("Hello; Goodbye");
testParseAst("Key = Value");
testParseAst("123", {AstNode("123", 10)});
testParseAst("12 00", {AstNode::fromInteger(12), AstNode::fromInteger(0)});
testParseAst("s.A s.B", {AstNode('s', "A"), AstNode('s', "B")});
testParseAst("a '=' b", {AstNode('a'), AstNode('='), AstNode('b')});
testParseAst("'This\\'<>#$@#^%\\n' 'another $3543543'");
}
void testAllFunctionDefs()
{
testParseFunc("Test { = HI; }");
testParseFunc("Palindrome { = T; s.A = T; s.A e.Middle s.A = <Palindrome e.Middle>; e.Ignored = F; } ");
}
void testAllEvals()
{
testEval("First {s.A e.Rest = s.A;}", "<First hello>", "h");
testEval("Number { = 123; }", "<Number>", QList<Token>{Token::fromInteger(123)});
testEval("<Br Name '=' Jim> <Dg Name>", "Jim");
testEval("<Br A '=' hello> <Br A '=' 'good bye'> <Dg A> <Dg A>", "'good byehello'");
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
Application a(argc, argv);
a.setApplicationName("REFAL");
a.setApplicationVersion("1.0-a1");
QCommandLineParser cli;
cli.setApplicationDescription("REFAL interpreter");
cli.addHelpOption();
cli.addVersionOption();
cli.addPositionalArgument("script", "REFAL script to run");
QCommandLineOption testOption(QStringList{"t", "test"}, "Run test suite.");
cli.addOption(testOption);
QCommandLineOption replOption(QStringList({"r", "repl"}), "Start CLI REPL");
cli.addOption(replOption);
cli.process(a);
if (cli.positionalArguments().length() > 0)
{
qDebug() << "Running script" << cli.positionalArguments()[0];
}
else if (cli.isSet(testOption))
{
testAllMatches();
qDebug() << "";
testAllParses();
qDebug() << "";
testAllFunctionDefs();
qDebug() << "";
testAllEvals();
qDebug() << "";
return testResults();
}
#ifdef INCLUDE_CLI
else if (cli.isSet(replOption))
{
Repl repl;
repl.start();
}
#endif
else
{
return ideMain(&a);
}
}