blob: 84aee1585e7d56018ef880709ae22c9e1bbaa1ea [file] [log] [blame]
swissChili7babd922021-12-02 22:46:48 -08001#pragma once
2
3#include "Token.h"
4#include "VarContext.h"
5
6struct MatchResult {
7 bool success;
8 VarContext context;
9};
10
swissChili3e98c062021-12-04 22:07:38 -080011template <typename T>
12bool 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
24template <typename T>
25void listDrop(QList<T> &list, int n) {
26 for (; n; n--) {
27 list.removeFirst();
28 }
29}
30
31template <typename T>
32QList<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
swissChili7babd922021-12-02 22:46:48 -080044MatchResult match(QList<Token> data, QList<Token> pattern, VarContext context);