swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 1 | #include <QCoreApplication> |
swissChili | 923bd53 | 2021-12-08 22:48:58 -0800 | [diff] [blame] | 2 | #include <QCommandLineParser> |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 3 | #include <QDebug> |
| 4 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 5 | #include "Matcher.h" |
| 6 | #include "Token.h" |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 7 | #include "AstNode.h" |
| 8 | #include "Parser.h" |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 9 | #include "Evaluator.h" |
| 10 | #include "VarContext.h" |
swissChili | 923bd53 | 2021-12-08 22:48:58 -0800 | [diff] [blame] | 11 | #include "Repl.h" |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 12 | #include "PPrint.h" |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 13 | |
| 14 | int g_numFailed = 0; |
| 15 | |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame^] | 16 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 17 | void testEval(QString function, QString expression, QString expected) |
| 18 | { |
| 19 | Evaluator eval; |
| 20 | Parser funcParser(function), |
| 21 | exprParser(expression), |
| 22 | resParser(expected); |
| 23 | |
| 24 | Function func; |
| 25 | |
| 26 | QList<AstNode> expr = exprParser.parseMany<AstNode>(); |
| 27 | QList<Token> res = resParser.parseMany<Token>(); |
| 28 | |
| 29 | QList<Token> result; |
| 30 | |
| 31 | exprParser.skip(); |
| 32 | resParser.skip(); |
| 33 | while (funcParser.parseFunctionDefinition(&func)) |
| 34 | { |
| 35 | eval.addFunction(func); |
| 36 | } |
| 37 | |
| 38 | funcParser.skip(); |
| 39 | |
| 40 | if (!exprParser.atEnd() || !resParser.atEnd() || !funcParser.atEnd()) |
| 41 | { |
| 42 | g_numFailed++; |
| 43 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 44 | qDebug() << "Failed to fully parse expression, function or result"; |
| 45 | qDebug() << function << expression << expected; |
| 46 | |
| 47 | goto end; |
| 48 | } |
| 49 | |
| 50 | for (const AstNode &node : expr) |
| 51 | { |
| 52 | RuntimeResult rr = eval.evaluate(node, VarContext()); |
| 53 | |
| 54 | if (!rr.success()) |
| 55 | { |
| 56 | g_numFailed++; |
| 57 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 58 | qDebug() << "Runtime error while evaluating" << node; |
| 59 | qDebug() << rr; |
| 60 | |
| 61 | goto end; |
| 62 | } |
| 63 | |
| 64 | result.append(rr.result()); |
| 65 | } |
| 66 | |
| 67 | if (result != res) |
| 68 | { |
| 69 | g_numFailed++; |
| 70 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 71 | qDebug() << "Expected result" << res; |
| 72 | qDebug() << "Got" << result; |
| 73 | } |
| 74 | |
| 75 | end: |
| 76 | qDebug() << "\033[36mEvaluate\033[0m" << function << expression << "->" << result; |
| 77 | } |
| 78 | |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame^] | 79 | void testEval(QString function, QString expression, QList<Token> expected) |
| 80 | { |
| 81 | testEval(function, expression, pprint(expected)); |
| 82 | } |
| 83 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 84 | void testMatch(const QString &test, bool shouldBe, const MatchResult &result) |
| 85 | { |
| 86 | if (result.success != shouldBe) |
| 87 | { |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 88 | g_numFailed++; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 89 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 90 | qDebug() << "with context" << result.context; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 91 | } |
| 92 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 93 | qDebug() << "\033[36mMatchResult\033[0m" << test << result.success; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 94 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 95 | if (result.success != shouldBe) |
| 96 | { |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 97 | qDebug() << ""; |
| 98 | } |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 99 | } |
| 100 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 101 | void testMatch(QString data, QString pattern, bool shouldBe = true) |
| 102 | { |
| 103 | Parser dataParser(data), |
| 104 | patternParser(pattern); |
| 105 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 106 | testMatch(pattern + " = " + data, shouldBe, |
| 107 | match(dataParser.parseMany<Token>(), patternParser.parseMany<Token>(), VarContext())); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 108 | } |
| 109 | |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 110 | void testParseAst(QString string, QList<AstNode> equals = {}) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 111 | { |
| 112 | Parser parser{string}; |
| 113 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 114 | QList<AstNode> result = parser.parseMany<AstNode>(); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 115 | |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 116 | if (!equals.empty() && result != equals) |
| 117 | { |
| 118 | g_numFailed++; |
| 119 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 120 | qDebug() << "Expected" << pprint(equals); |
| 121 | } |
| 122 | |
| 123 | qDebug() << "\033[36mParse\033[0m" << string << pprint(result); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 124 | } |
| 125 | |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 126 | void testParseFunc(QString string) |
| 127 | { |
| 128 | Parser parser{string}; |
| 129 | |
| 130 | Function func; |
| 131 | |
| 132 | if (!parser.parseFunctionDefinition(&func)) |
| 133 | { |
| 134 | g_numFailed++; |
| 135 | qDebug() << "\n\033[31mTEST FAILS:\033[0m"; |
| 136 | qDebug() << string; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | qDebug() << "\033[36mFunction\033[0m"; |
| 141 | qDebug().noquote() << func; |
| 142 | } |
| 143 | } |
| 144 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 145 | int testResults() |
| 146 | { |
| 147 | if (g_numFailed == 0) |
| 148 | { |
| 149 | qDebug() << "\033[32mALL TESTS SUCCEEDED\033[0m"; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | qDebug().nospace() << "\033[31m" << g_numFailed << " TESTS FAILED\033[0m"; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return g_numFailed; |
| 157 | } |
| 158 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 159 | void testAllMatches() |
| 160 | { |
| 161 | testMatch("a = a", true, match({Token('a')}, {Token('a')}, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 162 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 163 | testMatch("s.a = y", true, match({Token('y')}, {Token('s', "a")}, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 164 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 165 | LTok sameTwo = {Token('s', "a"), Token('s', "a")}; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 166 | testMatch("s.a s.a = aa", true, match({Token('a'), Token('a')}, sameTwo, VarContext())); |
| 167 | testMatch("s.a s.a = ab", false, match({Token('a'), Token('b')}, sameTwo, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 168 | |
| 169 | LTok sameStartEnd = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 170 | Token('s', "a"), |
| 171 | Token('e', "middle"), |
| 172 | Token('s', "a")}; |
| 173 | testMatch("s.a e.middle s.a = aea", true, |
| 174 | match({Token('a'), Token('e'), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 175 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 176 | testMatch("s.a e.middle s.a = aef Hi a", true, |
| 177 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('a')}, sameStartEnd, VarContext())); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 178 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 179 | testMatch("s.a e.middle s.a = aef Hi c", false, |
| 180 | match({Token('a'), Token('e'), Token('f'), Token("Hi"), Token('c')}, sameStartEnd, VarContext())); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 181 | |
| 182 | LTok parenthesized = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 183 | Token(LTok({Token('s', "a")})), |
| 184 | Token('e', "Middle"), |
| 185 | Token('s', "a"), |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 186 | }; |
| 187 | LTok parenTest1 = { |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 188 | Token(LTok({Token('y')})), |
| 189 | Token('f'), |
| 190 | Token("MiddleStuff"), |
| 191 | Token('y')}; |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 192 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 193 | testMatch("(s.a) e.Middle s.a = (y)f MiddleStuff y", true, |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 194 | match(parenTest1, parenthesized, VarContext())); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 195 | // testMatch("(y)f Middle-stuff y", "(s.a) e.Middle s.a"); |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 196 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 197 | testMatch("(a)", "(a)"); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 198 | testMatch("hello", "s.A e.Rest"); |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 199 | testMatch("123", "123"); |
| 200 | testMatch("(123)", "t.a"); |
| 201 | testMatch("(123)", "(s.a)"); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 202 | } |
| 203 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 204 | void testAllParses() |
| 205 | { |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 206 | testParseAst("all symbols"); |
| 207 | testParseAst("Identifier symbols Identifier"); |
| 208 | testParseAst("s.A"); |
| 209 | testParseAst("(s.A) Variable-quoted"); |
| 210 | testParseAst("<Func-name a b (c)>"); |
| 211 | testParseAst("<Prout hi>"); |
| 212 | testParseAst("(Prout hi)"); |
| 213 | testParseAst("(<Prout hi>)"); |
| 214 | testParseAst("<If T Then (<Prout hi>) Else (<Prout sorry>)>"); |
| 215 | testParseAst("(s.a) e.Middle s.a"); |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 216 | testParseAst("Hello; Goodbye"); |
| 217 | testParseAst("Key = Value"); |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 218 | testParseAst("123", {AstNode("123", 10)}); |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame^] | 219 | testParseAst("12 00", {AstNode::fromInteger(12), AstNode::fromInteger(0)}); |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void testAllFunctionDefs() |
| 223 | { |
| 224 | testParseFunc("Test { = HI; }"); |
swissChili | 923bd53 | 2021-12-08 22:48:58 -0800 | [diff] [blame] | 225 | testParseFunc("Palindrome { = T; s.A = T; s.A e.Middle s.A = <Palindrome e.Middle>; e.Ignored = F; } "); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void testAllEvals() |
| 229 | { |
| 230 | testEval("First {s.A e.Rest = s.A;}", "<First hello>", "h"); |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame^] | 231 | testEval("Number { = 123; }", "<Number>", QList<Token>{Token::fromInteger(123)}); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | int main(int argc, char *argv[]) |
| 235 | { |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 236 | QCoreApplication a(argc, argv); |
swissChili | 923bd53 | 2021-12-08 22:48:58 -0800 | [diff] [blame] | 237 | QCoreApplication::setApplicationName("REFAL"); |
| 238 | QCoreApplication::setApplicationVersion("1.0-a1"); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 239 | |
swissChili | 923bd53 | 2021-12-08 22:48:58 -0800 | [diff] [blame] | 240 | QCommandLineParser cli; |
| 241 | cli.setApplicationDescription("REFAL interpreter"); |
| 242 | cli.addHelpOption(); |
| 243 | cli.addVersionOption(); |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 244 | |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame^] | 245 | QCommandLineOption testOption(QStringList{"t", "test"}, "Run test suite."); |
swissChili | 923bd53 | 2021-12-08 22:48:58 -0800 | [diff] [blame] | 246 | cli.addOption(testOption); |
| 247 | |
| 248 | cli.process(a); |
| 249 | |
| 250 | if (cli.isSet(testOption)) |
| 251 | { |
| 252 | testAllMatches(); |
| 253 | qDebug() << ""; |
| 254 | testAllParses(); |
| 255 | qDebug() << ""; |
| 256 | testAllFunctionDefs(); |
| 257 | qDebug() << ""; |
| 258 | testAllEvals(); |
| 259 | |
| 260 | qDebug() << ""; |
| 261 | |
| 262 | return testResults(); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | Repl repl; |
| 267 | repl.start(); |
| 268 | } |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 269 | } |