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