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