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