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