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