swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "Token.h" |
| 4 | #include "VarContext.h" |
| 5 | |
| 6 | struct MatchResult { |
| 7 | bool success; |
| 8 | VarContext context; |
| 9 | }; |
| 10 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 11 | template <typename T> |
| 12 | bool listStartsWith(const QList<T> &haystack, const QList<T> &needle) { |
| 13 | if (needle.length() > haystack.length()) |
| 14 | return false; |
| 15 | |
| 16 | for (int i = 0; i < needle.length(); i++) { |
| 17 | if (haystack[i] != needle[i]) |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | template <typename T> |
| 25 | void listDrop(QList<T> &list, int n) { |
| 26 | for (; n; n--) { |
| 27 | list.removeFirst(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | template <typename T> |
| 32 | QList<T> listSlice(QList<T> &list, int from, int to) { |
| 33 | QList<T> prime; |
| 34 | |
| 35 | // I guess we'll just panic if it's too long |
| 36 | // TODO: ERROR HANDLING |
| 37 | for (int i = 0; i < to - from; i++) { |
| 38 | prime.append(list[from + i]); |
| 39 | } |
| 40 | |
| 41 | return prime; |
| 42 | } |
| 43 | |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 44 | MatchResult match(QList<Token> data, QList<Token> pattern, VarContext context); |